ask the user to input a value for each key specified as command line parameter
This commit is contained in:
65
env_loc.js
65
env_loc.js
@@ -3,6 +3,7 @@
|
|||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
const os = require('os')
|
const os = require('os')
|
||||||
|
const readline = require('readline')
|
||||||
|
|
||||||
const params = process.argv.slice(2)
|
const params = process.argv.slice(2)
|
||||||
|
|
||||||
@@ -28,21 +29,63 @@ const defaultEnv = {
|
|||||||
openShare: fs.existsSync('../../npm') ? path.resolve('../../npm') : fs.existsSync(`${os.homedir()}/opx`) ? path.resolve(`${os.homedir()}/opx`) : '',
|
openShare: fs.existsSync('../../npm') ? path.resolve('../../npm') : fs.existsSync(`${os.homedir()}/opx`) ? path.resolve(`${os.homedir()}/opx`) : '',
|
||||||
}
|
}
|
||||||
|
|
||||||
const localEnv = Object.assign(
|
const paramDefaults = params.reduce((acc, cur) => {
|
||||||
{},
|
|
||||||
params.reduce((acc, cur) => {
|
|
||||||
// 把参数名数组转化为对象
|
// 把参数名数组转化为对象
|
||||||
acc[cur] = ''
|
acc[cur] = ''
|
||||||
return acc
|
return acc
|
||||||
}, {}),
|
}, {})
|
||||||
defaultEnv, // 继承所有默认值
|
|
||||||
fs.existsSync('./env_loc.gitomit.sfomit.json') ? require(path.resolve('./env_loc.gitomit.sfomit.json')) : {}
|
|
||||||
)
|
|
||||||
|
|
||||||
fs.writeFileSync(path.resolve('./env_loc.gitomit.sfomit.json'), JSON.stringify(localEnv, null, 2), 'utf8')
|
const existingEnv = fs.existsSync('./env_loc.gitomit.sfomit.json') ? require(path.resolve('./env_loc.gitomit.sfomit.json')) : {}
|
||||||
|
|
||||||
console.log('\n::*** Configuring local enviroment:')
|
const baseEnv = Object.assign({}, paramDefaults, defaultEnv, existingEnv)
|
||||||
|
|
||||||
console.log(localEnv)
|
const askParamValues = (keys) =>
|
||||||
|
new Promise((resolve) => {
|
||||||
|
if (!keys.length || !process.stdin.isTTY) {
|
||||||
|
resolve({})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = localEnv
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user