From 044319efff558ca6ade85a231c7e4ea6c315c802 Mon Sep 17 00:00:00 2001 From: Pablo Klaschka Date: Fri, 8 Jan 2021 22:48:06 +0100 Subject: [PATCH] feat: Add fliegdoc [i|init] to generate a fliegdoc.config.js Adds the dependency enquirer. --- bin/fliegdoc.js | 144 +++++++++++++++++++++++++++++++++++++++++++++- package-lock.json | 4 +- package.json | 1 + 3 files changed, 144 insertions(+), 5 deletions(-) diff --git a/bin/fliegdoc.js b/bin/fliegdoc.js index a5e7afc7..39585880 100644 --- a/bin/fliegdoc.js +++ b/bin/fliegdoc.js @@ -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 --help') @@ -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() diff --git a/package-lock.json b/package-lock.json index 02637d62..981174ea 100644 --- a/package-lock.json +++ b/package-lock.json @@ -520,8 +520,7 @@ "ansi-colors": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/ansi-colors/-/ansi-colors-4.1.1.tgz", - "integrity": "sha512-JoX0apGbHaUJBNl6yF+p6JAFYZ666/hhCGKN5t9QFjbJQKUU/g8MNbFDbvfrgKXvI1QpZplPOnwIo99lX/AAmA==", - "dev": true + "integrity": "sha512-JoX0apGbHaUJBNl6yF+p6JAFYZ666/hhCGKN5t9QFjbJQKUU/g8MNbFDbvfrgKXvI1QpZplPOnwIo99lX/AAmA==" }, "ansi-regex": { "version": "5.0.0", @@ -1365,7 +1364,6 @@ "version": "2.3.6", "resolved": "https://registry.npmjs.org/enquirer/-/enquirer-2.3.6.tgz", "integrity": "sha512-yjNnPr315/FjS4zIsUxYguYUPP2e1NK4d7E7ZOLiyYCcbFBiTMyID+2wvm2w6+pZ/odMA7cRkjhsPbltwBOrLg==", - "dev": true, "requires": { "ansi-colors": "^4.1.1" } diff --git a/package.json b/package.json index 8c577fae..d8ee6353 100644 --- a/package.json +++ b/package.json @@ -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",