-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstallPackages.js
29 lines (25 loc) · 1.16 KB
/
installPackages.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const Path = require('path')
const FS = require('fs-extra')
const Spawn = require('cross-spawn')
const { resolve, coroutine, runNode } = require('creed')
const accessibleFile = (path) => runNode(FS.access, path).map(() => path).catch(() => null)
const installPackages = coroutine(function * installPackage(projectPath, packageNames, { dev = false, useYarn = false } = {}) {
const appPackage = yield FS.readJSON(Path.join(projectPath, 'package.json'))
const dependencies = (dev ? appPackage.devDependencies : appPackage.dependencies) || {}
const needInstalling = packageNames.filter(packageName => !dependencies[packageName])
if (needInstalling.length === 0) {
return
}
useYarn = !!(yield accessibleFile(Path.join(projectPath, 'yarn.lock'))) || useYarn
const command = useYarn ? 'yarnpkg' : 'npm'
let args = useYarn ? ['add'].concat(dev ? ['--dev'] : []) : ['install', dev ? '--save' : '--save-dev']
args.push.apply(args, needInstalling)
const proc = Spawn.sync(command, args, {
cwd: projectPath,
stdio: 'inherit'
})
if (proc.status !== 0) {
throw new Error(`\`${command} ${args.join(' ')}\` failed with status ${proc.status}`)
}
})
module.exports = installPackages