我想在nodejs的内置模块中过滤答案,称为"readline“,它允许您提示用户并问一些问题,yk之类的东西,我已经为我的用户创建了一个js文件,以方便地理解在我的不和谐的机器人上该做什么。安装依赖关系,git拉,耶,它解释它。但我希望它只接受某些答案。这是你理解它的代码
const readline = require("readline")
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
console.log("Hello! Welcome to the startup file of my bot dio! You can use this file to easily install,uninstall and update files!")
console.log('Type npm install if you want to install the needed packages. ')
console.log('Type git pull to update the whole folder but it needs a token and username so ye just enter it.')
console.log('Type node index.js of you want to start the bot')
rl.question('Now, what do you want to do? ', (answer) => {
const { execSync } = require('child_process')
const start = () => {
execSync(`${answer}`, { stdio: "inherit" });
}
start();
rl.close();
}); 我希望这段代码只接受我提供的答案(npm安装,git拉,节点index.js)
发布于 2022-02-09 03:07:35
我终于拿到了!我只是用了if数组lol。我完全忘记了。这是密码
const readline = require("readline")
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const answers = ['npm install', 'git pull', 'node index.js']
console.log("Hello! Welcome to the startup file of my bot dio! You can use this file to easily install,uninstall and update files!")
console.log('Type npm install if you want to install the needed packages. ')
console.log('Type git pull to update the whole folder but it needs a token and username so ye just enter it.')
console.log('Type node index.js of you want to start the bot')
rl.question('What do you want to do? ', (answer) => {
const { execSync } = require('child_process');
if(answer === answers[0]){
console.log('Installing packages...')
execSync('npm install', {stdio: 'inherit'})
console.log('Installed!')
}
else if(answer === answers[1]){
console.log('Updating...')
execSync('git pull', {stdio: 'inherit'})
console.log('Updated!')
}
else if(answer === answers[2]){
console.log('Starting...')
execSync('node index.js', {stdio: 'inherit'})
console.log('Started!')
}
else{
rl.setPrompt('Wrong input! Try again!')
rl.prompt()
}
rl.close();
});https://stackoverflow.com/questions/71034223
复制相似问题