Skip to content

Commit

Permalink
Async import sort-package-json
Browse files Browse the repository at this point in the history
  • Loading branch information
askoufis committed Nov 6, 2024
1 parent 5ad42b2 commit b556dab
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 38 deletions.
5 changes: 5 additions & 0 deletions .changeset/twelve-berries-reflect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@crackle/core': patch
---

Update `sort-package-json` dependency
8 changes: 5 additions & 3 deletions packages/core/src/utils/files.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import path from 'path';

import fse from 'fs-extra';
import sortPackageJson from 'sort-package-json';

import type { Format, PackageJson } from '../types';

Expand Down Expand Up @@ -64,12 +63,15 @@ export const writePackageJson = async <T extends PackageJson>({
}: {
dir: string;
contents: T;
}) =>
writeIfRequired({
}) => {
const { default: sortPackageJson } = await import('sort-package-json');

return writeIfRequired({
dir,
fileName: 'package.json',
contents: JSON.stringify(sortPackageJson(contents), null, 2).concat('\n'),
});
};

export const emptyDir = async (dir: string, skip = ['.git']): Promise<void> => {
if (!(await exists(dir))) {
Expand Down
66 changes: 36 additions & 30 deletions packages/core/src/utils/setup-package-json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ describe('diffPackageJson', () => {
diffs,
packageJson,
expectedPackageJson,
}: ReturnType<typeof diffPackageJson> & { packageJson?: PackageJson }) => {
}: Awaited<ReturnType<typeof diffPackageJson>> & {
packageJson?: PackageJson;
}) => {
expect(diffs).toMatchSnapshot('diffs');
expect(
packageJson
Expand All @@ -103,7 +105,7 @@ describe('diffPackageJson', () => {
};

test('empty package.json', async () => {
const { diffs, expectedPackageJson } = diffPackageJson(
const { diffs, expectedPackageJson } = await diffPackageJson(
packageRoot,
{},
entries,
Expand All @@ -113,17 +115,21 @@ describe('diffPackageJson', () => {
});

test('correct package.json', async () => {
const { diffs } = diffPackageJson(packageRoot, correctPackageJson, entries);
const { diffs } = await diffPackageJson(
packageRoot,
correctPackageJson,
entries,
);

expect(diffs).toHaveLength(0);
});

describe('incorrect package.json', () => {
test('main', () => {
test('main', async () => {
const packageJson = structuredClone(correctPackageJson);
packageJson.main = 'index.js';

const { diffs, expectedPackageJson } = diffPackageJson(
const { diffs, expectedPackageJson } = await diffPackageJson(
packageRoot,
packageJson,
entries,
Expand All @@ -132,11 +138,11 @@ describe('diffPackageJson', () => {
expectSnapshots({ diffs, packageJson, expectedPackageJson });
});

test('module', () => {
test('module', async () => {
const packageJson = structuredClone(correctPackageJson);
packageJson.module = 'something/else.js';

const { diffs, expectedPackageJson } = diffPackageJson(
const { diffs, expectedPackageJson } = await diffPackageJson(
packageRoot,
packageJson,
entries,
Expand All @@ -145,12 +151,12 @@ describe('diffPackageJson', () => {
expectSnapshots({ diffs, packageJson, expectedPackageJson });
});

test('types', () => {
test('types', async () => {
const packageJson = structuredClone(correctPackageJson);
// @ts-ignore
delete packageJson.types;

const { diffs, expectedPackageJson } = diffPackageJson(
const { diffs, expectedPackageJson } = await diffPackageJson(
packageRoot,
packageJson,
entries,
Expand All @@ -159,10 +165,10 @@ describe('diffPackageJson', () => {
expectSnapshots({ diffs, packageJson, expectedPackageJson });
});

test('exports missing', () => {
test('exports missing', async () => {
const { exports, ...packageJson } = structuredClone(correctPackageJson);

const { diffs, expectedPackageJson } = diffPackageJson(
const { diffs, expectedPackageJson } = await diffPackageJson(
packageRoot,
packageJson,
entries,
Expand All @@ -171,12 +177,12 @@ describe('diffPackageJson', () => {
expectSnapshots({ diffs, packageJson, expectedPackageJson });
});

test('files missing', () => {
test('files missing', async () => {
const packageJson = structuredClone(correctPackageJson);
// @ts-ignore
delete packageJson.files;

const { diffs, expectedPackageJson } = diffPackageJson(
const { diffs, expectedPackageJson } = await diffPackageJson(
packageRoot,
packageJson,
entries,
Expand All @@ -185,12 +191,12 @@ describe('diffPackageJson', () => {
expectSnapshots({ diffs, packageJson, expectedPackageJson });
});

test('files out of order', () => {
test('files out of order', async () => {
const packageJson = structuredClone(correctPackageJson);
const [first, ...otherFiles] = packageJson.files;
packageJson.files = [...otherFiles, first];

const { diffs, expectedPackageJson } = diffPackageJson(
const { diffs, expectedPackageJson } = await diffPackageJson(
packageRoot,
packageJson,
entries,
Expand All @@ -199,15 +205,15 @@ describe('diffPackageJson', () => {
expectSnapshots({ diffs, packageJson, expectedPackageJson });
});

test('keys out of order', () => {
test('keys out of order', async () => {
const { main, types, ...everythingElse } = correctPackageJson;
const packageJson = {
types,
...everythingElse,
main,
};

const { diffs, expectedPackageJson } = diffPackageJson(
const { diffs, expectedPackageJson } = await diffPackageJson(
packageRoot,
packageJson,
entries,
Expand All @@ -217,10 +223,10 @@ describe('diffPackageJson', () => {
});

describe('sideEffects', () => {
test('no flag', () => {
test('no flag', async () => {
const packageJson = structuredClone(correctPackageJson);

const { diffs, expectedPackageJson } = diffPackageJson(
const { diffs, expectedPackageJson } = await diffPackageJson(
packageRoot,
packageJson,
entries,
Expand All @@ -229,13 +235,13 @@ describe('diffPackageJson', () => {
expectSnapshots({ diffs, packageJson, expectedPackageJson });
});

test.each([true, false])('boolean flag %s', (flag) => {
test.each([true, false])('boolean flag %s', async (flag) => {
const packageJson = sortPackageJson({
...correctPackageJson,
sideEffects: flag,
});

const { diffs, expectedPackageJson } = diffPackageJson(
const { diffs, expectedPackageJson } = await diffPackageJson(
packageRoot,
packageJson,
entries,
Expand All @@ -244,15 +250,15 @@ describe('diffPackageJson', () => {
expectSnapshots({ diffs, packageJson, expectedPackageJson });
});

test('string flag', () => {
test('string flag', async () => {
const packageJson = sortPackageJson({
...correctPackageJson,
// Parcel says sideEffects can be a string
// https://parceljs.org/features/scope-hoisting/#side-effects
sideEffects: 'src/reset/**' as any,
});

const { diffs, expectedPackageJson } = diffPackageJson(
const { diffs, expectedPackageJson } = await diffPackageJson(
packageRoot,
packageJson,
entries,
Expand All @@ -261,13 +267,13 @@ describe('diffPackageJson', () => {
expectSnapshots({ diffs, packageJson, expectedPackageJson });
});

test('array flag with no overlap', () => {
test('array flag with no overlap', async () => {
const packageJson = sortPackageJson({
...correctPackageJson,
sideEffects: ['src/reset/**'],
});

const { diffs, expectedPackageJson } = diffPackageJson(
const { diffs, expectedPackageJson } = await diffPackageJson(
packageRoot,
packageJson,
entries,
Expand All @@ -276,13 +282,13 @@ describe('diffPackageJson', () => {
expectSnapshots({ diffs, packageJson, expectedPackageJson });
});

test('array flag with partial overlap', () => {
test('array flag with partial overlap', async () => {
const packageJson = sortPackageJson({
...correctPackageJson,
sideEffects: ['dist/side-effects/**', 'src/entries/css.*'],
});

const { diffs, expectedPackageJson } = diffPackageJson(
const { diffs, expectedPackageJson } = await diffPackageJson(
packageRoot,
packageJson,
entries,
Expand All @@ -291,13 +297,13 @@ describe('diffPackageJson', () => {
expectSnapshots({ diffs, packageJson, expectedPackageJson });
});

test('array flag with complete overlap', () => {
test('array flag with complete overlap', async () => {
const packageJson = sortPackageJson({
...correctPackageJson,
sideEffects: ['dist/side-effects/**', '**/themes/**'],
});

const { diffs, expectedPackageJson } = diffPackageJson(
const { diffs, expectedPackageJson } = await diffPackageJson(
packageRoot,
packageJson,
entries,
Expand Down Expand Up @@ -338,7 +344,7 @@ describe('diffPackageJson', () => {
types: 'index.d.ts',
} satisfies PackageJson;

const { diffs, expectedPackageJson } = diffPackageJson(
const { diffs, expectedPackageJson } = await diffPackageJson(
packageRoot,
packageJson,
entries,
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/utils/setup-package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { isDeepStrictEqual } from 'util';
// @ts-ignore no declaration file
import structuredClonePolyfill from '@ungap/structured-clone';
import fse from 'fs-extra';
import { sortPackageJson } from 'sort-package-json';

import { distDir, sideEffectsDir } from '../constants';
import type { PackageEntryPoint as Entry, PackageJson } from '../types';
Expand Down Expand Up @@ -132,14 +131,14 @@ const getSideEffectsForPackage = (
};
};

export const diffPackageJson = (
export const diffPackageJson = async (
packageRoot: string,
packageJson: PackageJson,
entries: Entry[],
): {
): Promise<{
diffs: Difference[];
expectedPackageJson: PackageJson;
} => {
}> => {
const diffs: Difference[] = [];

// create expected package.json
Expand Down Expand Up @@ -169,6 +168,7 @@ export const diffPackageJson = (
expected.sideEffects = sideEffects;
}

const { default: sortPackageJson } = await import('sort-package-json');
expected = sortPackageJson(expected);

// do checks against expected package.json
Expand Down Expand Up @@ -220,7 +220,7 @@ const setupPackageJson =
const packagePath = path.join(packageRoot, 'package.json');
const packageJson: PackageJson = await fse.readJson(packagePath, { fs });

const { diffs, expectedPackageJson } = diffPackageJson(
const { diffs, expectedPackageJson } = await diffPackageJson(
packageRoot,
packageJson,
entries,
Expand Down

0 comments on commit b556dab

Please sign in to comment.