forked from jordanwhunter/readme-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
145 lines (134 loc) · 4.97 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Global
const fs = require('fs');
const inquirer = require('inquirer');
const util = require('util');
// Internal NPMs
const api = require('./utils/api.js');
const generateMarkdown = require('./utils/generateMarkdown.js');
// Array of questions for user
const questions = [
// GitHub Username
{
type: 'input',
name: 'username',
message: 'Enter your GitHub username.',
// We need to validate that user entered at least one word
// https://stackoverflow.com/questions/57321266/how-to-test-inquirer-validation
validate: function (answer) {
if (answer.length < 1) {
return console.log("You must enter a GitHub username.");
}
return true;
}
},
// GitHub Repository
{
type: 'input',
name: 'repository',
message: 'Enter the name of your repository on GitHub.',
// We need to validate that user entered at least one word
validate: function (answer) {
if (answer.length < 1) {
return console.log("You must enter the name of your GitHub repository.");
}
return true;
}
},
// Title of Project
{
type: 'input',
name: 'title',
message: 'Enter the title of your project.',
// We need to validate that user entered at least one word
validate: function (answer) {
if (answer.length < 1) {
return console.log("You must enter the title of your project.");
}
return true;
}
},
// Project Description
{
type: 'input',
name: 'description',
message: 'Enter a description of your project.',
// We need to validate that user entered at least one word
validate: function (answer) {
if (answer.length < 1) {
return console.log("You must enter a description for your project.");
}
return true;
}
},
// Project Installation
{
type: 'input',
name: 'installation',
message: 'Explain how user would install (if necessary) for Installation Section.',
// Validation not required if question is optional
},
// Usage of project
{
type: 'input',
name: 'usage',
message: 'Enter your project instructions and examples of it in use for Usage Section.',
},
// Select license
{
type: 'list',
name: 'license',
message: 'Choose your license for your project.',
// https://docs.github.com/en/free-pro-team@latest/github/creating-cloning-and-archiving-repositories/licensing-a-repository
choices: ['afl-3.0', 'apache-2.0', 'artistic-2.0', 'bsl-1.0', 'bsd-2-clause', 'bsd-3-clause', 'bsd-3-clause-clear', 'cc', 'cc0-1.0', 'cc-by-4.0', 'cc-by-sa-4.0', 'wtfpl', 'ecl-2.0', 'epl-1.0', 'epl-2.0', 'eupl-1.1', 'agpl-3.0', 'gpl', 'gpl-2.0', 'gpl-3.0', 'lgpl', 'lgpl-2.1', 'lgpl-3.0', 'isc', 'lppl-1.3c', 'ms-pl', 'mit', 'mpl-2.0', 'osl-3.0', 'postgresql', 'ofl-1.1', 'ncsa', 'unlicense', 'zlib']
},
// Contributing to project
{
type: 'input',
name: 'contributing',
message: 'Explain how users can contribute to your project (if necessary).',
// Validation not required if question is optional
},
// Test for project
{
type: 'input',
name: 'tests',
message: 'Provide tests for project, and explain how to test (if necessary).',
// Validation not required if question is optional
},
];
// function to write README file
function writeToFile(fileName, data) {
fs.writeFile(fileName, data, err => {
if (err) {
return console.log(err);
}
console.log('Your markdown file has been created.')
});
}
// Reference: https://www.npmjs.com/package/util.promisify
const writeFileAsync = util.promisify(writeToFile);
// function to initialize program
async function init() {
// https://www.w3schools.com/js/js_errors.asp
try {
// Reference inquirer array with prompts
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await
// https://www.digitalocean.com/community/tutorials/nodejs-interactive-command-line-prompts
const userResponses = await inquirer.prompt(questions);
console.log("Your responses: ", userResponses);
console.log("Your responses have been logged. Calling to GitHub...");
// Referencing API.js
const userInfo = await api.getUser(userResponses);
console.log("Your GitHub user info: ", userInfo);
// Pass inquirer data and api data to markdown
console.log("Generating your markdown")
const markdown = generateMarkdown(userResponses, userInfo);
console.log(markdown);
// Write markdown
await writeFileAsync('ExampleREADME.md', markdown);
} catch (error) {
console.log(error);
}
};
// function call to initialize program
init();