Я не знаком с python-shell
, но вы можете использовать exec
из Node's child_process для запуска вашего скрипта
const { exec } = require('child_process')
function runPythonScript(script, runFile = false) {
console.log(`Run? ${runFile}`)
if (runFile) {
exec(`python ${script}`, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`)
return
}
console.log(`stdout: ${stdout}`)
console.error(`stderr: ${stderr}`)
})
}
}
Запуск кода:
// this will run
runPythonScript('./src/main.py', 1 === 1)
// this will not run
runPythonScript('./src/main.py', 0)
// this will run since both currentPhase and phaseId are empty variables
var currentPhase, phaseId
runPythonScript('./src/main.py', currentPhase === phaseId)
CLI
добавьте этот код, если вы хотите определить currentPhase
и phaseId
из командной строки
function init() {
const [, , ...args] = process.argv
var currentPhase, phaseId
currentPhase = args[args.indexOf('--currentPhase') - 1]
phaseId = args[args.indexOf('--phaseId') - 1]
runPythonScript('./program.py', currentPhase === phaseId)
}
init()
из CLI, вы можете запустить
# sets currentPhase to 99 and phaseId as 98 (does not run)
node lib/filename-to-runPythonScript.js 99 --currentPhase 98 --phaseId
# sets both currentPhase and phaseId to 123 (does run)
node lib/filename-to-runPythonScript.js 123 --currentPhase 123 --phaseId