I am completely new to node.js, All I want to do is to use the inquirer NPM to ask a few questions, get the answers to those questions plug into a template literals and create a readme.md file from it. for some reason, my code doesn't work. Could someone point out what have I done wrong here? I feel like I'm missing some parameter when creating the promise maybe??
const inquirer = require("inquirer")
const fs = require("fs")
const util = require("util")
const readme = () => {
return `
# ${data.title}
License covered under ${data.license}
## Table of Contents
1. [Description](#description)
2. [Installation](#installation)
3. [Usage](#usage)
4. [License](#license)
5. [Contributing](#contributing)
6. [Test](#test)
7. [Question](#question)
## Description
${data.description}
## Installation
${data.installation}
## Usage
\```
${data.usage}
\```
## License
${license}
## Contributing
Accepting contribution: ${data.contribution}
${data.contributionRequirement}
## Tests
${data.test}
## Questions
- Github Repo :
- Email : ${data.email} `
}
// array of questions for user
const questions = [
{
type: "input",
name: "title",
message: "What is the title of your project",
default: "Not Available"
},
{
type: "input",
name: "username",
message: "What is your Github username?",
default: "Not Available"
},
{
type: "input",
name: "email",
message: "What is your email address?",
default: "Not Available"
},
{
type: "input",
name: "contact",
message: "How do you want people to reach you?",
default: "Not Available"
},
{
type: "input",
name: "description",
message: "Please type a short description of your project ( When you are done, hit ESC & type \":wq\" to exit editor )",
default: "Not Available"
},
{
type: "input",
name: "installation",
message: "How do you install this application? ( When you are done, hit ESC type \":wq\" to exit editor) ",
default: "Not Available"
},
{
type: "input",
name: "usage",
message: "Give example of some ways you can use this application ( When you are done, hit ESC & type \":wq\" to exit editor)",
default: "Not Available"
},
{
type: "list",
name: "license",
message: "What kind of license would you like to use?",
choices: ["MIT","Microsoft Public License","Mozilla Public License 2.0","Academic Free License v3.0","Open Software License 3.0","Creative Commons Attribution 4.0"],
default: "Not Available"
},
{
type: "confirm",
name: "contribution",
message: "Are you open to contribution?",
default: "Not Available"
},
{
type: "input",
name: "contributionRequirement",
message: "If Yes, What are your requirement for giving contribution?",
default: "Not Available"
},
{
type: "input",
name: "test",
message: "Please give instructions for testing of this project ( When you are done, hit ESC & type \":wq\" to exit editor)",
default: "Not Available"
}
]
// function to write README file
function writeToFile(fileName, data) {
fs.writeFile(fileName, data, "utf8", function(err) {
if (err) {
console.log(err);}
})}
// function to initialize program
const init = new Promise(function(resolve,reject) {
resolve(inquirer.prompt(questions))
})
// function call to initialize program
init.then(writeToFile("test.md",readme)
readme should run with result when inquirer get all input.
const inquirer = require ('inquirer');
const fs = require ('fs');
const util = require ('util');
const readme = data => {
return `
# ${data.title}
License covered under ${data.license}
## Table of Contents
1. [Description](#description)
2. [Installation](#installation)
3. [Usage](#usage)
4. [License](#license)
5. [Contributing](#contributing)
6. [Test](#test)
7. [Question](#question)
## Description
${data.description}
## Installation
${data.installation}
## Usage
\`\`\`
${data.usage}
\`\`\`
## License
${data.license}
## Contributing
Accepting contribution: ${data.contribution}
${data.contributionRequirement}
## Tests
${data.test}
## Questions
- Github Repo :
- Email : ${data.email} `;
};
// array of questions for user
const questions = [
{
type: 'input',
name: 'title',
message: 'What is the title of your project',
default: 'Not Available',
},
{
type: 'input',
name: 'username',
message: 'What is your Github username?',
default: 'Not Available',
},
{
type: 'input',
name: 'email',
message: 'What is your email address?',
default: 'Not Available',
},
{
type: 'input',
name: 'contact',
message: 'How do you want people to reach you?',
default: 'Not Available',
},
{
type: 'input',
name: 'description',
message: 'Please type a short description of your project ( When you are done, hit ESC & type ":wq" to exit editor )',
default: 'Not Available',
},
{
type: 'input',
name: 'installation',
message: 'How do you install this application? ( When you are done, hit ESC type ":wq" to exit editor) ',
default: 'Not Available',
},
{
type: 'input',
name: 'usage',
message: 'Give example of some ways you can use this application ( When you are done, hit ESC & type ":wq" to exit editor)',
default: 'Not Available',
},
{
type: 'list',
name: 'license',
message: 'What kind of license would you like to use?',
choices: [
'MIT',
'Microsoft Public License',
'Mozilla Public License 2.0',
'Academic Free License v3.0',
'Open Software License 3.0',
'Creative Commons Attribution 4.0',
],
default: 'Not Available',
},
{
type: 'confirm',
name: 'contribution',
message: 'Are you open to contribution?',
default: 'Not Available',
},
{
type: 'input',
name: 'contributionRequirement',
message: 'If Yes, What are your requirement for giving contribution?',
default: 'Not Available',
},
{
type: 'input',
name: 'test',
message: 'Please give instructions for testing of this project ( When you are done, hit ESC & type ":wq" to exit editor)',
default: 'Not Available',
},
];
// function to write README file
function writeToFile (fileName, data) {
fs.writeFile (fileName, data, 'utf8', function (err) {
if (err) {
console.log (err);
}
});
}
// function call to initialize program
inquirer.prompt(questions).then(answer => {
writeToFile('test.md', readme(answer))
});
Related
Working on an assignment to write a ReadMe Generator,
but when I put in npm start I get the following error:
ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and 'C:\Users\Franco\Desktop\Readme-Generator\package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
This isn't code but Stack overflow is dumb sometimes
at file:///C:/Users/Franco/Desktop/Readme-Generator/index.js:1:18
at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
at async loadESM (node:internal/process/esm_loader:88:5)
at async handleMainPromise (node:internal/modules/run_main:61:12)
[nodemon] app crashed - waiting for file changes before starting...
Here is my actual code for context
const inquirer = require('inquirer')
const questions = [
{
type: 'input',
name: 'title',
message: 'What is your project called?',
},
{
type: 'input',
name: 'description',
message: 'Please descbribe your project',
},
{
type: 'input',
name: 'installation',
message: 'Describe how you install the project here',
},
{
type: 'input',
name: 'usage',
message: 'how is your project used?',
},
{
type: 'input',
name: 'contribution',
message: 'who contributed?',
},
{
type: 'input',
name: 'test',
message: 'any tests?',
},
{
type: 'input',
name: 'email',
message: 'What is your email address?',
},
{
type: 'input',
name: 'github',
message: 'enter your github username',
},
{
type: 'list',
name: 'License',
message: 'Choose your License',
choices: ['MIT', 'ISC', 'GNUPlv3'],
filter(val) {
return val.toLowerCase();
}
}
]
function runQuery() {
return inquirer.prompt(questions)
.then((answers) => {
console.log(answers)
return answers
})
.catch((error) => {
console.log(error)
})
}
runQuery
Does anyone have any idea what it could be?
When I run the script for my discord bot and use /warn remove it keeps throwing an error that user is of type string expected USER. It says the error is happening between lines 69:42 and I can't find the issue.
module.exports = {
category: 'Moderation',
description: 'Warn a user',
permissions: ['ADMINISTRATOR'],
slash: true,
guildOnly: true,
options: [
{
type: 'SUB_COMMAND',
name: 'remove',
description: 'Removes a warning from the user',
options: [
{
name: 'user',
type: 'USER',
description: 'The user to remove the warning from',
required: true,
},
{
name: 'id',
type: 'STRING',
description: 'The ID of the warning to remove',
required: true,
},
],
}
],
callback: async ({ guild, member: staff, interaction }) => {
const user = interaction.options.getUser('user')
if (subCommand === 'remove') {
const warning = await warnSchema.findByIdAndDelete(id)
return {
custom: true,
content: `Removed warning ${warning.id} from <#${user?.id}>`,
allowedMentions: {
users: []
}
}
}
}
}
I didn't post all the code because it seemed like it would be too much searching but this is the code that is causing the error.
I am trying to make a toggle able slash command, if they pick the disable option it turns it off but when if you pick the enable option it asks to pick a channel but it gives this error
Error:
DiscordAPIError[50035]: Invalid Form Body
23.name[BASE_TYPE_REQUIRED]: This field is required
rawError: {
code: 50035,
errors: { '23': [Object] },
message: 'Invalid Form Body'
},
code: 50035,
status: 400,
method: 'put',
url: 'https://discord.com/api/v9/applications/971024098098569327/commands'
Code:
module.exports = {
name: 'welcomer',
permissions: 'MANAGE_CHANNELS',
description: 'Set Where Welcome Messages Get Sent To.',
options: [
{
name: 'toggle',
description: 'Toggle On/Off The Welcomer',
type: 3,
required: true,
choices: [
{
name: 'disable',
value: 'off',
},
{
name: 'enable',
value: 'on',
choices: [
{
name: 'channel',
description: 'Select channel to send welcome messages to',
type: 7,
required: true,
},
]
},
],
},
],
Those would be an example of a subcommand and need to be indicated as such and will need descriptions in a couple places.
module.exports = {
name: 'welcomer',
permissions: 'MANAGE_CHANNELS',
description: 'Set Where Welcome Messages Get Sent To.',
options: [{
name: 'disable',
description: `'Disable welcomer`, // Added needed description
type: 1, //converted to subcommmand
}, {
name: 'enable',
description: `'Enable welcomer`, // Added needed description
type: 1, //converted to subcommmand
options: [{
name: 'channel',
description: 'Select channel to send welcome messages to',
type: 7,
required: true,
channel_types: [0] // allows only text channels to be selected
}]
}],
// run command pick only one of the below two
// if command.execute()
async execute(client, interaction, args)
// if command.run()
run: async (client, interaction, args) =>
// command code below here assumes you have the code in your `interactionCreate` listener to set up args
{
if (args.disable) {
// Code to turn off
} else if (args.enable) {
const channel = args.channel
// Code to turn on
};
}
}
I'm using NPM 'inquirer' package in order to present the user various questions. One of them is a 'choices' selection.
Is there a way to present follow up questions based on the 'choices' selection?
Here's my code:
const { prompt } = require('inquirer');
require('colors');
const questions = [
{
type: 'input',
name: 'appName',
message: 'Enter application name:'
},
{
type: 'input',
name: 'appDescription',
message: 'Enter app description:'
},
{
type: 'input',
name: 'appAuthor',
message: 'Enter app author:'
},
{
type: 'checkbox',
name: 'plugins',
message: 'Select plugins to install:',
choices: [
{
name: 'Cassandra'
}
]
}
];
module.exports.performQuestions = async () => {
console.log('\nWelcome to Chef!\n'.underline.italic.cyan);
const answers = await prompt(questions);
if (!answers.appName) {
console.error('\nPlease provide app name!\n'.red);
process.exit(1);
}
answers.appType = 'service';
return answers;
};
Here I want to present a few more questions if the user selects 'Cassandra' is that possible?
Thanks.
You can use "when" and like in the example bellow, the second question will popup only if "Cassandra" is selected:
const QUESTIONS = [
{
name: 'your-name',
type: 'list',
message: 'Your name:',
choices: ['Batman', 'Superman', 'Ultron', 'Cassandra'],
},
{
name: 'hello-cassandra',
type: 'confirm',
message: 'Oh, hello Cassandra!',
when: (answers) => answers['your-name'] === 'Cassandra',
},
];
inquirer.prompt(QUESTIONS)
.then(answers =>
{
console.log(answers);
});
Is there a way to reset the questions or have a certain answer direct the question to another previous question?
var questions = [{
{
name: 'morefood',
message: 'Do you want more food?',
type: 'list',
choices: [ 'Yes', 'No'],
},{
name: 'choiceoffood',
message: 'Which food do you want more of?',
type: 'list',
choices: [ 'Hamburgers', 'Fries', 'Hotdogs']
when: function(answers) {
return answers.morefood === 'Yes';
}
}, {
name: 'quantityoffood',
message: 'How much more do you want?',
type: 'input',
when: function(answers) {
return answers.quantityoffood === 'Yes';
}
},{
name: 'confirmfood',
message: 'Do you still want more food?',
type: 'list',
choices: [ 'Yes', 'No'], <=========== if yes then goes back to choiceoffood
},
]
It seems a little hacky, but I believe you have to handle that in your application logic. For example:
const questionA = {
type: 'input',
message: 'Do you like fruit?',
name: 'questionA',
}
const questionB = {
type: 'input',
message: 'What is your favorite fruit?',
name: 'questionB',
}
const questionC = {
type: 'input',
message: 'what is your favorite candy?',
name: 'questionC',
}
inquirer
.prompt(questionA)
.then(answers => {
if (answers.questionA === 'yes') {
return inquirer.prompt(questionB)
} else {
return inquirer.prompt(questionC)
}
})
.then(answers => {
if (answers.questionB) {
return console.log(answers.questionB, 'is a great fruit');
}
if (answers.questionC) {
return console.log(answers.questionC, 'is a great candy');
}
})
UPDATE:
After looking at the docs a bit more, it seems 'when' is the correct solution for this.
when: (Function, Boolean) Receive the current user answers hash and
should return true or false depending on whether or not this question
should be asked. The value can also be a simple boolean.