spb/spbgit Public MIT
SPB Git — the platform hosting itself
JavaScript 73.9%
CSS 11.7%
Nunjucks 11.6%
Shell 2.7%
1/**2 * ─────────────────────────────────────────────3 * SPB Git — Personal Git Platform4 * ─────────────────────────────────────────────5 * Author : Simon-Pierre Boucher6 * Contact : contact@spboucher.ai7 * File : cli/commands/commit.mjs8 * Purpose : `spbgit commit <name>|--all -m` — add-all + commit9 * License : MIT © Simon-Pierre Boucher10 * ─────────────────────────────────────────────11 */1213import { basename, join } from 'node:path';14import { existsSync } from 'node:fs';15import pc from 'picocolors';16import { requireCliConfig } from '../lib/config.mjs';17import { workspaceRepos, git } from '../lib/git.mjs';18import { mapLimit, SYM } from '../lib/ui.mjs';19import { EXIT } from '../lib/api.mjs';2021/** Resolve the list of target repo dirs for name/--all commands. */22export function resolveTargets(config, name, all) {23 if (all) return workspaceRepos(config.workspace);24 if (!name) {25 console.error(pc.red('✗ Provide a repository name or --all'));26 process.exit(EXIT.USER);27 }28 const dir = join(config.workspace, name);29 if (!existsSync(join(dir, '.git'))) {30 console.error(pc.red(`✗ No such workspace repo: ${name}`));31 process.exit(EXIT.USER);32 }33 return [dir];34}3536export function registerCommit(program) {37 program38 .command('commit [name]')39 .description('git add -A && git commit in target repo(s)')40 .option('--all', 'every workspace repository', false)41 .requiredOption('-m, --message <msg>', 'commit message')42 .action(async (name, opts) => {43 const config = requireCliConfig();44 const targets = resolveTargets(config, name, opts.all);45 let failures = 0;46 await mapLimit(targets, 4, async (dir) => {47 const repo = basename(dir);48 const status = await git(dir, ['status', '--porcelain']);49 if (status.ok && status.stdout.trim() === '') {50 console.log(`${SYM.skip} ${repo} ${pc.dim('clean — skipped')}`);51 return;52 }53 await git(dir, ['add', '-A']);54 const commit = await git(dir, ['commit', '-m', opts.message]);55 if (commit.ok) console.log(`${SYM.ok} ${repo} ${pc.dim('committed')}`);56 else {57 failures += 1;58 console.error(`${SYM.fail} ${repo}: ${commit.stderr.trim().split('\n').pop()}`);59 }60 });61 if (failures > 0) process.exit(EXIT.PARTIAL);62 });63}64