I am making a custom CLI so I wrote a code
#!/usr/bin/env node
var { program } = require("commander");
var { prompt } = require("inquirer");
const Science = require("./Science");
program.version("0.0.1");
// questions
const questions = [
{
type: "list",
name: "subjectType",
message: " Select export Source ",
default: false,
choices: [
"Science",
"Maths",
"English",
"Exit",
],
},
];
function startQuery() {
program
.command("run")
.alias("r")
.description("Subject")
.action(() => {
prompt(questions).then((answers) => {
if (answers.subjectType === "Science") {
Science();
} else if (answers.subjectType === "Maths") {
// Maths();
} else if (answers.subjectType === "Exit") {
wantToExit();
}
});
});
}
function wantToExit() {
inquirer
.prompt([
{
name: "moreQuery",
type: "confirm",
message: "Want to do anything else?",
},
])
.then((answer) => {
if (answer.moreQuery) return startQuery();
});
}
program.parse(process.argv);
so when I type Subject run in console it won’t run my cli but when I remove startQuery function its execute the Program and I Don’t know why its behaving like this
running by removing function startQuery()
program
.command("run")
.alias("r")
.description("Subject")
.action(() => {
prompt(questions).then((answers) => {
if (answers.subjectType === "Science") {
Science();
} else if (answers.subjectType === "Maths") {
// Maths();
} else if (answers.subjectType === "Exit") {
wantToExit();
}
});
});
Then in console I write Subject run its Execute properly
but if I do this its not executing the program
function startQuery() {
program
.command("run")
.alias("r")
.description("Subject")
.action(() => {
prompt(questions).then((answers) => {
if (answers.subjectType === "Science") {
Science();
} else if (answers.subjectType === "Maths") {
// Maths();
} else if (answers.subjectType === "Exit") {
wantToExit();
}
});
});
}
As i need this startQuery function in wantToExit function so I can use it again if user want to continue