通过nodejs里提供的readline,做到与终端控制台交互
const fs = require("fs");
const readline = require("readline");
const r1 = readline.createInterface({
output: process.stdout,
input: process.stdin,
});
function Question(question) {
return new Promise((reslove, reject) => {
r1.question(question, (answer) => {
reslove(answer);
});
});
}
async function createPakeage() {
const name = await Question("你的名字");
const age = await Question("你的年龄");
const sex = await Question("你的性别");
let content = {
name,
age,
sex,
};
fs.writeFileSync("answer.txt", JSON.stringify(content), "utf-8");
r1.close();
}
createPakeage();
r1.on("close", () => {
process.exit(0);
});