rsnext/packages/create-next-app/helpers/install.ts
Joe Haddad 104d18c607
Rewrite Create Next App (#8030)
* Completely rewrite Create Next App

* Sort imports

* Show what package manager is being used

* Fix project name suggestion

* Update tests for new implementation

* Use normal prepublish command for on-install

* Upgrade Node version

* Switch to 8.16

* Disable v8 cache

* Swap out update check package and fix CLI boot
2019-07-19 15:55:30 -04:00

48 lines
1.2 KiB
TypeScript

import chalk from 'chalk'
import spawn from 'cross-spawn'
export function install(
root: string,
dependencies: string[] | null,
{ useYarn, isOnline }: { useYarn: boolean; isOnline: boolean }
) {
return new Promise((resolve, reject) => {
let command: string
let args: string[]
if (useYarn) {
command = 'yarnpkg'
args = dependencies ? ['add', '--exact'] : ['install']
if (!isOnline) {
args.push('--offline')
}
if (dependencies) {
args.push(...dependencies)
}
args.push('--cwd', root)
if (!isOnline) {
console.log(chalk.yellow('You appear to be offline.'))
console.log(chalk.yellow('Falling back to the local Yarn cache.'))
console.log()
}
} else {
command = 'npm'
args = ([
'install',
dependencies && '--save',
dependencies && '--save-exact',
'--loglevel',
'error',
].filter(Boolean) as string[]).concat(dependencies || [])
}
const child = spawn(command, args, { stdio: 'inherit' })
child.on('close', code => {
if (code !== 0) {
reject({ command: `${command} ${args.join(' ')}` })
return
}
resolve()
})
})
}