world/book/node_modules/honkit/lib/plugins/PluginResolver.js
2025-05-12 05:38:44 +09:00

91 lines
3.7 KiB
JavaScript

// LICENSE : MIT
"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.PluginResolver = void 0;
const path_1 = __importDefault(require("path"));
const util = __importStar(require("./package-name-util"));
const try_resolve_1 = __importDefault(require("try-resolve"));
const SPECIAL_PACKAGE_NAME = [
"fontsettings", // → @honkit/honkit-plugin-fontsettings
"highlight", // → @honkit/honkit-plugin-highlight
"theme-default" // → @honkit/honkit-plugin-theme-default
];
/**
* This class aim to resolve honkit's package name and get the module path.
*
* Define
*
* - `package` is npm package
* - `module` is package's main module
*
* ## Support
*
* - honkit-plugin-*
* - gitbook-plugin-*
*/
class PluginResolver {
baseDirectory;
constructor(config = {}) {
/**
* @type {string} baseDirectory for resolving
*/
this.baseDirectory = config && config.baseDirectory ? config.baseDirectory : "";
}
/**
* Take package name, and return path to module.
* @param {string} packageName
* @returns {string} return path to module
*/
resolvePluginPackageName(packageName) {
const baseDir = this.baseDirectory;
const honkitFullPackageName = util.createFullPackageName("honkit-plugin-", packageName);
// honkit > gitbook > normal
const gitbookFullPackageName = util.createFullPackageName("gitbook-plugin-", packageName);
// special case for backward-compatible
// e.g.) load theme-default as @honkit/honkit-plugins-theme-default
const honkitScopePackageName = `@honkit/${honkitFullPackageName}`;
// In sometimes, HonKit package has not main field - so search package.json
const pkgPath = (0, try_resolve_1.default)(path_1.default.join(baseDir, honkitFullPackageName, "/package.json")) ||
(0, try_resolve_1.default)(path_1.default.join(baseDir, gitbookFullPackageName, "/package.json")) ||
(0, try_resolve_1.default)(path_1.default.join(baseDir, packageName, "/package.json")) ||
(SPECIAL_PACKAGE_NAME.includes(packageName) &&
(0, try_resolve_1.default)(path_1.default.join(baseDir, honkitScopePackageName, "/package.json")));
if (!pkgPath) {
throw new ReferenceError(`Failed to load HonKit's plugin module: "${packageName}" is not found.
cwd: ${process.cwd()}
baseDir: ${baseDir}
`);
}
return pkgPath.substring(0, pkgPath.length - "/package.json".length);
}
}
exports.PluginResolver = PluginResolver;