histoire
Version:
A fast story book powered by Vite
107 lines (106 loc) • 3.53 kB
JavaScript
import chokidar from 'chokidar';
import fs from 'fs-extra';
import path from 'pathe';
import pc from 'picocolors';
import { getContextRegistry } from './runtime/registry.js';
import { addStory, notifyStoryChange, notifyStoryListChange, removeStory } from './stories.js';
import { toTempPathSegment } from './util/temp-paths.js';
export class BasePluginApi {
ctx;
plugin;
moduleLoader;
colors = pc;
path = path;
fs = fs;
constructor(ctx, plugin, moduleLoader) {
this.ctx = ctx;
this.plugin = plugin;
this.moduleLoader = moduleLoader;
}
/** Resolved project root, independent from the caller's process cwd. */
get root() {
return this.ctx.root;
}
/** Trusted configuration/resource ownership for the captured generation. */
getContext() {
return getContextRegistry(this.ctx).pluginContext;
}
/** Isolated mutable plugin output, unique across operation captures. */
get pluginTempDir() {
return path.resolve(getContextRegistry(this.ctx).tempDir, 'plugins', toTempPathSegment(this.plugin.name));
}
log(...msg) {
console.log(this.colors.gray(`[Plugin:${this.plugin.name}]`), ...msg);
}
warn(...msg) {
console.warn(this.colors.yellow(`[Plugin:${this.plugin.name}]`), ...msg);
}
error(...msg) {
console.error(this.colors.red(`[Plugin:${this.plugin.name}]`), ...msg);
}
addStoryFile(file) {
if (!this.isActive())
return;
removeStory(this.ctx, file);
const story = addStory(this.ctx, file);
notifyStoryListChange(this.ctx);
notifyStoryChange(this.ctx, story);
}
/** Removes one owned story without touching another plugin's inventory. */
removeStoryFile(file) {
if (!this.isActive())
return;
removeStory(this.ctx, file);
notifyStoryListChange(this.ctx);
notifyStoryChange(this.ctx);
}
/** Captured cleanup and execution ownership jointly fence publication. */
isActive() {
const registry = getContextRegistry(this.ctx);
return !registry.cleanup.isClosing && (registry.execution?.isActive() ?? true);
}
getStories() {
return this.ctx.storyFiles.map(f => f.story).filter(Boolean);
}
getConfig() {
return this.ctx.config;
}
}
export class DevPluginApi extends BasePluginApi {
watcher = chokidar;
}
export class BuildPluginApi extends BasePluginApi {
/** Directory receiving the static book. */
outputDir;
changeViteConfigCallbacks = [];
/**
* @param ctx Histoire context.
* @param plugin Plugin owning this API.
* @param moduleLoader Loader bound to the build collection server.
* @param outputDir Static book directory; defaults to the configured `outDir`.
*/
constructor(ctx, plugin, moduleLoader, outputDir = ctx.config.outDir) {
super(ctx, plugin, moduleLoader);
this.outputDir = outputDir;
}
buildEndCallbacks = [];
previewStoryCallbacks = [];
changeViteConfig(cb) {
this.changeViteConfigCallbacks.push(cb);
}
onBuildEnd(cb) {
this.buildEndCallbacks.push(cb);
}
onPreviewStory(cb) {
this.previewStoryCallbacks.push(cb);
}
}
export class DevEventPluginApi extends BasePluginApi {
event;
payload;
constructor(ctx, plugin, moduleLoader, event, payload) {
super(ctx, plugin, moduleLoader);
this.event = event;
this.payload = payload;
}
}