Add ESLint config and update code style

Introduced a new eslint.config.js using neostandard and added related devDependencies. Updated codebase for consistent formatting, spacing, and function declarations. Minor refactoring and cleanup across multiple files to improve readability and maintain code style compliance.
This commit is contained in:
手瓜一十雪 committed 2025-11-15 16:21:59 +08:00
1 parent 1990761ad6
commit fa80441e36
42 files changed
+628 -580

No files matched your search

+30 -30
View File
@@ -1,7 +1,7 @@
import fs from "fs";
import path from "path";
import https from "https";
import { fileURLToPath } from "url";
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);
@@ -9,28 +9,28 @@ 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";
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() {
function readCache () {
try {
const stat = fs.statSync(cacheFile);
if (Date.now() - stat.mtimeMs < maxAgeMs) {
const data = JSON.parse(fs.readFileSync(cacheFile, "utf8"));
const data = JSON.parse(fs.readFileSync(cacheFile, 'utf8'));
if (data?.tag) return data.tag;
}
} catch {}
return null;
}
function writeCache(tag) {
function writeCache (tag) {
try {
fs.writeFileSync(
cacheFile,
@@ -39,38 +39,38 @@ export default function vitePluginNapcatVersion() {
} catch {}
}
async function fetchLatestTag() {
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",
'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", () => {
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"));
} else reject(new Error('Invalid GitHub tag response'));
} catch (e) {
reject(e);
}
});
}
);
req.on("error", reject);
req.on('error', reject);
});
}
async function getVersion() {
async function getVersion () {
const cached = readCache();
if (cached) return cached;
try {
@@ -78,37 +78,37 @@ export default function vitePluginNapcatVersion() {
writeCache(tag);
return tag;
} catch (e) {
console.warn("[vite-plugin-napcat-version] Failed to fetch tag:", e.message);
return cached ?? "v0.0.0";
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",
name: 'vite-plugin-napcat-version',
enforce: 'pre',
async config(userConfig) {
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),
'import.meta.env.VITE_NAPCAT_VERSION': JSON.stringify(tag),
},
};
},
handleHotUpdate(ctx) {
handleHotUpdate (ctx) {
if (path.resolve(ctx.file) === cacheFile) {
try {
const json = JSON.parse(fs.readFileSync(cacheFile, "utf8"));
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" });
ctx.server?.ws.send({ type: 'full-reload' });
}
} catch {}
}