From e9d7fa8a68a2e981d923a21be061ec540e60c199 Mon Sep 17 00:00:00 2001 From: Cody Kaup Date: Thu, 16 Jan 2025 12:30:31 -0600 Subject: [PATCH] Don't normalize package.json fields --- node-src/__mocks__/invalidPackageJson/package.json | 4 ++++ node-src/index.test.ts | 14 +++++++++++++- node-src/index.ts | 4 +++- node-src/lib/parseArguments.ts | 5 +++++ 4 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 node-src/__mocks__/invalidPackageJson/package.json diff --git a/node-src/__mocks__/invalidPackageJson/package.json b/node-src/__mocks__/invalidPackageJson/package.json new file mode 100644 index 000000000..551b78fff --- /dev/null +++ b/node-src/__mocks__/invalidPackageJson/package.json @@ -0,0 +1,4 @@ +{ + "name": "chromatic", + "version": "invalid-semver" +} diff --git a/node-src/index.test.ts b/node-src/index.test.ts index 6828507aa..81a9a8506 100644 --- a/node-src/index.test.ts +++ b/node-src/index.test.ts @@ -4,10 +4,11 @@ import { execaCommand as execaDefault } from 'execa'; import jsonfile from 'jsonfile'; import { confirm } from 'node-ask'; import fetchDefault from 'node-fetch'; +import path from 'path'; import { Readable } from 'stream'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; -import { getGitInfo, runAll } from '.'; +import { getGitInfo, run, runAll } from '.'; import * as git from './git/git'; import { DNSResolveAgent } from './io/getDNSResolveAgent'; import * as checkPackageJson from './lib/checkPackageJson'; @@ -853,3 +854,14 @@ describe('getGitInfo', () => { }); }); }); + +describe('parsing package.json', () => { + it('should handle invalid `version` strings', async () => { + vi.spyOn(process, 'cwd').mockReturnValue( + path.resolve('./node-src/__mocks__/invalidPackageJson') + ); + + const result = await run({ flags: { dryRun: true } }); + expect(result.code).toBeDefined(); + }); +}); diff --git a/node-src/index.ts b/node-src/index.ts index e6cd82e76..d14111e95 100644 --- a/node-src/index.ts +++ b/node-src/index.ts @@ -116,7 +116,9 @@ export async function run({ log = createLogger(config.flags, config.extraOptions), } = extraOptions || {}; - const packageInfo = await readPackageUp({ cwd: process.cwd() }); + // We don't normalize because if the `version` field isn't a proper semver string, the process + // silently exits. + const packageInfo = await readPackageUp({ cwd: process.cwd(), normalize: false }); if (!packageInfo) { log.error(noPackageJson()); process.exit(253); diff --git a/node-src/lib/parseArguments.ts b/node-src/lib/parseArguments.ts index 573dcee80..0181f26ee 100644 --- a/node-src/lib/parseArguments.ts +++ b/node-src/lib/parseArguments.ts @@ -76,6 +76,11 @@ export default function parseArguments(argv: string[]) { argv, booleanDefault: undefined, description: false, + + // Prevent meow from parsing the project's package.json file because if the `version` field + // isn't a proper semver string, the process silently exits. + pkg: {}, + version: pkg.version, flags: { // Required options