UNPKG

@shuyun-ep-team/scripts

Version:

为项目提供相应的脚本,使其减少繁琐的配置。

56 lines 2.48 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const cross_spawn_1 = require("../../tools/cross-spawn"); const options = { encoding: 'utf-8' }; function verifyGit() { const { stdout, output = [] } = cross_spawn_1.spawnSync('git', ['rev-parse', '--is-inside-work-tree'], options); if (stdout.trim() !== 'true') { throw new Error(`${output.join('')}`); } return true; } function getCommitId(short = false) { verifyGit(); const { stdout } = cross_spawn_1.spawnSync('git', ['rev-parse', short ? '--short' : '--default', 'HEAD'], options); return stdout.trim(); } exports.getCommitId = getCommitId; function getCommit(hash = '') { verifyGit(); const { stdout: committer } = cross_spawn_1.spawnSync('git', ['log', '--pretty=format:%cn'].concat(hash ? [hash] : []).concat(['-1']), options); const { stdout: commitDate } = cross_spawn_1.spawnSync('git', ['log', '--pretty=format:%ad', '--date=iso'].concat(hash ? [hash] : []).concat(['-1']), options); const { stdout: commitId } = cross_spawn_1.spawnSync('git', ['log', '--pretty=format:%H'].concat(hash ? [hash] : []).concat(['-1']), options); const { stdout: commitMessage } = cross_spawn_1.spawnSync('git', ['log', '--pretty=format:%s'].concat(hash ? [hash] : []).concat(['-1']), options); return { committer, commitId, commitDate, commitMessage }; } exports.getCommit = getCommit; function getBranch() { verifyGit(); const { stdout } = cross_spawn_1.spawnSync('git', ['rev-parse', '--abbrev-ref', 'HEAD'], options); return stdout.trim(); } exports.getBranch = getBranch; function getRemoteOrigin() { verifyGit(); const { stdout } = cross_spawn_1.spawnSync('git', ['remote', 'get-url', 'origin'], options); return stdout.trim(); } exports.getRemoteOrigin = getRemoteOrigin; function getTags() { verifyGit(); const { stdout } = cross_spawn_1.spawnSync('git', ['log', '--tags', '--pretty="format:%d>%H"'], options); const tags = stdout .split(/\n/) .map(str => { const [branchAndTagName, commitId] = str.split('<'); return { branchAndTagName, commitId }; }) .filter(({ branchAndTagName }) => Boolean(branchAndTagName)) .map(({ branchAndTagName, commitId }) => { const [, tag] = branchAndTagName.match(/tag:\s+([-a-z\d.]{6,})/) || []; return { tag, commitId }; }); return tags; } exports.getTags = getTags; //# sourceMappingURL=git.js.map