forked from kriasoft/react-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.js
103 lines (89 loc) · 2.74 KB
/
run.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* React App SDK (https://github.com/kriasoft/react-app)
*
* Copyright © 2015-present Kriasoft, LLC. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE.txt file in the root directory of this source tree.
*/
/* eslint-disable no-console, global-require */
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
/**
* Find an automation script by its name. Given the following list of files:
*
* ./node_modules/react-app-tools/scripts/build.js
* ./node_modules/react-app-tools/scripts/start.js
* ./scripts/start.js
*
* It should return:
*
* findScript('build') => './node_modules/react-app-tools/scripts/build.js'
* findScript('start') => './scripts/start.js'
* findScript('foo') => null
*/
const findScript = (() => {
const files = [];
return name => {
if (!files.length) {
try {
files.push(...fs.readdirSync(path.join(process.cwd(), 'scripts'))
.map(file => path.join(process.cwd(), 'scripts', file)));
} catch (err) {
if (process.env.VERBOSE === 'true') {
console.log(`INFO: No scripts found in ${path.join(process.cwd(), 'scripts')}`);
}
}
try {
files.push(...fs.readdirSync(path.join(__dirname, 'scripts'))
.map(file => path.join(path.join(__dirname, 'scripts', file))));
} catch (err) {
console.warn('WARNING: Cannot find \'react-app-tools\' npm module.');
}
}
return files.find(x => path.basename(x) === `${name}.js`);
};
})();
const findConfig = (() => {
let config;
return () => {
if (config) return config;
try {
config = require(path.resolve(process.cwd(), './config.js'));
} catch (err) {
config = {};
}
const webpackConfig = require('./webpack.config');
if (typeof config.webpack === 'function') {
config.webpack = config.webpack(webpackConfig) || webpackConfig;
} else {
config.webpack = webpackConfig;
}
return config;
};
})();
/**
* Find and execute a script. For example: run('clean').then(() => console.log('Done!');
*
* Starting 'clean'...
* Finished 'clean' after 45ms
* Done!
*/
function run(command) {
const script = findScript(command);
const config = findConfig();
if (!script) {
console.error(`ERROR: File not found: scripts/${command}.js`);
process.exit(1);
}
const start = new Date();
console.log(`Starting '${chalk.bold.gray(command)}'...`);
return Promise.resolve().then(() => require(script)(config)).then(() => {
console.log(
`Finished '${chalk.bold.gray(command)}' after ` +
`${chalk.bold(new Date().getTime() - start.getTime())}ms`
);
}, err => console.error(err.stack));
}
module.exports = run;