|
| 1 | +/** |
| 2 | + * Copyright (C) 2022-2023 Jen-Chieh Shen |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation; either version 3, or (at your option) |
| 7 | + * any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with GNU Emacs; see the file COPYING. If not, write to the |
| 16 | + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | + * Boston, MA 02110-1301, USA. |
| 18 | + */ |
| 19 | + |
| 20 | +"use strict"; |
| 21 | + |
| 22 | +const fs = require('fs'); |
| 23 | +const child_process = require("child_process"); |
| 24 | + |
| 25 | +exports.command = ['script [names..]']; |
| 26 | +exports.desc = 'Run script nameds [names..]'; |
| 27 | +exports.builder = yargs => yargs |
| 28 | + .positional( |
| 29 | + '[names..]', { |
| 30 | + description: 'specify scripts to run', |
| 31 | + type: 'array', |
| 32 | + }); |
| 33 | + |
| 34 | +exports.handler = async (argv) => { |
| 35 | + await UTIL.e_call(argv, 'run/script', argv.names); |
| 36 | + |
| 37 | + if (!fs.existsSync(EASK_HOMEDIR)) { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + let run = EASK_HOMEDIR + 'run'; |
| 42 | + |
| 43 | + if (!fs.existsSync(run)) { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + // this contain the full command! |
| 48 | + let instruction = fs.readFileSync(run, 'utf8'); |
| 49 | + let commands = instruction.split('\n').filter(element => element); |
| 50 | + |
| 51 | + let count = 0; |
| 52 | + startCommand(commands, count); |
| 53 | +}; |
| 54 | + |
| 55 | +/** |
| 56 | + * Recursive to execute commands in order. |
| 57 | + * |
| 58 | + * @param { array } commands - An array of commands to execute. |
| 59 | + * @param { integer } count - The current executing command's index. |
| 60 | + */ |
| 61 | +function startCommand(commands, count) { |
| 62 | + if (commands.length <= count) |
| 63 | + return; |
| 64 | + |
| 65 | + let command = commands[count]; |
| 66 | + |
| 67 | + console.log('[RUN]: ' + command); |
| 68 | + |
| 69 | + let proc = spawn(command, { stdio: 'inherit', shell: true }); |
| 70 | + |
| 71 | + proc.on('close', function (code) { |
| 72 | + if (code == 0) { |
| 73 | + startCommand(commands, ++count); // start next command! |
| 74 | + return; |
| 75 | + } |
| 76 | + process.exit(code); |
| 77 | + }); |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Spawn process to avoid `MODULE_NOT_FOUND` not found error, |
| 82 | + * see https://github.com/vercel/pkg/issues/1356. |
| 83 | + * |
| 84 | + * @param { String } command - Command string. |
| 85 | + * @param { JSON } options - Process options. |
| 86 | + * @return Process object. |
| 87 | + */ |
| 88 | +function spawn(command, options) { |
| 89 | + if (IS_PKG && command.includes('eask ')) { |
| 90 | + let cmds = command.split(' '); |
| 91 | + cmds = replaceEaskExec(cmds); |
| 92 | + return child_process.spawn(process.execPath, cmds, options); |
| 93 | + } |
| 94 | + return child_process.spawn(command, options); |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Replace all possible eask/cli executable to snapshot executable. |
| 99 | + * @param { Array } cmds - Command array. |
| 100 | + * @return Return updated command array. |
| 101 | + */ |
| 102 | +function replaceEaskExec(cmds) { |
| 103 | + for (let index = 0; index < cmds.length; ++index) { |
| 104 | + if (cmds[index] == "eask") { |
| 105 | + cmds[index] = process.argv[1]; // XXX: This is `/snapshot/cli/eask` |
| 106 | + } |
| 107 | + } |
| 108 | + return cmds; |
| 109 | +} |
0 commit comments