feat: 自动化version打包

This commit is contained in:
手瓜一十雪
2025-11-13 18:31:55 +08:00
parent 7b65fa581a
commit 5622f5a04a
9 changed files with 439 additions and 2 deletions

9
packages/napcat-common/src/env.d.ts vendored Normal file
View File

@@ -0,0 +1,9 @@
/// <reference types="vite/client" />
declare global {
interface ImportMetaEnv {
readonly VITE_NAPCAT_VERSION: string;
}
}
export {};

View File

@@ -1 +1 @@
export const napCatVersion = '4.9.42';
export const napCatVersion = import.meta.env.VITE_NAPCAT_VERSION || 'alpha';

View File

@@ -23,7 +23,8 @@
"napcat-qrcode": "workspace:*"
},
"devDependencies": {
"@types/node": "^22.0.1"
"@types/node": "^22.0.1",
"napcat-vite": "workspace:*"
},
"engines": {
"node": ">=18.0.0"

View File

@@ -3,6 +3,8 @@ import { defineConfig, PluginOption, UserConfig } from 'vite';
import path, { resolve } from 'path';
import nodeResolve from '@rollup/plugin-node-resolve';
import { builtinModules } from 'module';
import napcatVersion from "napcat-vite/vite-plugin-version.js";
//依赖排除
const external = [
'silk-wasm',
@@ -21,6 +23,7 @@ const ShellBaseConfigPlugin: PluginOption[] = [
],
}),
nodeResolve(),
napcatVersion(),
];
const ShellBaseConfig = () =>
defineConfig({

View File

@@ -0,0 +1,31 @@
{
"name": "napcat-vite",
"version": "0.0.1",
"private": true,
"type": "module",
"main": "index.ts",
"scripts": {
"build": "vite build"
},
"exports": {
".": {
"import": "./index.ts"
},
"./*": {
"import": "./*"
}
},
"dependencies": {
"napcat-core": "workspace:*",
"napcat-common": "workspace:*",
"napcat-onebot": "workspace:*",
"napcat-webui-backend": "workspace:*",
"napcat-qrcode": "workspace:*"
},
"devDependencies": {
"@types/node": "^22.0.1"
},
"engines": {
"node": ">=18.0.0"
}
}

View File

@@ -0,0 +1,49 @@
{
"compilerOptions": {
"target": "ES2021",
"module": "ESNext",
"moduleResolution": "Node",
"lib": [
"ES2021"
],
"typeRoots": [
"./node_modules/@types"
],
"esModuleInterop": true,
"outDir": "dist",
"rootDir": "./",
"noEmit": false,
"sourceMap": true,
"strict": true,
"noImplicitAny": false,
"strictFunctionTypes": true,
"strictBindCallApply": true,
"alwaysStrict": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"noPropertyAccessFromIndexSignature": true,
"noUncheckedIndexedAccess": true,
"exactOptionalPropertyTypes": false,
"forceConsistentCasingInFileNames": true,
"useUnknownInCatchVariables": true,
"noImplicitOverride": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"useDefineForClassFields": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"resolveJsonModule": true,
"baseUrl": "./",
"skipLibCheck": true,
"skipDefaultLibCheck": true,
"allowJs": true
},
"include": [
"vite-plugin-version.js"
],
"exclude": [
"node_modules",
"dist"
]
}

View File

@@ -0,0 +1,117 @@
import fs from "fs";
import path from "path";
import https from "https";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
/**
* NapCat Vite Plugin: fetches latest GitHub tag (not release) and injects into import.meta.env
*/
export default function vitePluginNapcatVersion() {
const pluginDir = path.resolve(__dirname, "dist");
const cacheFile = path.join(pluginDir, ".napcat-version.json");
const owner = "NapNeko";
const repo = "NapCatQQ";
const maxAgeMs = 24 * 60 * 60 * 1000; // cache 1 day
const githubToken = process.env.GITHUB_TOKEN;
fs.mkdirSync(pluginDir, { recursive: true });
function readCache() {
try {
const stat = fs.statSync(cacheFile);
if (Date.now() - stat.mtimeMs < maxAgeMs) {
const data = JSON.parse(fs.readFileSync(cacheFile, "utf8"));
if (data?.tag) return data.tag;
}
} catch {}
return null;
}
function writeCache(tag) {
try {
fs.writeFileSync(
cacheFile,
JSON.stringify({ tag, time: new Date().toISOString() }, null, 2)
);
} catch {}
}
async function fetchLatestTag() {
const url = `https://api.github.com/repos/${owner}/${repo}/tags`;
return new Promise((resolve, reject) => {
const req = https.get(
url,
{
headers: {
"User-Agent": "vite-plugin-napcat-version",
Accept: "application/vnd.github.v3+json",
...(githubToken ? { Authorization: `token ${githubToken}` } : {}),
},
},
(res) => {
let data = "";
res.on("data", (c) => (data += c));
res.on("end", () => {
try {
const json = JSON.parse(data);
if (Array.isArray(json) && json[0]?.name) {
resolve(json[0].name);
} else reject(new Error("Invalid GitHub tag response"));
} catch (e) {
reject(e);
}
});
}
);
req.on("error", reject);
});
}
async function getVersion() {
const cached = readCache();
if (cached) return cached;
try {
const tag = await fetchLatestTag();
writeCache(tag);
return tag;
} catch (e) {
console.warn("[vite-plugin-napcat-version] Failed to fetch tag:", e.message);
return cached ?? "v0.0.0";
}
}
let lastTag = null;
return {
name: "vite-plugin-napcat-version",
enforce: "pre",
async config(userConfig) {
const tag = await getVersion();
console.log(`[vite-plugin-napcat-version] Using version: ${tag}`);
lastTag = tag;
return {
define: {
...(userConfig.define || {}),
"import.meta.env.VITE_NAPCAT_VERSION": JSON.stringify(tag),
},
};
},
handleHotUpdate(ctx) {
if (path.resolve(ctx.file) === cacheFile) {
try {
const json = JSON.parse(fs.readFileSync(cacheFile, "utf8"));
const tag = json?.tag;
if (tag && tag !== lastTag) {
lastTag = tag;
ctx.server?.ws.send({ type: "full-reload" });
}
} catch {}
}
},
};
}