vue-docgen-cli
Version:
Generate documentation markdown files from VueJs components using the vue-docgen-api.
114 lines • 4.57 kB
JavaScript
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.resolveRequiresFromTag = exports.normalizePaths = exports.findFileCaseInsensitive = exports.getDocMap = exports.writeDownMdFile = void 0;
const path = __importStar(require("path"));
const fs = __importStar(require("fs"));
const util_1 = require("util");
const prettier_1 = __importDefault(require("prettier"));
const mkdirp = (0, util_1.promisify)(fs.mkdir);
/**
* Prettify then save a markdown content
* If the needed directory does not exist, create it
* @param content dirty looking markdown content to be saved
* @param destFilePath destination absolute file path
*/
async function writeDownMdFile(content, destFilePath) {
const conf = await prettier_1.default.resolveConfig(destFilePath);
const prettyMd = (cont) => prettier_1.default.format(cont, Object.assign(conf || {}, { parser: 'markdown' }));
const destFolder = path.dirname(destFilePath);
await mkdirp(destFolder, { recursive: true });
const writeStream = fs.createWriteStream(destFilePath);
if (Array.isArray(content)) {
content.forEach(cont => {
writeStream.write(prettyMd(cont));
});
}
else {
writeStream.write(prettyMd(content));
}
// close the stream
writeStream.close();
}
exports.writeDownMdFile = writeDownMdFile;
/**
* retrun an object matching document relative file path
* with their corresponding components, it's inteded to be use
* with watchers to update the right documentation on update of
* Readme.md files
* @param files file paths of the matched comeponents
* @param getDocFileName way to transform a comopnent path into it's Readme.md
* @param root componentRoot to de-absolutize the DocFileName path
*/
function getDocMap(files, getDocFileName, root) {
const docMap = {};
files.forEach(f => {
const docFilePath = normalizePaths(getDocFileName(path.join(root, f)));
docFilePath.forEach(doc => {
docMap[path.relative(root, doc)] = f;
});
});
return docMap;
}
exports.getDocMap = getDocMap;
/**
* Find a file in a directory, case-insensitive
*
* @param {string} filepath
* @return {string|undefined} File path with correct case
*/
function findFileCaseInsensitive(filepath) {
const dir = path.dirname(filepath);
const fileNameLower = path.basename(filepath).toLowerCase();
const files = fs.readdirSync(dir);
const found = files.find(file => file.toLowerCase() === fileNameLower);
return found && path.join(dir, found);
}
exports.findFileCaseInsensitive = findFileCaseInsensitive;
function normalizePaths(path) {
if (!path)
return [];
return Array.isArray(path) ? path : [path];
}
exports.normalizePaths = normalizePaths;
function resolveRequiresFromTag(requires, compDirName) {
return (requires?.reduce((acc, t) => {
const requirePath = (t.description ?? t.content);
requirePath.split('\n').map((p) => {
p.split(',').map((pMin) => {
const pMinTrimmed = pMin.trim().replace(/^['"]/, '').replace(/['"]$/, '');
if (pMinTrimmed.length === 0)
return;
acc.push(path.join(compDirName, pMinTrimmed));
});
});
return acc;
}, []) ?? []);
}
exports.resolveRequiresFromTag = resolveRequiresFromTag;
//# sourceMappingURL=utils.js.map
;