feat(sdk): ESM multi-fichiers + storage plugin-owned (#134/#139)
Documente et illustre deux contrats SDK : - Multi-fichiers ESM : le `main` du manifeste peut importer d'autres fichiers du package via specifiers relatifs, servis par IdeA sur `idea-plugin://` (build `tsc` non bundlé). Le packager embarque tout `dist/**/*.js` et vérifie la présence du `main`. Les bare specifiers (`node_modules`) restent hors contrat : à bundler ou vendorer. - Storage plugin-owned : `ctx.storage` est la place canonique de l'état interne du plugin (compteurs, flags, préférences, caches), hors des fichiers projet. Les APIs workspace/config restent pour le contenu project-owned. L'exemple hello-plugin est éclaté en modules (constants, core/layout, core/workspace, core/storage) pour exercer l'import relatif et le storage. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
32
examples/hello-plugin/src/core/storage.ts
Normal file
32
examples/hello-plugin/src/core/storage.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import type { ActivateContext } from "@idea/plugin-sdk";
|
||||
import { STORAGE_KEYS } from "../constants.js";
|
||||
|
||||
export async function initializePluginStorage(ctx: ActivateContext): Promise<void> {
|
||||
if (!ctx.storage) {
|
||||
ctx.logger.warn("plugin-owned storage unavailable");
|
||||
return;
|
||||
}
|
||||
|
||||
const activationCount = await incrementStoredNumber(ctx, STORAGE_KEYS.activationCount);
|
||||
const initialized = await ctx.storage.get<boolean>(STORAGE_KEYS.initialized);
|
||||
if (!initialized) {
|
||||
await ctx.storage.set(STORAGE_KEYS.initialized, true);
|
||||
}
|
||||
|
||||
ctx.logger.info("plugin-owned storage ready", {
|
||||
activationCount,
|
||||
initialized: initialized ?? false
|
||||
});
|
||||
}
|
||||
|
||||
export async function recordCommandRun(ctx: ActivateContext): Promise<number | undefined> {
|
||||
if (!ctx.storage) return undefined;
|
||||
return incrementStoredNumber(ctx, STORAGE_KEYS.commandRunCount);
|
||||
}
|
||||
|
||||
async function incrementStoredNumber(ctx: ActivateContext, key: string): Promise<number> {
|
||||
const current = await ctx.storage?.get<number>(key);
|
||||
const next = typeof current === "number" && Number.isFinite(current) ? current + 1 : 1;
|
||||
await ctx.storage?.set(key, next);
|
||||
return next;
|
||||
}
|
||||
Reference in New Issue
Block a user