Skip to content

Commit

Permalink
feat: Add fliegdoc [i|init] to generate a fliegdoc.config.js
Browse files Browse the repository at this point in the history
Adds the dependency enquirer.
  • Loading branch information
pklaschka committed Jan 8, 2021
1 parent c38445d commit 044319e
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 5 deletions.
144 changes: 142 additions & 2 deletions bin/fliegdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,26 @@ const {
const cl = require('colorette');

const yargs = require('yargs');
const util = require('util');
const path = require('path');
const fs = require('fs');
const { cosmiconfigSync } = require('cosmiconfig');
const qu = require('enquirer');

const { config, filepath } = cosmiconfigSync('fliegdoc').search();
const { config, filepath } = cosmiconfigSync('fliegdoc').search() || {
config: undefined,
filepath: undefined
};

setConfig(config, path.dirname(filepath));
if (config) setConfig(config, path.dirname(filepath));

process.on('unhandledRejection', err => {
console.error(err);
process.exit(1);
});

const newConfigFilePath = path.join(process.cwd(), 'fliegdoc.config.js');

yargs
.scriptName('fliegdoc')
.epilog('Get help for individual commands by running $0 <command> --help')
Expand Down Expand Up @@ -87,6 +95,138 @@ yargs
serveDynamic(tree, args['port']);
}
)
.command(
['init', 'i'],
'Initialize a fliegdoc configuration',
yargs =>
yargs.check(() => {
if (fs.existsSync(newConfigFilePath))
throw new Error(
'The config file already exists: ' + newConfigFilePath
);
return true;
}),
async () => {
const answers = await qu.prompt([
{
name: 'title',
type: 'text',
required: true,
message: 'Project title'
},
{
name: 'readme',
type: 'text',
required: true,
message: "Path to the project's README.md file",
initial: './README.md'
},
{
name: 'outDir',
type: 'text',
required: true,
message: 'Path to which the documentation gets generated',
initial: './docs'
},
{
name: 'baseUrl',
type: 'text',
required: true,
message: 'Base URL of the documentation, when hosted on a server',
initial: '/'
},
{
name: 'hidePrivateMembers',
type: 'confirm',
initial: true,
message: 'Hide private class members in the documentation?'
}
]);

answers['externalLinks'] = {};

while (
await new qu.Confirm({
name: 'addExternalLink',
type: 'confirm',
initial: 'false',
message:
'Do you want to add another external link to the documentation?'
}).run()
) {
const newExternalLink = await new qu.Form({
name: 'newExternalLink',
message: 'Please specify the details of the external link',
choices: [
{
name: 'key',
message: 'Link Label'
},
{
name: 'value',
message: 'Link'
}
]
}).run();

answers.externalLinks[newExternalLink['key']] =
newExternalLink['value'];

console.log('Link added successfully');
}

answers['modules'] = [];

do {
const newModule = await new qu.Form({
name: 'newModule',
message: 'Please specify the details of the module',
choices: [
{
name: 'tsconfig',
initial: './tsconfig.json',
message:
'tsconfig.json location, relative to the current working directory'
},
{
name: 'package',
initial: './package.json',
message:
'package.json location, relative to the current working directory'
},
{
name: 'mainFile',
initial: 'main.ts',
message:
"the package's main file, relative to the sources configured in the tsconfig.json"
}
]
}).run();

answers.modules.push(newModule);

console.log('Module added successfully');
} while (
await new qu.Confirm({
name: 'addModule',
type: 'confirm',
initial: 'false',
message:
'Do you want to add another module? You can add an arbitrary amount of modules!'
}).run()
);

answers['baseUrl'] = answers['baseUrl'].endsWith('/')
? answers['baseUrl']
: answers['baseUrl'] + '/';

let configString = util.inspect(answers, false, 5, false);
console.info(configString);
configString =
'// Generated using fliegdoc init\nmodule.exports = ' + configString;
fs.writeFileSync(newConfigFilePath, configString);
}
)
.help()
.demandCommand()
.completion()
Expand Down
4 changes: 1 addition & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"dependencies": {
"colorette": "^1.2.1",
"cosmiconfig": "^7.0.0",
"enquirer": "^2.3.6",
"eta": "^1.12.1",
"express": "^4.17.1",
"markdown-it": "^12.0.4",
Expand Down

0 comments on commit 044319e

Please sign in to comment.