python-exe-from-config.js 1004 B

123456789101112131415161718192021222324
  1. 'use strict'
  2. const path = require('path')
  3. /**
  4. * Python 可执行文件路径:只认根目录 config.js 的导出(pythonExePath / pythonDir / pythonPath.path)。
  5. * 禁止在业务代码里拼接 python/x64、venv、py/python.exe 等路径。
  6. *
  7. * @param {object} config - require(项目根/config.js) 的结果
  8. * @returns {string}
  9. */
  10. function getPythonExeFromConfig (config) {
  11. if (!config || typeof config !== 'object') return 'python'
  12. const explicit = config.pythonExePath != null && String(config.pythonExePath).trim()
  13. if (explicit) {
  14. const p = String(config.pythonExePath).trim()
  15. return path.isAbsolute(p) ? p : path.join(config.projectRoot || '', p)
  16. }
  17. const dir = config.pythonDir || (config.pythonPath && config.pythonPath.path)
  18. if (!dir) return 'python'
  19. const base = path.isAbsolute(dir) ? dir : path.join(config.projectRoot || '', dir)
  20. return path.join(base, process.platform === 'win32' ? 'python.exe' : 'python')
  21. }
  22. module.exports = { getPythonExeFromConfig }