Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(js): sort package.json fields by idiomatic order #29635

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/expo/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,16 @@ describe('app', () => {
},
]
`);
// Make sure keys are in idiomatic order
expect(Object.keys(readJson(tree, 'my-app/package.json')))
.toMatchInlineSnapshot(`
[
"name",
"version",
"private",
"nx",
]
`);
expect(readJson(tree, 'my-app/tsconfig.json')).toMatchInlineSnapshot(`
{
"extends": "../tsconfig.base.json",
Expand Down
3 changes: 3 additions & 0 deletions packages/expo/src/generators/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { Schema } from './schema';
import { ensureDependencies } from '../../utils/ensure-dependencies';
import { initRootBabelConfig } from '../../utils/init-root-babel-config';
import { logShowProjectCommand } from '@nx/devkit/src/utils/log-show-project-command';
import { sortPackageJsonFields } from '@nx/js/src/utils/package-json/sort-fields';

export async function expoApplicationGenerator(
host: Tree,
Expand Down Expand Up @@ -104,6 +105,8 @@ export async function expoApplicationGeneratorInternal(
addProjectToTsSolutionWorkspace(host, options.appProjectRoot);
}

sortPackageJsonFields(host, options.appProjectRoot);

if (!options.skipFormat) {
await formatFiles(host);
}
Expand Down
141 changes: 141 additions & 0 deletions packages/expo/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
readProjectConfiguration,
Tree,
updateJson,
writeJson,
} from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { Linter } from '@nx/eslint';
Expand Down Expand Up @@ -468,4 +469,144 @@ describe('lib', () => {
).not.toBeDefined();
});
});

describe('TS solution setup', () => {
beforeEach(() => {
updateJson(appTree, 'package.json', (json) => {
json.workspaces = ['packages/*', 'apps/*'];
return json;
});
writeJson(appTree, 'tsconfig.base.json', {
compilerOptions: {
composite: true,
declaration: true,
},
});
writeJson(appTree, 'tsconfig.json', {
extends: './tsconfig.base.json',
files: [],
references: [],
});
});

it('should add project references when using TS solution', async () => {
await expoLibraryGenerator(appTree, {
...defaultSchema,
strict: false,
});

expect(readJson(appTree, 'tsconfig.json').references)
.toMatchInlineSnapshot(`
[
{
"path": "./my-lib",
},
]
`);
// Make sure keys are in idiomatic order
expect(Object.keys(readJson(appTree, 'my-lib/package.json')))
.toMatchInlineSnapshot(`
[
"name",
"version",
"main",
"types",
"nx",
]
`);
expect(readJson(appTree, 'my-lib/tsconfig.json')).toMatchInlineSnapshot(`
{
"extends": "../tsconfig.base.json",
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json",
},
{
"path": "./tsconfig.spec.json",
},
],
}
`);
expect(readJson(appTree, 'my-lib/tsconfig.lib.json'))
.toMatchInlineSnapshot(`
{
"compilerOptions": {
"jsx": "react-jsx",
"module": "esnext",
"moduleResolution": "bundler",
"outDir": "out-tsc/my-lib",
"rootDir": "src",
"tsBuildInfoFile": "out-tsc/my-lib/tsconfig.lib.tsbuildinfo",
"types": [
"node",
],
},
"exclude": [
"out-tsc",
"dist",
"**/*.test.ts",
"**/*.spec.ts",
"**/*.test.tsx",
"**/*.spec.tsx",
"**/*.test.js",
"**/*.spec.js",
"**/*.test.jsx",
"**/*.spec.jsx",
"src/test-setup.ts",
"jest.config.ts",
"src/**/*.spec.ts",
"src/**/*.test.ts",
"eslint.config.js",
"eslint.config.cjs",
"eslint.config.mjs",
],
"extends": "../tsconfig.base.json",
"include": [
"**/*.js",
"**/*.jsx",
"**/*.ts",
"**/*.tsx",
],
}
`);
expect(readJson(appTree, 'my-lib/tsconfig.spec.json'))
.toMatchInlineSnapshot(`
{
"compilerOptions": {
"jsx": "react-jsx",
"module": "esnext",
"moduleResolution": "bundler",
"outDir": "./out-tsc/jest",
"types": [
"jest",
"node",
],
},
"extends": "../tsconfig.base.json",
"files": [
"src/test-setup.ts",
],
"include": [
"jest.config.ts",
"src/**/*.test.ts",
"src/**/*.spec.ts",
"src/**/*.test.tsx",
"src/**/*.spec.tsx",
"src/**/*.test.js",
"src/**/*.spec.js",
"src/**/*.test.jsx",
"src/**/*.spec.jsx",
"src/**/*.d.ts",
],
"references": [
{
"path": "./tsconfig.lib.json",
},
],
}
`);
});
});
});
3 changes: 3 additions & 0 deletions packages/expo/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
updateTsconfigFiles,
} from '@nx/js/src/utils/typescript/ts-solution-setup';
import { getImportPath } from '@nx/js/src/utils/get-import-path';
import { sortPackageJsonFields } from '@nx/js/src/utils/package-json/sort-fields';

export async function expoLibraryGenerator(
host: Tree,
Expand Down Expand Up @@ -137,6 +138,8 @@ export async function expoLibraryGeneratorInternal(
addProjectToTsSolutionWorkspace(host, options.projectRoot);
}

sortPackageJsonFields(host, options.projectRoot);

if (!options.skipFormat) {
await formatFiles(host);
}
Expand Down
10 changes: 10 additions & 0 deletions packages/express/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,16 @@ describe('app', () => {
},
]
`);
// Make sure keys are in idiomatic order
expect(Object.keys(readJson(appTree, 'myapp/package.json')))
.toMatchInlineSnapshot(`
[
"name",
"version",
"private",
"nx",
]
`);
expect(readJson(appTree, 'myapp/package.json')).toMatchInlineSnapshot(`
{
"name": "@proj/myapp",
Expand Down
14 changes: 14 additions & 0 deletions packages/js/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1694,6 +1694,20 @@ describe('lib', () => {
linter: 'none',
});

// Make sure keys are in idiomatic order
expect(Object.keys(readJson(tree, 'my-ts-lib/package.json')))
.toMatchInlineSnapshot(`
[
"name",
"version",
"private",
"type",
"main",
"types",
"exports",
"dependencies",
]
`);
expect(readJson(tree, 'my-ts-lib/package.json')).toMatchInlineSnapshot(`
{
"dependencies": {},
Expand Down
3 changes: 3 additions & 0 deletions packages/js/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import type {
LibraryGeneratorSchema,
NormalizedLibraryGeneratorOptions,
} from './schema';
import { sortPackageJsonFields } from '../../utils/package-json/sort-fields';

const defaultOutputDirectory = 'dist';

Expand Down Expand Up @@ -222,6 +223,8 @@ export async function libraryGeneratorInternal(
addProjectToTsSolutionWorkspace(tree, options.projectRoot);
}

sortPackageJsonFields(tree, options.projectRoot);

if (!options.skipFormat) {
await formatFiles(tree);
}
Expand Down
41 changes: 41 additions & 0 deletions packages/js/src/utils/package-json/sort-fields.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { joinPathFragments, updateJson, type Tree } from '@nx/devkit';

export function sortPackageJsonFields(tree: Tree, projectRoot: string) {
const packageJsonPath = joinPathFragments(projectRoot, 'package.json');
if (!tree.exists(packageJsonPath)) return;
updateJson(tree, packageJsonPath, (json) => {
// Note that these are fields that our generators may use, so it's not exhaustive.
const orderedTopFields = new Set([
'name',
'version',
'private',
'description',
'type',
'main',
'module',
'types',
'exported',
]);
const orderedBottomFields = new Set([
'dependencies',
'devDependencies',
'peerDependencies',
'optionalDependencies',
]);
const otherFields = new Set(
Object.keys(json).filter(
(k) => !orderedTopFields.has(k) && !orderedBottomFields.has(k)
)
);
const allFields = [
...orderedTopFields,
...otherFields,
...orderedBottomFields,
];
const sortedJson = {};
for (const k of allFields) {
sortedJson[k] = json[k];
}
return sortedJson;
});
}
11 changes: 11 additions & 0 deletions packages/next/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,17 @@ describe('app (legacy)', () => {
},
]
`);
// Make sure keys are in idiomatic order
expect(Object.keys(readJson(tree, 'myapp/package.json')))
.toMatchInlineSnapshot(`
[
"name",
"version",
"private",
"nx",
"dependencies",
]
`);
expect(readJson(tree, 'myapp/tsconfig.json')).toMatchInlineSnapshot(`
{
"compilerOptions": {
Expand Down
3 changes: 3 additions & 0 deletions packages/next/src/generators/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
addProjectToTsSolutionWorkspace,
updateTsconfigFiles,
} from '@nx/js/src/utils/typescript/ts-solution-setup';
import { sortPackageJsonFields } from '@nx/js/src/utils/package-json/sort-fields';

export async function applicationGenerator(host: Tree, schema: Schema) {
return await applicationGeneratorInternal(host, {
Expand Down Expand Up @@ -141,6 +142,8 @@ export async function applicationGeneratorInternal(host: Tree, schema: Schema) {
addProjectToTsSolutionWorkspace(host, options.appProjectRoot);
}

sortPackageJsonFields(host, options.appProjectRoot);

if (!options.skipFormat) {
await formatFiles(host);
}
Expand Down
Loading
Loading