t-comm
Version:
专业、稳定、纯粹的工具库
112 lines (107 loc) • 3.62 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var node_nodeCommand = require('../node/node-command.js');
var time_time = require('../time/time.js');
var versionTip_config = require('./config.js');
var git_git = require('../git/git.js');
require('@babel/runtime/helpers/typeof');
require('../tslib.es6-01322ba9.js');
/* eslint-disable @typescript-eslint/no-require-imports */
function getTimeStampFromDate(date) {
return new Date(date).getTime();
}
function doRelease(_a) {
var isFirstRelease = _a.isFirstRelease,
root = _a.root;
console.log('[GEN VERSION] Doing standard-version ...');
if (isFirstRelease) {
node_nodeCommand.execCommand('npx standard-version --first-release', root);
} else {
node_nodeCommand.execCommand('npx standard-version --release-as patch', root);
}
console.log('[GEN VERSION] Done standard-version.');
}
/**
* 是否应该执行 standard-version
* 返回0,不执行
* 返回1,执行--first-release
* 返回2,执行--release-as patch
* @private
* @param {string} [] 命令执行目录
* @returns {number} 是否应该执行 standard-version
*/
function shouldGenVersion(root, forceGenVersion) {
var path = require('path');
var fs = require('fs');
if (!root) {
console.log('[GEN VERSION] ERROR: Please input root. Such as process.cwd()');
return versionTip_config.TAG_MAP.NO_TAG;
}
var INTERVAL_TIME = 24 * 60 * 60 * 1000;
var gitPath = path.resolve(root, '.git');
console.log('[GEN VERSION] GitPath: ', gitPath);
if (!fs.existsSync(gitPath)) {
console.log("[GEN VERSION] ERROR: NOT FOUND .git of ".concat(gitPath));
return versionTip_config.TAG_MAP.NO_TAG;
}
var tag = git_git.getGitLastTag(root);
console.log("[GEN VERSION] Tag: ".concat(tag));
if (!tag) {
return versionTip_config.TAG_MAP.FIRST_TAG;
}
if (forceGenVersion) {
return versionTip_config.TAG_MAP.MORE_TAG;
}
var tagDate = git_git.getGitTagTime(tag, root);
console.log("[GEN VERSION] Tag Date: ".concat(tagDate));
var commits = git_git.getGitCommitsBeforeTag(tag, root);
console.log("[GEN VERSION] Commit number: ".concat(commits));
if (Number(commits) < 1) {
console.log('[GEN VERSION] ERROR: commit number less than 1');
return versionTip_config.TAG_MAP.NO_TAG;
}
var tagTimeStamp = getTimeStampFromDate(tagDate);
var dateStr = time_time.timeStampFormat(tagTimeStamp, 'yyyy-MM-dd hh:mm:ss');
console.log("[GEN VERSION] Tag TimeStamp: ".concat(dateStr));
if (Date.now() - tagTimeStamp < INTERVAL_TIME) {
console.log("[GEN VERSION] ERROR: \u95F4\u9694\u5C0F\u4E8E".concat(INTERVAL_TIME));
return versionTip_config.TAG_MAP.NO_TAG;
}
return versionTip_config.TAG_MAP.MORE_TAG;
}
/**
* 自动生成version,核心是利用 standard-version 命令
* @param {object} config 配置信息
* @param {string} config.root 项目根路径
* @returns {boolean} 是否执行了 standard-version
* @example
*
* genVersion({
* root: process.cwd()
* })
*
*/
function genVersion(_a) {
var root = _a.root,
_b = _a.forceGenVersion,
forceGenVersion = _b === void 0 ? false : _b;
var genType = shouldGenVersion(root, forceGenVersion);
if (!genType) return false;
if (genType === versionTip_config.TAG_MAP.FIRST_TAG) {
doRelease({
isFirstRelease: true,
root: root
});
return true;
}
if (genType === versionTip_config.TAG_MAP.MORE_TAG) {
doRelease({
isFirstRelease: false,
root: root
});
return true;
}
return false;
}
exports.genVersion = genVersion;
exports.shouldGenVersion = shouldGenVersion;