|
| 1 | +import { createRequire } from "node:module"; |
| 2 | +import path from "node:path"; |
| 3 | + |
| 4 | +import { IndiekitError } from "@indiekit/error"; |
| 5 | +import makeDebug from "debug"; |
| 6 | + |
| 7 | +import { getPluginId } from "./plugins.js"; |
| 8 | + |
| 9 | +const debug = makeDebug(`indiekit:plugin`); |
| 10 | + |
| 11 | +export class IndiekitPlugin { |
| 12 | + constructor(Indiekit) { |
| 13 | + this.indiekit = Indiekit; |
| 14 | + this.name = undefined; |
| 15 | + this.packageName = undefined; |
| 16 | + this.options = {}; |
| 17 | + } |
| 18 | + |
| 19 | + static async register(indiekit, packageName) { |
| 20 | + debug(`Registering ${packageName}`); |
| 21 | + |
| 22 | + const plugin = new this(indiekit); |
| 23 | + plugin.packageName = packageName; |
| 24 | + plugin.options = indiekit.config[packageName]; |
| 25 | + plugin.name = plugin.name || packageName; |
| 26 | + |
| 27 | + // Initiate plugin features |
| 28 | + await plugin.init(); |
| 29 | + |
| 30 | + // Add plugin to Indiekit |
| 31 | + indiekit.installedPlugins.add(plugin); |
| 32 | + |
| 33 | + return plugin; |
| 34 | + } |
| 35 | + |
| 36 | + get filePath() { |
| 37 | + const require = createRequire(import.meta.url); |
| 38 | + |
| 39 | + try { |
| 40 | + return path.dirname(require.resolve(`${this.packageName}/package.json`)); |
| 41 | + } catch (error) { |
| 42 | + throw new IndiekitError( |
| 43 | + `Could not resolve path for ${this.packageName}`, |
| 44 | + { |
| 45 | + cause: error, |
| 46 | + plugin: this.packageName, |
| 47 | + }, |
| 48 | + ); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + get id() { |
| 53 | + return getPluginId(this.packageName); |
| 54 | + } |
| 55 | + |
| 56 | + async init() { |
| 57 | + debug(`Initiating ${this.packageName}`); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +export class IndiekitPostTypePlugin extends IndiekitPlugin { |
| 62 | + constructor(Indiekit) { |
| 63 | + super(Indiekit); |
| 64 | + |
| 65 | + this.type = undefined; |
| 66 | + } |
| 67 | + |
| 68 | + get config() { |
| 69 | + return {}; |
| 70 | + } |
| 71 | + |
| 72 | + addPostTypes() { |
| 73 | + // Override post type configuration with user options |
| 74 | + const postType = { ...this.config, ...this.options }; |
| 75 | + |
| 76 | + if (postType) { |
| 77 | + debug("Adding post type configuration for", this.type); |
| 78 | + this.indiekit.postTypes.set(this.type, { |
| 79 | + ...this.indiekit.postTypes.get(this.type), |
| 80 | + ...postType, |
| 81 | + }); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + get validationSchemas() { |
| 86 | + return {}; |
| 87 | + } |
| 88 | + |
| 89 | + addValidationSchemas() { |
| 90 | + const validationSchemas = this.validationSchemas; |
| 91 | + if (validationSchemas) { |
| 92 | + for (const [field, schema] of Object.entries(validationSchemas)) { |
| 93 | + debug("Adding validation schemas for", field); |
| 94 | + this.indiekit.validationSchemas.set(field, schema); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + async init() { |
| 100 | + await super.init(); |
| 101 | + |
| 102 | + this.addPostTypes(); |
| 103 | + this.addValidationSchemas(); |
| 104 | + } |
| 105 | +} |
0 commit comments