@@ -13,18 +13,26 @@ const {
1313const cl = require ( 'colorette' ) ;
1414
1515const yargs = require ( 'yargs' ) ;
16+ const util = require ( 'util' ) ;
1617const path = require ( 'path' ) ;
18+ const fs = require ( 'fs' ) ;
1719const { cosmiconfigSync } = require ( 'cosmiconfig' ) ;
20+ const qu = require ( 'enquirer' ) ;
1821
19- const { config, filepath } = cosmiconfigSync ( 'fliegdoc' ) . search ( ) ;
22+ const { config, filepath } = cosmiconfigSync ( 'fliegdoc' ) . search ( ) || {
23+ config : undefined ,
24+ filepath : undefined
25+ } ;
2026
21- setConfig ( config , path . dirname ( filepath ) ) ;
27+ if ( config ) setConfig ( config , path . dirname ( filepath ) ) ;
2228
2329process . on ( 'unhandledRejection' , err => {
2430 console . error ( err ) ;
2531 process . exit ( 1 ) ;
2632} ) ;
2733
34+ const newConfigFilePath = path . join ( process . cwd ( ) , 'fliegdoc.config.js' ) ;
35+
2836yargs
2937 . scriptName ( 'fliegdoc' )
3038 . epilog ( 'Get help for individual commands by running $0 <command> --help' )
@@ -87,6 +95,138 @@ yargs
8795 serveDynamic ( tree , args [ 'port' ] ) ;
8896 }
8997 )
98+ . command (
99+ [ 'init' , 'i' ] ,
100+ 'Initialize a fliegdoc configuration' ,
101+ yargs =>
102+ yargs . check ( ( ) => {
103+ if ( fs . existsSync ( newConfigFilePath ) )
104+ throw new Error (
105+ 'The config file already exists: ' + newConfigFilePath
106+ ) ;
107+ return true ;
108+ } ) ,
109+ async ( ) => {
110+ const answers = await qu . prompt ( [
111+ {
112+ name : 'title' ,
113+ type : 'text' ,
114+ required : true ,
115+ message : 'Project title'
116+ } ,
117+ {
118+ name : 'readme' ,
119+ type : 'text' ,
120+ required : true ,
121+ message : "Path to the project's README.md file" ,
122+ initial : './README.md'
123+ } ,
124+ {
125+ name : 'outDir' ,
126+ type : 'text' ,
127+ required : true ,
128+ message : 'Path to which the documentation gets generated' ,
129+ initial : './docs'
130+ } ,
131+ {
132+ name : 'baseUrl' ,
133+ type : 'text' ,
134+ required : true ,
135+ message : 'Base URL of the documentation, when hosted on a server' ,
136+ initial : '/'
137+ } ,
138+ {
139+ name : 'hidePrivateMembers' ,
140+ type : 'confirm' ,
141+ initial : true ,
142+ message : 'Hide private class members in the documentation?'
143+ }
144+ ] ) ;
145+
146+ answers [ 'externalLinks' ] = { } ;
147+
148+ while (
149+ await new qu . Confirm ( {
150+ name : 'addExternalLink' ,
151+ type : 'confirm' ,
152+ initial : 'false' ,
153+ message :
154+ 'Do you want to add another external link to the documentation?'
155+ } ) . run ( )
156+ ) {
157+ const newExternalLink = await new qu . Form ( {
158+ name : 'newExternalLink' ,
159+ message : 'Please specify the details of the external link' ,
160+ choices : [
161+ {
162+ name : 'key' ,
163+ message : 'Link Label'
164+ } ,
165+ {
166+ name : 'value' ,
167+ message : 'Link'
168+ }
169+ ]
170+ } ) . run ( ) ;
171+
172+ answers . externalLinks [ newExternalLink [ 'key' ] ] =
173+ newExternalLink [ 'value' ] ;
174+
175+ console . log ( 'Link added successfully' ) ;
176+ }
177+
178+ answers [ 'modules' ] = [ ] ;
179+
180+ do {
181+ const newModule = await new qu . Form ( {
182+ name : 'newModule' ,
183+ message : 'Please specify the details of the module' ,
184+ choices : [
185+ {
186+ name : 'tsconfig' ,
187+ initial : './tsconfig.json' ,
188+ message :
189+ 'tsconfig.json location, relative to the current working directory'
190+ } ,
191+ {
192+ name : 'package' ,
193+ initial : './package.json' ,
194+ message :
195+ 'package.json location, relative to the current working directory'
196+ } ,
197+ {
198+ name : 'mainFile' ,
199+ initial : 'main.ts' ,
200+ message :
201+ "the package's main file, relative to the sources configured in the tsconfig.json"
202+ }
203+ ]
204+ } ) . run ( ) ;
205+
206+ answers . modules . push ( newModule ) ;
207+
208+ console . log ( 'Module added successfully' ) ;
209+ } while (
210+ await new qu . Confirm ( {
211+ name : 'addModule' ,
212+ type : 'confirm' ,
213+ initial : 'false' ,
214+ message :
215+ 'Do you want to add another module? You can add an arbitrary amount of modules!'
216+ } ) . run ( )
217+ ) ;
218+
219+ answers [ 'baseUrl' ] = answers [ 'baseUrl' ] . endsWith ( '/' )
220+ ? answers [ 'baseUrl' ]
221+ : answers [ 'baseUrl' ] + '/' ;
222+
223+ let configString = util . inspect ( answers , false , 5 , false ) ;
224+ console . info ( configString ) ;
225+ configString =
226+ '// Generated using fliegdoc init\nmodule.exports = ' + configString ;
227+ fs . writeFileSync ( newConfigFilePath , configString ) ;
228+ }
229+ )
90230 . help ( )
91231 . demandCommand ( )
92232 . completion ( )
0 commit comments