feat(insight): Refactor code structure for improved readability and maintainability

This commit is contained in:
DragonnZhang 2026-02-09 19:00:57 +08:00
parent 2edce464ae
commit e66c203cb0
18 changed files with 423 additions and 229 deletions

View file

@ -0,0 +1,63 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable no-undef */
import { writeFile, readFile } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
import { build } from 'vite';
const assetsDir = dirname(fileURLToPath(import.meta.url));
const distDir = join(assetsDir, 'dist');
const templateModulePath = join(
assetsDir,
'..',
'..',
'src',
'services',
'insight',
'templates',
'insightTemplate.ts',
);
console.log('Building insight assets with Vite...');
await build();
console.log('Reading generated files...');
let jsContent = '';
let cssContent = '';
try {
jsContent = await readFile(join(distDir, 'main.js'), 'utf-8');
} catch (e) {
console.error('Failed to read main.js from dist');
throw e;
}
try {
// Try style.css first (standard Vite lib mode output)
cssContent = await readFile(join(distDir, 'style.css'), 'utf-8');
} catch (e) {
try {
// Try main.css (if configured via assetFileNames)
cssContent = await readFile(join(distDir, 'main.css'), 'utf-8');
} catch (e2) {
console.warn(
'No CSS file found in dist (style.css or main.css). Using empty string.',
);
}
}
const templateModule = `/**
* @license
* Copyright 2025 Qwen Team
* SPDX-License-Identifier: Apache-2.0
*
* This file is code-generated; do not edit manually.
*/
export const INSIGHT_JS = ${JSON.stringify(jsContent.trim())};
export const INSIGHT_CSS = ${JSON.stringify(cssContent.trim())};
`;
await writeFile(templateModulePath, templateModule);
console.log(`Successfully generated ${templateModulePath}`);