Skip to content

Commit

Permalink
Don't normalize package.json fields
Browse files Browse the repository at this point in the history
  • Loading branch information
codykaup committed Jan 16, 2025
1 parent 78b3cd0 commit e9d7fa8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
4 changes: 4 additions & 0 deletions node-src/__mocks__/invalidPackageJson/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "chromatic",
"version": "invalid-semver"
}
14 changes: 13 additions & 1 deletion node-src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
});
});
4 changes: 3 additions & 1 deletion node-src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions node-src/lib/parseArguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e9d7fa8

Please sign in to comment.