Skip to content

Commit 044319e

Browse files
committed
feat: Add fliegdoc [i|init] to generate a fliegdoc.config.js
Adds the dependency enquirer.
1 parent c38445d commit 044319e

3 files changed

Lines changed: 144 additions & 5 deletions

File tree

bin/fliegdoc.js

Lines changed: 142 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,26 @@ const {
1313
const cl = require('colorette');
1414

1515
const yargs = require('yargs');
16+
const util = require('util');
1617
const path = require('path');
18+
const fs = require('fs');
1719
const { 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

2329
process.on('unhandledRejection', err => {
2430
console.error(err);
2531
process.exit(1);
2632
});
2733

34+
const newConfigFilePath = path.join(process.cwd(), 'fliegdoc.config.js');
35+
2836
yargs
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()

package-lock.json

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
"dependencies": {
7373
"colorette": "^1.2.1",
7474
"cosmiconfig": "^7.0.0",
75+
"enquirer": "^2.3.6",
7576
"eta": "^1.12.1",
7677
"express": "^4.17.1",
7778
"markdown-it": "^12.0.4",

0 commit comments

Comments
 (0)