AutoCommit/src/models/index.ts

99 lines
2.8 KiB
TypeScript
Raw Normal View History

2019-12-26 17:47:37 +08:00
/*
* Author : OBKoro1
* Date : 2019-12-25 17:08:18
2020-01-08 14:08:09 +08:00
* LastEditors : OBKoro1
2020-12-10 20:02:10 +08:00
* LastEditTime : 2020-12-10 16:39:26
* FilePath : \autoCommit\src\models\index.ts
2019-12-26 17:47:37 +08:00
* Description : 插件逻辑入口
* https://github.com/OBKoro1
*/
import * as vscode from 'vscode';
2019-12-30 21:13:08 +08:00
import * as fs from 'fs';
2020-01-02 15:20:07 +08:00
import { sep } from 'path';
2020-12-10 20:02:10 +08:00
import WebView from './WebView';
import { WebviewMsg } from '../util/dataStatement';
import { setPanelWebview, isProduction, outputLog } from '../util/vscodeUtil';
import CommitHandle from './commitHandle';
const hasGit = (itemSrc: string) => {
const url = `${itemSrc}${sep}.git`; // 文件路径
try {
const isDirectory = fs.statSync(url).isDirectory(); // 判断是否为文件夹 返回布尔值
if (isDirectory) {
return true;
}
return false;
} catch (err) {
return false;
}
};
2019-12-26 17:47:37 +08:00
class ExtensionLogic {
public readonly context: vscode.ExtensionContext;
2020-12-10 20:02:10 +08:00
public autoCommitView!: WebView ; // 告诉ts我已经对它初始化了
2019-12-30 21:13:08 +08:00
public CommitHandle: any;
2019-12-26 17:47:37 +08:00
public constructor(context: vscode.ExtensionContext) {
2020-12-10 20:02:10 +08:00
// 存储参数
2019-12-26 17:47:37 +08:00
this.context = context;
2020-12-10 20:02:10 +08:00
}
public init() {
2019-12-30 21:13:08 +08:00
this.autoCommitView = new WebView(
this.context,
2020-12-10 20:02:10 +08:00
this.messageCallBack.bind(this),
) as WebView;
2019-12-30 21:13:08 +08:00
setPanelWebview(this.autoCommitView);
this.createView();
2019-12-26 17:47:37 +08:00
}
2020-12-10 20:02:10 +08:00
2019-12-26 17:47:37 +08:00
createView() {
2019-12-30 21:13:08 +08:00
const option = {
type: 'autoCommit',
2020-01-10 17:02:16 +08:00
title: 'autoCommit',
2020-12-10 20:02:10 +08:00
fileName: 'autoCommit',
2019-12-30 21:13:08 +08:00
};
this.autoCommitView.create(option);
2019-12-31 18:01:35 +08:00
this.autoCommitView.postMessage('isProduction', isProduction());
2020-12-10 20:02:10 +08:00
const formData = this.context.globalState.get('commit-params');
this.autoCommitView.postMessage('init-formData', formData);
2019-12-26 17:47:37 +08:00
}
2020-12-10 20:02:10 +08:00
2019-12-26 17:47:37 +08:00
// 处理webview的消息
2020-12-10 20:02:10 +08:00
private messageCallBack(message: WebviewMsg) {
2019-12-30 21:13:08 +08:00
if (message.command === 'commit') {
this.CommitHandle = new CommitHandle(message);
} else if (message.command === 'choose-item') {
this.publishChooseFile();
2020-01-02 15:20:07 +08:00
} else if (message.command === 'cancel autoCommit') {
this.CommitHandle.closeCommit();
2019-12-30 21:13:08 +08:00
}
}
2020-12-10 20:02:10 +08:00
// 选择项目文件夹
2019-12-30 21:13:08 +08:00
async publishChooseFile() {
const urlArr: any = await vscode.window.showOpenDialog({
canSelectFiles: false, // 允许选择文件
canSelectFolders: true, // 是否可以选择文件夹
2020-12-10 20:02:10 +08:00
canSelectMany: false, // 是否可以选择多个文件
2019-12-30 21:13:08 +08:00
});
2019-12-31 18:01:35 +08:00
if (!urlArr) return; // 用户取消选择
2020-01-02 21:59:04 +08:00
let itemSrc = urlArr[0].path;
2020-12-10 20:02:10 +08:00
if (sep === '\\') {
2020-01-02 21:59:04 +08:00
// window 系統用不同的路径
2020-12-10 20:02:10 +08:00
itemSrc = urlArr[0].fsPath;
2020-01-02 21:59:04 +08:00
}
2020-12-10 20:02:10 +08:00
if (hasGit(itemSrc)) {
2019-12-30 21:13:08 +08:00
this.autoCommitView.postMessage('choose item success', itemSrc);
} else {
2019-12-31 18:01:35 +08:00
this.autoCommitView.postMessage('choose item error', itemSrc);
2019-12-30 21:13:08 +08:00
outputLog('项目地址错误', `${itemSrc}根目录没有.git文件夹`);
}
}
2019-12-26 17:47:37 +08:00
}
export default ExtensionLogic;