Skip to content

Commit f2b81bd

Browse files
committed
Add some flags to skip questions process
1 parent 464d61e commit f2b81bd

2 files changed

Lines changed: 96 additions & 61 deletions

File tree

lib/cli.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,23 @@ const flagsAvailable = {
1313
type: "string",
1414
alias: "p",
1515
isMultiple: false
16+
},
17+
gitignore: {
18+
type: "string"
19+
},
20+
name: {
21+
type: "string",
22+
alias: "n"
23+
},
24+
githubUser: {
25+
type: "string"
26+
},
27+
projectName: {
28+
type: "string"
29+
},
30+
description: {
31+
type: "string",
32+
alias: "d"
1633
}
1734
}
1835
};
@@ -39,6 +56,7 @@ const { input, flags } = meow(
3956
const flagsKeysAvailable = Object.keys(flagsAvailable.flags);
4057
const flagsEnter = Object.keys(flags);
4158
let customPath;
59+
let skipQuestions;
4260

4361
// Verif command.
4462
if (input.length === 0) {
@@ -66,7 +84,8 @@ const { input, flags } = meow(
6684

6785
// Verif flags.
6886
if (flagsEnter.length !== 0) {
69-
if (flagsEnter.length > 1) {
87+
// console.log(flagsKeysAvailable, flagsEnter);
88+
if (flagsEnter.length > flagsKeysAvailable.length) {
7089
process.stderr.write(chalk`{red You can use only one flag}`);
7190
process.exit(1);
7291
}
@@ -76,6 +95,12 @@ const { input, flags } = meow(
7695
process.exit(1);
7796
}
7897

98+
if (flagsEnter.length === flagsKeysAvailable.length) skipQuestions = true;
99+
100+
if (!skipQuestions) {
101+
skipQuestions = flagsKeysAvailable.every(f => flagsEnter.includes(f));
102+
}
103+
79104
customPath = resolve(process.cwd(), flags.path);
80105

81106
try {
@@ -88,7 +113,7 @@ const { input, flags } = meow(
88113

89114
switch (input[0]) {
90115
case "start":
91-
return startCommand(customPath);
116+
return startCommand(customPath, skipQuestions, flags);
92117
default:
93118
break;
94119
}

lib/commands/start.js

Lines changed: 69 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -12,74 +12,84 @@ const createTree = require("../utils/createTree");
1212
/**
1313
* Execute the `start` command
1414
* @param {String} pathToCreate - If the user enter a custom path the project must be create in this directory.
15+
* @param { Boolean } skipQuestions - If true skip the questions process.
16+
* @param { Object } flags - If present use this to create directory three.
1517
*/
16-
module.exports = async function startCommand(pathToCreate) {
18+
module.exports = async function startCommand(pathToCreate, skipQuestions, flags) {
1719
process.stdout.write(chalk`{cyan Start process..}`);
1820

1921
const directoryPath = pathToCreate === undefined ? process.cwd() : pathToCreate;
20-
const filesToExludes = ["README", "LICENSE", "CONTRIBUTING"];
21-
const res = await fetch("https://api.github.com/repos/github/gitignore/contents");
22-
const contents = await res.json();
22+
let aswr;
2323

24-
const languages = contents
25-
.map(function l(c) {
26-
const languageName = c.name.split(".")[0];
24+
if (!skipQuestions) {
25+
const filesToExludes = ["README", "LICENSE", "CONTRIBUTING"];
26+
const res = await fetch("https://api.github.com/repos/github/gitignore/contents");
27+
const contents = await res.json();
2728

28-
if (c.type === "dir" || filesToExludes.includes(languageName)) return;
29+
const languages = contents
30+
.map(function l(c) {
31+
const languageName = c.name.split(".")[0];
2932

30-
return c.name.split(".")[0]; // eslint-disable-line consistent-return
31-
})
32-
.filter(f => f !== undefined);
33+
if (c.type === "dir" || filesToExludes.includes(languageName)) return;
3334

34-
process.stdout.write(chalk`{cyan We need you to answer a few questions :)}`);
35+
return c.name.split(".")[0]; // eslint-disable-line consistent-return
36+
})
37+
.filter(f => f !== undefined);
3538

36-
const answer = await inquirer.prompt([
37-
{
38-
type: "list",
39-
message: `Which language or framework you will use ?`,
40-
name: "language",
41-
choices: [...languages]
42-
},
43-
{
44-
type: "input",
45-
message: `First what is your full name ? ${new inquirer.Separator(
46-
"We need this to configure your Licence and package.json file"
47-
)}`,
48-
name: "name",
49-
validate: a => {
50-
if (a.length === 0)
51-
return process.stdout.write(chalk`{red You should enter something}`);
52-
return true;
53-
}
54-
},
55-
{
56-
type: "input",
57-
message: "What is your github user name ?",
58-
name: "githubUser",
59-
validate: b => {
60-
if (/^([a-z\d]+-)*[a-z\d]+$/i.test(b)) return true;
61-
return process.stdout.write(chalk`{red Enter a valid username}`);
62-
}
63-
},
64-
{
65-
type: "input",
66-
message: "What is the name of your project ?",
67-
name: "projectName",
68-
validate: c => {
69-
if (c.length === 0) return process.stdout.write(chalk`{red Name invalid}`);
70-
return true;
39+
process.stdout.write(chalk`{cyan We need you to answer a few questions :)}`);
40+
41+
const answer = await inquirer.prompt([
42+
{
43+
type: "list",
44+
message: `Which language or framework you will use ?`,
45+
name: "gitignore",
46+
choices: [...languages]
47+
},
48+
{
49+
type: "input",
50+
message: `First what is your full name ? ${new inquirer.Separator(
51+
"We need this to configure your Licence and package.json file"
52+
)}`,
53+
name: "name",
54+
validate: a => {
55+
if (a.length === 0)
56+
return process.stdout.write(chalk`{red You should enter something}`);
57+
return true;
58+
}
59+
},
60+
{
61+
type: "input",
62+
message: "What is your github user name ?",
63+
name: "githubUser",
64+
validate: b => {
65+
if (/^([a-z\d]+-)*[a-z\d]+$/i.test(b)) return true;
66+
return process.stdout.write(chalk`{red Enter a valid username}`);
67+
}
68+
},
69+
{
70+
type: "input",
71+
message: "What is the name of your project ?",
72+
name: "projectName",
73+
validate: c => {
74+
if (c.length === 0) return process.stdout.write(chalk`{red Name invalid}`);
75+
return true;
76+
},
77+
filter: d => {
78+
if (d.includes(" ")) return d.replace(/\s+/g, "-").toLowerCase();
79+
return d;
80+
}
7181
},
72-
filter: d => {
73-
if (d.includes(" ")) return d.replace(/\s+/g, "-").toLowerCase();
74-
return d;
82+
{
83+
type: "input",
84+
message: "In a few lines why you're creating this project ?",
85+
name: "description"
7586
}
76-
},
77-
{
78-
type: "input",
79-
message: "In a few lines why you're creating this project ?",
80-
name: "description"
81-
}
82-
]);
87+
]);
88+
89+
aswr = answer;
90+
} else {
91+
aswr = { ...flags };
92+
}
8393

8494
const spinner = ora({
8595
text: chalk`{cyan Start creating your project.}\n`,
@@ -89,12 +99,12 @@ module.exports = async function startCommand(pathToCreate) {
8999
}
90100
}).start();
91101

92-
await createTree(directoryPath, tree, answer);
102+
await createTree(directoryPath, tree, aswr);
93103

94104
spinner.text = chalk`{cyan Start creating .gitignore file}.`;
95105

96106
const l = await fetch(
97-
`https://api.github.com/repos/github/gitignore/contents/${answer.language}.gitignore?ref=master`
107+
`https://api.github.com/repos/github/gitignore/contents/${aswr.gitignore}.gitignore?ref=master`
98108
);
99109
const gitignoreContent = await l.json();
100110
const contentBuf = Buffer.from(gitignoreContent.content, "base64");

0 commit comments

Comments
 (0)