UNPKG

12.6 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.commonTags = exports.crossEnv = exports.open = exports.mkdirp = exports.copy = exports.ifNotWindows = exports.ifWindows = exports.rimraf = exports.runInNewWindow = exports.series = exports.concurrent = undefined;
7
8var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
9
10var _path = require('path');
11
12var _path2 = _interopRequireDefault(_path);
13
14var _commonTags = require('common-tags');
15
16var commonTags = _interopRequireWildcard(_commonTags);
17
18function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
19
20function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
22function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
23
24var defaultColors = ['bgBlue.bold', 'bgMagenta.bold', 'bgGreen.bold', 'bgBlack.bold', 'bgCyan.bold', 'bgRed.bold', 'bgWhite.bold', 'bgYellow.bold'];
25
26exports.concurrent = concurrent;
27exports.series = series;
28exports.runInNewWindow = runInNewWindow;
29exports.rimraf = rimraf;
30exports.ifWindows = ifWindows;
31exports.ifNotWindows = ifNotWindows;
32exports.copy = copy;
33exports.mkdirp = mkdirp;
34exports.open = open;
35exports.crossEnv = crossEnv;
36exports.commonTags = commonTags;
37
38/**
39 * Accepts any number of scripts, filters out any
40 * falsy ones and joins them with ' && '
41 * @param {...string} scripts - Any number of strings representing commands
42 * @example
43 * // returns 'eslint && jest && webpack --env.production'
44 * series('eslint', 'jest', 'webpack --env.production')
45 * @return {string} - the command that will execute the given scripts in series
46 */
47
48function series() {
49 for (var _len = arguments.length, scripts = Array(_len), _key = 0; _key < _len; _key++) {
50 scripts[_key] = arguments[_key];
51 }
52
53 return scripts.filter(Boolean).join(' && ');
54}
55
56/**
57 * Accepts any number of nps script names, filters out
58 * any falsy ones, prepends `nps` to them, and passes
59 * the that to `series`
60 * @param {...string} scriptNames - the script names to run
61 * // returns 'nps lint && nps "test --coverage" && nps build'
62 * series.nps('lint', 'test --coverage', 'build')
63 * @return {string} - the command that will execute the nps scripts in series
64 */
65series.nps = function seriesNPS() {
66 for (var _len2 = arguments.length, scriptNames = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
67 scriptNames[_key2] = arguments[_key2];
68 }
69
70 return series.apply(undefined, _toConsumableArray(scriptNames.filter(Boolean).map(function (scriptName) {
71 return scriptName.trim();
72 }).filter(Boolean).map(function (scriptName) {
73 return `nps ${quoteScript(scriptName)}`;
74 })));
75};
76
77/**
78 * A concurrent script object
79 * @typedef {Object|string} ConcurrentScript
80 * @property {string} script - the command to run
81 * @property {string} color - the color to use
82 * (see concurrently's docs for valid values)
83 */
84/**
85 * An object of concurrent script objects
86 * @typedef {Object.<ConcurrentScript>} ConcurrentScripts
87 */
88
89/**
90 * Generates a command that uses `concurrently` to run
91 * scripts concurrently. Adds a few flags to make it
92 * behave as you probably want (like --kill-others-on-fail).
93 * In addition, it adds color and labels where the color
94 * can be specified or is defaulted and the label is based
95 * on the key for the script.
96 * @param {ConcurrentScripts} scripts - the scripts to run
97 * note: this function filters out falsy values :)
98 * @example
99 * // returns a bit of a long script that can vary slightly
100 * // based on your environment... :)
101 * concurrent({
102 * lint: {
103 * script: 'eslint .',
104 * color: 'bgGreen.white.dim',
105 * },
106 * test: 'jest',
107 * build: {
108 * script: 'webpack'
109 * }
110 * })
111 * @return {string} - the command to run
112 */
113function concurrent(scripts) {
114 var _Object$keys$reduce = Object.keys(scripts).reduce(reduceScripts, {
115 colors: [],
116 scripts: [],
117 names: []
118 }),
119 colors = _Object$keys$reduce.colors,
120 quotedScripts = _Object$keys$reduce.scripts,
121 names = _Object$keys$reduce.names;
122
123 var flags = ['--kill-others-on-fail', `--prefix-colors "${colors.join(',')}"`, '--prefix "[{name}]"', `--names "${names.join(',')}"`, shellEscape(quotedScripts)];
124 var concurrently = getBin('concurrently');
125 return `${concurrently} ${flags.join(' ')}`;
126
127 function reduceScripts(accumulator, scriptName, index) {
128 var scriptObj = scripts[scriptName];
129 if (!scriptObj) {
130 return accumulator;
131 } else if (typeof scriptObj === 'string') {
132 scriptObj = { script: scriptObj };
133 }
134 var _scriptObj = scriptObj,
135 script = _scriptObj.script,
136 _scriptObj$color = _scriptObj.color,
137 color = _scriptObj$color === undefined ? defaultColors[index % defaultColors.length] : _scriptObj$color;
138
139 if (!script) {
140 return accumulator;
141 }
142 accumulator.names.push(scriptName);
143 accumulator.colors.push(color);
144 accumulator.scripts.push(script);
145 return accumulator;
146 }
147}
148
149/**
150 * Accepts any number of nps script names, filters out
151 * any falsy ones, prepends `nps` to them, and passes
152 * the that to `concurrent`
153 * @param {...string} scriptNames - the script names to run
154 * @example
155 * // will basically return `nps lint & nps "test --coverage" & nps build`
156 * // but with the concurrently command and relevant flags to make
157 * // it super awesome with colors and whatnot. :)
158 * concurrent.nps('lint', 'test --coverage', 'build')
159 * @return {string} the command to run
160 */
161concurrent.nps = function concurrentNPS() {
162 for (var _len3 = arguments.length, scriptNames = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
163 scriptNames[_key3] = arguments[_key3];
164 }
165
166 return concurrent(scriptNames.map(mapNPSScripts).reduce(reduceNPSScripts, {}));
167
168 function mapNPSScripts(scriptName, index) {
169 var color = defaultColors[index];
170 if (!Boolean(scriptName)) {
171 return undefined;
172 } else if (typeof scriptName === 'string') {
173 return { script: scriptName, color };
174 } else {
175 return Object.assign({ color }, scriptName);
176 }
177 }
178
179 function reduceNPSScripts(scripts, scriptObj) {
180 if (!scriptObj) {
181 return scripts;
182 }
183 var color = scriptObj.color,
184 script = scriptObj.script;
185
186 var _script$split = script.split(' '),
187 _script$split2 = _slicedToArray(_script$split, 1),
188 name = _script$split2[0];
189
190 scripts[name] = {
191 script: `nps ${quoteScript(script.trim())}`,
192 color
193 };
194 return scripts;
195 }
196};
197
198/**
199 * EXPERIMENTAL: THIS DOES NOT CURRENTLY WORK FOR ALL TERMINALS
200 * Takes a command and returns a version that should run in
201 * another tab/window of your terminal. Currently only supports
202 * Windows cmd (new window) and Terminal.app (new tab)
203 * @param {string} command - the command to run in a new tab/window
204 * @example
205 * // returns some voodoo magic to make the terminal do what you want
206 * runInNewWindow('echo hello')
207 * @return {string} - the command to run
208 */
209function runInNewWindow(command) {
210 return isWindows() ? `start cmd /k "cd ${process.cwd()} && ${command}"` : commonTags.oneLine`
211 osascript
212 -e 'tell application "Terminal"'
213 -e 'tell application "System Events"
214 to keystroke "t" using {command down}'
215 -e 'do script "cd ${process.cwd()} && ${command}" in front window'
216 -e 'end tell'
217 `;
218}
219
220/**
221 * EXPERIMENTAL: THIS DOES NOT CURRENTLY WORK FOR ALL TERMINALS
222 * Takes an nps script name and prepends it with a call to nps
223 * then forwards that to `runInNewWindow` properly escaped.
224 * @param {string} scriptName - the name of the nps script to run
225 * @example
226 * // returns a script that runs
227 * // `node node_modules/.bin/nps "lint --cache"`
228 * // in a new tab/window
229 * runInNewWindow.nps('lint --cache')
230 * @return {string} - the command to run
231 */
232runInNewWindow.nps = function runInNewWindowNPS(scriptName) {
233 var escaped = true;
234 return runInNewWindow(`node node_modules/.bin/nps ${quoteScript(scriptName, escaped)}`);
235};
236
237/**
238 * Gets a script that uses the rimraf binary. rimraf
239 * is a dependency of nps-utils, so you don't need to
240 * install it yourself.
241 * @param {string} args - args to pass to rimraf
242 * learn more from http://npm.im/rimraf
243 * @return {string} - the command with the rimraf binary
244 */
245function rimraf(args) {
246 return `${getBin('rimraf')} ${args}`;
247}
248
249/**
250 * Takes two scripts and returns the first if the
251 * current environment is windows, and the second
252 * if the current environment is not windows
253 * @param {string} script - the script to use for windows
254 * @param {string} altScript - the script to use for non-windows
255 * @return {string} - the command to run
256 */
257function ifWindows(script, altScript) {
258 return isWindows() ? script : altScript;
259}
260
261/**
262 * Simply calls ifWindows(altScript, script)
263 * @param {string} script - the script to use for non-windows
264 * @param {string} altScript - the script to use for windows
265 * @return {string} - the command to run
266 */
267function ifNotWindows(script, altScript) {
268 return ifWindows(altScript, script);
269}
270
271/**
272 * Gets a script that uses the cpy-cli binary. cpy-cli
273 * is a dependency of nps-utils, so you don't need to
274 * install it yourself.
275 * @param {string} args - args to pass to cpy-cli
276 * learn more from http://npm.im/cpy-cli
277 * @return {string} - the command with the cpy-cli binary
278 */
279function copy(args) {
280 return `${getBin('cpy-cli', 'cpy')} ${args}`;
281}
282
283/**
284 * Gets a script that uses the mkdirp binary. mkdirp
285 * is a dependency of nps-utils, so you don't need to
286 * install it yourself.
287 * @param {string} args - args to pass to mkdirp
288 * learn more from http://npm.im/mkdirp
289 * @return {string} - the command with the mkdirp binary
290 */
291function mkdirp(args) {
292 return `${getBin('mkdirp')} ${args}`;
293}
294
295/**
296 * Gets a script that uses the opn-cli binary. opn-cli
297 * is a dependency of nps-utils, so you don't need to
298 * install it yourself.
299 * @param {string} args - args to pass to opn-cli
300 * learn more from http://npm.im/opn-cli
301 * @return {string} - the command with the opn-cli binary
302 */
303function open(args) {
304 return `${getBin('opn-cli', 'opn')} ${args}`;
305}
306
307/**
308 * Gets a script that uses the cross-env binary. cross-env
309 * is a dependency of nps-utils, so you don't need to
310 * install it yourself.
311 * @param {string} args - args to pass to cross-env
312 * learn more from http://npm.im/cross-env
313 * @return {string} - the command with the cross-env binary
314 */
315function crossEnv(args) {
316 return `${getBin('cross-env')} ${args}`;
317}
318
319// utils
320
321function quoteScript(script, escaped) {
322 var quote = escaped ? '\\"' : '"';
323 var shouldQuote = script.indexOf(' ') !== -1;
324 return shouldQuote ? `${quote}${script}${quote}` : script;
325}
326
327function getBin(packageName) {
328 var binName = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : packageName;
329
330 var packagePath = require.resolve(`${packageName}/package.json`);
331 var concurrentlyDir = _path2.default.dirname(packagePath);
332
333 var _require = require(packagePath),
334 binRelativeToPackage = _require.bin;
335
336 if (typeof binRelativeToPackage === 'object') {
337 binRelativeToPackage = binRelativeToPackage[binName];
338 }
339 var fullBinPath = _path2.default.join(concurrentlyDir, binRelativeToPackage);
340 var relativeBinPath = _path2.default.relative(process.cwd(), fullBinPath);
341 return `node ${relativeBinPath}`;
342}
343
344function isWindows() {
345 // lazily require for perf :)
346 return require('is-windows')();
347}
348
349function shellEscape() {
350 // lazily require for perf :)
351 return require('any-shell-escape').apply(undefined, arguments);
352}
353
354/*
355 eslint
356 func-name-matching:0,
357 global-require:0,
358 import/no-dynamic-require:0
359*/
\No newline at end of file