osr-mono/packages/osr-tasks/tasks/git.ts
2025-01-29 17:48:22 +01:00

63 lines
1.8 KiB
TypeScript

import { simpleGit } from 'simple-git'
import { resolve } from '@plastichub/osr-commons'
import { logger } from '../src/index'
import { option } from '../library'
const GIT_MESSAGE_PREFIX = (rel: string) => `Updating - ${rel}`
export async function git_update(options) {
const { cwd, rel, message, debug } = options
const git = simpleGit(cwd)
let commit: any = null
let push: any = null
try {
debug && logger.debug("commit " + rel + " in " + cwd)
commit = await git.add(rel)
}
catch (e) {
logger.error('Error Git Commit', e)
}
try {
debug && logger.debug("commit " + rel + " in " + cwd)
commit = await git.commit(message || GIT_MESSAGE_PREFIX(rel), rel)
}
catch (e) {
logger.error('Error Git Commit', e)
}
try {
push = await git.push()
}
catch (e) {
logger.error('Error Git Push', e)
}
return { commit, push }
}
export const register = (grunt) => {
grunt.registerMultiTask('git', 'Adds, commits and pushes folders', async function () {
const done = this.async()
const task_options = this.data || {}
const logLevel = option('logLevel', task_options, grunt, 'warn')
logger.setSettings({ minLevel: logLevel })
let rel: string = this.data.rel
const variables = {
...task_options.variables || {}
}
const options = {
rel: resolve(rel, false, variables),
cwd: resolve(option('cwd', task_options, grunt), false),
message: option('message', task_options, grunt),
debug: option('debug', task_options, grunt),
verbose: option('verbose', task_options, grunt),
logLevel
}
logger.info('Git - task data ', this.data)
await git_update(options)
done()
})
}