Skip to content

Commit

Permalink
fix(core): fix create package json root parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav committed Jun 27, 2024
1 parent c2c6a13 commit 7bb14ab
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 22 deletions.
115 changes: 95 additions & 20 deletions packages/nx/src/plugins/js/package-json/create-package-json.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import * as fs from 'fs';

import * as configModule from '../../../config/configuration';
import { ProjectGraph } from '../../../config/project-graph';
import {
FileData,
FileDataDependency,
ProjectFileMap,
ProjectGraph,
} from '../../../config/project-graph';
import * as hashModule from '../../../hasher/task-hasher';
import { createPackageJson } from './create-package-json';
import * as fileutilsModule from '../../../utils/fileutils';
Expand Down Expand Up @@ -294,6 +299,7 @@ describe('createPackageJson', () => {

expect(filterUsingGlobPatternsSpy).toHaveBeenCalledTimes(1);
});

it('should exclude devDependencies from production build when local package.json is imported', () => {
jest.spyOn(configModule, 'readNxJson').mockReturnValueOnce({
namedInputs: {
Expand Down Expand Up @@ -409,15 +415,23 @@ describe('createPackageJson', () => {
type: 'app',
name: 'app1',
data: {
targets: {},
targets: {
build: {
inputs: ['{projectRoot}/**/*'],
},
},
root: 'apps/app1',
},
},
lib1: {
type: 'lib',
name: 'lib1',
data: {
targets: {},
targets: {
build: {
inputs: ['{projectRoot}/**/*'],
},
},
root: 'libs/lib1',
},
},
Expand All @@ -426,12 +440,12 @@ describe('createPackageJson', () => {
'npm:@nx/devkit': {
type: 'npm',
name: 'npm:@nx/devkit',
data: { version: '16.0.0', hash: '', packageName: '@nx/devkit' },
data: { version: '16.0.3', hash: '', packageName: '@nx/devkit' },
},
'npm:nx': {
type: 'npm',
name: 'npm:nx',
data: { version: '16.0.0', hash: '', packageName: 'nx' },
data: { version: '16.0.3', hash: '', packageName: 'nx' },
},
'npm:tslib': {
type: 'npm',
Expand All @@ -450,6 +464,22 @@ describe('createPackageJson', () => {
},
};

const fileMap: ProjectFileMap = {
app1: [
createFile(`apps/app1/src/main.ts`, [
'npm:@nx/devkit',
'npm:typecript',
]),
],
lib1: [
createFile(`libs/lib1/src/main.ts`, [
'npm:@nx/devkit',
'npm:typecript',
'npm:tslib',
]),
],
};

const rootPackageJson = () => ({
dependencies: {
'@nx/devkit': '~16.0.0',
Expand All @@ -474,7 +504,13 @@ describe('createPackageJson', () => {
spies.push(
jest
.spyOn(hashModule, 'filterUsingGlobPatterns')
.mockImplementation(() => [])
.mockImplementation((root) => {
if (root === 'libs/lib1') {
return fileMap['lib1'];
} else {
return fileMap['app1'];
}
})
);
});

Expand Down Expand Up @@ -503,9 +539,13 @@ describe('createPackageJson', () => {
})
);

expect(createPackageJson('app1', graph, { root: '' })).toEqual({
expect(createPackageJson('app1', graph, { root: '' }, fileMap)).toEqual({
name: 'app1',
version: '0.0.1',
dependencies: {
'@nx/devkit': '16.0.3',
nx: '16.0.3',
},
});
});

Expand All @@ -531,12 +571,23 @@ describe('createPackageJson', () => {
);

expect(
createPackageJson('app1', graph, {
root: '',
})
createPackageJson(
'app1',
graph,
{
root: '',
},
fileMap
)
).toEqual({
name: 'app1',
version: '0.0.1',
name: 'other-name',
version: '1.2.3',
dependencies: {
'@nx/devkit': '16.0.3',
nx: '16.0.3',
random: '1.0.0',
typescript: '^4.8.4',
},
});
});

Expand All @@ -552,12 +603,21 @@ describe('createPackageJson', () => {
);

expect(
createPackageJson('lib1', graph, {
root: '',
})
createPackageJson(
'lib1',
graph,
{
root: '',
},
fileMap
)
).toEqual({
name: 'lib1',
version: '0.0.1',
dependencies: {
'@nx/devkit': '~16.0.0',
tslib: '~2.4.0',
},
});
});

Expand All @@ -583,13 +643,28 @@ describe('createPackageJson', () => {
);

expect(
createPackageJson('lib1', graph, {
root: '',
})
createPackageJson(
'lib1',
graph,
{
root: '',
},
fileMap
)
).toEqual({
name: 'lib1',
version: '0.0.1',
name: 'other-name',
version: '1.2.3',
dependencies: {
'@nx/devkit': '~16.0.0',
tslib: '~2.4.0',
random: '1.0.0',
typescript: '^4.8.4',
},
});
});
});
});

function createFile(f: string, deps?: FileDataDependency[]): FileData {
return { file: f, hash: '', deps };
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function createPackageJson(
const isLibrary = projectNode.type === 'lib';

const rootPackageJson = readJsonFile(
`${options.root || workspaceRoot}/package.json`
join(options.root ?? workspaceRoot, 'package.json')
);

const npmDeps = findProjectsNpmDependencies(
Expand All @@ -65,7 +65,7 @@ export function createPackageJson(
version: '0.0.1',
};
const projectPackageJsonPath = join(
options.root || workspaceRoot,
options.root ?? workspaceRoot,
projectNode.data.root,
'package.json'
);
Expand Down

0 comments on commit 7bb14ab

Please sign in to comment.