UNPKG

@codeque/cli

Version:

Multiline code search for every language. Structural code search for JavaScript, TypeScript, HTML and CSS

109 lines (108 loc) 3.8 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = __importDefault(require("fs")); const template_1 = __importDefault(require("./template")); const createCli_1 = require("../createCli"); function createCommandsInspector() { let currentCommand = null; const commands = []; const parseOption = (data, description, defaultValue, required) => { const argRegex = /(<|\[).+?(>|\])/g; const argument = data.match(argRegex); const [shortName, longName] = data .replace(argRegex, '') .trim() .split(/,\s+/); return { shortName, longName, argument: argument !== null && argument.length > 0 ? argument[0] : undefined, description, defaultValue, required, }; }; return { root(args) { if (currentCommand !== null) { commands.push(currentCommand); } currentCommand = { isRoot: true, name: '', arguments: args.map((arg) => ({ nameRaw: arg, name: arg.substring(1, arg.length - 1), required: arg.charAt(0) === '<', })), options: [], }; return this; }, command(cmd) { if (currentCommand !== null) { commands.push(currentCommand); } const [name, ...args] = cmd.split(/\s+/); currentCommand = { name, isRoot: false, arguments: args.map((arg) => ({ nameRaw: arg, name: arg.substring(1, arg.length - 1), required: arg.charAt(0) === '<', })), options: [], }; return this; }, description(description, argDescription) { if (currentCommand !== null) { currentCommand.description = description; if (argDescription !== undefined) { currentCommand.arguments.forEach((arg) => { //eslint-disable-next-line if (argDescription.hasOwnProperty(arg.name)) { arg.description = argDescription[arg.name]; } }); } } return this; }, option(data, description, defaultValue) { if (currentCommand !== null) { currentCommand.options.push(parseOption(data, description, defaultValue, false)); } return this; }, requiredOption(data, description, defaultValue) { if (currentCommand !== null) { currentCommand.options.push(parseOption(data, description, defaultValue, true)); } return this; }, action() { return this; }, getCommands() { if (currentCommand !== null) { commands.push(currentCommand); } return commands; }, }; } function generate(output, initialHeaderLevel = 3) { const commandInspector = createCommandsInspector(); commandInspector.root([]); //@ts-ignore (0, createCli_1.createCliProgram)(commandInspector); const commands = commandInspector.getCommands(); const document = (0, template_1.default)(commands, initialHeaderLevel); fs_1.default.writeFileSync(output, document); } exports.default = generate;