infra-cli
Version:
66 lines (51 loc) • 1.75 kB
JavaScript
const chalk = require('chalk')
const spawn = require('cross-spawn')
const { shouldUseYarn } = require('./util/utils')
function runAdd (targetDir, dependencies, devDependencies) {
try {
const useYarn = shouldUseYarn()
const command = useYarn ? 'yarn' : 'npm'
const args = useYarn ? ['add'] : ['install']
const gitProc = spawn.sync('git', ['init'], { stdio: 'inherit', cwd: targetDir })
if (gitProc.status !== 0) {
console.error('`git init` failed')
}
const dependenciesProc = spawn.sync(command, [...args, ...dependencies], { stdio: 'inherit', cwd: targetDir })
if (dependenciesProc.status !== 0) {
console.error(`\`${command} ${args.join(' ')}\` failed`)
}
const devDependenciesProc = spawn.sync(command, [...args, useYarn ? '--dev' : '--save-dev', ...devDependencies], {
stdio: 'inherit',
cwd: targetDir
})
if (devDependenciesProc.status !== 0) {
console.error(`\`${command} ${args.join(' ')}\` failed`)
}
} catch (error) {
console.error(chalk.redBright(error))
process.exit(1)
}
}
async function runInstall (targetDir) {
return new Promise((resolve, reject) => {
const useYarn = shouldUseYarn()
const command = useYarn ? 'yarn' : 'npm'
const args = ['install']
const gitProc = spawn.sync('git', ['init'], { stdio: 'inherit', cwd: targetDir })
if (gitProc.status !== 0) {
console.error('`git init` failed')
}
const child = spawn(command, args, { stdio: 'inherit', cwd: targetDir })
child.on('close', (code) => {
if (code !== 0) {
reject(new Error({ command: `${command} ${args.join(' ')}` }))
return
}
resolve()
})
})
}
module.exports = {
runInstall,
runAdd
}