import { readCMSConfig } from '@cromwell/core-backend' ; import { each as asyncEach } from 'async' ; import { spawn , spawnSync } from 'child_process' ; import colorsdef from 'colors/safe' ; import fs from 'fs-extra' ; import nodeCleanup from 'node-cleanup' ; import path , { resolve } from 'path' ; import symlinkDir from 'symlink-dir' ; import { downloader } from './downloader' ; import { collectPackagesInfo , globPackages , hoistDependencies } from './shared' ; import { THoistedDeps , TPackage } from './types' ; const colors = colorsdef ; /**
 * DEPRECATED
 * Cromwella package manager.
 */ export const installer = async ( projectRootDir , installationMode , isProduction , forceInstall ) => { param includeInPath param includeToPath const makeSymlink = async ( includeInPath , includeToPath ) => { if ( ! fs . existsSync ( includeInPath ) ) { const includeInDir = resolve ( includeInPath , '../' ) ; if ( ! fs . existsSync ( includeInDir ) ) { try { fs . ensureDir ( includeInDir ) ; } catch ( e ) { console . log ( e ) ; } } try { await symlinkDir ( includeToPath , includeInPath ) ; } catch ( e ) { console . log ( e ) ; } } } ; param dir param pckg const createInstallPackage = ( dir , pckg ) => { const hoistedPackageJsonPath = resolve ( dir , 'package.json' ) ; const hoistedPackageJsonBackupPath = resolve ( dir , '_backup_package.json' ) ; removeInstallPackage ( dir ) ; if ( ! fs . existsSync ( hoistedPackageJsonPath ) ) return ; try { const bytes = fs . readFileSync ( hoistedPackageJsonPath ) ; fs . writeFileSync ( hoistedPackageJsonBackupPath , bytes ) ; const mainPackageContent = JSON . parse ( bytes . toString ( ) ) ; const hoistedPackageJson = { ... mainPackageContent , ... pckg } ; fs . writeFileSync ( hoistedPackageJsonPath , JSON . stringify ( hoistedPackageJson ) ) ; } catch ( e ) { console . log ( colors . brightRed ( 'Failed to create install package file for ' + dir ) ) ; console . log ( e ) ; removeInstallPackage ( dir ) ; } } ; param dir const removeInstallPackage = ( dir ) => { const hoistedPackageJsonPath = resolve ( dir , 'package.json' ) ; const hoistedPackageJsonBackupPath = resolve ( dir , '_backup_package.json' ) ; try { if ( fs . existsSync ( hoistedPackageJsonBackupPath ) ) { fs . writeFileSync ( hoistedPackageJsonPath , fs . readFileSync ( hoistedPackageJsonBackupPath ) ) ; fs . unlinkSync ( hoistedPackageJsonBackupPath ) ; } } catch ( e ) { console . log ( colors . brightRed ( 'Failed to remove install package file for ' + dir ) ) ; console . log ( e ) ; } } ; const packagePaths = await globPackages ( projectRootDir ) ; const packages = collectPackagesInfo ( packagePaths ) ; const main = async ( ) => { const installPaths = [ projectRootDir ] ; nodeCleanup ( function ( exitCode , signal ) { installPaths . forEach ( path => removeInstallPackage ( path ) ) ; } ) ; const { hoistedDependencies , hoistedDevDependencies } = hoistDependencies ( packages , isProduction , forceInstall ) ; if ( packages . length === 0 || ! hoistedDependencies && ! hoistedDevDependencies ) { console . log ( colors . brightRed ( `\nCromwella:: Error. No packages found\n` ) ) ; return ; } const rootModules = resolve ( projectRootDir , 'node_modules' ) ; for ( const pkg of packages ) { if ( pkg . path ) { const modulesPath = resolve ( path . dirname ( pkg . path ) , 'node_modules' ) ; const packageLockPath = resolve ( path . dirname ( pkg . path ) , 'package-lock.json' ) ; if ( modulesPath !== rootModules ) { if ( fs . existsSync ( modulesPath ) ) { try { await fs . remove ( modulesPath ) ; } catch ( e ) { console . log ( e ) ; } } } if ( fs . existsSync ( packageLockPath ) ) { try { fs . unlinkSync ( packageLockPath ) ; } catch ( e ) { console . log ( e ) ; } } } } createInstallPackage ( projectRootDir , { dependencies : hoistedDependencies ?. hoisted , devDependencies : hoistedDevDependencies ?. hoisted } ) ; const uniques = { } ; if ( hoistedDependencies ?. nonHoisted ) Object . entries ( hoistedDependencies . nonHoisted ) . forEach ( ( [ packagePath , packageModules ] ) => { if ( ! uniques [ packagePath ] ) { uniques [ packagePath ] = { } ; } uniques [ packagePath ] . deps = packageModules . modules ; } ) ; if ( hoistedDevDependencies ?. nonHoisted ) Object . entries ( hoistedDevDependencies . nonHoisted ) . forEach ( ( [ packagePath , packageModules ] ) => { if ( ! uniques [ packagePath ] ) { uniques [ packagePath ] = { } ; } uniques [ packagePath ] . devDeps = packageModules . modules ; } ) ; Object . keys ( uniques ) . forEach ( uni => { installPaths . push ( uni ) ; createInstallPackage ( uni , { dependencies : uniques [ uni ] . deps , devDependencies : uniques [ uni ] . devDeps } ) ; } ) ; asyncEach ( installPaths , ( path , callback ) => { console . log ( colors . cyan ( `\nCromwella:: Installing modules for: ${ colors . brightCyan ( `${ path }` ) } package in ${ colors . brightCyan ( `${ installationMode }` ) } mode...\n` ) ) ; const modeStr = isProduction ? ' --production' : '' ; try { const proc = spawn ( `npm install${ modeStr }` , { shell : true , cwd : path , stdio : 'inherit' } ) ; proc . on ( 'close' , ( code ) => { console . log ( colors . brightCyan ( `\nCromwella:: Installation for ${ path } package completed\n` ) ) ; removeInstallPackage ( path ) ; callback ( ) ; } ) ; } catch ( e ) { console . log ( colors . brightRed ( `\nCromwella:: Error. Failed to install node_modules for ${ path } package\n` ) ) ; } } , ( ) => onInstallationDone ( hoistedDependencies , hoistedDevDependencies , packages ) ) ; } ; const onInstallationDone = ( hoistedDependencies ? , hoistedDevDependencies ? , packages ? ) => { hoistedDependencies ?. localSymlinks ?. forEach ( link => { makeSymlink ( link . linkPath , link . referredDir ) ; } ) ; hoistedDevDependencies ?. localSymlinks ?. forEach ( link => { makeSymlink ( link . linkPath , link . referredDir ) ; } ) ; } ; const config = await readCMSConfig ( ) ; const packageManager = config ?. pm ?? 'yarn' ; if ( packageManager === 'cromwella' ) { try { main ( ) ; } catch ( e ) { console . log ( e ) ; } } if ( packageManager === 'yarn' ) { spawnSync ( `yarn ${ process . argv . slice ( 3 ) . join ( ' ' ) }` , { shell : true , cwd : process . cwd ( ) , stdio : 'inherit' } ) ; } downloader ( { rootDir : projectRootDir , packages : packages } ) ; } ;