UNPKG

@knodes/typedoc-pluginutils

Version:
127 lines 6.08 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.getApplication = exports.getPlugin = exports.ABasePlugin = void 0; const assert_1 = __importDefault(require("assert")); const lodash_1 = require("lodash"); const pkg_up_1 = require("pkg-up"); const semver_1 = require("semver"); const typedoc_1 = require("typedoc"); const events_extra_1 = require("./events-extra"); const plugin_logger_1 = require("./plugin-logger"); const miscUtils = __importStar(require("./utils/misc")); const path_1 = require("./utils/path"); class ABasePlugin { get name() { return `${this.package.name}:${this.constructor.name}`; } get rootDir() { return miscUtils.rootDir(this.application); } /** * Instanciate a new instance of the base plugin. The `package.json` file will be read to obtain the plugin name & the TypeDoc compatible range. * Logs a warning if the current TypeDoc version is not compatible. * * @param application - The application instance. * @param pluginFilename - The actual plugin file name. Used to lookup the `package.json` file. */ constructor(application, pluginFilename) { var _a, _b, _c; this.application = application; const pkgFile = (0, pkg_up_1.sync)({ cwd: (0, path_1.dirname)(pluginFilename) }); if (!pkgFile) { throw new Error('Could not determine package.json'); } // eslint-disable-next-line @typescript-eslint/no-var-requires -- Require package.json const pkg = require(pkgFile); (0, assert_1.default)(pkg.name); (0, assert_1.default)(pkg.version); this.pluginDir = (0, path_1.dirname)(pkgFile); this.package = pkg; this.logger = new plugin_logger_1.PluginLogger(application.logger, this); this.logger.verbose(`Using plugin version ${pkg.version}`); this.optionsPrefix = (0, lodash_1.camelCase)((0, path_1.basename)(pkg.name).replace(/^typedoc-/, '')); events_extra_1.EventsExtra.for(this.application) .onSetOption(`${this.optionsPrefix}:logLevel`, v => { this.logger.level = v; }); const typedocPeerDep = (_b = (_a = pkg.peerDependencies) === null || _a === void 0 ? void 0 : _a.typedoc) !== null && _b !== void 0 ? _b : (_c = pkg.dependencies) === null || _c === void 0 ? void 0 : _c.typedoc; (0, assert_1.default)(typedocPeerDep); if (!(0, semver_1.satisfies)(typedoc_1.Application.VERSION, typedocPeerDep)) { this.logger.warn(`TypeDoc version ${typedoc_1.Application.VERSION} does not match the plugin's peer dependency range ${typedocPeerDep}. You might encounter problems.`); } this.application.converter.on(typedoc_1.Converter.EVENT_RESOLVE_BEGIN, ABasePlugin._addSourceToProject.bind(this)); } /** * Return the path as a relative path from the {@link rootDir}. * * @param path - The path to convert. * @returns the relative path. */ relativeToRoot(path) { return (0, typedoc_1.normalizePath)((0, path_1.relative)(this.rootDir, path)); } /** * Resolve the path to a plugin file (resolved from the plugin `package.json`). * * @param path - The path to resolve. * @returns the resolved path. */ resolvePackageFile(path) { return (0, typedoc_1.normalizePath)((0, path_1.resolve)(this.pluginDir, path)); } } ABasePlugin._addSourceToProject = (0, lodash_1.once)(function (context) { var _a; const packagePlugin = context.converter.getComponent('package'); const errMsg = 'It is used to complete README & package sources for better tracking of markdown issues.'; if (!packagePlugin) { this.logger.warn(`Missing \`package\` plugin. ${errMsg}`); return; } if (!('readmeFile' in packagePlugin && (0, lodash_1.isString)(packagePlugin.readmeFile))) { this.logger.warn(`Missing \`readmeFile\` in \`package\` plugin. ${errMsg}`); return; } const extraSources = [ new typedoc_1.SourceReference(packagePlugin.readmeFile, 1, 1), ]; if ('packageFile' in packagePlugin && (0, lodash_1.isString)(packagePlugin.packageFile)) { extraSources.push(new typedoc_1.SourceReference(packagePlugin.packageFile, 1, 1)); } context.project.sources = [ ...extraSources, ...((_a = context.project.sources) !== null && _a !== void 0 ? _a : []), ]; }); exports.ABasePlugin = ABasePlugin; const getPlugin = (pluginAccessor) => pluginAccessor instanceof ABasePlugin ? pluginAccessor : pluginAccessor.plugin; exports.getPlugin = getPlugin; const getApplication = (applicationAccessor) => applicationAccessor instanceof typedoc_1.Application ? applicationAccessor : (0, exports.getPlugin)(applicationAccessor).application; exports.getApplication = getApplication; //# sourceMappingURL=base-plugin.js.map