UNPKG

vue-docgen-cli

Version:

Generate documentation markdown files from VueJs components using the vue-docgen-api.

107 lines 4.48 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.compile = void 0; const fs_1 = require("fs"); const path = __importStar(require("path")); const log = __importStar(require("loglevel")); const utils_1 = require("./utils"); const compileTemplates_1 = __importDefault(require("./compileTemplates")); /** * Build one md file per given compnent and save it respecting original scaffolding * if `config.watch` is true will jkeep on watch file changes * and update all needed files * @param files * @param config */ async function default_1(files, watcher, config, docMap, _compile = compile) { const compileWithConfig = _compile.bind(null, config, docMap, watcher); await Promise.all(files.map(f => compileWithConfig('init', f, false))); if (config.watch) { watcher // on fileChange, recompile the corresponding file .on('add', filePath => { files.push(filePath); compileWithConfig('add', filePath); }) .on('change', compileWithConfig.bind(null, 'change')) // on file delete, delete corresponding md file .on('unlink', (relPath) => { if (files.includes(relPath)) { files.splice(files.indexOf(relPath), 1); fs_1.promises.unlink(config.getDestFile(relPath, config)).catch(e => { log.error(`[vue-docgen-cli] Error while deleting file ${relPath}: ${e}`); }); } else { // if it's not a main file recompile the file connected to it compileWithConfig('delete', docMap[relPath]); } }); } } exports.default = default_1; /** * Compile a markdown file from a components and save it * This will use the filePath to know where to save * @param config config passed to the current chunk * @param docMap a map of each documentation file to the component they refer to * @param watcher * @param filePath relative path where the MD file is going to be saved */ async function compile(config, docMap, watcher, event, filePath, fromWatcher = true) { if (fromWatcher) { log.info(`[vue-docgen-cli] ${event} to ${filePath} detected.`); } const componentFile = docMap[filePath] || filePath; const file = config.getDestFile(componentFile, config); // if getDestFile is null, will not create files if (file) { try { const { content, dependencies } = await (0, compileTemplates_1.default)(event, path.join(config.componentsRoot, componentFile), config, componentFile); dependencies.forEach(d => { watcher.add(d); docMap[d] = componentFile; }); await (0, utils_1.writeDownMdFile)(content, file); log.info(`[vue-docgen-cli] File ${file} updated.`); } catch (e) { if (config.watch) { log.error('\x1b[31m%s\x1b[0m', `[vue-docgen-cli] Error compiling file ${file}:`); log.error(e); } else { const err = e; throw new Error(`[vue-docgen-cli] Error compiling file ${file}: ${err.message}`); } } } } exports.compile = compile; //# sourceMappingURL=multiMd.js.map