// usage: node env_loc.js hbxCli iosTransporter // defaultEnv里不存在的作为参数 const fs = require('fs') const path = require('path') const os = require('os') const readline = require('readline') const params = process.argv.slice(2) // 默认值。如要覆盖,请在 env_loc.gitomit.sfomit.json 中修改。 const defaultEnv = { hbxCli: os.type() === 'Darwin' && fs.existsSync('/Applications/HBuilderX.app/Contents/MacOS/cli') ? '/Applications/HBuilderX.app/Contents/MacOS/cli' : os.type() === 'Windows_NT' && fs.existsSync('C:/HBuilderX/cli.exe') ? 'C:/HBuilderX/cli.exe' : '', iosTransporter: os.type() === 'Darwin' && fs.existsSync('/Applications/Transporter.app/Contents/itms/bin/iTMSTransporter') ? '/Applications/Transporter.app/Contents/itms/bin/iTMSTransporter' : '', simsimPath: module.paths.some((modulesPath) => fs.existsSync(path.join(modulesPath, 'simsim_key'))) ? 'simsim_key' : fs.existsSync('../../../simsim_key') ? path.resolve('../../../simsim_key') : fs.existsSync(`${os.homedir()}/simsim_key`) ? path.resolve(`${os.homedir()}/simsim_key`) : '', openShare: fs.existsSync('../../npm') ? path.resolve('../../npm') : fs.existsSync(`${os.homedir()}/opx`) ? path.resolve(`${os.homedir()}/opx`) : '', } const paramDefaults = params.reduce((acc, cur) => { // 把参数名数组转化为对象 acc[cur] = '' return acc }, {}) const existingEnv = fs.existsSync('./env_loc.gitomit.sfomit.json') ? require(path.resolve('./env_loc.gitomit.sfomit.json')) : {} const baseEnv = Object.assign({}, paramDefaults, defaultEnv, existingEnv) const askParamValues = (keys) => new Promise((resolve) => { if (!keys.length || !process.stdin.isTTY) { resolve({}) return } const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }) const values = {} let index = 0 const askNext = () => { if (index >= keys.length) { rl.close() resolve(values) return } const key = keys[index] rl.question(`Input value for "${key}" (Enter for empty): `, (answer) => { values[key] = answer === '' ? '' : answer index += 1 askNext() }) } askNext() }) const main = async () => { const inputValues = await askParamValues(params) const localEnv = Object.assign({}, baseEnv, inputValues) fs.writeFileSync(path.resolve('./env_loc.gitomit.sfomit.json'), JSON.stringify(localEnv, null, 2), 'utf8') console.log('\n::*** Configuring local enviroment:') console.log(localEnv) } if (require.main === module) { main().catch((error) => { console.error(error) process.exit(1) }) } module.exports = baseEnv