UNPKG

2.91 kBJavaScriptView Raw
1//-------------------------------------
2//-- CLI helpers
3//-------------------------------------
4'use strict';
5
6const chalk = require('chalk');
7const spawn = require('cross-spawn');
8const fs = require('fs');
9const path = require('path');
10const semver = require('semver');
11const util = require('../../helpers/util');
12
13//-- PUBLIC
14module.exports = {
15 packageConfig: require(`${__dirname}/../../package`), // eslint-disable-line global-require
16
17
18 //-- Echo
19 echo: console.log, // eslint-disable-line no-console
20
21
22 //-- Error
23 error: function(message) {
24 if (message) {
25 this.echo(chalk.red(`\n ${message}`));
26 }
27
28 return process ? process.exit(1) : undefined; // eslint-disable-line no-process-exit, unicorn/no-process-exit
29 },
30
31
32 //-- Usage
33 usage: function() {
34 return this.echo(`
35 Usage: ${chalk.yellow('nwayo')} ${chalk.cyan('<command>')}
36
37 ${chalk.underline('Project commands')}
38 ${chalk.yellow('run')} [${chalk.cyan('<task>')} [${chalk.cyan('<bundle>')}]] Run a task
39 ${chalk.yellow('rebuild')} [${chalk.cyan('<bundle>')}] Run rebuild task
40 ${chalk.yellow('watch')} [${chalk.cyan('<bundle>')}] Run watch task
41 ${chalk.yellow('install')} [${chalk.cyan('<scope>')}] [${chalk.yellow('--force')}] Install dependencies ex:[workflow|vendors]
42 ${chalk.yellow('doctor')} Diagnose project dependencies
43
44 ${chalk.underline('Global commands')}
45${util.usageTasks}
46 Legacy mode
47 nwayo@${this.packageConfig.version} ${path.normalize(`${__dirname}/../`)}
48 `);
49 },
50
51
52
53 //-- Run
54 run: function(task, context) {
55 const tool = semver.lt(context.packageConfig.nwayo.version, '2.2.0') ? 'grunt' : 'gulp';
56 const base = `${context.cwd}/node_modules/${tool}`;
57
58 if (fs.existsSync(`${base}/package.json`)) {
59 const params = [task];
60
61 const gruntCli = `${__dirname}/../../node_modules/grunt-cli`;
62 let packageConfig;
63 let bin = '';
64
65 switch (tool) {
66
67 case 'gulp':
68 packageConfig = require(`${base}/package`); // eslint-disable-line global-require
69 bin = `${base}/${packageConfig.bin.gulp}`;
70 params.push('--cwd', context.cwd);
71 break;
72
73 case 'grunt':
74 packageConfig = require(`${gruntCli}/package`); // eslint-disable-line global-require
75
76 bin = `${gruntCli}/${packageConfig.bin.grunt}`;
77 params.push('--gruntfile', `${context.cwd}/gruntfile.js`);
78 break;
79
80 default: break;
81
82 }
83
84 if (context.flags) {
85 context.flags.forEach((value, flag) => {
86 params.push(`--${flag}`, value);
87 });
88 }
89
90 const cmd = spawn(`${bin}`, params, {
91 env: process.env, // eslint-disable-line no-process-env, unicorn/prevent-abbreviations
92 stdio: 'inherit'
93 });
94
95 return cmd.on('close', (code) => {
96 return code && code !== 65 ? this.echo(code) : undefined;
97 });
98
99 }
100
101 // Shouldn't be used anymore
102 return this.error('Build tool not found. Please run `npm install`');
103
104 }
105};