Skip to content

Commit 2f5127f

Browse files
committed
refactor: 调整项目模块结构
1 parent 5a159eb commit 2f5127f

25 files changed

+580
-567
lines changed

src/base.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import * as vscode from 'vscode';
2+
3+
class Module {
4+
protected dependencies?: Module[];
5+
6+
activate(context: vscode.ExtensionContext): void {
7+
if (this.dependencies) {
8+
this.dependencies.forEach(module => {
9+
module.activate && module.activate(context);
10+
});
11+
}
12+
}
13+
14+
deactivate(): void {
15+
if (this.dependencies) {
16+
this.dependencies.forEach(module => {
17+
module.deactivate && module.deactivate();
18+
});
19+
}
20+
}
21+
}
22+
23+
export default Module;

src/commands/base.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import * as vscode from 'vscode';
2+
import Module from '../base';
3+
import { saveMiniprogramProject } from './compile/utils';
4+
5+
class Command extends Module {
6+
register(command: string, callback: (e: any) => void): void {
7+
vscode.commands.registerCommand(command, async e => {
8+
try {
9+
await callback(e);
10+
} catch (error: any) {
11+
if (error.message === '未找到 project.config.json 文件') {
12+
const result = await vscode.window.showErrorMessage('未找到 project.config.json 文件,请手动选择', '选择文件');
13+
if (result === '选择文件') {
14+
saveMiniprogramProject();
15+
}
16+
} else {
17+
vscode.window.showErrorMessage(error.message);
18+
}
19+
}
20+
});
21+
}
22+
}
23+
24+
export default Command;

src/commands/compile/analyse.ts

+53-50
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,65 @@
11
import * as vscode from 'vscode';
22
import * as path from 'path';
33
import * as fs from 'fs';
4+
import Command from '../base';
45
import { getCurrentFolderPath, getAnalyseViewerPath } from '../../utils/path';
56
import { readProjectConfig, createProject } from '../../utils/project';
67
import { openWebView, openDocument } from '../../utils/ui';
7-
import { registerCommand } from './utils';
88
import { WebviewMessage } from '../../types';
99

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);
4455
}
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+
});
5860
});
59-
});
61+
}
6062
}
6163

62-
export default analyseCode;
64+
65+
export default new AnalyseCodeCommand();

src/commands/compile/artifact.ts

+29-26
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,37 @@ import * as vscode from 'vscode';
22
import * as os from 'os';
33
import * as path from 'path';
44
import open from 'open';
5+
import Command from '../base';
56
import { readProjectConfig, createProject } from '../../utils/project';
6-
import { getCompileOptions, getTemporaryFileName, registerCommand } from './utils';
7+
import { getCompileOptions, getTemporaryFileName } from './utils';
78

8-
function artifact(context: vscode.ExtensionContext): void {
9-
registerCommand('MiniProgram.commands.compile.artifact', async () => {
10-
const projectConfig = readProjectConfig();
11-
const options = await createProject(context);
12-
const artifactZipPath = path.join(os.tmpdir(), getTemporaryFileName('artifact', options.appid, 'zip'));
13-
await vscode.window.withProgress({
14-
title: '正在编译小程序',
15-
location: vscode.ProgressLocation.Notification,
16-
cancellable: true,
17-
}, async progress => {
18-
const ci = await import('miniprogram-ci');
19-
const project = new ci.Project(options);
20-
21-
await ci.getCompiledResult({
22-
project,
23-
version: '1.0.0',
24-
setting: getCompileOptions(projectConfig.setting),
25-
onProgressUpdate(message): void {
26-
progress.report(typeof message === 'string' ? { message } : message);
27-
},
28-
}, artifactZipPath);
29-
30-
open(artifactZipPath);
9+
class ArtifactCommand extends Command {
10+
activate(context: vscode.ExtensionContext): void {
11+
this.register('MiniProgram.commands.compile.artifact', async () => {
12+
const projectConfig = readProjectConfig();
13+
const options = await createProject(context);
14+
const artifactZipPath = path.join(os.tmpdir(), getTemporaryFileName('artifact', options.appid, 'zip'));
15+
await vscode.window.withProgress({
16+
title: '正在编译小程序',
17+
location: vscode.ProgressLocation.Notification,
18+
cancellable: true,
19+
}, async progress => {
20+
const ci = await import('miniprogram-ci');
21+
const project = new ci.Project(options);
22+
23+
await ci.getCompiledResult({
24+
project,
25+
version: '1.0.0',
26+
setting: getCompileOptions(projectConfig.setting),
27+
onProgressUpdate(message): void {
28+
progress.report(typeof message === 'string' ? { message } : message);
29+
},
30+
}, artifactZipPath);
31+
32+
open(artifactZipPath);
33+
});
3134
});
32-
});
35+
}
3336
}
3437

35-
export default artifact;
38+
export default new ArtifactCommand();

src/commands/compile/directory.ts

+16-14
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
import * as vscode from 'vscode';
22
import * as path from 'path';
33
import * as fs from 'fs';
4+
import Command from '../base';
45
import { updateJSON } from '../../utils/json';
56
import { getCurrentFolderPath, getProjectConfigPath } from '../../utils/path';
6-
import { registerCommand } from './utils';
77

8-
function compileDirectory(): void {
9-
registerCommand('MiniProgram.commands.config.compileDir', async (e: vscode.Uri) => {
10-
const rootPath = getCurrentFolderPath();
11-
const projectFilePath = getProjectConfigPath(rootPath);
12-
13-
if (!fs.existsSync(projectFilePath)) {
14-
throw new Error('未找到 project.config.json 文件');
15-
}
16-
17-
await updateJSON(projectFilePath, 'miniprogramRoot', path.relative(rootPath, e.fsPath));
18-
vscode.window.showInformationMessage('设置成功,当预览或上传小程序时,将仅打包此目录下的文件');
19-
});
8+
class CompileDirectoryCommand extends Command {
9+
activate(): void {
10+
this.register('MiniProgram.commands.config.compileDir', async (e: vscode.Uri) => {
11+
const rootPath = getCurrentFolderPath();
12+
const projectFilePath = getProjectConfigPath(rootPath);
13+
14+
if (!fs.existsSync(projectFilePath)) {
15+
throw new Error('未找到 project.config.json 文件');
16+
}
17+
18+
await updateJSON(projectFilePath, 'miniprogramRoot', path.relative(rootPath, e.fsPath));
19+
vscode.window.showInformationMessage('设置成功,当预览或上传小程序时,将仅打包此目录下的文件');
20+
});
21+
}
2022
}
2123

22-
export default compileDirectory;
24+
export default new CompileDirectoryCommand();

src/commands/compile/index.ts

+19-26
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,22 @@
1-
import * as vscode from 'vscode';
2-
import analyse from './analyse';
3-
import directory from './directory';
4-
import npm from './npm';
5-
import preview from './preview';
6-
import upload from './upload';
7-
import artifact from './artifact';
8-
import sourcemap from './sourcemap';
1+
import Command from '../base';
2+
import AnalyseCodeCommand from './analyse';
3+
import CompileDirectoryCommand from './directory';
4+
import NPMCommand from './npm';
5+
import PreviewCommand from './preview';
6+
import UploadCommand from './upload';
7+
import ArtifactCommand from './artifact';
8+
import SourceMapCommand from './sourcemap';
99

10-
const comipleCommands = [
11-
analyse,
12-
directory,
13-
npm,
14-
preview,
15-
upload,
16-
artifact,
17-
sourcemap
18-
];
19-
20-
function activate(context: vscode.ExtensionContext): void {
21-
comipleCommands.forEach(command => command(context));
10+
class CompileCommand extends Command {
11+
dependencies = [
12+
AnalyseCodeCommand,
13+
CompileDirectoryCommand,
14+
NPMCommand,
15+
PreviewCommand,
16+
UploadCommand,
17+
ArtifactCommand,
18+
SourceMapCommand
19+
];
2220
}
2321

24-
function deactivate(): void {}
25-
26-
export {
27-
activate,
28-
deactivate,
29-
};
22+
export default new CompileCommand();

0 commit comments

Comments
 (0)