-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion-control.js
More file actions
78 lines (66 loc) · 2.1 KB
/
version-control.js
File metadata and controls
78 lines (66 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* VersionControl
* 1. Auto detect Git or Svn
* 2. Manually specify Git or Svn
*/
const exec = require('./exec');
class VersionControl {
constructor(versionControlSystemType) {
if (versionControlSystemType && !['svn', 'git'].contains(versionControlSystemType)) throw new Error('Only git or svn can be pass in.');
this.type = versionControlSystemType;
}
async status(codePath) {
const vcs = await this.getVcs(codePath);
return vcs.status(codePath);
}
async update(codePath) {
const vcs = await this.getVcs(codePath);
return vcs.update(codePath);
}
async commit(codePath, commitMsg) {
const vcs = await this.getVcs(codePath);
return vcs.commit(codePath, commitMsg);
}
async log(codePath) {
const vcs = await this.getVcs(codePath);
return vcs.log(codePath);
}
async revert(codePath, version, commitMsg) {
const vcs = await this.getVcs(codePath);
return vcs.revert(codePath, version, commitMsg);
}
async push(codePath) {
const vcs = await this.getVcs(codePath);
if (!vcs.push) throw new Error(`push is only avaliable for Git, your code in ${codePath} is not a git repository.`);
return vcs.push(codePath);
}
// 获取实际的版本管理脚本:若有指定,则用指定的,若无,则自动检测
// vsc = versionControlSystem
async getVcs(codePath) {
switch (true) {
case this.type === 'git' || await VersionControl.isGit(codePath):
return require('./git');
case this.type === 'svn' || await VersionControl.isSvn(codePath):
return require('./svn');
default:
throw new Error(`Can't detect git or svn in ${codePath}`);
}
}
static async isGit(codePath) {
const result = await require('./git').status(codePath);
return Boolean(result);
}
static async isSvn(codePath) {
let isSvn = false;
await exec('svn info', codePath)
.then(() => {
isSvn = true;
})
.catch((error) => {
const { message } = error;
if (message.indexOf('svn upgrade') !== -1) isSvn = true;
});
return isSvn;
}
}
module.exports = VersionControl;