JavaScript 和 Python 执行脚本前执行 task_before

This commit is contained in:
whyour
2024-07-20 17:28:48 +08:00
parent f47a788184
commit 1b39d3ab48
4 changed files with 60 additions and 27 deletions
+23
View File
@@ -1,3 +1,4 @@
const { execSync } = require('child_process');
require(`./env.js`);
function expandRange(rangeStr, max) {
@@ -17,6 +18,28 @@ function expandRange(rangeStr, max) {
}
function run() {
try {
const splitStr = '__sitecustomize__';
let command = `bash -c "source ${process.env.taskBefore} ${process.env.fileName}`;
if (process.env.task_before) {
command = `${command} && echo -e '执行前置命令\n' && eval "${process.env.task_before}" && echo -e '\n执行前置命令结束\n'`;
}
const res = execSync(
`${command} && echo "${splitStr}" && NODE_OPTIONS= node -p 'JSON.stringify(process.env)'"`,
{
encoding: 'utf-8',
},
);
const [output, envStr] = res.split(splitStr);
const json = JSON.parse(envStr.trim());
for (const key in json) {
process.env[key] = json[key];
}
console.log(output);
} catch (error) {
console.log(`run task before error `, error);
}
if (process.env.envParam && process.env.numParam) {
const { envParam, numParam } = process.env;
const array = (process.env[envParam] || '').split('&');