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('&');
+25
View File
@@ -1,6 +1,8 @@
import os
import re
import env
import subprocess
import json
def try_parse_int(value):
@@ -30,6 +32,29 @@ def expand_range(range_str, max_value):
def run():
try:
split_str = "__sitecustomize__"
command = f'bash -c "source {os.getenv("taskBefore")} {os.getenv("fileName")}'
if os.getenv("task_before"):
command += f" && echo -e '执行前置命令\n' && eval \"{os.getenv('task_before')}\" && echo -e '\n执行前置命令结束\n'"
python_command = "PYTHONPATH= python3 -c 'import os, json; print(json.dumps(dict(os.environ)))'"
command += f' && echo "{split_str}" && {python_command}"'
res = subprocess.check_output(command, shell=True, encoding="utf-8")
output, env_str = res.split(split_str)
env_json = json.loads(env_str.strip())
for key, value in env_json.items():
os.environ[key] = value
print(output)
except subprocess.CalledProcessError as error:
print(f"run task before error: {error}")
env_param = os.getenv("envParam")
num_param = os.getenv("numParam")