import * as deepExtend from 'deep-extend' ; import * as fs from 'fs' ; import * as path from 'path' ; import * as writePkg from 'write-pkg' ; import conf from '../services/conf' ; import { debug } from '../services/logger' ; import npm from '../services/npm' ; import _ from '../util' ; import ide from './ide' ; import { MRDA } from './message' ; const { resolve , join } = path ; const { RdTypes , rdcConfName , } = conf ; class Sync { public get appConf ( ) : AppConf { return require ( resolve ( conf . cwd , conf . appConfName ) ) ; } public get from ( ) { if ( conf . isApp ) { const { container } = conf . getAppConf ( ) ; return container . name ; } if ( conf . rdType === RdTypes . Container ) { const rdcConfPath = resolve ( conf . cwd , rdcConfName ) ; const rdcConf = require ( rdcConfPath ) ; return rdcConf . nodeVersion || 'node:latest' ; } } /**
     * sync all
     * used for local dev env, resync is needed when container updates
     * 1. generate Dockerfile to local .cache
     * 2. generate docker-compose to local .cache
     *
     * if rdType is app
     * need to gen extra staged files
     */ public async start ( { skipInstall = false , npmCmd = 'npm' } ) { await _ . asyncSpawn ( 'mkdir' , [ '-p' , conf . localCacheDir ] , { } ) ; await _ . asyncExec ( 'rm -rf app/node_modules' ) ; await _ . asyncExec ( `rm -rf ${ conf . templateDir }/node_modules` ) ; if ( conf . isApp ) { await npm . pull ( this . appConf . container . name ) ; await this . genAppStagedFiles ( ) ; } else { await _ . copy ( resolve ( conf . templateDir , 'package.json' ) , resolve ( conf . localCacheDir , 'package.json' ) , { } ) ; } if ( ! skipInstall ) { await this . install ( npmCmd ) ; } await ide . initSettings ( conf . isApp ) ; await this . registerHooks ( ) ; } /**
     * 1. copy template/package.json to .cache/package.json
     * 2. copy rde/rdc.config.js to .cache/rdc.config.js
     * 3. merge app optional pkg.json & rdc package.json
     */ public async genAppStagedFiles ( createRdc = null ) { const { cwd , rdcConfName , npmPkgDir , templateDir , localCacheDir , } = conf ; const image = createRdc || this . appConf . container . name ; await npm . copy ( [ { from : resolve ( npmPkgDir , image , templateDir , 'package.json' ) , to : resolve ( localCacheDir , 'package.json' ) , } , { from : resolve ( npmPkgDir , image , templateDir ) , to : resolve ( cwd , templateDir ) , } , { from : resolve ( npmPkgDir , image , rdcConfName ) , to : resolve ( localCacheDir , rdcConfName ) , } ] ) ; const abcJsonPath = resolve ( npmPkgDir , image , 'abc.json' ) ; if ( fs . existsSync ( abcJsonPath ) ) { await npm . copy ( [ { from : abcJsonPath , to : resolve ( cwd , 'abc.json' ) , } ] ) ; } if ( ! createRdc ) { await this . mergePkgJson ( resolve ( cwd , 'app' , 'package.json' ) , resolve ( cwd , localCacheDir , 'package.json' ) , resolve ( localCacheDir ) ) ; } } public async registerHooks ( ) { if ( ! fs . existsSync ( '.git' ) ) { await _ . asyncExec ( 'git init' ) ; } fs . writeFileSync ( join ( '.git' , 'hooks' , 'pre-commit' ) , `#!/bin/sh

rde lint -s
      ` , { encoding : 'UTF-8' , mode : '755' } ) ; fs . writeFileSync ( join ( '.git' , 'hooks' , 'commit-msg' ) , `#!/bin/sh

commitMsg=$(cat $1)
rde lint -m "$commitMsg"
      ` , { encoding : 'UTF-8' , mode : '755' } ) ; } public async mergePkgJson ( appPkgPath , rdcPkgPath , destPath ) { debug ( 'appPkgPath' , appPkgPath ) ; debug ( 'rdcPkgPath' , rdcPkgPath ) ; let pkgJson = require ( rdcPkgPath ) ; if ( fs . existsSync ( appPkgPath ) && fs . statSync ( appPkgPath ) . isFile ( ) ) { const appPkgJson = require ( appPkgPath ) ; deepExtend ( pkgJson , appPkgJson ) ; } await writePkg ( destPath , pkgJson ) ; return pkgJson ; } public async install ( npmCmd ? ) { const { cwd , RdTypes , templateDir , localCacheDir , } = conf ; if ( ! npmCmd ) { npmCmd = 'npm' ; } debug ( `npm Cmd: ${ npmCmd }` ) ; if ( conf . isApp ) { const cachePkgJsonPath = resolve ( localCacheDir , 'package.json' ) ; if ( ! fs . existsSync ( cachePkgJsonPath ) ) { throw Error ( MRDA . NO_PKG_IN_CACHE_DIR ) ; } const symPkgJsonPath = resolve ( cwd , 'package.json' ) ; if ( fs . existsSync ( symPkgJsonPath ) ) { fs . unlinkSync ( symPkgJsonPath ) ; } const appNpmRcPath = resolve ( 'app' , '.npmrc' ) ; const rdcNpmRcPath = resolve ( templateDir , '.npmrc' ) ; if ( fs . existsSync ( rdcNpmRcPath ) ) { await _ . asyncExec ( `cp -f ${ rdcNpmRcPath } ${ cwd }` ) ; } if ( fs . existsSync ( appNpmRcPath ) ) { await _ . asyncExec ( `cp -f ${ appNpmRcPath } ${ cwd }` ) ; } fs . symlinkSync ( cachePkgJsonPath , symPkgJsonPath ) ; await _ . asyncSpawn ( npmCmd , [ 'i' , '--package-lock' , 'false' ] , { cwd , } ) ; fs . unlinkSync ( symPkgJsonPath ) ; } if ( conf . rdType === RdTypes . Container ) { const runtimePkgJsonPath = resolve ( templateDir , 'package.json' ) ; const runtimeNpmRcPath = resolve ( templateDir , '.npmrc' ) ; const symPkgJsonPath = resolve ( cwd , 'package.json' ) ; if ( fs . existsSync ( symPkgJsonPath ) ) { fs . unlinkSync ( symPkgJsonPath ) ; } if ( fs . existsSync ( runtimeNpmRcPath ) ) { await _ . asyncExec ( `cp -f ${ runtimeNpmRcPath } ${ cwd }` ) ; } fs . symlinkSync ( runtimePkgJsonPath , symPkgJsonPath ) ; await _ . asyncSpawn ( npmCmd , [ 'i' , '--package-lock' , 'false' ] , { cwd , } ) ; fs . unlinkSync ( symPkgJsonPath ) ; } if ( fs . existsSync ( resolve ( cwd , '.npmrc' ) ) ) { await _ . asyncExec ( 'rm -rf .npmrc' ) ; } } } export default new Sync ( ) ;