archive-org-link-grabber/scripts/build-and-commit.js
wagesj45 982a16d86e feat(theme): apply new color palette and polish UI
- Add theme variables and restyle popup/options using Primary 3 as background and Primary 2 as surfaces; accents use Primary 1.\n- Move Save to a footer section on the options page; add responsive layout and button variants.\n- Update icon build defaults to use Primary 2 for add-on and Primary 1 for toolbar icons.\n- Add build-and-commit helper to run build, stage generated assets, commit, and optionally push.\n- Add npm scripts: build:commit and build:commit:push.
2025-08-24 00:39:20 -05:00

101 lines
3.3 KiB
JavaScript

#!/usr/bin/env node
/*
Build + commit (+ optional push) helper.
- Runs the existing build (`npm run build`)
- Stages generated assets (icons by default; optionally dist with INCLUDE_DIST=true)
- Commits with a conventional message including the package version
- If `--push` or `PUSH=true`, pushes to the current branch's upstream
Notes:
- Respects .gitignore (dist is ignored unless INCLUDE_DIST=true)
- Safe no-op when there is nothing to commit
*/
const fs = require('fs');
const path = require('path');
const { spawnSync } = require('child_process');
const REPO = path.join(__dirname, '..');
const PKG_PATH = path.join(REPO, 'package.json');
function run(cmd, args, opts = {}) {
const res = spawnSync(cmd, args, { stdio: 'inherit', ...opts });
if (res.error) throw res.error;
return res.status || 0;
}
function runOut(cmd, args, opts = {}) {
const res = spawnSync(cmd, args, { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'], ...opts });
if (res.error) throw res.error;
return { code: res.status || 0, out: (res.stdout || '').trim(), err: (res.stderr || '').trim() };
}
function ensureGit() {
const { code } = runOut('git', ['--version']);
if (code !== 0) {
console.error('git not found in PATH');
process.exit(1);
}
}
function pkgVersion() {
const pkg = JSON.parse(fs.readFileSync(PKG_PATH, 'utf8'));
return pkg.version || '0.0.0';
}
function main() {
ensureGit();
// 1) Build (icons + xpi)
console.log('• Running build…');
const codeBuild = run('npm', ['run', 'build'], { cwd: REPO });
if (codeBuild !== 0) process.exit(codeBuild);
// 2) Stage generated assets
const includeDist = String(process.env.INCLUDE_DIST || '').toLowerCase() === 'true';
console.log('• Staging generated assets…');
// Always stage icons (tracked or untracked)
run('git', ['add', 'icons/icon-*.png'], { cwd: REPO });
if (includeDist) {
// dist is ignored; force add if requested
run('git', ['add', '-f', 'dist'], { cwd: REPO });
}
// 3) Check if anything is staged
const { out: staged } = runOut('git', ['diff', '--cached', '--name-only'], { cwd: REPO });
if (!staged) {
console.log('✓ Nothing to commit. Up to date.');
return;
}
// 4) Commit
const version = pkgVersion();
const msg = `chore(build): update generated assets (v${version})`;
console.log(`• Committing: ${msg}`);
const codeCommit = run('git', ['commit', '-m', msg], { cwd: REPO });
if (codeCommit !== 0) process.exit(codeCommit);
// 5) Optional push
const shouldPush = process.argv.includes('--push') || String(process.env.PUSH || '').toLowerCase() === 'true';
if (!shouldPush) {
console.log('✓ Commit created. Skipping push (enable with --push or PUSH=true).');
return;
}
console.log('• Pushing to upstream…');
// If upstream exists, push; else set upstream to origin/<branch>
const { code: upCode } = runOut('git', ['rev-parse', '--abbrev-ref', '--symbolic-full-name', '@{u}'], { cwd: REPO });
if (upCode === 0) {
const codePush = run('git', ['push'], { cwd: REPO });
if (codePush !== 0) process.exit(codePush);
} else {
const { out: branch } = runOut('git', ['rev-parse', '--abbrev-ref', 'HEAD'], { cwd: REPO });
const codePush = run('git', ['push', '-u', 'origin', branch], { cwd: REPO });
if (codePush !== 0) process.exit(codePush);
}
console.log('✓ Pushed.');
}
main();