From 3665d03d7d9a3c39fdb906e13e6b9b3d321ccaa3 Mon Sep 17 00:00:00 2001 From: Ejiro Pinnock Date: Sun, 15 Nov 2026 15:21:48 +0000 Subject: [PATCH 1/3] perf(upload): skip the second validation of a capture web folder, store PNGs in the zip Co-Authored-By: Claude Fable 5.2 --- .changeset/upload-skip-revalidate.md | 5 + bin/cli.js | 3 +- lib/bundle.js | 38 ++++++- lib/capture/web/index.js | 4 + lib/captureRecord.js | 99 +++++++++++++++++ lib/scf.js | 4 +- lib/uploadCommand.js | 1 + test/capture-web-integration.test.js | 5 + test/upload-skip-revalidate.test.js | 159 +++++++++++++++++++++++++++ 9 files changed, 315 insertions(+), 3 deletions(-) create mode 100644 .changeset/upload-skip-revalidate.md create mode 100644 lib/captureRecord.js create mode 100644 test/upload-skip-revalidate.test.js diff --git a/.changeset/upload-skip-revalidate.md b/.changeset/upload-skip-revalidate.md new file mode 100644 index 0000000..42986d8 --- /dev/null +++ b/.changeset/upload-skip-revalidate.md @@ -0,0 +1,5 @@ +--- +"@scrymore/scry-deployer": patch +--- + +`scry-deployer upload DIR` is faster on a folder that `capture web` just made. After capture validates its bundle it leaves a small record next to the folder (not inside it). If upload finds the folder exactly as capture left it (same files, sizes, times, scf.json, deployer and validator version) it skips its second validation and says so in the log. Anything else (no record, any change, another tool's folder, `--include-source`) runs the full validation as before, and the new `--validate` flag forces it. What is uploaded does not change. Upload also stores PNG images in the zip instead of compressing them again, which saves zip time at the cost of a slightly larger file. diff --git a/bin/cli.js b/bin/cli.js index 5c63506..2802d36 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -877,11 +877,12 @@ async function main() { .option('include-source', { describe: "Also upload each capture's component source text (code.componentFile). Off by default: Scry never needs your code", type: 'boolean', default: false }) .option('repo-root', { describe: 'Repository root that code.componentFile paths are relative to (default: cwd)', type: 'string' }) .option('dry-run', { describe: 'Validate and zip only; print where the ZIP is', type: 'boolean', default: false }) + .option('validate', { describe: 'Always run the full validation, even when capture web already validated this folder', type: 'boolean', default: false }) .option('allow-partial-bundle', { type: 'boolean', default: false, hidden: true }) .option('verbose', { describe: 'Enable verbose logging', type: 'boolean' }); }, async (argv) => { const config = loadConfig(argv); - const outcome = await runUploadBundle({ ...config, path: argv.path, includeSource: argv.includeSource, repoRoot: argv.repoRoot, source: argv.source, dryRun: argv.dryRun, allowPartialBundle: argv.allowPartialBundle }); + const outcome = await runUploadBundle({ validate: argv.validate, ...config, path: argv.path, includeSource: argv.includeSource, repoRoot: argv.repoRoot, source: argv.source, dryRun: argv.dryRun, allowPartialBundle: argv.allowPartialBundle }); process.exitCode = outcome.exitCode; }) .command('capture ', 'Capture stories into a Scry Capture Format bundle (adapters: rn, web)', (yargs) => { diff --git a/lib/bundle.js b/lib/bundle.js index a66e95c..308efd8 100644 --- a/lib/bundle.js +++ b/lib/bundle.js @@ -19,6 +19,8 @@ const { listFiles, } = require('./scf.js'); +const { checkCaptureRecord, sha256 } = require('./captureRecord.js'); + const SOURCE_TEXT_MAX_BYTES = 1024 * 1024; const UNZIP_HELPER = path.join(__dirname, 'scf-unzip.mjs'); @@ -185,6 +187,40 @@ function copySourceFile(file, root, stageDir, skipped) { return bundlePath; } +/** + * Validate the staged bundle, unless `capture web` already validated this exact folder: the input + * is a directory, it matches the record capture left (same files, sizes, times, deployer and + * validator), and staging left scf.json byte for byte as it was. Anything else, or `--validate`, + * runs the full validation. Logs which path was taken. + */ +function validateOrSkip(input, stageDir, opts) { + const { logger } = opts; + let why = 'forced by --validate'; + if (!opts.validate && opts.includeSource) why = '--include-source adds files'; + else if (!opts.validate) { + why = 'input is not a folder'; + if (fs.statSync(input).isDirectory()) { + const check = checkCaptureRecord(input); + why = check.reason; + if (check.match) { + let staged = null; + try { + staged = sha256(fs.readFileSync(path.join(stageDir, 'scf.json'))); + } catch { + staged = null; + } + if (staged === check.manifestSha256) { + logger.info('Validation skipped: capture web validated this exact folder and nothing changed (pass --validate to run it again).'); + return { ok: true, errors: [], warnings: [], manifest: null, skipped: true }; + } + why = 'staging changed scf.json'; + } + } + } + logger.info(`Running the full validation (${why}).`); + return validateBundle(stageDir); +} + /** * Stage, opt-in, validate and zip a bundle. Throws BundleRejectedError (after printing every * problem) when the validator rejects it. @@ -266,7 +302,7 @@ async function prepareBundle(input, opts) { const { legacy, sourceText, manifest } = stageManifestAndSourceText(stageDir, opts); - const validation = validateBundle(stageDir); + const validation = validateOrSkip(input, stageDir, opts); if (!validation.ok) { logger.error(`\n❌ The bundle was rejected by the Scry Capture Format validator. Nothing was uploaded.`); printValidation(validation, logger); diff --git a/lib/capture/web/index.js b/lib/capture/web/index.js index 8abb555..9c6d020 100644 --- a/lib/capture/web/index.js +++ b/lib/capture/web/index.js @@ -18,6 +18,7 @@ const { changedFiles, selectAffected, buildGraph, toProjectRelative } = require( const { captureEntry, buildManifest, writeManifest, resetOutDir, sealOutDir, assertOutDirUsable, fileStem, skippedEntry } = require('./scf-writer.js'); const { onSignals } = require('./signals.js'); const { validateBundle, printValidation } = require('../../scf.js'); +const { writeCaptureRecord } = require('../../captureRecord.js'); const toPosix = (p) => p.split(path.sep).join('/'); @@ -171,6 +172,9 @@ function finishRun({ run, selected, options, deps, sb, baseReport, timingsMs, st logger.error(`The bundle at ${options.outDir} did not pass SCF validation:`); printValidation(validation, logger); exitCode = 1; + } else { + // Lets `upload ` skip its own second validation while the folder is untouched. + writeCaptureRecord(options.outDir); } } timingsMs.write = Date.now() - writeStarted; diff --git a/lib/captureRecord.js b/lib/captureRecord.js new file mode 100644 index 0000000..3ecd8ad --- /dev/null +++ b/lib/captureRecord.js @@ -0,0 +1,99 @@ +/** + * The record `capture web` leaves next to its bundle once the validator has passed it, and the + * check `upload DIR` makes before it decides to skip a second validation. + * + * The record lives OUTSIDE the bundle (a sibling file, `.scry-validated.json`), so the bundle + * format, the zip and the service's own validation never see it. It holds a fingerprint of the + * folder: every file's relative path, size and modification time (sorted), the SHA-256 of + * scf.json, plus the deployer version and the vendored validator version. `upload` recomputes the + * fingerprint and skips validation only on an exact match. No record, any difference (a file + * added, removed, resized, touched, a different deployer or validator) means the full validation + * runs as before. The fingerprint is a stat walk plus one small file read, a fraction of a second. + */ +const fs = require('fs'); +const path = require('path'); +const crypto = require('crypto'); +const { listFiles, vendoredScfVersion } = require('./scf.js'); + +const RECORD_VERSION = 1; + +function deployerVersion() { + try { + return require('../package.json').version; + } catch { + return 'unknown'; + } +} + +/** `.scry-validated.json`, next to (never inside) the bundle folder. */ +function recordPathFor(dir) { + return `${path.resolve(dir)}.scry-validated.json`; +} + +function sha256(buf) { + return crypto.createHash('sha256').update(buf).digest('hex'); +} + +/** + * Fingerprint a bundle folder. + * @returns {{digest:string, files:number, manifestSha256:string|null}} + */ +function fingerprintDir(dir) { + const files = listFiles(dir); + const hash = crypto.createHash('sha256'); + for (const rel of files) { + const st = fs.statSync(path.join(dir, ...rel.split('/'))); + hash.update(`${rel}\0${st.size}\0${Math.floor(st.mtimeMs)}\n`); + } + let manifestSha256 = null; + try { + manifestSha256 = sha256(fs.readFileSync(path.join(dir, 'scf.json'))); + } catch { + manifestSha256 = null; + } + return { digest: hash.digest('hex'), files: files.length, manifestSha256 }; +} + +function expectedStamp() { + return { version: RECORD_VERSION, deployer: deployerVersion(), validator: vendoredScfVersion() }; +} + +/** Write the record after the validator passed `dir`. Never throws: a failed write only costs a later validation. */ +function writeCaptureRecord(dir) { + try { + const record = { ...expectedStamp(), ...fingerprintDir(dir) }; + fs.writeFileSync(recordPathFor(dir), `${JSON.stringify(record, null, 2)}\n`); + return true; + } catch { + return false; + } +} + +/** + * Does `dir` match the record capture left for it? + * @returns {{match:boolean, reason:string, manifestSha256?:string}} + */ +function checkCaptureRecord(dir) { + let record; + try { + record = JSON.parse(fs.readFileSync(recordPathFor(dir), 'utf8')); + } catch { + return { match: false, reason: 'no capture record' }; + } + const stamp = expectedStamp(); + if (!record || record.version !== stamp.version || record.deployer !== stamp.deployer || record.validator !== stamp.validator) { + return { match: false, reason: 'record is from another deployer or validator version' }; + } + let now; + try { + now = fingerprintDir(dir); + } catch { + return { match: false, reason: 'folder could not be read' }; + } + if (!record.manifestSha256 || record.digest !== now.digest || record.files !== now.files || record.manifestSha256 !== now.manifestSha256) { + return { match: false, reason: 'folder changed since capture validated it' }; + } + return { match: true, reason: 'matches the capture record', manifestSha256: now.manifestSha256 }; +} + +module.exports = { recordPathFor, fingerprintDir, writeCaptureRecord, checkCaptureRecord, sha256 }; diff --git a/lib/scf.js b/lib/scf.js index 8900205..ad54101 100644 --- a/lib/scf.js +++ b/lib/scf.js @@ -141,7 +141,9 @@ function zipBundleDir(dir, outPath) { output.on('close', () => resolve({ bytes: fs.statSync(outPath).size, members })); archive.pipe(output); for (const rel of members) { - archive.file(path.join(dir, ...rel.split('/')), { name: rel }); + // PNG data is already compressed: deflate level 9 costs seconds and saves almost nothing, so + // store those members (zip method 0, which the upload service reads). Text and JSON are deflated. + archive.file(path.join(dir, ...rel.split('/')), { name: rel, store: /\.png$/i.test(rel) }); } archive.finalize().catch(reject); }); diff --git a/lib/uploadCommand.js b/lib/uploadCommand.js index 3014dcc..fab9928 100644 --- a/lib/uploadCommand.js +++ b/lib/uploadCommand.js @@ -30,6 +30,7 @@ async function prepareOrFail(input, argv, logger) { try { return await prepareBundle(input, { includeSource: Boolean(argv.includeSource), + validate: Boolean(argv.validate), repoRoot: argv.repoRoot, logger, }); diff --git a/test/capture-web-integration.test.js b/test/capture-web-integration.test.js index 896525c..89ca76f 100644 --- a/test/capture-web-integration.test.js +++ b/test/capture-web-integration.test.js @@ -109,6 +109,11 @@ maybe('capture web on the fixture project', () => { expect(report).toBeDefined(); }); + test('leaves a capture record next to the folder that matches it, so upload can skip a second validation', () => { + const { checkCaptureRecord } = require('../lib/captureRecord.js'); + expect(checkCaptureRecord(out)).toMatchObject({ match: true }); + }); + test('counts are honest: 15 declared, 13 captured, 2 skipped with reasons', () => { expect(manifest.counts.declared).toBe(15); expect(manifest.counts.captured).toBe(13); diff --git a/test/upload-skip-revalidate.test.js b/test/upload-skip-revalidate.test.js new file mode 100644 index 0000000..61249ea --- /dev/null +++ b/test/upload-skip-revalidate.test.js @@ -0,0 +1,159 @@ +/** + * `upload DIR` skips its second validation only when it can prove the folder is exactly what + * `capture web` validated (a record next to the folder), and the zip stores PNG members. + */ +const fs = require('fs'); +const os = require('os'); +const path = require('path'); +const { spawnSync } = require('child_process'); +const { runUploadBundle } = require('../lib/uploadCommand.js'); +const { writeCaptureRecord, checkCaptureRecord, recordPathFor } = require('../lib/captureRecord.js'); +const { encodePng } = require('../lib/capture/png.js'); + +const UNZIP = path.join(__dirname, '..', 'lib', 'scf-unzip.mjs'); + +function logger() { + const lines = []; + const rec = (level) => (m) => lines.push(`${level}: ${m}`); + return { lines, info: rec('info'), warn: rec('warn'), error: rec('error'), success: rec('success'), debug: () => {} }; +} + +function makeBundle() { + const root = fs.mkdtempSync(path.join(os.tmpdir(), 'scry-skip-test-')); + const bundle = path.join(root, 'capture'); + fs.mkdirSync(path.join(bundle, 'images'), { recursive: true }); + const png = encodePng({ width: 4, height: 4, data: Buffer.alloc(64, 120) }); + fs.writeFileSync(path.join(bundle, 'images/0001.png'), png); + const manifest = { + scf: '1.0', + source: { kind: 'storybook', platform: 'web', tool: { name: 'scry-deployer capture web' } }, + counts: { declared: 1, captured: 1, skipped: [] }, + captures: [{ id: 'a--b', image: 'images/0001.png', kind: 'component' }], + }; + fs.writeFileSync(path.join(bundle, 'scf.json'), `${JSON.stringify(manifest, null, 2)}\n`); + return { root, bundle }; +} + +const skipped = (l) => l.lines.some((x) => x.includes('Validation skipped')); +const ranFull = (l) => l.lines.some((x) => x.includes('Running the full validation')); + +describe('skip the second validation', () => { + test('exact match skips, and says so', async () => { + const { bundle } = makeBundle(); + expect(writeCaptureRecord(bundle)).toBe(true); + const l = logger(); + const res = await runUploadBundle({ path: bundle, dryRun: true }, { logger: l }); + expect(res.exitCode).toBe(0); + expect(skipped(l)).toBe(true); + expect(ranFull(l)).toBe(false); + }); + + test('--validate forces the full validation', async () => { + const { bundle } = makeBundle(); + writeCaptureRecord(bundle); + const l = logger(); + const res = await runUploadBundle({ path: bundle, dryRun: true, validate: true }, { logger: l }); + expect(res.exitCode).toBe(0); + expect(skipped(l)).toBe(false); + expect(l.lines.join('\n')).toContain('forced by --validate'); + }); + + test('no record forces validation', async () => { + const { bundle } = makeBundle(); + const l = logger(); + await runUploadBundle({ path: bundle, dryRun: true }, { logger: l }); + expect(skipped(l)).toBe(false); + expect(l.lines.join('\n')).toContain('no capture record'); + }); + + test('a changed file size forces validation', async () => { + const { bundle } = makeBundle(); + writeCaptureRecord(bundle); + fs.appendFileSync(path.join(bundle, 'images/0001.png'), Buffer.from([0])); + const l = logger(); + await runUploadBundle({ path: bundle, dryRun: true }, { logger: l }); + expect(skipped(l)).toBe(false); + expect(ranFull(l)).toBe(true); + }); + + test('a changed file list forces validation (and the validator still rejects what is wrong)', async () => { + const { bundle } = makeBundle(); + writeCaptureRecord(bundle); + fs.rmSync(path.join(bundle, 'images/0001.png')); + const l = logger(); + const res = await runUploadBundle({ path: bundle, dryRun: true }, { logger: l }); + expect(res.exitCode).toBe(1); + expect(skipped(l)).toBe(false); + }); + + test('same size, new content in scf.json forces validation', () => { + const { bundle } = makeBundle(); + writeCaptureRecord(bundle); + const p = path.join(bundle, 'scf.json'); + const text = fs.readFileSync(p, 'utf8').replace('a--b', 'a--c'); + const st = fs.statSync(p); + fs.writeFileSync(p, text); + fs.utimesSync(p, st.atime, st.mtime); + expect(checkCaptureRecord(bundle).match).toBe(false); + }); + + test('a record from another deployer version forces validation', () => { + const { bundle } = makeBundle(); + writeCaptureRecord(bundle); + const rp = recordPathFor(bundle); + const rec = JSON.parse(fs.readFileSync(rp, 'utf8')); + rec.deployer = '0.0.0'; + fs.writeFileSync(rp, JSON.stringify(rec)); + expect(checkCaptureRecord(bundle)).toMatchObject({ match: false }); + }); + + test('--include-source always validates', async () => { + const { bundle } = makeBundle(); + writeCaptureRecord(bundle); + const l = logger(); + await runUploadBundle({ path: bundle, dryRun: true, includeSource: true, repoRoot: bundle }, { logger: l }); + expect(skipped(l)).toBe(false); + expect(ranFull(l)).toBe(true); + }); + + test('the record is outside the bundle: not in the zip, not in the folder', async () => { + const { bundle } = makeBundle(); + writeCaptureRecord(bundle); + expect(fs.readdirSync(bundle).sort()).toEqual(['images', 'scf.json']); + expect(recordPathFor(bundle).startsWith(`${bundle}${path.sep}`)).toBe(false); + }); +}); + +describe('zip members', () => { + function methods(zipPath) { + const buf = fs.readFileSync(zipPath); + let i = buf.length - 22; + while (buf.readUInt32LE(i) !== 0x06054b50) i--; + const n = buf.readUInt16LE(i + 10); + let off = buf.readUInt32LE(i + 16); + const out = {}; + for (let k = 0; k < n; k++) { + const nameLen = buf.readUInt16LE(off + 28); + const extra = buf.readUInt16LE(off + 30); + const comment = buf.readUInt16LE(off + 32); + out[buf.toString('utf8', off + 46, off + 46 + nameLen)] = buf.readUInt16LE(off + 10); + off += 46 + nameLen + extra + comment; + } + return out; + } + + test('PNG members are stored (method 0), JSON is deflated (8), and the zip round-trips', async () => { + const { bundle } = makeBundle(); + const l = logger(); + const res = await runUploadBundle({ path: bundle, dryRun: true }, { logger: l }); + expect(res.exitCode).toBe(0); + const m = methods(res.prepared.zipPath); + expect(m['images/0001.png']).toBe(0); + expect(m['scf.json']).toBe(8); + const out = fs.mkdtempSync(path.join(os.tmpdir(), 'scry-skip-unzip-')); + const r = spawnSync(process.execPath, [UNZIP, res.prepared.zipPath, out], { encoding: 'utf8' }); + expect(r.status).toBe(0); + expect(fs.readFileSync(path.join(out, 'images/0001.png')).equals(fs.readFileSync(path.join(bundle, 'images/0001.png')))).toBe(true); + expect(fs.readFileSync(path.join(out, 'scf.json'), 'utf8')).toBe(fs.readFileSync(path.join(bundle, 'scf.json'), 'utf8')); + }); +}); From 5ab4a93cdf8ef3187827c57095af17bef15be292 Mon Sep 17 00:00:00 2001 From: Ejiro Pinnock Date: Sun, 15 Nov 2026 15:23:55 +0000 Subject: [PATCH 2/3] fix(upload): clear the capture record on reset, keep --validate over config, tests Co-Authored-By: Claude Fable 5.2 --- bin/cli.js | 2 +- lib/capture/web/scf-writer.js | 3 +++ lib/scf.js | 4 ++-- test/upload-skip-revalidate.test.js | 31 +++++++++++++++++++++++++++++ 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/bin/cli.js b/bin/cli.js index 2802d36..ddbe61c 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -882,7 +882,7 @@ async function main() { .option('verbose', { describe: 'Enable verbose logging', type: 'boolean' }); }, async (argv) => { const config = loadConfig(argv); - const outcome = await runUploadBundle({ validate: argv.validate, ...config, path: argv.path, includeSource: argv.includeSource, repoRoot: argv.repoRoot, source: argv.source, dryRun: argv.dryRun, allowPartialBundle: argv.allowPartialBundle }); + const outcome = await runUploadBundle({ ...config, validate: argv.validate, path: argv.path, includeSource: argv.includeSource, repoRoot: argv.repoRoot, source: argv.source, dryRun: argv.dryRun, allowPartialBundle: argv.allowPartialBundle }); process.exitCode = outcome.exitCode; }) .command('capture ', 'Capture stories into a Scry Capture Format bundle (adapters: rn, web)', (yargs) => { diff --git a/lib/capture/web/scf-writer.js b/lib/capture/web/scf-writer.js index 026efea..a9dcfb8 100644 --- a/lib/capture/web/scf-writer.js +++ b/lib/capture/web/scf-writer.js @@ -9,6 +9,7 @@ const fs = require('fs'); const path = require('path'); const { SCF_SCHEMA_URL } = require('../../scf.js'); +const { recordPathFor } = require('../../captureRecord.js'); const { CaptureWebError } = require('./storybook-config.js'); const TOOL_NAME = 'scry-deployer capture web'; @@ -273,6 +274,8 @@ function resetOutDir(outDir) { removeFiles(path.join(outDir, 'images'), '.png'); removeFiles(path.join(outDir, 'structure'), '.json'); fs.rmSync(path.join(outDir, 'scf.json'), { force: true }); + // An old record must not outlive a re-capture that then fails validation. + fs.rmSync(recordPathFor(outDir), { force: true }); fs.mkdirSync(path.join(outDir, 'images'), { recursive: true }); fs.mkdirSync(path.join(outDir, 'structure'), { recursive: true }); } diff --git a/lib/scf.js b/lib/scf.js index ad54101..4153da7 100644 --- a/lib/scf.js +++ b/lib/scf.js @@ -142,8 +142,8 @@ function zipBundleDir(dir, outPath) { archive.pipe(output); for (const rel of members) { // PNG data is already compressed: deflate level 9 costs seconds and saves almost nothing, so - // store those members (zip method 0, which the upload service reads). Text and JSON are deflated. - archive.file(path.join(dir, ...rel.split('/')), { name: rel, store: /\.png$/i.test(rel) }); + // store those members (zip method 0, which the upload service reads). Text and JSON are deflated. + archive.file(path.join(dir, ...rel.split('/')), { name: rel, store: /\.png$/i.test(rel) }); } archive.finalize().catch(reject); }); diff --git a/test/upload-skip-revalidate.test.js b/test/upload-skip-revalidate.test.js index 61249ea..cc36671 100644 --- a/test/upload-skip-revalidate.test.js +++ b/test/upload-skip-revalidate.test.js @@ -124,6 +124,37 @@ describe('skip the second validation', () => { }); }); +describe('bad records fall back to full validation', () => { + test('malformed JSON record', async () => { + const { bundle } = makeBundle(); + fs.writeFileSync(recordPathFor(bundle), '{not json'); + const l = logger(); + const res = await runUploadBundle({ path: bundle, dryRun: true }, { logger: l }); + expect(res.exitCode).toBe(0); + expect(skipped(l)).toBe(false); + expect(ranFull(l)).toBe(true); + }); + + test('unreadable record (a directory in its place)', async () => { + const { bundle } = makeBundle(); + fs.mkdirSync(recordPathFor(bundle)); + const l = logger(); + const res = await runUploadBundle({ path: bundle, dryRun: true }, { logger: l }); + expect(res.exitCode).toBe(0); + expect(skipped(l)).toBe(false); + expect(ranFull(l)).toBe(true); + }); + + test('resetOutDir removes an old record', () => { + const { resetOutDir } = require('../lib/capture/web/scf-writer.js'); + const { bundle } = makeBundle(); + writeCaptureRecord(bundle); + expect(fs.existsSync(recordPathFor(bundle))).toBe(true); + resetOutDir(bundle); + expect(fs.existsSync(recordPathFor(bundle))).toBe(false); + }); +}); + describe('zip members', () => { function methods(zipPath) { const buf = fs.readFileSync(zipPath); From 1b5792e6d01bf19a3ac82293ddabdb88430026c7 Mon Sep 17 00:00:00 2001 From: Ejiro Pinnock Date: Sun, 15 Nov 2026 15:31:17 +0000 Subject: [PATCH 3/3] lint: split validateOrSkip to stay under the complexity warning Co-Authored-By: Claude Fable 5.2 --- lib/bundle.js | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/lib/bundle.js b/lib/bundle.js index 308efd8..09e0d62 100644 --- a/lib/bundle.js +++ b/lib/bundle.js @@ -193,31 +193,29 @@ function copySourceFile(file, root, stageDir, skipped) { * validator), and staging left scf.json byte for byte as it was. Anything else, or `--validate`, * runs the full validation. Logs which path was taken. */ +function skipReason(input, stageDir, opts) { + if (opts.validate) return { why: 'forced by --validate' }; + if (opts.includeSource) return { why: '--include-source adds files' }; + if (!fs.statSync(input).isDirectory()) return { why: 'input is not a folder' }; + const check = checkCaptureRecord(input); + if (!check.match) return { why: check.reason }; + let staged = null; + try { + staged = sha256(fs.readFileSync(path.join(stageDir, 'scf.json'))); + } catch { + staged = null; + } + return staged === check.manifestSha256 ? { skip: true } : { why: 'staging changed scf.json' }; +} + function validateOrSkip(input, stageDir, opts) { const { logger } = opts; - let why = 'forced by --validate'; - if (!opts.validate && opts.includeSource) why = '--include-source adds files'; - else if (!opts.validate) { - why = 'input is not a folder'; - if (fs.statSync(input).isDirectory()) { - const check = checkCaptureRecord(input); - why = check.reason; - if (check.match) { - let staged = null; - try { - staged = sha256(fs.readFileSync(path.join(stageDir, 'scf.json'))); - } catch { - staged = null; - } - if (staged === check.manifestSha256) { - logger.info('Validation skipped: capture web validated this exact folder and nothing changed (pass --validate to run it again).'); - return { ok: true, errors: [], warnings: [], manifest: null, skipped: true }; - } - why = 'staging changed scf.json'; - } - } + const verdict = skipReason(input, stageDir, opts); + if (verdict.skip) { + logger.info('Validation skipped: capture web validated this exact folder and nothing changed (pass --validate to run it again).'); + return { ok: true, errors: [], warnings: [], manifest: null, skipped: true }; } - logger.info(`Running the full validation (${why}).`); + logger.info(`Running the full validation (${verdict.why}).`); return validateBundle(stageDir); }