Skip to content

Commit 67929aa

Browse files
committed
refactor: 使用 typescript 重构项目
1 parent 78bd7d1 commit 67929aa

40 files changed

+575
-346
lines changed

.vscodeignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.vscode
22
.github
33
build
4-
contributes.json
4+
contributes.json
5+
tsconfig.json

package-lock.json

+113
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -533,10 +533,14 @@
533533
"dependencies": {
534534
"ejs": "^3.1.7",
535535
"miniprogram-ci": "^1.8.12",
536-
"open": "^8.4.0"
536+
"open": "^8.4.0",
537+
"ts-node": "^10.7.0"
537538
},
538539
"devDependencies": {
540+
"@types/ejs": "^3.1.0",
541+
"@types/vscode": "^1.25.0",
539542
"glob": "^8.0.1",
540-
"lodash": "^4.17.21"
543+
"lodash": "^4.17.21",
544+
"typescript": "^4.6.3"
541545
}
542546
}

src/commands/compile/analyse.js src/commands/compile/analyse.ts

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
const vscode = require('vscode');
2-
const path = require('path');
3-
const fs = require('fs');
4-
const { getCurrentFolderPath } = require('../../utils/path');
5-
const { readProjectConfig, createProject } = require('../../utils/project');
6-
const { openWebView, openDocument } = require('../../utils/ui');
7-
const { registerCommand } = require('./utils');
1+
import * as vscode from 'vscode';
2+
import * as path from 'path';
3+
import * as fs from 'fs';
4+
import { getCurrentFolderPath } from '../../utils/path';
5+
import { readProjectConfig, createProject } from '../../utils/project';
6+
import { openWebView, openDocument } from '../../utils/ui';
7+
import { registerCommand } from './utils';
8+
import { WebviewMessage } from '../../types';
89

9-
function analyseCode(context) {
10+
function analyseCode(context: vscode.ExtensionContext): void {
1011
registerCommand('MiniProgram.commands.compile.analyse', async () => {
1112
const projectConfig = readProjectConfig();
1213

@@ -19,7 +20,7 @@ function analyseCode(context) {
1920
const panel = openWebView('', '代码依赖分析', vscode.ViewColumn.One);
2021
html = html.replace(/vscode:\/\//g, panel.webview.asWebviewUri(vscode.Uri.file(viewerPath)).toString() + '/');
2122
panel.webview.html = html;
22-
panel.webview.onDidReceiveMessage(async message => {
23+
panel.webview.onDidReceiveMessage(async (message: WebviewMessage) => {
2324
switch (message.command) {
2425
case 'syncState':
2526
panel.webview.postMessage({
@@ -63,4 +64,4 @@ function analyseCode(context) {
6364
});
6465
}
6566

66-
module.exports = analyseCode;
67+
export default analyseCode;

src/commands/compile/artifact.js src/commands/compile/artifact.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
const vscode = require('vscode');
2-
const os = require('os');
3-
const path = require('path');
4-
const open = require('open');
5-
const { readProjectConfig, createProject } = require('../../utils/project');
6-
const { getCompileOptions, getTemporaryFileName, registerCommand } = require('./utils');
1+
import * as vscode from 'vscode';
2+
import * as os from 'os';
3+
import * as path from 'path';
4+
import open from 'open';
5+
import { readProjectConfig, createProject } from '../../utils/project';
6+
import { getCompileOptions, getTemporaryFileName, registerCommand } from './utils';
77

8-
function artifact(context) {
8+
function artifact(context: vscode.ExtensionContext): void {
99
registerCommand('MiniProgram.commands.compile.artifact', async () => {
1010
const projectConfig = readProjectConfig();
1111

@@ -27,14 +27,14 @@ function artifact(context) {
2727
project,
2828
version: '1.0.0',
2929
setting: getCompileOptions(projectConfig.setting),
30-
onProgressUpdate(message) {
31-
progress.report(message);
30+
onProgressUpdate(message: string): void {
31+
progress.report(message as any);
3232
},
3333
}, artifactZipPath);
34-
34+
3535
open(artifactZipPath);
3636
});
3737
});
3838
}
3939

40-
module.exports = artifact;
40+
export default artifact;

src/commands/compile/directory.js

-22
This file was deleted.

src/commands/compile/directory.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import * as vscode from 'vscode';
2+
import * as path from 'path';
3+
import * as fs from 'fs';
4+
import { updateJSON } from '../../utils/json';
5+
import { getCurrentFolderPath, getProjectConfigPath } from '../../utils/path';
6+
import { registerCommand } from './utils';
7+
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+
});
20+
}
21+
22+
export default compileDirectory;

src/commands/compile/index.js

-20
This file was deleted.

src/commands/compile/index.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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';
9+
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));
22+
}
23+
24+
function deactivate(): void {}
25+
26+
export {
27+
activate,
28+
deactivate,
29+
};

0 commit comments

Comments
 (0)