Skip to content

Commit

Permalink
fix(core): generated package.json should not include duplicates and o…
Browse files Browse the repository at this point in the history
…ptional peer deeps (#13438)

(cherry picked from commit a95b91d)
  • Loading branch information
meeroslav authored and FrozenPandaz committed Nov 28, 2022
1 parent 796fd27 commit 78861e6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
7 changes: 3 additions & 4 deletions e2e/node/src/node.test.ts
Expand Up @@ -227,7 +227,9 @@ describe('Node Applications', () => {
describe('Build Node apps', () => {
beforeEach(() => newProject());

it('should generate a package.json with the `--generatePackageJson` flag', async () => {
afterEach(() => cleanupProject());

it('should generate a package.json with the `--generatePackageJson` flag MMM', async () => {
const scope = newProject();
const nestapp = uniq('nestapp');
runCLI(`generate @nrwl/nest:app ${nestapp} --linter=eslint`);
Expand All @@ -251,9 +253,6 @@ describe('Build Node apps', () => {
expect(
satisfies(packageJson.dependencies['@nestjs/core'], '^9.0.0')
).toBeTruthy();
expect(
satisfies(packageJson.dependencies['@nestjs/platform-express'], '^9.0.0')
).toBeTruthy();
expect(
satisfies(packageJson.dependencies['reflect-metadata'], '^0.1.13')
).toBeTruthy();
Expand Down
6 changes: 3 additions & 3 deletions packages/nx/package.json
Expand Up @@ -36,6 +36,7 @@
"@yarnpkg/lockfile": "^1.1.0",
"@yarnpkg/parsers": "^3.0.0-rc.18",
"@zkochan/js-yaml": "0.0.6",
"axios": "^1.0.0",
"chalk": "4.1.0",
"chokidar": "^3.5.1",
"cli-cursor": "3.1.0",
Expand All @@ -49,6 +50,7 @@
"fs-extra": "^10.1.0",
"glob": "7.1.4",
"ignore": "^5.0.4",
"js-yaml": "4.1.0",
"jsonc-parser": "3.2.0",
"minimatch": "3.0.5",
"npm-run-path": "^4.0.1",
Expand All @@ -62,9 +64,7 @@
"tslib": "^2.3.0",
"v8-compile-cache": "2.3.0",
"yargs": "^17.6.2",
"yargs-parser": "21.1.1",
"js-yaml": "4.1.0",
"axios": "^1.0.0"
"yargs-parser": "21.1.1"
},
"peerDependencies": {
"@swc-node/register": "^1.4.2",
Expand Down
13 changes: 10 additions & 3 deletions packages/workspace/src/utilities/create-package-json.ts
Expand Up @@ -35,7 +35,10 @@ export function createPackageJson(

const rootPackageJson = readJsonFile(`${options.root}/package.json`);
Object.entries(npmDeps).forEach(([packageName, version]) => {
if (rootPackageJson.devDependencies?.[packageName]) {
if (
rootPackageJson.devDependencies?.[packageName] &&
!packageJson.dependencies[packageName]
) {
packageJson.devDependencies[packageName] = version;
} else {
packageJson.dependencies[packageName] = version;
Expand Down Expand Up @@ -99,8 +102,12 @@ function recursivelyCollectPeerDependencies(
.map((dependency) => graph.externalNodes[dependency])
.filter(Boolean)
.forEach((node) => {
list[node.data.packageName] = node.data.version;
recursivelyCollectPeerDependencies(node.name, graph, list, seen);
if (
!packageJson.peerDependenciesMeta?.[node.data.packageName]?.optional
) {
list[node.data.packageName] = node.data.version;
recursivelyCollectPeerDependencies(node.name, graph, list, seen);
}
});
return list;
} catch (e) {
Expand Down

0 comments on commit 78861e6

Please sign in to comment.