gittttup
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

162 lines
4.7 KiB

#!/usr/bin/env node
// https://github.com/tj/commander.js/blob/HEAD/Readme_zh-CN.md
// https://www.npmjs.com/package/inquirer
// https://www.npmjs.com/package/ora
const git_util = require('./git_util.js');
const program = require('commander');
const inquirer = require('inquirer');
const ora = require('ora');
const exec = require('child_process').exec;
const fs = require('fs');
const haveGit = fs.existsSync('./.git');
/**
* 1. 检测是否有GIT管理
* 2. 添加GIT源
*/
// 检查git是否干净
function isClean() {
return new Promise((resolve, reject) => {
exec('git status', function (error, stdout, stderr) {
if (error) {
console.log(error);
reject(error);
return;
}
let str = stdout.replace(/( |\\n)/g, '');
// console.log(str);
let isClean = str.indexOf('clean') != -1;
console.log('分支是否干净', isClean);
resolve(isClean);
})
})
}
// 获取当前所在分支
function getBranch() {
return new Promise((resolve, reject) => {
exec('git branch', function (error, stdout, stderr) {
if (error) {
console.log(error);
reject(error);
return;
}
let branches = stdout.split('\n').slice(0, stdout.split('\n').length - 1);
branches = branches.filter(v => {
if (v.indexOf('*') != -1) {
return true;
} else {
return false;
}
})
let currentBranch = branches[0].slice(2);
resolve(currentBranch);
});
})
}
async function up() {
if (haveGit) {
exec('git remote', function (error, stdout, stderr) {
// 获取命令执行的输出
if (error) {
console.log(error);
reject(error);
return;
}
// 所有的源
let origin = stdout.split('\n').slice(0, stdout.split('\n').length - 1);
console.log(origin);
if (origin.length == 0) {
inquirer.prompt([{
type: 'input', // 问题类型,包括input,number,confirm,list,rawlist,password
name: 'name',
message: '源名字', // 问题
default: '', // 默认值
validate: (input) => {
if (input.length == 0) {
return '请输入源名字';
}
return true;
}
}, {
type: 'input', // 问题类型,包括input,number,confirm,list,rawlist,password
name: 'url',
message: '源url', // 问题
default: '', // 默认值
validate: (input) => {
if (input.length == 0) {
return '请输入源url';
}
return true;
}
}]).then(async answers => {
exec('git remote add ' + answers.name + ' ' + answers.url, function (error, stdout, stderr) {
if (error) {
console.log(error);
reject(error);
return;
}
console.log('添加成功');
})
})
return;
}
inquirer.prompt([{
type: 'confirm', // 问题类型,包括input,number,confirm,list,rawlist,password
name: 'all',
message: '是否提交所有源', // 问题
default: true // 默认值
}]).then(async answers => {
if (answers.all) {
// 所有源提交
let branch = await getBranch(); //当前分支
for (let i = 0; i < origin.length; i++) {
const o = origin[i];
let isclean = await isClean();
if (!isclean) {
await git_util.all('sa', o, branch);
} else {
await git_util.push(o, branch);
}
}
} else {
inquirer.prompt([{
type: 'checkbox', // 问题类型,包括input,number,confirm,list,rawlist,password
choices: origin,
name: 'choices',
message: '请选择一个源提交', // 问题
default: true // 默认值
}]).then(async answers => {
console.log(answers.choices);
let ooo = answers.choices;
let branch = await getBranch(); //当前分支
for (let i = 0; i < ooo.length; i++) {
const o = ooo[i];
let isclean = await isClean();
if (!isclean) {
await git_util.all('sa', o, branch);
} else {
await git_util.push(o, branch);
}
}
})
}
})
});
} else {
exec('git init', function (error, stdout, stderr) {
// 获取命令执行的输出
if (error) {
console.log(error);
reject(error);
return;
}
console.log('git项目初始化成功');
});
}
}
up();
module.exports = {
up
}