2025-05-12 05:38:44 +09:00

143 lines
3.9 KiB
JavaScript

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const immutable_1 = __importDefault(require("immutable"));
const templateBlock_1 = __importDefault(require("./templateBlock"));
const themePrefix_1 = __importDefault(require("../constants/themePrefix"));
const DEFAULT_VERSION = "*";
class Plugin extends immutable_1.default.Record({
name: String(),
// Requirement version (ex: ">1.0.0")
version: String(DEFAULT_VERSION),
// Path to load this plugin
path: String(),
// Depth of this plugin in the dependency tree
depth: Number(0),
// Parent depending on this plugin
parent: String(),
// Content of the "package.json"
package: immutable_1.default.Map(),
// Content of the package itself
content: immutable_1.default.Map()
}, "Plugin") {
getName() {
return this.get("name");
}
getPath() {
return this.get("path");
}
getVersion() {
return this.get("version");
}
getPackage() {
return this.get("package");
}
getContent() {
return this.get("content");
}
getDepth() {
return this.get("depth");
}
getParent() {
return this.get("parent");
}
/**
* Return the ID on NPM for this plugin
* return package.json's name
* @return {string}
*/
getNpmID() {
const pkg = this.getPackage().toJS();
if ("name" in pkg) {
return pkg["name"];
}
throw new Error(`${this.getName()} plugin's package.json does not have "name" fields.`);
}
/**
* Check if a plugin is loaded
* @return {boolean}
*/
isLoaded() {
return Boolean(this.getPackage().size > 0);
}
/**
* Check if a plugin is a theme given its name
* @return {boolean}
*/
isTheme() {
const name = this.getName();
return name && name.indexOf(themePrefix_1.default) === 0;
}
/**
* Return map of hooks
*/
getHooks() {
return this.getContent().get("hooks") || immutable_1.default.Map();
}
/**
* Return infos about resources for a specific type
* @param {string} type
* @return {Map<String:Mixed>}
*/
getResources(type) {
if (type != "website" && type != "ebook") {
throw new Error(`Invalid assets type ${type}`);
}
const content = this.getContent();
return content.get(type) || (type == "website" ? content.get("book") : null) || immutable_1.default.Map();
}
/**
* Return map of filters
* @return {Map<String:Function>}
*/
getFilters() {
return this.getContent().get("filters");
}
/**
* Return map of blocks
* @return {Map<String:TemplateBlock>}
*/
getBlocks() {
let blocks = this.getContent().get("blocks");
blocks = blocks || immutable_1.default.Map();
return blocks.map((block, blockName) => {
return templateBlock_1.default.create(blockName, block);
});
}
/**
* Return a specific hook
* @param {string} name
* @return {Function|undefined}
*/
getHook(name) {
return this.getHooks().get(name);
}
/**
* Create a plugin from a string
* @return {Plugin}
* @param s
*/
static createFromString(s) {
const parts = s.split("@");
const name = parts[0];
const version = parts.slice(1).join("@");
return new Plugin({
name: name,
version: version || DEFAULT_VERSION
});
}
/**
* Create a plugin from a dependency
* @return {Plugin}
*/
static createFromDep(dep) {
return new Plugin({
name: dep.getName(),
version: dep.getVersion()
});
}
}
exports.default = Plugin;