@final-commerce/common 2.12.0-staging.5 → 2.12.2-staging2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -0
- package/bin/gate-done.mjs +71 -36
- package/bin/lib/budget.mjs +71 -0
- package/dist/index.d.mts +24 -248
- package/dist/index.d.ts +24 -248
- package/dist/index.js +164 -1025
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +164 -992
- package/dist/index.mjs.map +1 -1
- package/dist/pos-types/index.d.mts +8 -58
- package/dist/pos-types/index.d.ts +8 -58
- package/dist/pos-types/index.js +2 -7
- package/dist/pos-types/index.js.map +1 -1
- package/dist/pos-types/index.mjs +1 -5
- package/dist/pos-types/index.mjs.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -219,6 +219,26 @@ node -p "const c = require('@final-commerce/common'); c.formatCurrency(2500, 'EU
|
|
|
219
219
|
# → €25.00
|
|
220
220
|
```
|
|
221
221
|
|
|
222
|
+
## Quality gates
|
|
223
|
+
|
|
224
|
+
### `gate-done` time budgets
|
|
225
|
+
|
|
226
|
+
`gate-done` (the pre-push hook) runs four sweeps: npm audit, gitleaks, Vitest with coverage, and a bundle-size or build check. It gives them two separate time budgets, both set in milliseconds through environment variables:
|
|
227
|
+
|
|
228
|
+
| Variable | Default | Covers |
|
|
229
|
+
| -------------------------- | -------- | ---------------------------------------- |
|
|
230
|
+
| `GATE_DONE_BUDGET_MS` | `90000` | npm audit, gitleaks and the bundle sweep |
|
|
231
|
+
| `GATE_DONE_TEST_BUDGET_MS` | `300000` | the Vitest sweep on its own |
|
|
232
|
+
|
|
233
|
+
A budget only counts time spent in its own sweeps, so a slow test suite never takes time from the other sweeps, and the other sweeps never take time from the tests. Husky's pre-push hook inherits your shell environment, so you can raise a budget for one push or for good:
|
|
234
|
+
|
|
235
|
+
```bash
|
|
236
|
+
GATE_DONE_TEST_BUDGET_MS=600000 git push # one push
|
|
237
|
+
export GATE_DONE_TEST_BUDGET_MS=600000 # in ~/.zshrc, for every push
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
If a sweep runs past its budget, `gate-done` kills it and blocks the push with a timeout message (`Vitest killed after 300.0s by the test budget … not a test failure`), not as a test or audit failure. Values that are not positive integers are ignored with a warning, and the default applies.
|
|
241
|
+
|
|
222
242
|
## License
|
|
223
243
|
|
|
224
244
|
Private — Final Commerce internal use only.
|
package/bin/gate-done.mjs
CHANGED
|
@@ -2,59 +2,80 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* gate-done — pre-push hook.
|
|
4
4
|
*
|
|
5
|
-
* Runs four sweeps
|
|
5
|
+
* Runs four sweeps under two time budgets:
|
|
6
6
|
*
|
|
7
7
|
* 1. npm audit — known CVE check against the installed dependency tree (warn only)
|
|
8
8
|
* 2. gitleaks — credential / secret scanning (git log + staged)
|
|
9
9
|
* 3. vitest — full test suite with coverage threshold enforcement
|
|
10
10
|
* 4. bundle — size-limit or custom bundle-size script (if configured)
|
|
11
11
|
*
|
|
12
|
+
* Budgets (milliseconds, read from the environment — Husky's pre-push inherits the shell):
|
|
13
|
+
*
|
|
14
|
+
* GATE_DONE_BUDGET_MS default 90000 shared by sweeps 1, 2 and 4
|
|
15
|
+
* GATE_DONE_TEST_BUDGET_MS default 300000 sweep 3 (Vitest) on its own
|
|
16
|
+
*
|
|
17
|
+
* A budget only drains while its own sweeps run, so a slow test suite cannot
|
|
18
|
+
* starve the bundle sweep and vice versa.
|
|
19
|
+
*
|
|
12
20
|
* Any sweep failure except npm audit exits 1, blocking the push.
|
|
13
|
-
*
|
|
21
|
+
* A sweep killed by its budget also exits 1, but is reported as a timeout,
|
|
22
|
+
* not as a failure of the thing it was checking.
|
|
14
23
|
*
|
|
15
24
|
* Install as Husky pre-push hook:
|
|
16
25
|
* echo "npx gate-done" > .husky/pre-push
|
|
17
26
|
*/
|
|
18
27
|
|
|
19
|
-
import { spawnSync } from 'node:child_process';
|
|
20
28
|
import { readFileSync } from 'node:fs';
|
|
21
29
|
import { resolve } from 'node:path';
|
|
22
30
|
|
|
23
|
-
|
|
31
|
+
import {
|
|
32
|
+
DEFAULT_BUDGET_MS,
|
|
33
|
+
DEFAULT_TEST_BUDGET_MS,
|
|
34
|
+
budgetKillMessage,
|
|
35
|
+
createBudget,
|
|
36
|
+
readBudgetMs,
|
|
37
|
+
runInBudget,
|
|
38
|
+
} from './lib/budget.mjs';
|
|
39
|
+
|
|
24
40
|
const t0 = Date.now();
|
|
25
41
|
|
|
26
42
|
// ── helpers ──────────────────────────────────────────────────────────────────
|
|
27
43
|
|
|
28
|
-
function remaining() {
|
|
29
|
-
return Math.max(0, BUDGET_MS - (Date.now() - t0));
|
|
30
|
-
}
|
|
31
|
-
|
|
32
44
|
function elapsed() {
|
|
33
45
|
return ((Date.now() - t0) / 1000).toFixed(1);
|
|
34
46
|
}
|
|
35
47
|
|
|
36
|
-
function
|
|
37
|
-
|
|
38
|
-
|
|
48
|
+
function makeBudget(name, envVar, fallback) {
|
|
49
|
+
const { ms, invalid } = readBudgetMs(process.env, envVar, fallback);
|
|
50
|
+
if (invalid !== undefined) {
|
|
51
|
+
warn(`${envVar}="${invalid}" is not a positive integer of milliseconds — using ${fallback}.`);
|
|
52
|
+
}
|
|
53
|
+
return createBudget(name, envVar, ms);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function checkBudget(budget, label) {
|
|
57
|
+
if (budget.exhausted()) {
|
|
58
|
+
fail(
|
|
59
|
+
`${budget.name} budget (${budget.envVar}=${budget.ms}) exhausted before "${label}" could start. ` +
|
|
60
|
+
`Push blocked — not a ${label} failure.`,
|
|
61
|
+
);
|
|
39
62
|
}
|
|
40
63
|
}
|
|
41
64
|
|
|
42
65
|
/**
|
|
43
|
-
* Run a command, streaming output
|
|
44
|
-
*
|
|
45
|
-
*
|
|
66
|
+
* Run a command, streaming output, charged to `budget`. The budget's remaining
|
|
67
|
+
* time is the child's timeout, so a runaway child cannot overrun it. A child
|
|
68
|
+
* killed by that timeout blocks the push with a timeout message instead of
|
|
69
|
+
* being reported as a failure of the sweep.
|
|
46
70
|
*/
|
|
47
|
-
function run(cmd) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
stdio: 'inherit',
|
|
52
|
-
timeout: remaining(),
|
|
53
|
-
});
|
|
71
|
+
function run(budget, label, cmd) {
|
|
72
|
+
const result = runInBudget(budget, cmd, { stdio: 'inherit' });
|
|
73
|
+
if (result.timedOut) fail(budgetKillMessage(label, budget, result.durationMs) + ' Push blocked.');
|
|
74
|
+
return result;
|
|
54
75
|
}
|
|
55
76
|
|
|
56
|
-
function capture(cmd) {
|
|
57
|
-
return
|
|
77
|
+
function capture(budget, cmd) {
|
|
78
|
+
return runInBudget(budget, cmd);
|
|
58
79
|
}
|
|
59
80
|
|
|
60
81
|
function fail(msg) {
|
|
@@ -74,12 +95,22 @@ function ok(msg) {
|
|
|
74
95
|
process.stdout.write(`\x1b[32m✔ [${elapsed()}s] ${msg}\x1b[0m\n`);
|
|
75
96
|
}
|
|
76
97
|
|
|
98
|
+
// ── budgets ──────────────────────────────────────────────────────────────────
|
|
99
|
+
|
|
100
|
+
const sweepBudget = makeBudget('sweep', 'GATE_DONE_BUDGET_MS', DEFAULT_BUDGET_MS);
|
|
101
|
+
const testBudget = makeBudget('test', 'GATE_DONE_TEST_BUDGET_MS', DEFAULT_TEST_BUDGET_MS);
|
|
102
|
+
|
|
103
|
+
info(
|
|
104
|
+
`Budgets: sweeps ${sweepBudget.ms / 1000}s (GATE_DONE_BUDGET_MS), ` +
|
|
105
|
+
`tests ${testBudget.ms / 1000}s (GATE_DONE_TEST_BUDGET_MS).`,
|
|
106
|
+
);
|
|
107
|
+
|
|
77
108
|
// ── 1. npm audit ─────────────────────────────────────────────────────────────
|
|
78
109
|
|
|
79
110
|
info('Sweep 1/4 — npm audit (known CVEs)…');
|
|
80
|
-
checkBudget('npm audit');
|
|
111
|
+
checkBudget(sweepBudget, 'npm audit');
|
|
81
112
|
|
|
82
|
-
const auditResult = run('npm audit --audit-level=high');
|
|
113
|
+
const auditResult = run(sweepBudget, 'npm audit', 'npm audit --audit-level=high');
|
|
83
114
|
if (auditResult.status !== 0) {
|
|
84
115
|
warn('npm audit found high or critical vulnerabilities. Run `npm audit` for details.');
|
|
85
116
|
} else {
|
|
@@ -89,9 +120,9 @@ if (auditResult.status !== 0) {
|
|
|
89
120
|
// ── 2. gitleaks ───────────────────────────────────────────────────────────────
|
|
90
121
|
|
|
91
122
|
info('Sweep 2/4 — gitleaks secrets scan…');
|
|
92
|
-
checkBudget('gitleaks');
|
|
123
|
+
checkBudget(sweepBudget, 'gitleaks');
|
|
93
124
|
|
|
94
|
-
const hasGitleaks = capture('command -v gitleaks 2>/dev/null').status === 0;
|
|
125
|
+
const hasGitleaks = capture(sweepBudget, 'command -v gitleaks 2>/dev/null').status === 0;
|
|
95
126
|
|
|
96
127
|
if (!hasGitleaks) {
|
|
97
128
|
warn(
|
|
@@ -101,7 +132,7 @@ if (!hasGitleaks) {
|
|
|
101
132
|
} else {
|
|
102
133
|
// Scan the full git history visible from HEAD — correct mode for a pre-push hook.
|
|
103
134
|
// --no-git-serial scans refs in parallel for speed within the budget.
|
|
104
|
-
const glResult = run('gitleaks git --exit-code 1 .');
|
|
135
|
+
const glResult = run(sweepBudget, 'gitleaks', 'gitleaks git --exit-code 1 .');
|
|
105
136
|
if (glResult.status !== 0) {
|
|
106
137
|
fail('gitleaks detected a potential secret. Remove it and retry the push.');
|
|
107
138
|
}
|
|
@@ -111,9 +142,9 @@ if (!hasGitleaks) {
|
|
|
111
142
|
// ── 3. vitest coverage ────────────────────────────────────────────────────────
|
|
112
143
|
|
|
113
144
|
info('Sweep 3/4 — Vitest test suite + coverage thresholds…');
|
|
114
|
-
checkBudget('
|
|
145
|
+
checkBudget(testBudget, 'Vitest');
|
|
115
146
|
|
|
116
|
-
const vitestResult = run('npx vitest run --coverage');
|
|
147
|
+
const vitestResult = run(testBudget, 'Vitest', 'npx vitest run --coverage');
|
|
117
148
|
|
|
118
149
|
if (vitestResult.status !== 0) {
|
|
119
150
|
fail('Vitest coverage gate failed — fix failing tests or low-coverage areas before pushing.');
|
|
@@ -124,7 +155,7 @@ ok('Vitest: all tests passed, coverage thresholds met.');
|
|
|
124
155
|
// ── 4. bundle size ────────────────────────────────────────────────────────────
|
|
125
156
|
|
|
126
157
|
info('Sweep 4/4 — bundle size audit…');
|
|
127
|
-
checkBudget('bundle audit');
|
|
158
|
+
checkBudget(sweepBudget, 'bundle audit');
|
|
128
159
|
|
|
129
160
|
const pkgPath = resolve(process.cwd(), 'package.json');
|
|
130
161
|
const pkg = JSON.parse(readFileSync(pkgPath, 'utf8'));
|
|
@@ -135,21 +166,21 @@ const hasBundlesize = Boolean(pkg.devDependencies?.['bundlesize']) || Boolean(pk
|
|
|
135
166
|
const hasBuildScript = Boolean(pkg.scripts?.build);
|
|
136
167
|
|
|
137
168
|
if (hasBundleSizeScript) {
|
|
138
|
-
const result = run('npm run bundle-size');
|
|
169
|
+
const result = run(sweepBudget, 'bundle-size', 'npm run bundle-size');
|
|
139
170
|
if (result.status !== 0) fail('bundle-size script reported a violation. Push blocked.');
|
|
140
171
|
ok('Bundle size: within limits.');
|
|
141
172
|
} else if (hasSizeLimit) {
|
|
142
|
-
const result = run('npx size-limit');
|
|
173
|
+
const result = run(sweepBudget, 'size-limit', 'npx size-limit');
|
|
143
174
|
if (result.status !== 0) fail('size-limit reported a violation. Push blocked.');
|
|
144
175
|
ok('Bundle size: size-limit passed.');
|
|
145
176
|
} else if (hasBundlesize) {
|
|
146
|
-
const result = run('npx bundlesize');
|
|
177
|
+
const result = run(sweepBudget, 'bundlesize', 'npx bundlesize');
|
|
147
178
|
if (result.status !== 0) fail('bundlesize reported a violation. Push blocked.');
|
|
148
179
|
ok('Bundle size: bundlesize passed.');
|
|
149
180
|
} else if (hasBuildScript) {
|
|
150
181
|
// No size tool configured — at minimum verify the build succeeds cleanly.
|
|
151
182
|
info('No size-limit/bundlesize configured — running build as a smoke-check.');
|
|
152
|
-
const result = run('npm run build');
|
|
183
|
+
const result = run(sweepBudget, 'build', 'npm run build');
|
|
153
184
|
if (result.status !== 0) fail('Build failed during gate-done. Push blocked.');
|
|
154
185
|
ok('Build: succeeded (no size budget configured).');
|
|
155
186
|
} else {
|
|
@@ -158,5 +189,9 @@ if (hasBundleSizeScript) {
|
|
|
158
189
|
|
|
159
190
|
// ── done ─────────────────────────────────────────────────────────────────────
|
|
160
191
|
|
|
161
|
-
ok(
|
|
192
|
+
ok(
|
|
193
|
+
`gate-done completed in ${elapsed()}s ` +
|
|
194
|
+
`(sweeps ${(sweepBudget.spent() / 1000).toFixed(1)}/${sweepBudget.ms / 1000}s, ` +
|
|
195
|
+
`tests ${(testBudget.spent() / 1000).toFixed(1)}/${testBudget.ms / 1000}s). Push proceeding.`,
|
|
196
|
+
);
|
|
162
197
|
process.exit(0);
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Time budgets for the gate-* hooks.
|
|
3
|
+
*
|
|
4
|
+
* A budget is a pool of milliseconds that only drains while one of its own
|
|
5
|
+
* sweeps is running, so gate-done can give the Vitest sweep its own allowance
|
|
6
|
+
* without the fast sweeps (audit, gitleaks, bundle) eating into it.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { spawnSync } from 'node:child_process';
|
|
10
|
+
|
|
11
|
+
export const DEFAULT_BUDGET_MS = 90_000;
|
|
12
|
+
export const DEFAULT_TEST_BUDGET_MS = 300_000;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Read a budget in milliseconds from `env[name]`.
|
|
16
|
+
* Unset or empty falls back to the default; anything that is not a positive
|
|
17
|
+
* integer also falls back, and `invalid` carries the rejected raw value so the
|
|
18
|
+
* caller can warn about it.
|
|
19
|
+
*/
|
|
20
|
+
export function readBudgetMs(env, name, fallback) {
|
|
21
|
+
const raw = env[name];
|
|
22
|
+
if (raw === undefined || raw.trim() === '') return { ms: fallback, source: 'default' };
|
|
23
|
+
const ms = Number(raw.trim());
|
|
24
|
+
if (!Number.isInteger(ms) || ms <= 0) return { ms: fallback, source: 'default', invalid: raw };
|
|
25
|
+
return { ms, source: 'env' };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function createBudget(name, envVar, ms, now = Date.now) {
|
|
29
|
+
let spentMs = 0;
|
|
30
|
+
return {
|
|
31
|
+
name,
|
|
32
|
+
envVar,
|
|
33
|
+
ms,
|
|
34
|
+
spent: () => spentMs,
|
|
35
|
+
remaining: () => Math.max(0, ms - spentMs),
|
|
36
|
+
exhausted: () => spentMs >= ms,
|
|
37
|
+
/** Charge the time `fn` takes to this budget and return its result. */
|
|
38
|
+
track(fn) {
|
|
39
|
+
const start = now();
|
|
40
|
+
try {
|
|
41
|
+
return fn();
|
|
42
|
+
} finally {
|
|
43
|
+
spentMs += now() - start;
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Run a shell command with the budget's remaining time as the child timeout.
|
|
51
|
+
* `timedOut` is true when the child was killed because the budget ran out,
|
|
52
|
+
* as opposed to exiting non-zero on its own.
|
|
53
|
+
*/
|
|
54
|
+
export function runInBudget(budget, cmd, options = {}) {
|
|
55
|
+
const before = budget.spent();
|
|
56
|
+
const result = budget.track(() =>
|
|
57
|
+
spawnSync(cmd, { shell: true, encoding: 'utf8', ...options, timeout: budget.remaining() }),
|
|
58
|
+
);
|
|
59
|
+
const durationMs = budget.spent() - before;
|
|
60
|
+
return { ...result, durationMs, timedOut: result.error?.code === 'ETIMEDOUT' };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Human-readable reason a sweep was killed by its budget; not a test failure. */
|
|
64
|
+
export function budgetKillMessage(label, budget, durationMs) {
|
|
65
|
+
const secs = (durationMs / 1000).toFixed(1);
|
|
66
|
+
return (
|
|
67
|
+
`${label} killed after ${secs}s by the ${budget.name} budget ` +
|
|
68
|
+
`(${budget.envVar}=${budget.ms}) — not a test failure. ` +
|
|
69
|
+
`Raise ${budget.envVar} (milliseconds) if the run is legitimately slow.`
|
|
70
|
+
);
|
|
71
|
+
}
|