From 01d1bf9db6e4b76a903036847cc4165fa96dcd24 Mon Sep 17 00:00:00 2001 From: Simon Boudrias Date: Fri, 25 Oct 2024 14:39:32 -0400 Subject: [PATCH] Fix(*): Make sure peerDependencies of deps are hoisted through each package. Fixes #1584 --- internals/hoist-peer-dependencies/index.js | 35 +++++++++++++++++++ .../hoist-peer-dependencies/package.json | 10 ++++++ package.json | 1 + packages/checkbox/package.json | 3 ++ packages/confirm/package.json | 3 ++ packages/demo/package.json | 3 ++ packages/editor/package.json | 3 ++ packages/expand/package.json | 3 ++ packages/input/package.json | 3 ++ packages/inquirer/package.json | 3 ++ packages/number/package.json | 3 ++ packages/password/package.json | 3 ++ packages/prompts/package.json | 3 ++ packages/rawlist/package.json | 3 ++ packages/search/package.json | 3 ++ packages/select/package.json | 3 ++ packages/testing/package.json | 3 ++ tools/setup-packages.mjs | 2 ++ yarn.lock | 35 +++++++++++++++++++ 19 files changed, 125 insertions(+) create mode 100644 internals/hoist-peer-dependencies/index.js create mode 100644 internals/hoist-peer-dependencies/package.json diff --git a/internals/hoist-peer-dependencies/index.js b/internals/hoist-peer-dependencies/index.js new file mode 100644 index 000000000..463b0bf1d --- /dev/null +++ b/internals/hoist-peer-dependencies/index.js @@ -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'); +} diff --git a/internals/hoist-peer-dependencies/package.json b/internals/hoist-peer-dependencies/package.json new file mode 100644 index 000000000..cbdfee843 --- /dev/null +++ b/internals/hoist-peer-dependencies/package.json @@ -0,0 +1,10 @@ +{ + "name": "@repo/hoist-peer-dependencies", + "version": "0.0.0", + "private": true, + "type": "module", + "main": "index.js", + "engines": { + "node": ">=18" + } +} diff --git a/package.json b/package.json index 7cc45a637..c6fcca41f 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/packages/checkbox/package.json b/packages/checkbox/package.json index 366ddc1d2..fbdfa13ac 100644 --- a/packages/checkbox/package.json +++ b/packages/checkbox/package.json @@ -100,5 +100,8 @@ "./package.json": "./package.json", ".": "./src/index.ts" } + }, + "peerDependencies": { + "@types/node": ">=18" } } diff --git a/packages/confirm/package.json b/packages/confirm/package.json index 889c5e6e3..536d312f2 100644 --- a/packages/confirm/package.json +++ b/packages/confirm/package.json @@ -97,5 +97,8 @@ "./package.json": "./package.json", ".": "./src/index.ts" } + }, + "peerDependencies": { + "@types/node": ">=18" } } diff --git a/packages/demo/package.json b/packages/demo/package.json index cb48df8b7..9d00dbb5d 100644 --- a/packages/demo/package.json +++ b/packages/demo/package.json @@ -97,5 +97,8 @@ "./package.json": "./package.json", ".": "./src/index.ts" } + }, + "peerDependencies": { + "@types/node": ">=18" } } diff --git a/packages/editor/package.json b/packages/editor/package.json index 86639ab26..45f6d1958 100644 --- a/packages/editor/package.json +++ b/packages/editor/package.json @@ -98,5 +98,8 @@ "./package.json": "./package.json", ".": "./src/index.ts" } + }, + "peerDependencies": { + "@types/node": ">=18" } } diff --git a/packages/expand/package.json b/packages/expand/package.json index 64d994773..8c75cb418 100644 --- a/packages/expand/package.json +++ b/packages/expand/package.json @@ -98,5 +98,8 @@ "./package.json": "./package.json", ".": "./src/index.ts" } + }, + "peerDependencies": { + "@types/node": ">=18" } } diff --git a/packages/input/package.json b/packages/input/package.json index 16ca5020f..383555d2d 100644 --- a/packages/input/package.json +++ b/packages/input/package.json @@ -97,5 +97,8 @@ "./package.json": "./package.json", ".": "./src/index.ts" } + }, + "peerDependencies": { + "@types/node": ">=18" } } diff --git a/packages/inquirer/package.json b/packages/inquirer/package.json index ce0f261c0..5531174b3 100644 --- a/packages/inquirer/package.json +++ b/packages/inquirer/package.json @@ -99,5 +99,8 @@ "./package.json": "./package.json", ".": "./src/index.ts" } + }, + "peerDependencies": { + "@types/node": ">=18" } } diff --git a/packages/number/package.json b/packages/number/package.json index 8b6ed03a9..0a8ab7db6 100644 --- a/packages/number/package.json +++ b/packages/number/package.json @@ -97,5 +97,8 @@ "./package.json": "./package.json", ".": "./src/index.ts" } + }, + "peerDependencies": { + "@types/node": ">=18" } } diff --git a/packages/password/package.json b/packages/password/package.json index 6daa6f07c..a5da97d84 100644 --- a/packages/password/package.json +++ b/packages/password/package.json @@ -98,5 +98,8 @@ "./package.json": "./package.json", ".": "./src/index.ts" } + }, + "peerDependencies": { + "@types/node": ">=18" } } diff --git a/packages/prompts/package.json b/packages/prompts/package.json index 1b4e473ab..7b81cae66 100644 --- a/packages/prompts/package.json +++ b/packages/prompts/package.json @@ -104,5 +104,8 @@ "./package.json": "./package.json", ".": "./src/index.ts" } + }, + "peerDependencies": { + "@types/node": ">=18" } } diff --git a/packages/rawlist/package.json b/packages/rawlist/package.json index c1c3142a7..2517d0c9f 100644 --- a/packages/rawlist/package.json +++ b/packages/rawlist/package.json @@ -98,5 +98,8 @@ "./package.json": "./package.json", ".": "./src/index.ts" } + }, + "peerDependencies": { + "@types/node": ">=18" } } diff --git a/packages/search/package.json b/packages/search/package.json index ef9dad91b..c0ad0139b 100644 --- a/packages/search/package.json +++ b/packages/search/package.json @@ -99,5 +99,8 @@ "./package.json": "./package.json", ".": "./src/index.ts" } + }, + "peerDependencies": { + "@types/node": ">=18" } } diff --git a/packages/select/package.json b/packages/select/package.json index 4bccac509..11e153e6a 100644 --- a/packages/select/package.json +++ b/packages/select/package.json @@ -100,5 +100,8 @@ "./package.json": "./package.json", ".": "./src/index.ts" } + }, + "peerDependencies": { + "@types/node": ">=18" } } diff --git a/packages/testing/package.json b/packages/testing/package.json index 1e38ed627..57ac531f5 100644 --- a/packages/testing/package.json +++ b/packages/testing/package.json @@ -100,5 +100,8 @@ "./package.json": "./package.json", ".": "./src/index.ts" } + }, + "peerDependencies": { + "@types/node": ">=18" } } diff --git a/tools/setup-packages.mjs b/tools/setup-packages.mjs index ed2086510..ee2e43148 100644 --- a/tools/setup-packages.mjs +++ b/tools/setup-packages.mjs @@ -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'); @@ -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')); diff --git a/yarn.lock b/yarn.lock index 9382de8bd..c743b1742 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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" @@ -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 @@ -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 @@ -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 @@ -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" @@ -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