t-comm
Version:
专业、稳定、纯粹的工具库
119 lines (116 loc) • 3.75 kB
JavaScript
import { getFs } from '../nodejs/fs.mjs';
import { execCommand } from '../node/node-command.mjs';
import '@babel/runtime/helpers/slicedToArray';
import '@babel/runtime/regenerator';
import '../tslib.es6-848120af.js';
import { getPath } from '../nodejs/path.mjs';
import { getGitLastTag, getGitTagTime, getGitCommitsBeforeTag } from '../git/git.mjs';
import { timeStampFormat } from '../time/time.mjs';
import { TAG_MAP } from './config.mjs';
import '@babel/runtime/helpers/typeof';
import '../nodejs/child_process.mjs';
import '../validate/validate.mjs';
function getTimeStampFromDate(date) {
return new Date(date).getTime();
}
function doRelease(_ref) {
var isFirstRelease = _ref.isFirstRelease,
root = _ref.root;
console.log('[GEN VERSION] Doing standard-version ...');
if (isFirstRelease) {
execCommand('npx standard-version --first-release', root);
} else {
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
* @example
* ```ts
* const code = shouldGenVersion(process.cwd());
* // 0 = 不执行;1 = --first-release;2 = --release-as patch
*
* // 强制生成
* shouldGenVersion(process.cwd(), true);
* ```
*/
function shouldGenVersion(root, forceGenVersion) {
var fs = getFs();
if (!root) {
console.log('[GEN VERSION] ERROR: Please input root. Such as process.cwd()');
return TAG_MAP.NO_TAG;
}
var INTERVAL_TIME = 24 * 60 * 60 * 1000;
var gitPath = getPath().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 TAG_MAP.NO_TAG;
}
var tag = getGitLastTag(root);
console.log("[GEN VERSION] Tag: ".concat(tag));
if (!tag) {
return TAG_MAP.FIRST_TAG;
}
if (forceGenVersion) {
return TAG_MAP.MORE_TAG;
}
var tagDate = getGitTagTime(tag, root);
console.log("[GEN VERSION] Tag Date: ".concat(tagDate));
var commits = 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 TAG_MAP.NO_TAG;
}
var tagTimeStamp = getTimeStampFromDate(tagDate);
var dateStr = 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 TAG_MAP.NO_TAG;
}
return 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(_ref2) {
var root = _ref2.root,
_ref2$forceGenVersion = _ref2.forceGenVersion,
forceGenVersion = _ref2$forceGenVersion === void 0 ? false : _ref2$forceGenVersion;
var genType = shouldGenVersion(root, forceGenVersion);
if (!genType) return false;
if (genType === TAG_MAP.FIRST_TAG) {
doRelease({
isFirstRelease: true,
root: root
});
return true;
}
if (genType === TAG_MAP.MORE_TAG) {
doRelease({
isFirstRelease: false,
root: root
});
return true;
}
return false;
}
export { genVersion, shouldGenVersion };