UNPKG

1.39 kBJavaScriptView Raw
1'use strict';
2
3const chalk = require('chalk');
4const execa = require('execa');
5const ora = require('ora');
6
7function install(packages, currentState) {
8 if (!packages.length) {
9 return Promise.resolve(currentState);
10 }
11
12 const installer = currentState.get('installer');
13 const color = chalk.supportsColor ? '--color=always' : null;
14
15 const isYarn = installer === 'yarn';
16
17 const installGlobal = currentState.get('global') ? (isYarn ? 'global' : '--global'): null;
18 const saveExact = currentState.get('saveExact') ? (isYarn ? '--exact' : '--save-exact') : null;
19
20 const installCmd = isYarn ? 'add' : 'install';
21
22 const npmArgs = [installCmd]
23 .concat(installGlobal)
24 .concat(saveExact)
25 .concat(packages)
26 .concat(color)
27 .filter(Boolean);
28
29 console.log('');
30 console.log(`$ ${chalk.green(installer)} ${chalk.green(npmArgs.join(' '))}`);
31 const spinner = ora(`Installing using ${chalk.green(installer)}...`);
32 spinner.enabled = spinner.enabled && currentState.get('spinner');
33 spinner.start();
34
35 return execa(installer, npmArgs, {cwd: currentState.get('cwd')}).then(output => {
36 spinner.stop();
37 console.log(output.stdout);
38 console.log(output.stderr);
39
40 return currentState;
41 }).catch(err => {
42 spinner.stop();
43 throw err;
44 });
45}
46
47module.exports = install;