Skip to content

Commit

Permalink
Fix(*): Make sure peerDependencies of deps are hoisted through each p…
Browse files Browse the repository at this point in the history
…ackage.

Fixes #1584
  • Loading branch information
SBoudrias committed Oct 25, 2024
1 parent 2f5518d commit 01d1bf9
Show file tree
Hide file tree
Showing 19 changed files with 125 additions and 0 deletions.
35 changes: 35 additions & 0 deletions internals/hoist-peer-dependencies/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// @ts-check

import { writeFileSync } from 'node:fs';
import Module from 'node:module';
const require = Module.createRequire(import.meta.url);

/**
* @param {string} target
*/
export function fixPeerDeps(target) {
const pkg = require(`${target}/package.json`);

for (const name of Object.keys(pkg.dependencies ?? {})) {
// Import the dependency package.json file and parse it
let depPkg;
try {
depPkg = require(`${name}/package.json`);
} catch {
// If the sub package doesn't expose their package.json; skip it.
console.error(`Could not find package.json for ${name}. Skipping...`);
continue;
}

for (const [peerName, peerVersion] of Object.entries(depPkg.peerDependencies ?? {})) {
// If the peer dependency is not already a dependency, add it
if (!pkg.dependencies[peerName]) {
pkg.peerDependencies = pkg.peerDependencies ?? {};
pkg.peerDependencies[peerName] = peerVersion;
}
}
}

// Write the updated package.json file
writeFileSync(`${target}/package.json`, JSON.stringify(pkg, null, 2) + '\n');
}
10 changes: 10 additions & 0 deletions internals/hoist-peer-dependencies/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "@repo/hoist-peer-dependencies",
"version": "0.0.0",
"private": true,
"type": "module",
"main": "index.js",
"engines": {
"node": ">=18"
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
},
"devDependencies": {
"@eslint/js": "^9.13.0",
"@repo/hoist-peer-dependencies": "workspace:*",
"@repo/tsconfig": "workspace:*",
"@types/node": "^22.8.0",
"@vitest/coverage-v8": "^2.1.3",
Expand Down
3 changes: 3 additions & 0 deletions packages/checkbox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,8 @@
"./package.json": "./package.json",
".": "./src/index.ts"
}
},
"peerDependencies": {
"@types/node": ">=18"
}
}
3 changes: 3 additions & 0 deletions packages/confirm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,8 @@
"./package.json": "./package.json",
".": "./src/index.ts"
}
},
"peerDependencies": {
"@types/node": ">=18"
}
}
3 changes: 3 additions & 0 deletions packages/demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,8 @@
"./package.json": "./package.json",
".": "./src/index.ts"
}
},
"peerDependencies": {
"@types/node": ">=18"
}
}
3 changes: 3 additions & 0 deletions packages/editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,8 @@
"./package.json": "./package.json",
".": "./src/index.ts"
}
},
"peerDependencies": {
"@types/node": ">=18"
}
}
3 changes: 3 additions & 0 deletions packages/expand/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,8 @@
"./package.json": "./package.json",
".": "./src/index.ts"
}
},
"peerDependencies": {
"@types/node": ">=18"
}
}
3 changes: 3 additions & 0 deletions packages/input/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,8 @@
"./package.json": "./package.json",
".": "./src/index.ts"
}
},
"peerDependencies": {
"@types/node": ">=18"
}
}
3 changes: 3 additions & 0 deletions packages/inquirer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,8 @@
"./package.json": "./package.json",
".": "./src/index.ts"
}
},
"peerDependencies": {
"@types/node": ">=18"
}
}
3 changes: 3 additions & 0 deletions packages/number/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,8 @@
"./package.json": "./package.json",
".": "./src/index.ts"
}
},
"peerDependencies": {
"@types/node": ">=18"
}
}
3 changes: 3 additions & 0 deletions packages/password/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,8 @@
"./package.json": "./package.json",
".": "./src/index.ts"
}
},
"peerDependencies": {
"@types/node": ">=18"
}
}
3 changes: 3 additions & 0 deletions packages/prompts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,8 @@
"./package.json": "./package.json",
".": "./src/index.ts"
}
},
"peerDependencies": {
"@types/node": ">=18"
}
}
3 changes: 3 additions & 0 deletions packages/rawlist/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,8 @@
"./package.json": "./package.json",
".": "./src/index.ts"
}
},
"peerDependencies": {
"@types/node": ">=18"
}
}
3 changes: 3 additions & 0 deletions packages/search/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,8 @@
"./package.json": "./package.json",
".": "./src/index.ts"
}
},
"peerDependencies": {
"@types/node": ">=18"
}
}
3 changes: 3 additions & 0 deletions packages/select/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,8 @@
"./package.json": "./package.json",
".": "./src/index.ts"
}
},
"peerDependencies": {
"@types/node": ">=18"
}
}
3 changes: 3 additions & 0 deletions packages/testing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,8 @@
"./package.json": "./package.json",
".": "./src/index.ts"
}
},
"peerDependencies": {
"@types/node": ">=18"
}
}
2 changes: 2 additions & 0 deletions tools/setup-packages.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'node:path';
import fs from 'node:fs/promises';
import { globby } from 'globby';
import { fixPeerDeps } from '@repo/hoist-peer-dependencies';

function readFile(filepath) {
return fs.readFile(filepath, 'utf8');
Expand Down Expand Up @@ -39,6 +40,7 @@ Promise.all(
).then((packages) =>
packages.forEach(async ([pkgPath, pkg]) => {
const dir = path.dirname(pkgPath);
fixPeerDeps(path.resolve(path.join(dir)));

const isTS = await fileExists(path.join(dir, 'src/index.ts'));
const hasReadme = await fileExists(path.join(dir, 'README.md'));
Expand Down
35 changes: 35 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,8 @@ __metadata:
ansi-escapes: "npm:^4.3.2"
tshy: "npm:^3.0.2"
yoctocolors-cjs: "npm:^2.1.2"
peerDependencies:
"@types/node": ">=18"
languageName: unknown
linkType: soft

Expand All @@ -489,6 +491,8 @@ __metadata:
"@inquirer/type": "npm:^3.0.0"
"@repo/tsconfig": "workspace:*"
tshy: "npm:^3.0.2"
peerDependencies:
"@types/node": ">=18"
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -526,6 +530,8 @@ __metadata:
"@types/node": "npm:^22.8.0"
tshy: "npm:^3.0.2"
yoctocolors-cjs: "npm:^2.1.2"
peerDependencies:
"@types/node": ">=18"
bin:
inquirer-demo: ./dist/esm/index.js
languageName: unknown
Expand All @@ -542,6 +548,8 @@ __metadata:
"@repo/tsconfig": "workspace:*"
external-editor: "npm:^3.1.0"
tshy: "npm:^3.0.2"
peerDependencies:
"@types/node": ">=18"
languageName: unknown
linkType: soft

Expand All @@ -567,6 +575,8 @@ __metadata:
"@repo/tsconfig": "workspace:*"
tshy: "npm:^3.0.2"
yoctocolors-cjs: "npm:^2.1.2"
peerDependencies:
"@types/node": ">=18"
languageName: unknown
linkType: soft

Expand All @@ -590,6 +600,8 @@ __metadata:
"@inquirer/type": "npm:^3.0.0"
"@repo/tsconfig": "workspace:*"
tshy: "npm:^3.0.2"
peerDependencies:
"@types/node": ">=18"
languageName: unknown
linkType: soft

Expand All @@ -603,6 +615,8 @@ __metadata:
"@inquirer/type": "npm:^3.0.0"
"@repo/tsconfig": "workspace:*"
tshy: "npm:^3.0.2"
peerDependencies:
"@types/node": ">=18"
languageName: unknown
linkType: soft

Expand All @@ -617,6 +631,8 @@ __metadata:
"@repo/tsconfig": "workspace:*"
ansi-escapes: "npm:^4.3.2"
tshy: "npm:^3.0.2"
peerDependencies:
"@types/node": ">=18"
languageName: unknown
linkType: soft

Expand All @@ -638,6 +654,8 @@ __metadata:
"@inquirer/type": "npm:^3.0.0"
"@repo/tsconfig": "workspace:*"
tshy: "npm:^3.0.2"
peerDependencies:
"@types/node": ">=18"
languageName: unknown
linkType: soft

Expand All @@ -652,6 +670,8 @@ __metadata:
"@repo/tsconfig": "workspace:*"
tshy: "npm:^3.0.2"
yoctocolors-cjs: "npm:^2.1.2"
peerDependencies:
"@types/node": ">=18"
languageName: unknown
linkType: soft

Expand All @@ -660,6 +680,7 @@ __metadata:
resolution: "@inquirer/root@workspace:."
dependencies:
"@eslint/js": "npm:^9.13.0"
"@repo/hoist-peer-dependencies": "workspace:*"
"@repo/tsconfig": "workspace:*"
"@types/node": "npm:^22.8.0"
"@vitest/coverage-v8": "npm:^2.1.3"
Expand Down Expand Up @@ -695,6 +716,8 @@ __metadata:
"@repo/tsconfig": "workspace:*"
tshy: "npm:^3.0.2"
yoctocolors-cjs: "npm:^2.1.2"
peerDependencies:
"@types/node": ">=18"
languageName: unknown
linkType: soft

Expand All @@ -711,6 +734,8 @@ __metadata:
ansi-escapes: "npm:^4.3.2"
tshy: "npm:^3.0.2"
yoctocolors-cjs: "npm:^2.1.2"
peerDependencies:
"@types/node": ">=18"
languageName: unknown
linkType: soft

Expand All @@ -727,6 +752,8 @@ __metadata:
mute-stream: "npm:^2.0.0"
strip-ansi: "npm:^6.0.1"
tshy: "npm:^3.0.2"
peerDependencies:
"@types/node": ">=18"
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -1436,6 +1463,12 @@ __metadata:
languageName: node
linkType: hard

"@repo/hoist-peer-dependencies@workspace:*, @repo/hoist-peer-dependencies@workspace:internals/hoist-peer-dependencies":
version: 0.0.0-use.local
resolution: "@repo/hoist-peer-dependencies@workspace:internals/hoist-peer-dependencies"
languageName: unknown
linkType: soft

"@repo/tsconfig@workspace:*, @repo/tsconfig@workspace:internals/tsconfig":
version: 0.0.0-use.local
resolution: "@repo/tsconfig@workspace:internals/tsconfig"
Expand Down Expand Up @@ -4819,6 +4852,8 @@ __metadata:
run-async: "npm:^3.0.0"
rxjs: "npm:^7.8.1"
tshy: "npm:^3.0.2"
peerDependencies:
"@types/node": ">=18"
languageName: unknown
linkType: soft

Expand Down

0 comments on commit 01d1bf9

Please sign in to comment.