import * as fs from 'fs-extra' ; import * as _ from 'lodash' ; import * as path from 'path' ; import { logInfo , logWarn , logFatal } from './log' ; import { plugin } from './plugins' ; import { priEvent } from './pri-events' ; import { freshGlobalState , globalState } from './global-state' ; export const ensureFiles = async ( ) => { if ( ! globalState . sourceConfig . ensureFiles ) { return ; } priEvent . emit ( 'beforeEnsureFiles' ) ; const ensureProjectFilesQueueGroupByPath = _ . groupBy ( plugin . ensureProjectFilesQueue , 'fileName' ) ; await Promise . all ( Object . keys ( ensureProjectFilesQueueGroupByPath ) . map ( async ( filePath ) => { const ensureProjectFilesQueue = ensureProjectFilesQueueGroupByPath [ filePath ] ; await ensureFile ( filePath , ensureProjectFilesQueue . map ( ensureProjectFiles => { return ensureProjectFiles . pipeContent ; } ) ) ; } ) ) ; await freshGlobalState ( globalState . selectedSourceType ) ; } ; export async function ensureFile ( filePath , pipeContents ) { if ( ! path . isAbsolute ( filePath ) ) { logFatal ( `Plugin error: ensureProjectFiles path need be absolute path, not: ${ filePath }` ) ; } const fileExist = fs . existsSync ( filePath ) ; let exitFileContent = '' ; try { exitFileContent = fs . readFileSync ( filePath , 'utf8' ) . toString ( ) ; } catch ( error ) { } const nextContent = await pipeContents . reduce ( async ( preContent , pipeContent ) => { return Promise . resolve ( pipeContent ( await preContent ) ) ; } , Promise . resolve ( exitFileContent ) ) ; if ( fileExist ) { if ( exitFileContent === nextContent ) { } else { logWarn ( `${ filePath } exist, but the content is not correct, has been recovered.` ) ; } } else { logInfo ( `${ filePath } not exist, created.` ) ; } fs . outputFileSync ( filePath , nextContent ) ; }