import * as colors from 'colors' ; import * as path from 'path' ; import * as mkdirpCallback from 'mkdirp' ; import { readFile as readFileCallback , writeFile as writeFileCallback , exists as existsCallback , readdir as readdirCallback , } from 'fs' ; import { ncp as ncpCallback } from 'ncp' ; import * as rimrafCallback from 'rimraf' ; import { promisify } from 'util' ; import { ConfigManager } from './../configManager' ; import * as spinner from './../cli/logIndicator' ; import { GitIgnoreManager } from '../gitignoreManager' ; const exists = promisify ( existsCallback ) ; const mkdirp = promisify ( mkdirpCallback ) ; const writeFile = promisify ( writeFileCallback ) ; const readFile = promisify ( readFileCallback ) ; const readdir = promisify ( readdirCallback ) ; const rimraf = promisify ( rimrafCallback ) ; const ncp = promisify ( ncpCallback ) ; /** Default encoding */ const ENCODING = 'utf8' ; /** Recommended Lamington package scripts */ const DEFAULT_SCRIPTS = { build : 'lamington build' , test : 'lamington test' , } ; /** Required project dependencies */ const DEFAULT_DEV_DEPENDENCIES = { lamington : 'latest' , chai : 'latest' , '@types/chai' : 'latest' , '@types/mocha' : 'latest' , } ; author Mitch Pierias <github.com/MitchPierias>
  export class ProjectManager { hidden private static cache : { scripts ? : { [ key ] : string ; } ; devDependencies ? : { [ key ] : string ; } ; } ; author Mitch Pierias <github.com/MitchPierias>
      public static async initWithDefaults ( ) { await ProjectManager . cloneExampleProject ( ) ; await ProjectManager . loadExistingProject ( ) ; await ProjectManager . injectScripts ( ) ; await ProjectManager . configureDependencies ( ) ; await ProjectManager . createDirectoryIfMissing ( '.lamington' ) ; await writeFile ( path . join ( process . cwd ( ) , 'package.json' ) , JSON . stringify ( ProjectManager . cache , null , 4 ) , ENCODING ) ; await ConfigManager . createConfigIfMissing ( ) ; await GitIgnoreManager . createIfMissing ( ) ; } author Mitch Pierias <github.com/MitchPierias>
     *  hidden private static async loadExistingProject ( ) { let packageJson = '' ; try { packageJson = await readFile ( path . join ( process . cwd ( ) , 'package.json' ) , ENCODING ) ; } catch ( error ) { console . error ( ) ; console . error ( colors . red ( 'Could not read project package.json file in this folder. Is this a node project folder?' ) ) ; console . error ( ) ; process . exit ( 1 ) ; } ProjectManager . cache = JSON . parse ( packageJson ) ; } author Mitch Pierias <github.com/MitchPierias>
     *  hidden private static async injectScripts ( ) { spinner . create ( 'Injecting recommended scripts' ) ; const existingScripts = ProjectManager . cache . scripts || { } ; ProjectManager . cache . scripts = { ... existingScripts , ... DEFAULT_SCRIPTS } ; spinner . end ( 'Added recommended scripts' ) ; } author Mitch Pierias <github.com/MitchPierias>
     *  hidden private static async configureDependencies ( ) { spinner . create ( 'Adding Lamington dependencies' ) ; const existingDependencies = ProjectManager . cache . devDependencies || { } ; ProjectManager . cache . devDependencies = { ... DEFAULT_DEV_DEPENDENCIES , ... existingDependencies } ; spinner . end ( 'Added required dependencies' ) ; } author Mitch Pierias <github.com/MitchPierias>
     *  hidden private static async cloneExampleProject ( ) { spinner . create ( 'Pulling example project' ) ; await ProjectManager . createDirectoryIfMissing ( 'contracts' ) ; const files = await readdir ( path . join ( process . cwd ( ) , 'contracts' ) ) ; if ( files . length > 0 ) return spinner . end ( 'Existing contracts found' ) ; try { const got = require ( 'got' ) ; const tar = require ( 'tar' ) ; const cloneUrl = `https://codeload.github.com/MitchPierias/EOSIO-Lamington-Boilerplate/tar.gz/master` ; spinner . update ( 'Cloning example project' ) ; return new Promise ( async ( resolve , reject ) => { const tmpPath = await ProjectManager . createDirectoryIfMissing ( '__tmp__' ) ; got . stream ( cloneUrl ) . pipe ( tar . extract ( { cwd : tmpPath , strip : 1 , } ) ) . on ( 'error' , ( error ) => { reject ( error ) ; } ) . on ( 'end' , async ( ) => { const clonedFiles = await readdir ( tmpPath ) ; if ( clonedFiles . length <= 0 ) throw new Error ( `No files cloned from repo ${ cloneUrl }` ) ; await ncp ( path . join ( tmpPath , 'contracts' ) , path . join ( process . cwd ( ) , 'contracts' ) ) ; spinner . update ( 'Cleaning temporary files' ) ; await rimraf ( tmpPath ) ; spinner . end ( 'Created example contracts' ) ; resolve ( true ) ; } ) ; } ) ; } catch ( error ) { spinner . fail ( 'Failed to clone repository' ) ; console . log ( error ) ; } } author Mitch Pierias <github.com/MitchPierias>
     *  param dirName returns private private static async createDirectoryIfMissing ( dirName ) { const dirPath = path . join ( process . cwd ( ) , dirName ) ; if ( ! ( await exists ( dirPath ) ) ) await mkdirp ( path . join ( process . cwd ( ) , dirName ) , { } ) ; return dirPath ; } }