fix(js): sort package.json fields by idiomatic order (#29635)
This PR updates our app/lib generators such that `package.json` files
generated have fields in idiomatic order.
e.g.
```json
{
"name": "...",
"version": "...",
"private": true,
"type": "module",
"main": "...",
...
"dependencies": { ... }
}
```
The import fields such as name, version, private, and type are at the
top. Dep fields that could be noisy are at the bottom.
## Current Behavior
<!-- This is the behavior we have today -->
## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->
## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->
Fixes #
This commit is contained in:
parent
f9c306af28
commit
cc441a6dc7
@ -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",
|
||||
|
||||
@ -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,
|
||||
@ -104,6 +105,8 @@ export async function expoApplicationGeneratorInternal(
|
||||
addProjectToTsSolutionWorkspace(host, options.appProjectRoot);
|
||||
}
|
||||
|
||||
sortPackageJsonFields(host, options.appProjectRoot);
|
||||
|
||||
if (!options.skipFormat) {
|
||||
await formatFiles(host);
|
||||
}
|
||||
|
||||
@ -6,6 +6,7 @@ import {
|
||||
readProjectConfiguration,
|
||||
Tree,
|
||||
updateJson,
|
||||
writeJson,
|
||||
} from '@nx/devkit';
|
||||
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
|
||||
import { Linter } from '@nx/eslint';
|
||||
@ -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",
|
||||
},
|
||||
],
|
||||
}
|
||||
`);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -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,
|
||||
@ -137,6 +138,8 @@ export async function expoLibraryGeneratorInternal(
|
||||
addProjectToTsSolutionWorkspace(host, options.projectRoot);
|
||||
}
|
||||
|
||||
sortPackageJsonFields(host, options.projectRoot);
|
||||
|
||||
if (!options.skipFormat) {
|
||||
await formatFiles(host);
|
||||
}
|
||||
|
||||
@ -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",
|
||||
|
||||
@ -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": {},
|
||||
|
||||
@ -67,6 +67,7 @@ import type {
|
||||
LibraryGeneratorSchema,
|
||||
NormalizedLibraryGeneratorOptions,
|
||||
} from './schema';
|
||||
import { sortPackageJsonFields } from '../../utils/package-json/sort-fields';
|
||||
|
||||
const defaultOutputDirectory = 'dist';
|
||||
|
||||
@ -222,6 +223,8 @@ export async function libraryGeneratorInternal(
|
||||
addProjectToTsSolutionWorkspace(tree, options.projectRoot);
|
||||
}
|
||||
|
||||
sortPackageJsonFields(tree, options.projectRoot);
|
||||
|
||||
if (!options.skipFormat) {
|
||||
await formatFiles(tree);
|
||||
}
|
||||
|
||||
41
packages/js/src/utils/package-json/sort-fields.ts
Normal file
41
packages/js/src/utils/package-json/sort-fields.ts
Normal file
@ -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;
|
||||
});
|
||||
}
|
||||
@ -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": {
|
||||
|
||||
@ -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, {
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import { installedCypressVersion } from '@nx/cypress/src/utils/cypress-version';
|
||||
import { readJson } from '@nx/devkit';
|
||||
import { readJson, Tree, updateJson, writeJson } from '@nx/devkit';
|
||||
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
|
||||
import { Linter } from '@nx/eslint';
|
||||
import libraryGenerator from './library';
|
||||
@ -113,4 +113,148 @@ describe('next library', () => {
|
||||
readJson(appTree, 'package.json').devDependencies['cypress']
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
describe('TS solution setup', () => {
|
||||
let tree: Tree;
|
||||
|
||||
beforeEach(() => {
|
||||
tree = createTreeWithEmptyWorkspace();
|
||||
updateJson(tree, 'package.json', (json) => {
|
||||
json.workspaces = ['packages/*', 'apps/*'];
|
||||
return json;
|
||||
});
|
||||
writeJson(tree, 'tsconfig.base.json', {
|
||||
compilerOptions: {
|
||||
composite: true,
|
||||
declaration: true,
|
||||
},
|
||||
});
|
||||
writeJson(tree, 'tsconfig.json', {
|
||||
extends: './tsconfig.base.json',
|
||||
files: [],
|
||||
references: [],
|
||||
});
|
||||
});
|
||||
|
||||
it('should add project references when using TS solution', async () => {
|
||||
await libraryGenerator(tree, {
|
||||
directory: 'mylib',
|
||||
linter: Linter.EsLint,
|
||||
skipFormat: false,
|
||||
skipTsConfig: false,
|
||||
unitTestRunner: 'jest',
|
||||
style: 'css',
|
||||
component: false,
|
||||
});
|
||||
|
||||
expect(readJson(tree, 'tsconfig.json').references).toMatchInlineSnapshot(`
|
||||
[
|
||||
{
|
||||
"path": "./mylib",
|
||||
},
|
||||
]
|
||||
`);
|
||||
// Make sure keys are in idiomatic order
|
||||
expect(Object.keys(readJson(tree, 'mylib/package.json')))
|
||||
.toMatchInlineSnapshot(`
|
||||
[
|
||||
"name",
|
||||
"version",
|
||||
"main",
|
||||
"types",
|
||||
"exports",
|
||||
"nx",
|
||||
]
|
||||
`);
|
||||
expect(readJson(tree, 'mylib/tsconfig.json')).toMatchInlineSnapshot(`
|
||||
{
|
||||
"extends": "../tsconfig.base.json",
|
||||
"files": [],
|
||||
"include": [],
|
||||
"references": [
|
||||
{
|
||||
"path": "./tsconfig.lib.json",
|
||||
},
|
||||
{
|
||||
"path": "./tsconfig.spec.json",
|
||||
},
|
||||
],
|
||||
}
|
||||
`);
|
||||
expect(readJson(tree, 'mylib/tsconfig.lib.json')).toMatchInlineSnapshot(`
|
||||
{
|
||||
"compilerOptions": {
|
||||
"jsx": "react-jsx",
|
||||
"module": "esnext",
|
||||
"moduleResolution": "bundler",
|
||||
"outDir": "out-tsc/mylib",
|
||||
"rootDir": "src",
|
||||
"tsBuildInfoFile": "out-tsc/mylib/tsconfig.lib.tsbuildinfo",
|
||||
"types": [
|
||||
"node",
|
||||
"@nx/react/typings/cssmodule.d.ts",
|
||||
"@nx/react/typings/image.d.ts",
|
||||
"next",
|
||||
"@nx/next/typings/image.d.ts",
|
||||
],
|
||||
},
|
||||
"exclude": [
|
||||
"out-tsc",
|
||||
"dist",
|
||||
"jest.config.ts",
|
||||
"src/**/*.spec.ts",
|
||||
"src/**/*.test.ts",
|
||||
"src/**/*.spec.tsx",
|
||||
"src/**/*.test.tsx",
|
||||
"src/**/*.spec.js",
|
||||
"src/**/*.test.js",
|
||||
"src/**/*.spec.jsx",
|
||||
"src/**/*.test.jsx",
|
||||
"eslint.config.js",
|
||||
"eslint.config.cjs",
|
||||
"eslint.config.mjs",
|
||||
],
|
||||
"extends": "../tsconfig.base.json",
|
||||
"include": [
|
||||
"src/**/*.js",
|
||||
"src/**/*.jsx",
|
||||
"src/**/*.ts",
|
||||
"src/**/*.tsx",
|
||||
],
|
||||
}
|
||||
`);
|
||||
expect(readJson(tree, 'mylib/tsconfig.spec.json')).toMatchInlineSnapshot(`
|
||||
{
|
||||
"compilerOptions": {
|
||||
"jsx": "react-jsx",
|
||||
"module": "esnext",
|
||||
"moduleResolution": "bundler",
|
||||
"outDir": "./out-tsc/jest",
|
||||
"types": [
|
||||
"jest",
|
||||
"node",
|
||||
],
|
||||
},
|
||||
"extends": "../tsconfig.base.json",
|
||||
"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",
|
||||
},
|
||||
],
|
||||
}
|
||||
`);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -20,6 +20,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 libraryGenerator(host: Tree, rawOptions: Schema) {
|
||||
return await libraryGeneratorInternal(host, {
|
||||
@ -166,6 +167,8 @@ export async function libraryGeneratorInternal(host: Tree, rawOptions: Schema) {
|
||||
addProjectToTsSolutionWorkspace(host, options.projectRoot);
|
||||
}
|
||||
|
||||
sortPackageJsonFields(host, options.projectRoot);
|
||||
|
||||
if (!options.skipFormat) {
|
||||
await formatFiles(host);
|
||||
}
|
||||
|
||||
@ -599,6 +599,16 @@ describe('app', () => {
|
||||
},
|
||||
]
|
||||
`);
|
||||
// Make sure keys are in idiomatic order
|
||||
expect(Object.keys(readJson(tree, 'myapp/package.json')))
|
||||
.toMatchInlineSnapshot(`
|
||||
[
|
||||
"name",
|
||||
"version",
|
||||
"private",
|
||||
"nx",
|
||||
]
|
||||
`);
|
||||
expect(readJson(tree, 'myapp/package.json')).toMatchInlineSnapshot(`
|
||||
{
|
||||
"name": "@proj/myapp",
|
||||
|
||||
@ -60,6 +60,7 @@ import {
|
||||
isUsingTsSolutionSetup,
|
||||
updateTsconfigFiles,
|
||||
} from '@nx/js/src/utils/typescript/ts-solution-setup';
|
||||
import { sortPackageJsonFields } from '@nx/js/src/utils/package-json/sort-fields';
|
||||
|
||||
export interface NormalizedSchema extends Schema {
|
||||
appProjectRoot: string;
|
||||
@ -594,6 +595,8 @@ export async function applicationGeneratorInternal(tree: Tree, schema: Schema) {
|
||||
addProjectToTsSolutionWorkspace(tree, options.appProjectRoot);
|
||||
}
|
||||
|
||||
sortPackageJsonFields(tree, options.appProjectRoot);
|
||||
|
||||
if (!options.skipFormat) {
|
||||
await formatFiles(tree);
|
||||
}
|
||||
|
||||
@ -553,6 +553,20 @@ describe('lib', () => {
|
||||
},
|
||||
]
|
||||
`);
|
||||
// Make sure keys are in idiomatic order
|
||||
expect(Object.keys(readJson(tree, 'mylib/package.json')))
|
||||
.toMatchInlineSnapshot(`
|
||||
[
|
||||
"name",
|
||||
"version",
|
||||
"private",
|
||||
"main",
|
||||
"types",
|
||||
"exports",
|
||||
"nx",
|
||||
"dependencies",
|
||||
]
|
||||
`);
|
||||
expect(readJson(tree, 'mylib/package.json')).toMatchInlineSnapshot(`
|
||||
{
|
||||
"dependencies": {},
|
||||
|
||||
@ -34,6 +34,7 @@ import {
|
||||
isUsingTsSolutionSetup,
|
||||
} 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 interface NormalizedSchema extends Schema {
|
||||
fileName: string;
|
||||
@ -117,6 +118,8 @@ export async function libraryGeneratorInternal(tree: Tree, schema: Schema) {
|
||||
addProjectToTsSolutionWorkspace(tree, options.projectRoot);
|
||||
}
|
||||
|
||||
sortPackageJsonFields(tree, options.projectRoot);
|
||||
|
||||
if (!schema.skipFormat) {
|
||||
await formatFiles(tree);
|
||||
}
|
||||
|
||||
@ -242,6 +242,16 @@ describe('app', () => {
|
||||
},
|
||||
]
|
||||
`);
|
||||
// Make sure keys are in idiomatic order
|
||||
expect(Object.keys(readJson(tree, 'myapp/package.json')))
|
||||
.toMatchInlineSnapshot(`
|
||||
[
|
||||
"name",
|
||||
"version",
|
||||
"private",
|
||||
"nx",
|
||||
]
|
||||
`);
|
||||
expect(readJson(tree, 'myapp/tsconfig.json')).toMatchInlineSnapshot(`
|
||||
{
|
||||
"extends": "../tsconfig.base.json",
|
||||
|
||||
@ -37,6 +37,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(tree: Tree, schema: Schema) {
|
||||
const tasks: GeneratorCallback[] = [];
|
||||
@ -197,6 +198,8 @@ export async function applicationGenerator(tree: Tree, schema: Schema) {
|
||||
addProjectToTsSolutionWorkspace(tree, options.appProjectRoot);
|
||||
}
|
||||
|
||||
sortPackageJsonFields(tree, options.appProjectRoot);
|
||||
|
||||
if (!options.skipFormat) await formatFiles(tree);
|
||||
|
||||
tasks.push(() => {
|
||||
|
||||
@ -291,6 +291,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",
|
||||
|
||||
@ -29,6 +29,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 reactNativeApplicationGenerator(
|
||||
host: Tree,
|
||||
@ -152,6 +153,8 @@ export async function reactNativeApplicationGeneratorInternal(
|
||||
addProjectToTsSolutionWorkspace(host, options.appProjectRoot);
|
||||
}
|
||||
|
||||
sortPackageJsonFields(host, options.appProjectRoot);
|
||||
|
||||
if (!options.skipFormat) {
|
||||
await formatFiles(host);
|
||||
}
|
||||
|
||||
@ -5,6 +5,7 @@ import {
|
||||
readProjectConfiguration,
|
||||
Tree,
|
||||
updateJson,
|
||||
writeJson,
|
||||
} from '@nx/devkit';
|
||||
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
|
||||
import libraryGenerator from './library';
|
||||
@ -459,4 +460,140 @@ describe('lib', () => {
|
||||
expect(readJson(appTree, 'package.json')).toEqual(packageJsonBefore);
|
||||
});
|
||||
});
|
||||
|
||||
describe('TS solution setup', () => {
|
||||
it('should add project references when using TS solution', async () => {
|
||||
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: [],
|
||||
});
|
||||
|
||||
await libraryGenerator(appTree, {
|
||||
...defaultSchema,
|
||||
});
|
||||
|
||||
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",
|
||||
"exports",
|
||||
"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",
|
||||
"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/test-setup.ts",
|
||||
"jest.config.ts",
|
||||
"eslint.config.js",
|
||||
"eslint.config.cjs",
|
||||
"eslint.config.mjs",
|
||||
],
|
||||
"extends": "../tsconfig.base.json",
|
||||
"include": [
|
||||
"src/**/*.js",
|
||||
"src/**/*.jsx",
|
||||
"src/**/*.ts",
|
||||
"src/**/*.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",
|
||||
},
|
||||
],
|
||||
}
|
||||
`);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -40,6 +40,7 @@ import {
|
||||
} from '@nx/js/src/utils/typescript/ts-solution-setup';
|
||||
import { getImportPath } from '@nx/js/src/utils/get-import-path';
|
||||
import type { PackageJson } from 'nx/src/utils/package-json';
|
||||
import { sortPackageJsonFields } from '@nx/js/src/utils/package-json/sort-fields';
|
||||
|
||||
export async function reactNativeLibraryGenerator(
|
||||
host: Tree,
|
||||
@ -135,6 +136,8 @@ export async function reactNativeLibraryGeneratorInternal(
|
||||
addProjectToTsSolutionWorkspace(host, options.projectRoot);
|
||||
}
|
||||
|
||||
sortPackageJsonFields(host, options.projectRoot);
|
||||
|
||||
if (!options.skipFormat) {
|
||||
await formatFiles(host);
|
||||
}
|
||||
|
||||
@ -1314,6 +1314,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/tsconfig.json')).toMatchInlineSnapshot(`
|
||||
{
|
||||
"extends": "../tsconfig.base.json",
|
||||
|
||||
@ -43,6 +43,7 @@ import {
|
||||
setupVitestConfiguration,
|
||||
} from './lib/bundlers/add-vite';
|
||||
import { Schema } from './schema';
|
||||
import { sortPackageJsonFields } from '@nx/js/src/utils/package-json/sort-fields';
|
||||
|
||||
export async function applicationGenerator(
|
||||
tree: Tree,
|
||||
@ -183,6 +184,8 @@ export async function applicationGeneratorInternal(
|
||||
addProjectToTsSolutionWorkspace(tree, options.appProjectRoot);
|
||||
}
|
||||
|
||||
sortPackageJsonFields(tree, options.appProjectRoot);
|
||||
|
||||
if (!options.skipFormat) {
|
||||
await formatFiles(tree);
|
||||
}
|
||||
|
||||
@ -1017,6 +1017,20 @@ module.exports = withNx(
|
||||
},
|
||||
]
|
||||
`);
|
||||
// Make sure keys are in idiomatic order
|
||||
expect(Object.keys(readJson(tree, 'mylib/package.json')))
|
||||
.toMatchInlineSnapshot(`
|
||||
[
|
||||
"name",
|
||||
"version",
|
||||
"type",
|
||||
"main",
|
||||
"module",
|
||||
"types",
|
||||
"nx",
|
||||
"exports",
|
||||
]
|
||||
`);
|
||||
expect(readJson(tree, 'mylib/tsconfig.json')).toMatchInlineSnapshot(`
|
||||
{
|
||||
"extends": "../tsconfig.base.json",
|
||||
|
||||
@ -36,6 +36,7 @@ import {
|
||||
updateTsconfigFiles,
|
||||
} from '@nx/js/src/utils/typescript/ts-solution-setup';
|
||||
import { determineEntryFields } from './lib/determine-entry-fields';
|
||||
import { sortPackageJsonFields } from '@nx/js/src/utils/package-json/sort-fields';
|
||||
|
||||
export async function libraryGenerator(host: Tree, schema: Schema) {
|
||||
return await libraryGeneratorInternal(host, {
|
||||
@ -275,6 +276,9 @@ export async function libraryGeneratorInternal(host: Tree, schema: Schema) {
|
||||
if (options.isUsingTsSolutionConfig) {
|
||||
addProjectToTsSolutionWorkspace(host, options.projectRoot);
|
||||
}
|
||||
|
||||
sortPackageJsonFields(host, options.projectRoot);
|
||||
|
||||
if (!options.skipFormat) {
|
||||
await formatFiles(host);
|
||||
}
|
||||
|
||||
@ -346,6 +346,21 @@ describe('Remix Application', () => {
|
||||
tags: 'foo',
|
||||
});
|
||||
|
||||
// Make sure keys are in idiomatic order
|
||||
expect(Object.keys(readJson(tree, 'myapp/package.json')))
|
||||
.toMatchInlineSnapshot(`
|
||||
[
|
||||
"name",
|
||||
"private",
|
||||
"type",
|
||||
"scripts",
|
||||
"engines",
|
||||
"sideEffects",
|
||||
"nx",
|
||||
"dependencies",
|
||||
"devDependencies",
|
||||
]
|
||||
`);
|
||||
expect(readJson(tree, 'myapp/package.json')).toMatchInlineSnapshot(`
|
||||
{
|
||||
"dependencies": {
|
||||
|
||||
@ -49,6 +49,7 @@ import {
|
||||
isUsingTsSolutionSetup,
|
||||
updateTsconfigFiles,
|
||||
} from '@nx/js/src/utils/typescript/ts-solution-setup';
|
||||
import { sortPackageJsonFields } from '@nx/js/src/utils/package-json/sort-fields';
|
||||
|
||||
export function remixApplicationGenerator(
|
||||
tree: Tree,
|
||||
@ -332,6 +333,8 @@ export default {...nxPreset};
|
||||
addProjectToTsSolutionWorkspace(tree, options.projectRoot);
|
||||
}
|
||||
|
||||
sortPackageJsonFields(tree, options.projectRoot);
|
||||
|
||||
if (!options.skipFormat) {
|
||||
await formatFiles(tree);
|
||||
}
|
||||
|
||||
@ -164,6 +164,18 @@ describe('Remix Library Generator', () => {
|
||||
addPlugin: true,
|
||||
});
|
||||
|
||||
// Make sure keys are in idiomatic order
|
||||
expect(Object.keys(readJson(tree, 'packages/foo/package.json')))
|
||||
.toMatchInlineSnapshot(`
|
||||
[
|
||||
"name",
|
||||
"version",
|
||||
"main",
|
||||
"types",
|
||||
"exports",
|
||||
"nx",
|
||||
]
|
||||
`);
|
||||
expect(readJson(tree, 'packages/foo/package.json'))
|
||||
.toMatchInlineSnapshot(`
|
||||
{
|
||||
|
||||
@ -13,6 +13,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 remixLibraryGenerator(
|
||||
tree: Tree,
|
||||
@ -80,6 +81,8 @@ export async function remixLibraryGeneratorInternal(
|
||||
addProjectToTsSolutionWorkspace(tree, options.projectRoot);
|
||||
}
|
||||
|
||||
sortPackageJsonFields(tree, options.projectRoot);
|
||||
|
||||
if (!options.skipFormat) {
|
||||
await formatFiles(tree);
|
||||
}
|
||||
|
||||
@ -244,6 +244,16 @@ describe('application generator', () => {
|
||||
},
|
||||
]
|
||||
`);
|
||||
// Make sure keys are in idiomatic order
|
||||
expect(Object.keys(readJson(tree, 'test/package.json')))
|
||||
.toMatchInlineSnapshot(`
|
||||
[
|
||||
"name",
|
||||
"version",
|
||||
"private",
|
||||
"nx",
|
||||
]
|
||||
`);
|
||||
expect(readJson(tree, 'test/tsconfig.json')).toMatchInlineSnapshot(`
|
||||
{
|
||||
"extends": "../tsconfig.base.json",
|
||||
|
||||
@ -27,6 +27,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 function applicationGenerator(tree: Tree, options: Schema) {
|
||||
return applicationGeneratorInternal(tree, { addPlugin: false, ...options });
|
||||
@ -150,6 +151,8 @@ export async function applicationGeneratorInternal(
|
||||
addProjectToTsSolutionWorkspace(tree, options.appProjectRoot);
|
||||
}
|
||||
|
||||
sortPackageJsonFields(tree, options.appProjectRoot);
|
||||
|
||||
if (!options.skipFormat) await formatFiles(tree);
|
||||
|
||||
tasks.push(() => {
|
||||
|
||||
@ -501,6 +501,19 @@ module.exports = [
|
||||
},
|
||||
]
|
||||
`);
|
||||
// Make sure keys are in idiomatic order
|
||||
expect(Object.keys(readJson(tree, 'my-lib/package.json')))
|
||||
.toMatchInlineSnapshot(`
|
||||
[
|
||||
"name",
|
||||
"version",
|
||||
"private",
|
||||
"module",
|
||||
"types",
|
||||
"exports",
|
||||
"nx",
|
||||
]
|
||||
`);
|
||||
expect(readJson(tree, 'my-lib/tsconfig.json')).toMatchInlineSnapshot(`
|
||||
{
|
||||
"extends": "../tsconfig.base.json",
|
||||
|
||||
@ -29,6 +29,7 @@ import {
|
||||
updateTsconfigFiles,
|
||||
} from '@nx/js/src/utils/typescript/ts-solution-setup';
|
||||
import { determineEntryFields } from './lib/determine-entry-fields';
|
||||
import { sortPackageJsonFields } from '@nx/js/src/utils/package-json/sort-fields';
|
||||
|
||||
export function libraryGenerator(tree: Tree, schema: Schema) {
|
||||
return libraryGeneratorInternal(tree, { addPlugin: false, ...schema });
|
||||
@ -154,6 +155,8 @@ export async function libraryGeneratorInternal(tree: Tree, schema: Schema) {
|
||||
addProjectToTsSolutionWorkspace(tree, options.projectRoot);
|
||||
}
|
||||
|
||||
sortPackageJsonFields(tree, options.projectRoot);
|
||||
|
||||
if (!options.skipFormat) await formatFiles(tree);
|
||||
|
||||
// Always run install to link packages.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user