UNPKG

standard-commit

Version:
80 lines (79 loc) 2.82 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.gitTopLevel = exports.gitCanCommit = exports.gitCommitAndEdit = exports.gitCommit = exports.gitStagedPaths = void 0; const execa_1 = __importDefault(require("execa")); const path_1 = __importDefault(require("path")); const fs_1 = __importDefault(require("fs")); const util_1 = require("util"); const writeFile = (0, util_1.promisify)(fs_1.default.writeFile); async function gitStagedPaths() { try { const args = ['diff', '--name-only', '--cached']; const git = await (0, execa_1.default)('git', args); const staged = git.stdout .split('\n') .map((f) => f.trim()) .filter((f) => f); return staged; } catch { return []; } } exports.gitStagedPaths = gitStagedPaths; async function gitCommit(message, ...args) { const commitArgs = ['commit', ...args, '--file', '-']; const git = (0, execa_1.default)('git', commitArgs); git.stdout.pipe(process.stdout); git.stderr.pipe(process.stderr); git.stdin.write(message); git.stdin.end(); return (await git).exitCode; } exports.gitCommit = gitCommit; async function gitCommitAndEdit(message, ...args) { let editPath; try { const gitDir = (0, execa_1.default)('git', ['rev-parse', '--absolute-git-dir']); const gitPath = (await gitDir).stdout.trim(); editPath = path_1.default.join(gitPath, 'COMMIT_STDMSG'); await writeFile(editPath, message); } catch (err) { process.stdout.write(err.stdout); throw err; } const commitArgs = ['commit', ...args, '-e', '-F', JSON.stringify(editPath)]; const shellCommand = 'git ' + commitArgs.join(' '); const gitCommit = (0, execa_1.default)(shellCommand, { shell: true, }); gitCommit.stdout.pipe(process.stdout); gitCommit.stderr.pipe(process.stderr); gitCommit.stdin.write(message); gitCommit.stdin.end(); return (await gitCommit).exitCode; } exports.gitCommitAndEdit = gitCommitAndEdit; async function gitCanCommit(...args) { try { const dryRun = await (0, execa_1.default)('git', ['commit', ...args, '--dry-run']); return dryRun.exitCode === 0; } catch (err) { process.stdout.write(err.stdout); process.stdout.write('\n'); return false; } } exports.gitCanCommit = gitCanCommit; async function gitTopLevel() { // git rev-parse --show-toplevel const gitDir = (0, execa_1.default)('git', ['rev-parse', '--show-toplevel']); const gitPath = (await gitDir).stdout.trim(); return gitPath; } exports.gitTopLevel = gitTopLevel;