提交 e053fd09 作者: 潘亚楠

git工具类#5

上级 e088d3bb
...@@ -27,6 +27,8 @@ ...@@ -27,6 +27,8 @@
"license": "ISC", "license": "ISC",
"devDependencies": { "devDependencies": {
"@types/commander": "^2.12.2", "@types/commander": "^2.12.2",
"@types/cross-spawn": "^6.0.1",
"@types/fs-extra": "^8.0.1",
"@types/mocha": "^5.2.7", "@types/mocha": "^5.2.7",
"@types/node": "^12.12.17", "@types/node": "^12.12.17",
"@types/power-assert": "^1.5.2", "@types/power-assert": "^1.5.2",
...@@ -39,6 +41,10 @@ ...@@ -39,6 +41,10 @@
}, },
"dependencies": { "dependencies": {
"chalk": "^3.0.0", "chalk": "^3.0.0",
"commander": "^4.0.1" "commander": "^4.0.1",
"cross-spawn": "^7.0.1",
"execa": "^3.4.0",
"fs-extra": "^8.1.0",
"ora": "^4.0.3"
} }
} }
import * as chalk from 'chalk';
/** /**
* @date 2019.12.16 * @date 2019.12.16
* @author panyanan * @author panyanan
......
import Git from '../../util/Git'
import * as assert from 'power-assert'
import * as path from 'path'
import * as spawn from 'cross-spawn'
describe('#util/Git', function () {
it('#hasGit', function () {
console.log(Git.hasGit)
})
it('#localGitUserInfo', function () {
let res = Git.localGitUserInfo();
console.log(res);
})
it('#pull', async function () {
this.skip()
let url = 'git@git.allhome.com.cn:panyanan/my-vue-template.git';
const dir = path.join(__dirname, 'abc');
const git = new Git(url, dir);
await git.pull();
})
it('#push', function () {
this.skip();
let url = 'git@git.allhome.com.cn:Platform/UPA/UPA_QJCLI/UPA_test.git';
const dir = path.join(__dirname, 'abc');
const git = new Git(url, dir);
let res = git.push();
assert.ok(res);
})
})
\ No newline at end of file
/**
* @date 2019.12.17
* @author panyanan
* @information git工具类 提供git拉取,推送等
*/
import * as execa from 'execa'
import * as assert from 'power-assert'
import * as fs from 'fs-extra'
import * as chalk from 'chalk'
import * as spawn from 'cross-spawn'
class Git {
private gitURL: string; // 仓库地址
private dir: string; // 拉取或推送的目录
constructor(gitURL: string, dir: string) {
assert.ok(Git.hasGit, '请先安装Git');
this.gitURL = gitURL;
this.dir = dir;
}
/**
* @date 2019.12.17
* @author panyanan
* @information 拉取
*/
async pull(): Promise<boolean> {
let res = false;
try {
// 检测目录是否存在
const dirExits = await fs.pathExists(this.dir);
// 目录存在需要删除,才能clone
if (dirExits) {
console.log(chalk.red(`delete ${this.dir}`));
await fs.remove(this.dir);
}
await execa('git', ['clone', this.gitURL, this.dir]);
res = true;
} catch (error) {
assert.ok(false, `拉取 ${this.gitURL} 失败: ${ error.message }`);
}
return res;
}
/**
* @date 2019.12.17
* @author panyanan
* @information 推送
*/
push(): boolean {
let res = false;
const options = {
cwd: this.dir,
}
// remove origin
spawn.sync('git', ['remote', 'remove', 'origin',], options);
// set origin 为当前url
spawn.sync('git', ['remote', 'add', 'origin', this.gitURL], options);
// 推送
const { status, stderr } = spawn.sync('git', ['push', '-u', 'origin', '--all'], options);
// status 为0时命令执行成功, 为1是命令执行失败
if (status === 0) {
res = true;
} else {
// 抛异常
throw new Error(stderr.toString())
}
return res;
}
/**
* @date 2019.12.17
* @author panyanan
* @information 获取git配置中的用户名及邮箱
*/
static localGitUserInfo():{ name: string, email: string} {
// 检测git是否安装
assert.ok(Git.hasGit, '请安装Git');
// 获取用户名
const { stdout: name } = execa.sync('git', ['config', 'user.name']);
// 获取邮箱
const { stdout: email } = execa.sync('git', ['config', 'user.email']);
return { name, email };
}
/**
* @date 2019.12.17
* @author panyanan
* @information 是否安装了git
*/
static get hasGit(): boolean {
let res = false;
try {
// 执行git --help 是否抛异常
execa.sync('git', ['--help']);
res = true;
} catch (error) {}
return res;
}
}
export default Git;
\ No newline at end of file
{ {
"compilerOptions": { "compilerOptions": {
"module": "commonjs", "module": "CommonJS",
"outDir": "dist", "outDir": "dist",
"declaration": true,
"listFiles": true,
"noImplicitAny": true, "noImplicitAny": true,
"allowSyntheticDefaultImports": true,
"target": "es5",
"baseUrl": "src",
"removeComments": true, "removeComments": true,
"preserveConstEnums": true, "preserveConstEnums": true,
"sourceMap": true "sourceMap": true
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论