|
1 | 1 | import * as vscode from 'vscode';
|
2 | 2 | import * as path from 'path';
|
3 | 3 | import * as fs from 'fs';
|
| 4 | +import Command from '../base'; |
4 | 5 | import { getCurrentFolderPath, getAnalyseViewerPath } from '../../utils/path';
|
5 | 6 | import { readProjectConfig, createProject } from '../../utils/project';
|
6 | 7 | import { openWebView, openDocument } from '../../utils/ui';
|
7 |
| -import { registerCommand } from './utils'; |
8 | 8 | import { WebviewMessage } from '../../types';
|
9 | 9 |
|
10 |
| -function analyseCode(context: vscode.ExtensionContext): void { |
11 |
| - registerCommand('MiniProgram.commands.compile.analyse', async () => { |
12 |
| - readProjectConfig(); |
13 |
| - const viewerPath = getAnalyseViewerPath(); |
14 |
| - let html = await fs.promises.readFile(path.join(viewerPath, 'index.html'), { encoding: 'utf-8' }); |
15 |
| - const panel = openWebView('', '代码依赖分析', vscode.ViewColumn.One); |
16 |
| - html = html.replace(/vscode:\/\//g, panel.webview.asWebviewUri(vscode.Uri.file(viewerPath)).toString() + '/'); |
17 |
| - panel.webview.html = html; |
18 |
| - panel.webview.onDidReceiveMessage(async (message: WebviewMessage) => { |
19 |
| - switch (message.command) { |
20 |
| - case 'syncState': |
21 |
| - panel.webview.postMessage({ |
22 |
| - command: 'syncState', |
23 |
| - data: { |
24 |
| - analyseResult: null, |
25 |
| - currentModuleId: '', |
26 |
| - filterKeyword: '', |
27 |
| - filterType: 'all', |
28 |
| - navigatePath: '', |
29 |
| - sort: 'desc', |
30 |
| - }, |
31 |
| - }); |
32 |
| - |
33 |
| - break; |
34 |
| - case 'analyse': |
35 |
| - const options = await createProject(context); |
36 |
| - const ci = await import('miniprogram-ci'); |
37 |
| - const project = new ci.Project(options); |
38 |
| - const result = await ci.analyseCode(project); |
39 |
| - |
40 |
| - panel.webview.postMessage({ |
41 |
| - command: 'updateState', |
42 |
| - data: { |
43 |
| - analyseResult: result, |
| 10 | +class AnalyseCodeCommand extends Command { |
| 11 | + activate(context: vscode.ExtensionContext): void { |
| 12 | + this.register('MiniProgram.commands.compile.analyse', async () => { |
| 13 | + readProjectConfig(); |
| 14 | + const viewerPath = getAnalyseViewerPath(); |
| 15 | + let html = await fs.promises.readFile(path.join(viewerPath, 'index.html'), { encoding: 'utf-8' }); |
| 16 | + const panel = openWebView('', '代码依赖分析', vscode.ViewColumn.One); |
| 17 | + html = html.replace(/vscode:\/\//g, panel.webview.asWebviewUri(vscode.Uri.file(viewerPath)).toString() + '/'); |
| 18 | + panel.webview.html = html; |
| 19 | + panel.webview.onDidReceiveMessage(async (message: WebviewMessage) => { |
| 20 | + switch (message.command) { |
| 21 | + case 'syncState': |
| 22 | + panel.webview.postMessage({ |
| 23 | + command: 'syncState', |
| 24 | + data: { |
| 25 | + analyseResult: null, |
| 26 | + currentModuleId: '', |
| 27 | + filterKeyword: '', |
| 28 | + filterType: 'all', |
| 29 | + navigatePath: '', |
| 30 | + sort: 'desc', |
| 31 | + }, |
| 32 | + }); |
| 33 | + |
| 34 | + break; |
| 35 | + case 'analyse': |
| 36 | + const options = await createProject(context); |
| 37 | + const ci = await import('miniprogram-ci'); |
| 38 | + const project = new ci.Project(options); |
| 39 | + const result = await ci.analyseCode(project); |
| 40 | + |
| 41 | + panel.webview.postMessage({ |
| 42 | + command: 'updateState', |
| 43 | + data: { |
| 44 | + analyseResult: result, |
| 45 | + } |
| 46 | + }); |
| 47 | + |
| 48 | + break; |
| 49 | + case 'report': |
| 50 | + const rootPath = getCurrentFolderPath(); |
| 51 | + const filePath = message.data.ext.replace('topLevel/MainPackage', rootPath); |
| 52 | + |
| 53 | + if (message.data.action === 'clickTreemap' && fs.existsSync(filePath)) { |
| 54 | + openDocument(filePath); |
44 | 55 | }
|
45 |
| - }); |
46 |
| - |
47 |
| - break; |
48 |
| - case 'report': |
49 |
| - const rootPath = getCurrentFolderPath(); |
50 |
| - const filePath = message.data.ext.replace('topLevel/MainPackage', rootPath); |
51 |
| - |
52 |
| - if (message.data.action === 'clickTreemap' && fs.existsSync(filePath)) { |
53 |
| - openDocument(filePath); |
54 |
| - } |
55 |
| - |
56 |
| - break; |
57 |
| - } |
| 56 | + |
| 57 | + break; |
| 58 | + } |
| 59 | + }); |
58 | 60 | });
|
59 |
| - }); |
| 61 | + } |
60 | 62 | }
|
61 | 63 |
|
62 |
| -export default analyseCode; |
| 64 | + |
| 65 | +export default new AnalyseCodeCommand(); |
0 commit comments