#!/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. 设置默认源以供一键上传
 */
(async () => {
  if (haveGit) {
    exec('git remote', function (error, stdout, stderr) {
      // 获取命令执行的输出
      if (error) {
        throw error;
      }
      // 所有的源
      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) {
          // 所有源提交
          for (let i = 0; i < origin.length; i++) {
            const o = origin[i];
            await git_util.all('a', o, 'master');
          }
        } 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) {
        throw error;
      }
      console.log('git项目初始化成功');
    });
  }
})();