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:
1 parent
1990761ad6
commit
fa80441e36
42 files changed
+628
-580
No files matched your search
@@ -1,78 +1,78 @@
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
|
||||
export function autoIncludeTSPlugin(options) {
|
||||
// options: { entries: [{ entry: 'napcat.ts', dir: './utils' }, ...] }
|
||||
const { entries } = options;
|
||||
let tsFilesMap = {};
|
||||
export function autoIncludeTSPlugin (options) {
|
||||
// options: { entries: [{ entry: 'napcat.ts', dir: './utils' }, ...] }
|
||||
const { entries } = options;
|
||||
let tsFilesMap = {};
|
||||
|
||||
return {
|
||||
name: 'vite-auto-include',
|
||||
return {
|
||||
name: 'vite-auto-include',
|
||||
|
||||
async buildStart() {
|
||||
tsFilesMap = {};
|
||||
for (const { entry, dir } of entries) {
|
||||
if (!tsFilesMap[entry]) {
|
||||
tsFilesMap[entry] = [];
|
||||
}
|
||||
const fullDir = path.resolve(dir);
|
||||
const allTsFiles = await findTSFiles(fullDir);
|
||||
async buildStart () {
|
||||
tsFilesMap = {};
|
||||
for (const { entry, dir } of entries) {
|
||||
if (!tsFilesMap[entry]) {
|
||||
tsFilesMap[entry] = [];
|
||||
}
|
||||
const fullDir = path.resolve(dir);
|
||||
const allTsFiles = await findTSFiles(fullDir);
|
||||
|
||||
const validFiles = [];
|
||||
allTsFiles.forEach((filePath) => {
|
||||
try {
|
||||
const source = fs.readFileSync(filePath, 'utf-8');
|
||||
if (source && source.trim() !== '') {
|
||||
validFiles.push(filePath);
|
||||
} else {
|
||||
// Skipping empty file: ${filePath}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Error reading file: ${filePath}`, error);
|
||||
}
|
||||
});
|
||||
|
||||
tsFilesMap[entry].push(...validFiles);
|
||||
const validFiles = [];
|
||||
allTsFiles.forEach((filePath) => {
|
||||
try {
|
||||
const source = fs.readFileSync(filePath, 'utf-8');
|
||||
if (source && source.trim() !== '') {
|
||||
validFiles.push(filePath);
|
||||
} else {
|
||||
// Skipping empty file: ${filePath}
|
||||
}
|
||||
},
|
||||
} catch (error) {
|
||||
console.error(`Error reading file: ${filePath}`, error);
|
||||
}
|
||||
});
|
||||
|
||||
transform(code, id) {
|
||||
for (const [entry, tsFiles] of Object.entries(tsFilesMap)) {
|
||||
const isMatch = id.endsWith(entry) || id.includes(entry);
|
||||
if (isMatch && tsFiles.length > 0) {
|
||||
const imports = tsFiles.map(filePath => {
|
||||
const relativePath = path.relative(path.dirname(id), filePath).replace(/\\/g, '/');
|
||||
return `import './${relativePath}';`;
|
||||
}).join('\n');
|
||||
tsFilesMap[entry].push(...validFiles);
|
||||
}
|
||||
},
|
||||
|
||||
const transformedCode = imports + '\n' + code;
|
||||
transform (code, id) {
|
||||
for (const [entry, tsFiles] of Object.entries(tsFilesMap)) {
|
||||
const isMatch = id.endsWith(entry) || id.includes(entry);
|
||||
if (isMatch && tsFiles.length > 0) {
|
||||
const imports = tsFiles.map(filePath => {
|
||||
const relativePath = path.relative(path.dirname(id), filePath).replace(/\\/g, '/');
|
||||
return `import './${relativePath}';`;
|
||||
}).join('\n');
|
||||
|
||||
return {
|
||||
code: transformedCode,
|
||||
map: { mappings: '' } // 空映射即可,VSCode 可以在 TS 上断点
|
||||
};
|
||||
}
|
||||
}
|
||||
const transformedCode = imports + '\n' + code;
|
||||
|
||||
return null;
|
||||
},
|
||||
return {
|
||||
code: transformedCode,
|
||||
map: { mappings: '' }, // 空映射即可,VSCode 可以在 TS 上断点
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
return null;
|
||||
},
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
// 辅助函数:查找所有 .ts 文件
|
||||
async function findTSFiles(dir) {
|
||||
const files = [];
|
||||
const items = await fs.promises.readdir(dir, { withFileTypes: true });
|
||||
async function findTSFiles (dir) {
|
||||
const files = [];
|
||||
const items = await fs.promises.readdir(dir, { withFileTypes: true });
|
||||
|
||||
for (let item of items) {
|
||||
const fullPath = path.join(dir, item.name);
|
||||
if (item.isDirectory()) {
|
||||
files.push(...await findTSFiles(fullPath)); // 递归查找子目录
|
||||
} else if (item.isFile() && fullPath.endsWith('.ts')) {
|
||||
files.push(fullPath); // 收集 .ts 文件
|
||||
}
|
||||
for (const item of items) {
|
||||
const fullPath = path.join(dir, item.name);
|
||||
if (item.isDirectory()) {
|
||||
files.push(...await findTSFiles(fullPath)); // 递归查找子目录
|
||||
} else if (item.isFile() && fullPath.endsWith('.ts')) {
|
||||
files.push(fullPath); // 收集 .ts 文件
|
||||
}
|
||||
}
|
||||
|
||||
return files;
|
||||
return files;
|
||||
}
|
||||
@@ -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 {}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user