import { argv } from "yargs" ; import * as gulp from "gulp" ; import * as convertEncoding from "gulp-convert-encoding" ; import chalk from "chalk" ; import { printResults , includesPaths } from "../common/utility" ; import { workspaceConfig as config } from "../common/config" ; import { getOnlyChangedFiles } from "../common/globs" ; import { compileFile , compileSelection } from "../common/base" ; import * as git from "../common/git" ; import { exportFilesFromDbAsync } from "../tasks/import" ; import * as noop from "gulp-noop" ; import * as data from "gulp-data" ; const compileFilesToDb = async ( { file = argv . file , env = argv . env || "DEV" , changed = argv . changed , force = argv . force , user = argv . user , } ) => { const source = config . get ( { field : "source.input" , env } ) ; const src = file || ( changed ? await getOnlyChangedFiles ( source ) : source ) ; const warnings = config . get ( { field : "compile.warnings" , env } ) ; const stageFile = config . get ( { field : "compile.stageFile" , env } ) ; const encoding = config . get ( { field : "source.encoding" , env } ) ; const processFile = async ( file , done ) => { let resp ; try { resp = await compileFile ( file . contents , file . path , env , force , warnings , < string > user ) ; printResults ( resp ) ; if ( stageFile && ! resp . errors . hasErrors ( ) ) { await git . exec ( { args : `add "${ resp . file }"` } ) ; } } catch ( error ) { console . error ( error . message ) ; } finally { done ( null , resp ) ; } } ; if ( src . length === 0 ) { src . push ( "nonvalidfile" ) ; } return ( gulp . src ( src , { allowEmpty : true } ) . pipe ( convertEncoding ( { from : encoding } ) ) . pipe ( data ( processFile ) ) . on ( "data" , noop ) ) ; } ; export const compileOnSaveTask = ( { env = argv . env || "DEV" } ) => { const source = config . get ( "source.input" ) ; const watcher = gulp . watch ( source , { awaitWriteFinish : true } ) ; console . log ( chalk . magenta ( `Watching for file changes in ${ source } ...` ) ) ; watcher . on ( "change" , async ( file ) => { console . log ( `\nStarting compilation...` ) ; const files = await getOnlyChangedFiles ( source ) ; await compileFilesToDbAsync ( { env , file : files } ) ; if ( ! includesPaths ( files , file ) ) { await compileFilesToDbAsync ( { env , file } ) ; } console . log ( "Compilation complete." ) ; } ) ; } ; const compileFilesToDbAsync = async ( { file , env , changed = false , force = true } ) => { let results = [ ] ; return new Promise ( async ( res , rej ) => { const p = await compileFilesToDb ( { file , env , changed , force } ) ; p . on ( "data" , ( resp ) => results . push ( resp . data ) ) ; p . on ( "end" , ( ) => res ( results ) ) ; p . on ( "error" , rej ) ; } ) ; } ; const mergeLocalAndDbChanges = async ( { file , env , changed } ) => { const source = config . get ( { field : "source.input" , env } ) ; const src = file || ( changed ? await getOnlyChangedFiles ( source ) : source ) ; if ( src . length !== 0 ) { try { await git . stash ( ) ; await exportFilesFromDbAsync ( { file : src , env , quiet : true } ) ; await git . unstash ( ) ; } catch ( error ) { console . log ( error . message ) ; } } } ; const compileAndMergeFilesToDb = async ( { file , env , changed , force } ) => { try { const results = await compileFilesToDbAsync ( { file , env , changed , force } ) ; if ( results . some ( ( file ) => file . errors && file . errors . hasDirt ( ) ) ) { mergeLocalAndDbChanges ( { file , env , changed } ) ; } } catch ( error ) { throw error ; } } ; export const compileTestTask = ( { env = argv . env || "DEV" } ) => { const input = config . get ( { field : "test.input" , env } ) ; return compileFilesToDbAsync ( { file : input , env } ) ; } ; const compileObjectToDb = async ( { file = argv . file , env = argv . env || "DEV" , object = argv . object , line = argv . line , user = argv . user , } ) => { try { if ( ! object ) { throw Error ( "Object cannot be empty." ) ; } let resp = await compileSelection ( object , file , env , line , < string > user ) ; printResults ( resp ) ; } catch ( err ) { console . error ( err . message ) ; } } ; export function compileTask ( { env = argv . env || "DEV" , file = argv . file , changed = ( argv . changed as boolean ) || false , object = argv . object , line = argv . line , force = argv . force , } ) { let s_force = force || config . get ( { field : "compile.force" , env } ) ; let b_force = s_force . toString ( ) === "true" ; if ( object ) { return compileObjectToDb ( { file , env , object , line } ) ; } else { return compileAndMergeFilesToDb ( { file , env , changed , force : b_force } ) ; } }