Skip to content

Commit 6620e29

Browse files
committed
fix: 修复当项目不是git 项目的时候运行时的预期之外报错
1 parent 81d88b7 commit 6620e29

4 files changed

Lines changed: 100 additions & 10 deletions

File tree

src/Scheduler.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { exec } from 'child_process';
22
// your extension is activated the very first time the command is executed
33
let timer: NodeJS.Timeout;
44
import * as vscode from 'vscode';
5-
import { getNow } from './utils'
5+
import { getNow,throwErrorType } from './utils'
66
import {ReminderView} from './TextView'
77
export default class Scheduler {
88
$option: Option
@@ -21,6 +21,21 @@ export default class Scheduler {
2121
clearTimeout(timer)
2222
}, this.$option.commitTimeInterval)
2323
}
24+
private _getDiff(path:string){
25+
let cmd = `cd ${path}`;
26+
return new Promise((resolve) => {
27+
cmd = cmd + ' && git diff'
28+
let res = ''
29+
exec(cmd, (err, stdout) => {
30+
if (err) {
31+
res = err.message;
32+
} else {
33+
res = stdout
34+
}
35+
resolve(res);
36+
})
37+
})
38+
}
2439
private _getUnCommitChange(path: string) {
2540
let cmd = `cd ${path}`;
2641
return new Promise((resolve) => {
@@ -42,13 +57,14 @@ export default class Scheduler {
4257
}
4358
run() {
4459
if (this.$option.path) {
45-
const cmd = `cd ${this.$option.path} && git add . && git commit -n -m "自动提交" `;
60+
const cmd = `cd ${this.$option.path} && git add . && git commit -n -m "auto-commit" `;
4661
exec(cmd, (err, stdout) => {
4762
if (err) {
63+
const errorType = throwErrorType(err.message)
4864
ReminderView.show(this.$option.context,err.message)
4965
} else {
5066
this._getUnCommitChange(this.$option.path as string).then(res => {
51-
ReminderView.show(this.$option.context,`${getNow()} 自动提交成功 ${stdout} ${res}`,)
67+
ReminderView.show(this.$option.context,`${getNow()} auto-commit success ${stdout} ${res}`,)
5268
})
5369
}
5470
})

src/TextView.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ export class ReminderView {
2828
<meta charset="UTF-8">
2929
<meta name="viewport" content="width=device-width, initial-scale=1.0">
3030
<style>
31+
.block{
32+
33+
}
3134
.code{
3235
margin: 0;
3336
padding: 0;

src/extension.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,24 @@
33
import * as vscode from 'vscode';
44
// this method is called when your extension is activated
55
import Scheduler from './Scheduler'
6-
import { getConfig } from './utils'
6+
import { checkPathSafe, getConfig } from './utils'
77
let instance:Scheduler
88

9-
export function activate(context: vscode.ExtensionContext) {
9+
export async function activate(context: vscode.ExtensionContext) {
1010
let config:Option = {
1111
commitTimeInterval:getConfig<number>('commitTimeInterval')|| 0,
1212
autoPush:getConfig<boolean>('autoPush') || false,
1313
context
1414
}
15-
if(!vscode.workspace.workspaceFolders){
16-
vscode.window.showErrorMessage('当前目录不合法');
15+
let checkPathRes = await checkPathSafe(vscode.workspace.workspaceFolders)
16+
if(checkPathRes){
17+
vscode.window.showErrorMessage(checkPathRes);
1718
return
1819
}else{
19-
config.path = vscode.workspace.workspaceFolders[0].uri.fsPath
20+
config.path = vscode.workspace.workspaceFolders?.[0].uri.fsPath
2021
instance = new Scheduler({...config,context})
2122
vscode.window.showInformationMessage('git-auto-commit成功启用');
22-
vscode.workspace.onDidSaveTextDocument((e)=>{instance.changeListener(e)})
23+
vscode.workspace.onDidSaveTextDocument((e)=> instance.changeListener(e))
2324
}
2425
let disposable = vscode.commands.registerCommand('code-auto-commit.runCommit', () => {
2526

src/utils.ts

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import path = require('path');
12
import * as vscode from 'vscode';
2-
3+
const fs = require('fs');
4+
import { exec } from 'child_process';
5+
import { rejects } from 'assert';
36
export function getNow() {
47
let dateTime
58
let yy = new Date().getFullYear()
@@ -18,3 +21,70 @@ export function getNow() {
1821
export function getConfig<T = any>(key: string) {
1922
return vscode.workspace.getConfiguration().get<T>(`code-auto-commit.${key}`);
2023
}
24+
25+
const errorType: {
26+
[propName: string]: string;
27+
}
28+
= {
29+
notRepository: "fatal: not a git repository (or any of the parent directories): .git",
30+
}
31+
32+
export const checkIsRepository = async(path:string)=>{
33+
const cmd = `cd ${path} && git log`
34+
let res:boolean;
35+
try{
36+
const msg = await runCommand(cmd)
37+
res = true
38+
}catch(e){
39+
console.log(e)
40+
res = false
41+
}
42+
return res
43+
}
44+
45+
export const throwErrorType = function (errMsg: string) {
46+
for (let key in errorType) {
47+
if (errMsg.includes(errorType[key])) {
48+
return key
49+
}
50+
}
51+
return errMsg
52+
}
53+
export const checkPathSafe = async (path?:readonly vscode.WorkspaceFolder[])=>{
54+
if(!path){
55+
return '不是合法目录'
56+
}
57+
let isRepository = await checkIsRepository(path[0].uri.fsPath)
58+
if(!isRepository){
59+
return '不是一个git管理项目'
60+
}
61+
return null
62+
}
63+
export const runCommand = (cmd:string)=>{
64+
return new Promise((resolve,reject) => {
65+
cmd = cmd + ' && git diff'
66+
let res = ''
67+
exec(cmd, (err, stdout) => {
68+
if (err) {
69+
reject(err.message);
70+
} else {
71+
resolve(stdout)
72+
}
73+
})
74+
})
75+
}
76+
/**
77+
* 从某个HTML文件读取能被Webview加载的HTML内容
78+
* @param {string} context 上下文
79+
* @param {string} templatePath 相对于插件根目录的html文件相对路径
80+
*/
81+
function getWebViewContent(context:vscode.ExtensionContext, templatePath:string) {
82+
const resourcePath = path.join(context.extensionPath, templatePath);
83+
const dirPath = path.dirname(resourcePath);
84+
let html:string = fs.readFileSync(resourcePath, 'utf-8');
85+
// vscode不支持直接加载本地资源,需要替换成其专有路径格式,这里只是简单的将样式和JS的路径替换
86+
html = html.replace(/(<link.+?href="|<script.+?src="|<img.+?src=")(.+?)"/g, (m:string, $1:string, $2:string) => {
87+
return $1 + vscode.Uri.file(path.resolve(dirPath, $2)).with({ scheme: 'vscode-resource' }).toString() + '"';
88+
});
89+
return html;
90+
}

0 commit comments

Comments
 (0)