From f04c4de9beaff0a43eba990dea97b10f7ca8ec4b Mon Sep 17 00:00:00 2001 From: Mohamed Houssein Douici Date: Fri, 15 Mar 2024 21:06:56 +0100 Subject: [PATCH] fix: use detect-package-manager to detect the correct pm and its version --- package-lock.json | 35 ++++++++++++++++++++++++++++------ package.json | 1 + src/service/package-manager.ts | 33 +++++++++++++++++++++++++------- 3 files changed, 56 insertions(+), 13 deletions(-) diff --git a/package-lock.json b/package-lock.json index beb0e38..7e42341 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,6 +16,7 @@ "ci-info": "^2.0.0", "commander": "^6.2.0", "cosmiconfig": "^7.0.0", + "detect-package-manager": "^3.0.1", "execa": "^5.0.0", "express": "^4.17.1", "fs-extra": "^8.1.0", @@ -3128,6 +3129,17 @@ "node": ">=8" } }, + "node_modules/detect-package-manager": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/detect-package-manager/-/detect-package-manager-3.0.1.tgz", + "integrity": "sha512-qoHDH6+lMcpJPAScE7+5CYj91W0mxZNXTwZPrCqi1KMk+x+AoQScQ2V1QyqTln1rHU5Haq5fikvOGHv+leKD8A==", + "dependencies": { + "execa": "^5.1.1" + }, + "engines": { + "node": ">=12" + } + }, "node_modules/diagnostics": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/diagnostics/-/diagnostics-1.1.1.tgz", @@ -3711,9 +3723,9 @@ } }, "node_modules/execa": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/execa/-/execa-5.0.0.tgz", - "integrity": "sha512-ov6w/2LCiuyO4RLYGdpFGjkcs0wMTgGE8PrkTHikeUy5iJekXyPIKUjifk5CsE0pt7sMCrMZ3YNqoCj6idQOnQ==", + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/execa/-/execa-5.1.1.tgz", + "integrity": "sha512-8uSpZZocAZRBAPIEINJj3Lo9HyGitllczc27Eh5YYojjMFMn8yHMDMaUHE2Jqfq05D/wucwI4JGURyXt1vchyg==", "dependencies": { "cross-spawn": "^7.0.3", "get-stream": "^6.0.0", @@ -3727,6 +3739,9 @@ }, "engines": { "node": ">=10" + }, + "funding": { + "url": "https://github.com/sindresorhus/execa?sponsor=1" } }, "node_modules/execa/node_modules/cross-spawn": { @@ -12612,6 +12627,14 @@ "integrity": "sha512-TLz+x/vEXm/Y7P7wn1EJFNLxYpUD4TgMosxY6fAVJUnJMbupHBOncxyWUG9OpTaH9EBD7uFI5LfEgmMOc54DsA==", "dev": true }, + "detect-package-manager": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/detect-package-manager/-/detect-package-manager-3.0.1.tgz", + "integrity": "sha512-qoHDH6+lMcpJPAScE7+5CYj91W0mxZNXTwZPrCqi1KMk+x+AoQScQ2V1QyqTln1rHU5Haq5fikvOGHv+leKD8A==", + "requires": { + "execa": "^5.1.1" + } + }, "diagnostics": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/diagnostics/-/diagnostics-1.1.1.tgz", @@ -13076,9 +13099,9 @@ "integrity": "sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc=" }, "execa": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/execa/-/execa-5.0.0.tgz", - "integrity": "sha512-ov6w/2LCiuyO4RLYGdpFGjkcs0wMTgGE8PrkTHikeUy5iJekXyPIKUjifk5CsE0pt7sMCrMZ3YNqoCj6idQOnQ==", + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/execa/-/execa-5.1.1.tgz", + "integrity": "sha512-8uSpZZocAZRBAPIEINJj3Lo9HyGitllczc27Eh5YYojjMFMn8yHMDMaUHE2Jqfq05D/wucwI4JGURyXt1vchyg==", "requires": { "cross-spawn": "^7.0.3", "get-stream": "^6.0.0", diff --git a/package.json b/package.json index a4eaec8..64a90bd 100644 --- a/package.json +++ b/package.json @@ -69,6 +69,7 @@ "ci-info": "^2.0.0", "commander": "^6.2.0", "cosmiconfig": "^7.0.0", + "detect-package-manager": "^3.0.1", "execa": "^5.0.0", "express": "^4.17.1", "fs-extra": "^8.1.0", diff --git a/src/service/package-manager.ts b/src/service/package-manager.ts index 1270c97..ef86189 100644 --- a/src/service/package-manager.ts +++ b/src/service/package-manager.ts @@ -1,4 +1,4 @@ -import hasYarn from "has-yarn"; +import { detect, getNpmVersion, PM } from "detect-package-manager"; import latestVersion from "latest-version"; import semver from "semver"; import { CLIError } from "../errors/CLIError"; @@ -40,30 +40,49 @@ export async function getLatestVersions(packages: string[]): Promise { + const version = await getNpmVersion(pm); + const parsedVersion = parseInt(version, 10); + + if (!isNaN(parsedVersion)) { + return parsedVersion; + } + + return undefined; +} + export async function installPackages(packages: Record, { installGlobal = false } = {}): Promise { - const yarn = hasYarn(); + const pm = await detect(); + const version = await getPackageManagerVersion(pm); const args = []; - const npmClient = yarn ? "yarn" : "npm"; - if (yarn) { + if (pm === "yarn") { if (installGlobal) { args.push("global"); - } else { + } else if (version && version === 1) { args.push("--ignore-workspace-root-check", "--dev"); + } else { + args.push("--dev"); } args.push("add"); - } else { + } else if (pm === "npm") { args.push("install"); if (installGlobal) { args.push("--global"); } else { args.push("--save-dev"); } + } else if (pm === "pnpm") { + throw new Error("Not implemented"); + } else if (pm === "bun") { + throw new Error("Not implemented"); + } else { + throw new Error("Not supported package manager"); } const packagesWithVersions = Object.keys(packages).map(p => `${p}@${packages[p]}`); - const command = `${npmClient} ${args.join(" ")} ${packagesWithVersions.join(" ")}`; + const command = `${pm} ${args.join(" ")} ${packagesWithVersions.join(" ")}`; await runCommand(command); }