UNPKG

3.23 kBJavaScriptView Raw
1"use strict";
2
3var _path = _interopRequireDefault(require("path"));
4
5var _crossSpawn = _interopRequireDefault(require("cross-spawn"));
6
7var _glob = _interopRequireDefault(require("glob"));
8
9var _chalk = _interopRequireDefault(require("chalk"));
10
11var _utils = require("./utils");
12
13function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
14
15const [processExecutor, ignoredBin, script, ...args] = process.argv;
16const executor = (0, _utils.isGdScripts)() ? "ts-node" : processExecutor;
17
18const handleSignal = result => {
19 if (result.signal === "SIGKILL") {
20 // eslint-disable-next-line no-console
21 console.log(`The script "${script}" failed because the process exited too early. ` + "This probably means the system ran out of memory or someone called " + "`kill -9` on the process.");
22 } else if (result.signal === "SIGTERM") {
23 // eslint-disable-next-line no-console
24 console.log(`The script "${script}" failed because the process exited too early. ` + "Someone might have called `kill` or `killall`, or the system could " + "be shutting down.");
25 }
26
27 process.exit(1);
28};
29
30const attemptResolve = (...resolveArgs) => {
31 try {
32 return require.resolve(...resolveArgs);
33 } catch (error) {
34 return null;
35 }
36}; // this is required to address an issue in cross-spawn
37// https://github.com/kentcdodds/kcd-scripts/issues/4
38
39
40const getEnv = () => Object.keys(process.env).filter(key => process.env[key] !== undefined).reduce((envCopy, key) => {
41 envCopy[key] = process.env[key];
42 return envCopy;
43}, {
44 [`SCRIPTS_${script.toUpperCase()}`]: true.toString()
45});
46
47const spawnScript = () => {
48 const relativeScriptPath = _path.default.join(__dirname, "./scripts", script);
49
50 const scriptPath = attemptResolve(relativeScriptPath);
51
52 if (!scriptPath) {
53 throw new Error(`Unknown script "${script}".`);
54 }
55
56 const result = _crossSpawn.default.sync(executor, [scriptPath, ...args], {
57 stdio: "inherit",
58 env: getEnv()
59 });
60
61 if (result.signal) {
62 handleSignal(result);
63 } else {
64 process.exit(result.status);
65 }
66};
67
68if (script) {
69 spawnScript();
70} else {
71 const scriptsPath = _path.default.join(__dirname, "scripts/");
72
73 const scriptsAvailable = _glob.default.sync(_path.default.join(__dirname, "scripts", "*")); // `glob.sync` returns paths with unix style path separators even on Windows.
74 // So we normalize it before attempting to strip out the scripts path.
75
76
77 const scriptsAvailableMessage = scriptsAvailable.map(_path.default.normalize).map(s => s.replace(scriptsPath, "").replace(/__tests__/, "").replace(/\.ts$/, "")).filter(Boolean).join("\n ").trim();
78 const fullMessage = `
79${_chalk.default.cyan("Usage")}:
80 ${ignoredBin} [script] [--flags]
81
82${_chalk.default.cyan("Available Scripts")}:
83 ${scriptsAvailableMessage}
84
85${_chalk.default.cyan("Options")}:
86 All options depend on the script. Docs will be improved eventually,
87 but for most scripts you can assume that the args you pass will be
88 forwarded to the respective tool that's being run under the hood.
89
90${_chalk.default.green("May the force be with you")}.
91 `.trim();
92 console.log(`\n${fullMessage}\n`); // eslint-disable-line no-console
93}
94//# sourceMappingURL=run-script.js.map
\No newline at end of file