1 |
|
2 |
|
3 |
|
4 | 'use strict';
|
5 |
|
6 | const chalk = require('chalk');
|
7 | const spawn = require('cross-spawn');
|
8 | const fs = require('fs');
|
9 | const path = require('path');
|
10 | const semver = require('semver');
|
11 | const util = require('../../helpers/util');
|
12 |
|
13 |
|
14 | module.exports = {
|
15 | packageConfig: require(`${__dirname}/../../package`),
|
16 |
|
17 |
|
18 |
|
19 | echo: console.log,
|
20 |
|
21 |
|
22 |
|
23 | error: function(message) {
|
24 | if (message) {
|
25 | this.echo(chalk.red(`\n ${message}`));
|
26 | }
|
27 |
|
28 | return process ? process.exit(1) : undefined;
|
29 | },
|
30 |
|
31 |
|
32 |
|
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 |
|
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`);
|
69 | bin = `${base}/${packageConfig.bin.gulp}`;
|
70 | params.push('--cwd', context.cwd);
|
71 | break;
|
72 |
|
73 | case 'grunt':
|
74 | packageConfig = require(`${gruntCli}/package`);
|
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,
|
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 |
|
102 | return this.error('Build tool not found. Please run `npm install`');
|
103 |
|
104 | }
|
105 | };
|