'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var fs_fs = require('../fs/fs.js');
var node_nodeCommand = require('../node/node-command.js');
var wecomRobot_base = require('../wecom-robot/base.js');
var versionTip_genVersion = require('./gen-version.js');
var versionTip_genVersionTip = require('./gen-version-tip.js');
require('../nodejs/crypto.js');
require('../nodejs/fs.js');
require('../nodejs/os.js');
require('@babel/runtime/helpers/typeof');
require('../nodejs/child_process.js');
require('@babel/runtime/regenerator');
require('../tslib.es6-0d92ef81.js');
require('../wecom-robot/helper.js');
require('axios');
require('../nodejs/path.js');
require('../time/time.js');
require('./config.js');
require('../git/git.js');
require('../validate/validate.js');
require('../bite/format-bite.js');
require('../wecom-robot/optimize-robot-content.js');
require('../string/replace-all.js');

/**
 * 运行standard-version，并且发送changelog到机器人
 * @param {object} options 配置
 * @param {object} config.appInfo package.json信息
 * @param {string} config.root 项目根路径
 * @param {string} config.changeLogFilePath changelog文件地址
 * @param {string} config.webhookUrl 机器人hook地址
 * @param {string} config.chatId 会话id
 * @example
 * ```ts
 * await genVersionAndSendChangeLog({
 *   root: process.cwd(),
 *   changeLogFilePath: `${process.cwd()}/CHANGELOG.md`,
 *   webhookUrl: 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxx',
 *   chatId: 'group-chat-id',
 * });
 * ```
 */
function genVersionAndSendChangeLog(_ref) {
  var root = _ref.root,
    changeLogFilePath = _ref.changeLogFilePath,
    webhookUrl = _ref.webhookUrl,
    chatId = _ref.chatId,
    forceGenVersion = _ref.forceGenVersion,
    _ref$pushRemote = _ref.pushRemote,
    pushRemote = _ref$pushRemote === void 0 ? true : _ref$pushRemote;
  return new Promise(function (resolve, reject) {
    if (!root) {
      reject('ERROR: root不可以为空');
      return;
    }
    if (!changeLogFilePath) {
      changeLogFilePath = "".concat(root, "/CHANGELOG.md");
    }
    var hasGenVersion = versionTip_genVersion.genVersion({
      root: root,
      forceGenVersion: forceGenVersion
    });
    if (!hasGenVersion) {
      resolve({
        hasGenVersion: false
      });
      return;
    }
    if (pushRemote) {
      try {
        console.log('[GEN SEND VERSION] Doing git push --follow-tags origin ...');
        node_nodeCommand.execCommand('git push --follow-tags origin', root, 'inherit');
        console.log('[GEN SEND VERSION] Done git push --follow-tags origin.');
      } catch (err) {
        reject(err);
        return;
      }
    }
    // appInfo 不要用传入的参数，因此传入是旧的，新的还未生成
    var appInfo = fs_fs.readFileSync("".concat(root, "/package.json"), true);
    sendVersionTip({
      appInfo: appInfo,
      changeLogFilePath: changeLogFilePath,
      webhookUrl: webhookUrl,
      chatId: chatId
    }).then(resolve)["catch"](reject);
  });
}
function sendVersionTip(_ref2) {
  var appInfo = _ref2.appInfo,
    changeLogFilePath = _ref2.changeLogFilePath,
    webhookUrl = _ref2.webhookUrl,
    chatId = _ref2.chatId;
  return new Promise(function (resolve, reject) {
    var content = versionTip_genVersionTip.genVersionTip({
      readmeFilePath: changeLogFilePath,
      appInfo: appInfo
    });
    if (!content) {
      resolve({
        hasGenVersion: true,
        hasSendChangeLog: false,
        sendChangeLogError: 'empty changelog content'
      });
      return;
    }
    console.log('[GEN SEND VERSION] Doing send wecom robot markdown ...');
    wecomRobot_base.sendWxRobotMarkdown({
      chatId: chatId,
      content: content,
      webhookUrl: webhookUrl
    }).then(function () {
      resolve({
        hasGenVersion: true,
        hasSendChangeLog: true
      });
      console.log('[GEN SEND VERSION] Done send wecom robot markdown.');
    })["catch"](function (err) {
      reject(err);
    });
  });
}

exports.genVersionAndSendChangeLog = genVersionAndSendChangeLog;
