From d3186240fa6a2a0f2b21df49254fad8cb3fd8d5a Mon Sep 17 00:00:00 2001 From: QCQCQC <1220204124@zust.edu.cn> Date: Sun, 10 Nov 2024 12:39:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8oakLogger=E6=90=AD=E9=85=8Des?= =?UTF-8?q?build=E6=8F=92=E4=BB=B6=EF=BC=8C=E6=9B=BF=E4=BB=A3=E4=BA=86?= =?UTF-8?q?=E4=B9=8B=E5=89=8D=E7=9A=84=E6=97=A5=E5=BF=97=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- esbuild.js | 48 ++++++++++++++++++++++++++++++++++++++++++ src/extension.ts | 2 +- src/utils/logger.ts | 28 +++++++++++++++--------- src/utils/oakConfig.ts | 1 - types.d.ts | 10 +++++++++ 5 files changed, 77 insertions(+), 12 deletions(-) create mode 100644 types.d.ts diff --git a/esbuild.js b/esbuild.js index a22f66a..f815632 100644 --- a/esbuild.js +++ b/esbuild.js @@ -69,6 +69,49 @@ const esbuildProblemMatcherPlugin = { }, }; +/** + * @type {import('esbuild').Plugin} + */ +const loggerPlugin = { + name: 'logger-plugin', + setup(build) { + build.onLoad({ filter: /\.ts$/ }, async (args) => { + if ( + ['logger.ts', 'analyzeWorker.ts'].includes( + path.basename(args.path) + ) + ) { + return; + } + + const contents = await fs.readFile(args.path, 'utf8'); + const transformed = contents.replace( + /(console\.)(log|error|warn)\(/g, + (match, p1, p2) => { + const method = + p2 === 'log' + ? 'log' + : p2 === 'error' + ? 'error' + : 'warn'; + + console.log( + `transform: ${path.basename( + args.path + )} ${match} to oakLogger.${method}(` + ); + + return `oakLogger.${method}(`; + } + ); + + // 添加import oakLogger from './oakLogger'; + const contentsAdd = `import oakLogger from '@/utils/logger';\n${transformed}`; + return { contents: contentsAdd, loader: 'ts' }; + }); + }, +}; + async function main() { const ctx = await esbuild.context({ entryPoints: [ @@ -94,7 +137,12 @@ async function main() { /* add to the end of plugins array */ esbuildProblemMatcherPlugin, copyTemplatesPlugin, + loggerPlugin, ], + // 路径别名 + alias: { + '@': path.resolve(__dirname, 'src'), + }, }); if (watch) { await ctx.watch(); diff --git a/src/extension.ts b/src/extension.ts index b576f39..82d75b9 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -280,7 +280,7 @@ export async function activate(context: vscode.ExtensionContext) { return; } const projectHome = join(uri.fsPath, '..', config.projectDir); - log('projectHome:', projectHome); + log('log', 'projectHome:', projectHome); // 设置projectHome setProjectHome(projectHome); // 通知已经启用 diff --git a/src/utils/logger.ts b/src/utils/logger.ts index 18faef8..5165730 100644 --- a/src/utils/logger.ts +++ b/src/utils/logger.ts @@ -1,9 +1,10 @@ import * as vscode from 'vscode'; + // 通过createOutputChannel创建一个输出频道 export const logger = vscode.window.createOutputChannel('oak-assistant'); // 输出日志 -export const log = (...message: any[]) => { +export const log = (level: 'log' | 'warn' | 'error' = 'log', ...message: any[]) => { // 将日志信息输出到频道。需要判断类型 const msg: string[] = message.map((msg) => { if (typeof msg === 'object') { @@ -14,27 +15,34 @@ export const log = (...message: any[]) => { return msg; } }); - logger.appendLine(msg.join(' ')); + const prefix = { + // log: '\x1b[32m[INFO] \x1b[0m', // 绿色 + // warn: '\x1b[33m[WARN] \x1b[0m', // 黄色 + // error: '\x1b[31m[ERROR] \x1b[0m' // 红色 + log: '[INFO] ', + warn: '[WARN] ', + error: '[ERROR] ' + }[level]; + logger.appendLine(`${prefix}${msg.join(' ')}`); }; -// polyfill全局的console对象,重定向到输出频道 -const console = global.console; -global.console = { - ...console, +const oakLogger = { log: (...args: any[]) => { console.log(...args); - log(...args); + log('log', ...args); }, error: (...args: any[]) => { console.error(...args); - log(...args); + log('error', ...args); }, warn: (...args: any[]) => { console.warn(...args); - log(...args); + log('warn', ...args); }, info: (...args: any[]) => { console.info(...args); - log(...args); + log('log', ...args); }, }; + +export default oakLogger; \ No newline at end of file diff --git a/src/utils/oakConfig.ts b/src/utils/oakConfig.ts index 0dfa19e..ad5cad4 100644 --- a/src/utils/oakConfig.ts +++ b/src/utils/oakConfig.ts @@ -79,7 +79,6 @@ export const loadConfig = () => { const content = fs.readFileSync(path, 'utf-8'); const config = JSON.parse(content); cachedConfig = deepMergeObject(defaultConfig, config); - console.log('load config:', cachedConfig); }; diff --git a/types.d.ts b/types.d.ts new file mode 100644 index 0000000..fb63bfc --- /dev/null +++ b/types.d.ts @@ -0,0 +1,10 @@ +export {}; // This ensures the file is treated as a module + +declare global { + interface oakLogger { + log: (...args: any[]) => void; + error: (...args: any[]) => void; + warn: (...args: any[]) => void; + info: (...args: any[]) => void; + } +} \ No newline at end of file