@@ -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