import * as path from 'path' import { simpleGit } from 'simple-git' import { resolve } from '@plastichub/osr-cli-commons/fs' import { logger } from '../src/index' import { option } from './' 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 || {} 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) } options.debug && logger.info('Git - task data ', this.data) await git_update(options) done() }); };