File size: 3,572 Bytes
3e8ea5d
 
 
 
 
11486ce
3e8ea5d
 
 
 
 
 
 
 
 
11486ce
3e8ea5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env node

import { isMainModule, pickFailureOutput, printJson, runCommand } from './command-center-utils.mjs';

const FULL_VERIFY_PLAN = [
    { name: 'version:check', command: 'npm', args: ['run', 'version:check'] },
    { name: 'test', command: 'npm', args: ['test'] },
    { name: 'lint', command: 'npm', args: ['run', 'lint'] },
    { name: 'lint:scripts', command: 'npm', args: ['run', 'lint:scripts'] },
    { name: 'build', command: 'npm', args: ['run', 'build'] },
    { name: 'diff-check', command: 'git', args: ['diff', '--check'] },
    { name: 'diff-cached-check', command: 'git', args: ['diff', '--cached', '--check'] }
];

const QUICK_VERIFY_PLAN = [
    { name: 'version:check', command: 'npm', args: ['run', 'version:check'] },
    { name: 'test:scripts', command: 'npm', args: ['run', 'test:scripts'] },
    { name: 'lint:scripts', command: 'npm', args: ['run', 'lint:scripts'] },
    { name: 'diff-check', command: 'git', args: ['diff', '--check'] },
    { name: 'diff-cached-check', command: 'git', args: ['diff', '--cached', '--check'] }
];

const POSTGRES_VERIFY_STEP = { name: 'test:postgres', command: 'npm', args: ['run', 'test:postgres'] };

export function buildVerifyPlan(options = {}) {
    const source = options.quick ? QUICK_VERIFY_PLAN : FULL_VERIFY_PLAN;
    const plan = source.filter((step) => !(options.skipBuild && step.name === 'build'));
    if (!options.postgres) return plan;
    const firstDiffCheckIndex = plan.findIndex((step) => step.name.startsWith('diff-'));
    if (firstDiffCheckIndex === -1) return [...plan, POSTGRES_VERIFY_STEP];
    return [...plan.slice(0, firstDiffCheckIndex), POSTGRES_VERIFY_STEP, ...plan.slice(firstDiffCheckIndex)];
}

function parseArgs(argv) {
    const unknown = argv.find((arg) => !['--help', '-h', '--quick', '--skip-build', '--postgres'].includes(arg));
    if (unknown) throw new Error(`Unknown option: ${unknown}`);
    return {
        help: argv.includes('--help') || argv.includes('-h'),
        postgres: argv.includes('--postgres'),
        quick: argv.includes('--quick'),
        skipBuild: argv.includes('--skip-build')
    };
}

function printHelp() {
    console.log(`Usage:
  npm run verify
  npm run verify -- --quick

Options:
  --quick        Run script tests, script syntax checks, and git diff whitespace checks.
  --postgres     Include the live PostgreSQL gate with npm run test:postgres.
  --skip-build   Run the full plan without npm run build.
  --help         Show this help.`);
}

function runVerify(options) {
    const checks = [];
    for (const step of buildVerifyPlan(options)) {
        const result = runCommand(step.command, step.args);
        checks.push({
            name: step.name,
            command: [step.command, ...step.args].join(' '),
            status: result.ok ? 'pass' : 'fail',
            elapsed_ms: result.elapsed_ms,
            ...(result.ok ? {} : { output: pickFailureOutput(result) })
        });
        if (!result.ok) break;
    }

    const ok = checks.every((check) => check.status === 'pass');
    printJson({ ok, profile: options.quick ? 'quick' : 'full', postgres: Boolean(options.postgres), checks });
    if (!ok) process.exit(1);
}

try {
    if (isMainModule(import.meta.url, process.argv[1])) {
        const options = parseArgs(process.argv.slice(2));
        if (options.help) {
            printHelp();
        } else {
            runVerify(options);
        }
    }
} catch (error) {
    printJson({ ok: false, error: error instanceof Error ? error.message : String(error) });
    process.exit(1);
}