#!/usr/bin/env node /** * 把本工程构建出的插件**交付给插件仓库**。 * * ## 它替你做了哪四件事 * * 1. 读 `plugin.config.json` —— 元数据的唯一真源; * 2. 构建产物(除非 `--no-build`); * 3. 把产物放进出仓库的构建目录(`/plugins/dist/`); * 4. 把元数据条目**合并**进 `/repo.config.json`(按 id 覆盖,不动别人); * 5. 跑一次仓库的发布脚本,归档 + 重写索引。 * * 可选再加一步:`--host <宿主路径>` 让宿主立刻同步仓库(不然宿主要等下一次构建)。 * * ## 为什么第 4 步是「合并」而不是「覆盖整个文件」 * * `repo.config.json` 是**整个仓库**的发布清单,里面有别人的插件。用本工程的 * 单条元数据去覆盖它,等于把仓库里其它插件全部删掉 —— 这是最容易造成 * 「发布一个插件、下架全部插件」这种事故的地方,所以这里按 id 精确定位。 * * ## 用法 * * npm run build && node scripts/deploy.mjs --repo ../funjia-plugin-repository * node scripts/deploy.mjs --repo ../funjia-plugin-repository --host "../插件化管理系统" * node scripts/deploy.mjs --repo <路径> --no-build # 用 dist/ 里已有的产物 * * 仓库路径也可以走环境变量:`PLUGIN_REPO=/path/to/repo npm run deploy` */ import { spawnSync } from "node:child_process"; import { copyFileSync, existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs"; import { dirname, join, resolve } from "node:path"; import { fileURLToPath } from "node:url"; const root = resolve(dirname(fileURLToPath(import.meta.url)), ".."); const distDir = join(root, "dist"); const args = process.argv.slice(2); const argValue = (name) => { const i = args.indexOf(name); return i >= 0 && args[i + 1] ? args[i + 1] : null; }; const repoPath = argValue("--repo") || process.env.PLUGIN_REPO; const hostPath = argValue("--host"); const skipBuild = args.includes("--no-build"); if (!repoPath) { console.error("[deploy] missing --repo "); console.error("[deploy] example: node scripts/deploy.mjs --repo ../funjia-plugin-repository"); process.exit(1); } const repoRoot = resolve(root, repoPath); const configFile = join(repoRoot, "repo.config.json"); const repoSrcDir = join(repoRoot, "plugins", "dist"); const publishScript = join(repoRoot, "scripts", "publish-repo.mjs"); if (!existsSync(configFile) || !existsSync(publishScript)) { console.error(`[deploy] ${repoRoot} does not look like a plugin repository`); console.error("[deploy] expected repo.config.json and scripts/publish-repo.mjs"); process.exit(1); } /* ------------------------------------------------------------------ 1. 读元数据 */ const meta = JSON.parse(readFileSync(join(root, "plugin.config.json"), "utf8")); if (!meta.id || !meta.artifact) { console.error("[deploy] plugin.config.json needs at least id and artifact"); process.exit(1); } console.log(`[deploy] plugin: ${meta.id}@${meta.version} (${meta.kind})`); /* ------------------------------------------------------------------ 2. 构建 */ if (skipBuild) { console.log("[deploy] --no-build: using existing dist/"); } else { const viteBin = join(root, "node_modules", "vite", "bin", "vite.js"); if (!existsSync(viteBin)) { console.error(`[deploy] vite not found at ${viteBin} - run npm install first`); process.exit(1); } console.log("[deploy] building…"); const build = spawnSync(process.execPath, [viteBin, "build"], { cwd: root, stdio: "inherit" }); if (build.status !== 0) { console.error(`[deploy] build failed (exit ${build.status})`); process.exit(1); } } /* ------------------------------------------------------------------ 3. 交付产物 */ /** 覆盖式复制(不用 copyFileSync:目标已存在时本机 shim 会拦 unlink) */ function put(from, to) { mkdirSync(dirname(to), { recursive: true }); writeFileSync(to, readFileSync(from)); } const files = [meta.artifact, meta.style].filter(Boolean); mkdirSync(repoSrcDir, { recursive: true }); for (const file of files) { const src = join(distDir, file); if (!existsSync(src)) { console.error(`[deploy] built artifact missing: dist/${file}`); console.error("[deploy] check plugin.config.json 的 artifact / style 是否与产物名一致"); process.exit(1); } put(src, join(repoSrcDir, file)); console.log(`[deploy] artifact -> plugins/dist/${file} (${(statSync(src).size / 1024).toFixed(1)} KB)`); } /* ------------------------------------------------------------------ 4. 合并仓库清单 */ const repoConfig = JSON.parse(readFileSync(configFile, "utf8")); if (!Array.isArray(repoConfig.plugins)) repoConfig.plugins = []; // 只把「发布清单认识的字段」写进去:checksum / size / entry / publishedAt // 由仓库的发布脚本计算,手工写进去只会过期。 const entry = { id: meta.id, name: meta.name, version: meta.version, description: meta.description, author: meta.author, kind: meta.kind, artifact: meta.artifact, style: meta.style, globalKey: meta.globalKey, requires: meta.requires, permissions: meta.permissions, contributes: meta.contributes, activation: meta.activation, tags: meta.tags, homepage: meta.homepage, }; const clean = Object.fromEntries(Object.entries(entry).filter(([, v]) => v !== undefined)); const slot = repoConfig.plugins.findIndex((p) => p.id === meta.id); if (slot >= 0) { const before = repoConfig.plugins[slot]; repoConfig.plugins[slot] = clean; console.log( `[deploy] repo.config.json: updated ${meta.id} (${before.version} -> ${clean.version})` ); } else { repoConfig.plugins.push(clean); console.log(`[deploy] repo.config.json: added ${meta.id} (${clean.version})`); } writeFileSync(configFile, JSON.stringify(repoConfig, null, 2) + "\n", "utf8"); /* ------------------------------------------------------------------ 5. 发布 */ console.log("[deploy] publishing into the repository…"); const publish = spawnSync(process.execPath, [publishScript], { cwd: repoRoot, stdio: "inherit" }); if (publish.status !== 0) { console.error(`[deploy] publish failed (exit ${publish.status})`); process.exit(1); } /* ------------------------------------------------------------------ 6. 可选:同步到宿主 */ if (hostPath) { const hostRoot = resolve(root, hostPath); const syncScript = join(hostRoot, "scripts", "sync-repo.mjs"); if (!existsSync(syncScript)) { console.warn(`[deploy] --host given but no scripts/sync-repo.mjs in ${hostRoot} - skipped`); } else { console.log("[deploy] syncing repository into the host…"); const sync = spawnSync(process.execPath, [syncScript, "--from", join(repoRoot, "repository")], { cwd: hostRoot, stdio: "inherit", }); if (sync.status !== 0) { console.warn(`[deploy] host sync failed (exit ${sync.status}) - run it manually`); } } } console.log(""); console.log(`[deploy] done: ${meta.id}@${meta.version} is in the repository.`); console.log("[deploy] next:"); console.log("[deploy] - 仓库服务已开着的话,刷新管理台就能看到它"); console.log(`[deploy] - 宿主:cd <宿主> && npm run sync:repo (或 npm run build:all)`);