Files
print-tag-system/plugin/scripts/deploy.mjs
T
funjia ef41d98838 feat: 新增 plugin/ 插件化形态(funjia.print-tag)+ 可选 SN 条码/二维码
把本工程的标签排版能力改造成 FunjiaUI 远端插件,放进 plugin/ 子目录,
与原独立 Web 版并存(两者功能一致,模板/版面/打印行为逐项对齐)。

- 入口从 createApp().mount() 改为 activate(ctx) + ctx.addRoute,注册 1 菜单 / 1 路由 / 2 命令
- 11 个模板的渲染收敛成 SpecTag,差异集中到 templates/registry.ts(字段/选项/默认值原样保留)
- 店铺名 / 电话从写死改为可配置,记忆在插件私有存储
- 内置二维码图片改为打包 data URL —— 远端插件没有自己的 public/
- 去掉 @headlessui/vue(换原生 select)与 Tailwind,样式类名统一 pt- 前缀避免污染宿主
- 可选接入宿主 api::sn-code:按 SN 生成真 Code 128 条码与可扫二维码;
  关闭时(默认)完全保持原静态二维码行为。领码优先贴到已排版的格子,不够才新建
- 新增 commands: print-tag:issue-sn / print-tag:render-svg(后者纯本地出图,供其它插件复用)
- code128.ts 实现真 Code 128 Set B(含校验位);二维码走 qrcode-generator(真 QR)
- 自带本地预览器(preview/mock-api.ts 补了 /api/sn-codes mock)与 deploy / verify 脚本
- verify.mjs 修掉一处误报:源码边界判据改为「解析后是否逃出工程根」,
  而不是「说明符是否以 .. 开头」(后者会把 src/components/X.vue → ../codes 误判为越界)

已发布到插件仓库:funjia.print-tag@1.0.0
2026-10-03 23:48:32 +08:00

188 lines
7.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env node
/**
* 把本工程构建出的插件**交付给插件仓库**。
*
* ## 它替你做了哪四件事
*
* 1. 读 `plugin.config.json` —— 元数据的唯一真源;
* 2. 构建产物(除非 `--no-build`);
* 3. 把产物放进出仓库的构建目录(`<repo>/plugins/dist/`);
* 4. 把元数据条目**合并**进 `<repo>/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 <path to funjia-plugin-repository>");
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)`);