1 | "use strict";
|
2 |
|
3 | var _path = _interopRequireDefault(require("path"));
|
4 |
|
5 | var _crossSpawn = _interopRequireDefault(require("cross-spawn"));
|
6 |
|
7 | var _glob = _interopRequireDefault(require("glob"));
|
8 |
|
9 | var _chalk = _interopRequireDefault(require("chalk"));
|
10 |
|
11 | var _utils = require("./utils");
|
12 |
|
13 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
14 |
|
15 | const [processExecutor, ignoredBin, script, ...args] = process.argv;
|
16 | const executor = (0, _utils.isGdScripts)() ? "ts-node" : processExecutor;
|
17 |
|
18 | const handleSignal = result => {
|
19 | if (result.signal === "SIGKILL") {
|
20 |
|
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 |
|
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 |
|
30 | const attemptResolve = (...resolveArgs) => {
|
31 | try {
|
32 | return require.resolve(...resolveArgs);
|
33 | } catch (error) {
|
34 | return null;
|
35 | }
|
36 | };
|
37 |
|
38 |
|
39 |
|
40 | const 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 |
|
47 | const 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 |
|
68 | if (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", "*"));
|
74 |
|
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`);
|
93 | }
|
94 |
|
\ | No newline at end of file |