@serpent/common-cli
Version:
通用的 cli 相关的函数
307 lines (304 loc) • 11.4 kB
JavaScript
import { _ as __awaiter, a as __generator } from './tslib.es6-636c4f06.mjs';
import { silentRun, silentRunOutput, runOutput } from './run.mjs';
import 'node:buffer';
import 'node:path';
import 'node:child_process';
import 'node:process';
import './_commonjsHelpers-7d1333e8.mjs';
import 'child_process';
import 'path';
import 'fs';
import 'node:url';
import 'os';
import 'node:os';
import './index-3b2c9171.mjs';
import 'assert';
import 'events';
import 'buffer';
import 'stream';
import 'util';
import 'url';
import './info-fd7bb244.mjs';
import './warn-1994a28d.mjs';
import './supportColor-2e661db8.mjs';
import './helper-1331d234.mjs';
import './exists-d3d14d46.mjs';
import 'module';
import 'crypto';
/** 执行 git fetch 更新本地仓库 */
function runGitFetch() {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, silentRun('git fetch')];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
}
/**
* 获取 git 的配置项,如 'user.name', 'user.email'
*/
function getGitConfig(field) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, silentRunOutput(['git', 'config', '--get', field])];
case 1: return [2 /*return*/, _a.sent()];
case 2:
_a.sent();
return [2 /*return*/];
case 3: return [2 /*return*/];
}
});
});
}
/**
* 获取 git 的本地配置项,如 'user.name', 'user.email'
*/
function getGitLocalConfig(field) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, silentRunOutput(['git', 'config', '--local', '--get', field])];
case 1: return [2 /*return*/, _a.sent()];
case 2:
_a.sent();
return [2 /*return*/];
case 3: return [2 /*return*/];
}
});
});
}
/**
* 设置 git 的本地配置项,如 'user.name', 'user.email'
*/
function setGitLocalConfig(field, value) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, silentRunOutput(['git', 'config', '--local', field, value])];
});
});
}
/** 获取本地当前最新的 commit id */
function getCurrentGitCommitId() {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, silentRunOutput('git rev-parse HEAD')];
});
});
}
/** 获取本地当前 branch */
function getCurrentGitBranch() {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, silentRunOutput('git symbolic-ref --short HEAD')];
});
});
}
/** 本地是否有某个分支 */
function hasLocalGitBranch(branch) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 2, , 3]);
return [4 /*yield*/, silentRun(['git', 'show-ref', '--verify', '--quiet', "refs/heads/".concat(branch)])];
case 1:
_b.sent();
return [2 /*return*/, true];
case 2:
_b.sent();
return [2 /*return*/, false];
case 3: return [2 /*return*/];
}
});
});
}
/** 检查某个文件是否在 gitignore 中 */
function isFileGitIgnored(pathToFile) {
return __awaiter(this, void 0, void 0, function () {
var output, error_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, silentRunOutput(['git', 'check-ignore', pathToFile])];
case 1:
output = _a.sent();
return [2 /*return*/, Boolean(output)];
case 2:
error_1 = _a.sent();
// If file is not ignored, `git check-ignore` throws an empty error and exits.
// Check that and return false so as not to throw an unwanted error.
if (error_1.stdout === '' && error_1.stderr === '') {
return [2 /*return*/, false];
}
throw error_1;
case 3: return [2 /*return*/];
}
});
});
}
/** 检查当前 git status 是否是干净的 */
function isGitStatusClean() {
return __awaiter(this, void 0, void 0, function () {
var output;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 2, , 3]);
return [4 /*yield*/, silentRunOutput('git status --porcelain')];
case 1:
output = _b.sent();
if (output === '') {
return [2 /*return*/, true];
}
return [3 /*break*/, 3];
case 2:
_b.sent();
return [3 /*break*/, 3];
case 3: return [2 /*return*/, false];
}
});
});
}
/** 检查远程是否可以更新,如果返回 false,表示本地需要执行 git pull */
function isGitRemoteHistoryClean() {
return __awaiter(this, void 0, void 0, function () {
var history;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 2, , 3]);
return [4 /*yield*/, silentRunOutput(['git', 'rev-list', '--count', '--left-only', '@{u}...HEAD'])];
case 1:
// Gracefully handle no remote set up.
history = _b.sent();
return [3 /*break*/, 3];
case 2:
_b.sent();
return [3 /*break*/, 3];
case 3:
if (history && history !== '0') {
return [2 /*return*/, false];
}
return [2 /*return*/, true];
}
});
});
}
/** 判断某个 tag 是否存在,使用之前建议先执行 `git fetch` */
function isGitTagExists(tagName) {
return __awaiter(this, void 0, void 0, function () {
var output, e_3;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, silentRunOutput(['git', 'rev-parse', '--quiet', '--verify', "refs/tags/".concat(tagName)])];
case 1:
output = _a.sent();
return [2 /*return*/, !!output];
case 2:
e_3 = _a.sent();
// Command fails with code 1 and no output if the tag does not exist, even though `--quiet` is provided
// https://github.com/sindresorhus/np/pull/73#discussion_r72385685
if (e_3.stdout === '' && e_3.stderr === '') {
return [2 /*return*/, false];
}
throw e_3;
case 3: return [2 /*return*/];
}
});
});
}
/** 获取最新的 tag,注意没有 tag 的话会抛出异常 */
function getLatestGitTag() {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, silentRunOutput('git describe --abbrev=0 --tags')];
});
});
}
/** 获取 tag 列表 */
function getGitTagList() {
return __awaiter(this, void 0, void 0, function () {
var output;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, silentRunOutput('git tag --sort=creatordate')];
case 1:
output = _a.sent();
if (!output)
return [2 /*return*/, []];
return [2 /*return*/, output.split('\n')];
}
});
});
}
/** 获取第一次提交的 commit id */
function getFirstGitCommitId() {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, silentRunOutput('git rev-list --max-parents=0 HEAD')];
});
});
}
/** 获取版本之间的所有提交记录 */
function getGitCommitLogs(fromRevision, toRevision) {
if (toRevision === void 0) { toRevision = 'HEAD'; }
return __awaiter(this, void 0, void 0, function () {
var output;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, runOutput(['git', 'log', '--format=%s %h', "".concat(fromRevision, "..").concat(toRevision)])];
case 1:
output = _a.sent();
if (output) {
return [2 /*return*/, output.split('\n').map(function (commit) {
var splitIndex = commit.lastIndexOf(' ');
return {
message: commit.slice(0, splitIndex),
id: commit.slice(splitIndex + 1),
};
})];
}
return [2 /*return*/, []];
}
});
});
}
/** 删除本地的 tag */
function deleteLocalGitTag(tagName) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, silentRun(['git', 'tag', '--delete', tagName])];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
}
/** 删除上一次的提交记录 */
function removeLastGitCommit() {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, silentRun(['git', 'reset', '--hard', 'HEAD~1'])];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
}
export { deleteLocalGitTag, getCurrentGitBranch, getCurrentGitCommitId, getFirstGitCommitId, getGitCommitLogs, getGitConfig, getGitLocalConfig, getGitTagList, getLatestGitTag, hasLocalGitBranch, isFileGitIgnored, isGitRemoteHistoryClean, isGitStatusClean, isGitTagExists, removeLastGitCommit, runGitFetch, setGitLocalConfig };