UNPKG

vitepress-jsdoc

Version:

A bridge between Vitepress and JSDoc-style commented codebases for hassle-free documentation.

101 lines 3.77 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.writeContentToFile = exports.createDocsFolder = exports.deleteDocsFolder = void 0; const promises_1 = __importDefault(require("node:fs/promises")); const node_path_1 = require("node:path"); const mkdirp_1 = require("mkdirp"); const interfaces_js_1 = require("../interfaces.js"); const rimraf_path_remover_js_1 = require("../classes/rimraf-path-remover.js"); /** * Deletes the specified documentation folder and any additional paths matching the provided patterns. * * @function * @async * * @param {string} docsFolder - The path to the documentation folder to be deleted. * @param {string[]} rmPattern - An array of glob patterns specifying additional paths to be deleted. * * @returns {Promise<string[]>} A promise that resolves with an array of paths that were deleted. * * @throws Will throw an error if not all paths were successfully deleted. * * @example * * const docsFolder = './docs'; * const rmPattern = ['**\/*.tmp']; // remove backslash * * const deletedPaths = await deleteDocsFolder(docsFolder, rmPattern); */ const deleteDocsFolder = async (docsFolder, rmPattern) => { const remover = new rimraf_path_remover_js_1.RimrafPathRemover(); const patterns = [docsFolder, ...rmPattern]; const allDeleted = await remover.delete(patterns); if (!allDeleted) { throw new Error('Not all paths were successfully deleted.'); } return remover.getDeletedPaths(); }; exports.deleteDocsFolder = deleteDocsFolder; const createDocsFolder = (docsFolder) => { mkdirp_1.mkdirp.sync(docsFolder); }; exports.createDocsFolder = createDocsFolder; /** * Writes the parsed content to a file on disk. * * @function * @async * * @param {ParseReturn | undefined} parseData - The parsed data to be written to the file. * @param {string} dest - The destination path where the file should be written. * * @returns {Promise<object | null>} A promise that resolves with an object containing details of the saved file, or null if the operation fails. * The returned object includes the original parsed data and a type indicating the status of the operation (e.g., included, excluded, error). * * @throws Will throw an error if there's an issue creating directories or writing to the file system. * * @example * * const parseData = { * content: '# My Documentation', * file: {name: 'example', ext: '.md'}, * empty: false, * excluded: false * }; * const dest = './docs'; * * const result = await writeContentToFile(parseData, dest); */ const writeContentToFile = async (parseData, dest) => { const root = process.cwd(); dest = (0, node_path_1.join)(root, dest); let type = interfaces_js_1.StatisticType.ERROR; if (parseData?.excluded) { type = interfaces_js_1.StatisticType.EXCLUDE; } try { if (parseData?.content) { const path = `${(0, node_path_1.join)(dest, parseData.file.name)}.md`; mkdirp_1.mkdirp.sync(dest); await promises_1.default.writeFile(path, parseData.content, 'utf8'); type = parseData?.empty ? interfaces_js_1.StatisticType.EMPTY : interfaces_js_1.StatisticType.INCLUDE; } if (!parseData) { throw new Error('Data is undefined'); } if (!parseData.dest) { throw new Error('Destination is undefined'); } return { ...parseData, type, }; } catch { } return undefined; }; exports.writeContentToFile = writeContentToFile; //# sourceMappingURL=file-operations.js.map