UNPKG

@acot/cli

Version:
66 lines (65 loc) 2.32 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 path_1 = __importDefault(require("path")); const document_1 = require("@acot/document"); const command_1 = require("../command"); const debug = require('debug')('acot:cli'); exports.default = (0, command_1.createCommand)({ name: 'docgen', summary: 'Document generation of the list of rules provided by the preset.', args: { target: { type: 'string', default: 'README.md', description: 'File path to inject the document.', }, }, options: { project: { type: 'string', alias: 'p', default: '.', description: 'Directory path that contains the package.json that makes up the preset.', }, docs: { type: 'string', alias: 'd', default: path_1.default.join('docs', 'rules'), description: 'Directory path that contains the rule documentation.', }, 'dry-run': { type: 'boolean', default: false, description: 'Writes the document to standard output instead of a file.', }, }, })(async ({ cwd, logger, args }) => { const proj = path_1.default.resolve(cwd, args.project); const target = path_1.default.resolve(proj, args.target); const loader = new document_1.DocProjectLoader({ docs: args.docs }); const generator = new document_1.DocGenerator(); try { const project = await loader.load(proj); const rules = await generator.generate(project); const markdown = fs_1.default.readFileSync(target, 'utf8'); const injector = new document_1.DocInjector(); const output = injector.inject(markdown, rules); if (args['dry-run'] === true) { debug('write to stdout'); logger.print(output); } else { debug('write to file (%s)', target); fs_1.default.writeFileSync(target, output, 'utf8'); } } catch (e) { logger.error(e); return 1; } return 0; });