|
1 | 1 | 'use strict' |
2 | | -const path = require('path') |
| 2 | + |
3 | 3 | const chalk = require('chalk') |
4 | 4 | const spawn = require('cross-spawn') |
5 | | -const getPackageName = require('./getPackageName') |
6 | 5 | const console = require('console') |
7 | | -const process = require('process') |
| 6 | +const path = require('path') |
| 7 | + |
| 8 | +const getPackageName = require('./getPackageName') |
8 | 9 |
|
9 | | -module.exports = function installScripts (appFolder, appName, flavor, verbose) { |
10 | | - const originalDirectory = process.cwd() |
11 | | - process.chdir(appFolder) |
| 10 | +module.exports = function installScripts (appFolder, appName, options) { |
| 11 | + const verbose = options.verbose |
| 12 | + const flavor = options.flavor |
| 13 | + const cli = options.cli |
12 | 14 |
|
13 | | - // Find the right version |
| 15 | + // Check if the the the flavor to be used is local |
14 | 16 | const local = /^\.\/|^\//.test(flavor) |
| 17 | + // Get the right name of the flavor package |
15 | 18 | const packageName = getPackageName(flavor) |
16 | | - |
17 | 19 | // Install dependencies |
| 20 | + const args = { |
| 21 | + npm: [ |
| 22 | + 'install', |
| 23 | + verbose && '--verbose', |
| 24 | + '--save-dev', |
| 25 | + '--save-exact', |
| 26 | + flavor |
| 27 | + ].filter(a => a), |
| 28 | + yarn: [ |
| 29 | + 'add', |
| 30 | + '--exact', |
| 31 | + verbose && '--verbose', |
| 32 | + local ? 'file:' + flavor : flavor |
| 33 | + ].filter(a => a) |
| 34 | + } |
| 35 | + |
| 36 | + // Trigger npm installation |
18 | 37 | console.log(chalk.green('Installing packages. This might take a couple minutes.')) |
19 | 38 | console.log(chalk.green(`Installing ${packageName} from ${(local ? 'local' : 'npm')} ...`)) |
20 | 39 | console.log() |
21 | 40 |
|
22 | | - const args = [ |
23 | | - 'install', |
24 | | - verbose && '--verbose', |
25 | | - '--save-dev', |
26 | | - '--save-exact', |
27 | | - local ? path.resolve(originalDirectory, flavor) : flavor |
28 | | - ].filter(function (a) { return a }) |
29 | | - |
30 | | - // Trigger npm installation |
31 | | - const proc = spawn('npm', args, {stdio: 'inherit'}) |
| 41 | + const proc = spawn(cli, args[cli], {stdio: 'inherit', cwd: appFolder}) |
| 42 | + proc.on('error', (e) => console.log(e)) |
32 | 43 | proc.on('close', function (code) { |
33 | 44 | if (code !== 0) { |
34 | | - console.error(chalk.red(`npm \`${args.join(' ')}\` failed`)) |
| 45 | + console.error(chalk.red(`${cli} \`${args[cli].join(' ')}\` failed`)) |
35 | 46 | return |
36 | 47 | } |
37 | 48 |
|
38 | 49 | const initScriptPath = path.resolve( |
39 | | - process.cwd(), |
| 50 | + appFolder, |
40 | 51 | 'node_modules', |
41 | 52 | packageName, |
42 | 53 | 'scripts', |
43 | 54 | 'init.js' |
44 | 55 | ) |
45 | 56 | const init = require(initScriptPath) |
46 | 57 |
|
47 | | - // Execute the cycle-scripts's specific initialization |
48 | | - init(appFolder, appName, verbose, originalDirectory) |
| 58 | + // Execute the flavor's specific initialization |
| 59 | + init(appFolder, appName, options) |
49 | 60 | }) |
50 | 61 | } |
0 commit comments