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.
95 lines
2.7 KiB
95 lines
2.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源
|
|
* 3. 设置默认源以供一键上传
|
|
*/
|
|
// exec('git status', function (error, stdout, stderr) {
|
|
// let str = stdout.replace(/( |\\n)/g, '');
|
|
// console.log(str);
|
|
|
|
// })
|
|
// return
|
|
|
|
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 () => {
|
|
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);
|
|
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];
|
|
await git_util.all(o, branch);
|
|
}
|
|
} else {
|
|
inquirer.prompt([{
|
|
type: 'checkbox', // 问题类型,包括input,number,confirm,list,rawlist,password
|
|
choices: origin,
|
|
name: 'choices',
|
|
message: '请选择一个源提交', // 问题
|
|
default: true // 默认值
|
|
}]).then(answers => {
|
|
console.log(answers.choices);
|
|
})
|
|
}
|
|
})
|
|
});
|
|
} else {
|
|
exec('git init', function (error, stdout, stderr) {
|
|
// 获取命令执行的输出
|
|
if (error) {
|
|
console.log(error);
|
|
reject(error);
|
|
return;
|
|
}
|
|
console.log('git项目初始化成功');
|
|
});
|
|
}
|
|
})();
|