From 273175fabf66773c19b6aca1077016c6d5462daf Mon Sep 17 00:00:00 2001 From: Eduardo Wesley Date: Sat, 9 Apr 2022 17:58:46 -0300 Subject: [PATCH] feat: add support to others package mangers --- packages/cli/commands/install.mjs | 8 ++-- packages/cli/package.json | 4 +- .../cli/utils/get-workdir-pkg-manager.mjs | 11 ++++++ packages/cli/utils/resolve-pkg-json.mjs | 12 ++++++ yarn.lock | 39 +++++++++++++++++++ 5 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 packages/cli/utils/get-workdir-pkg-manager.mjs create mode 100644 packages/cli/utils/resolve-pkg-json.mjs diff --git a/packages/cli/commands/install.mjs b/packages/cli/commands/install.mjs index 21a3a85..3f9f016 100644 --- a/packages/cli/commands/install.mjs +++ b/packages/cli/commands/install.mjs @@ -3,17 +3,19 @@ import fs from 'fs'; import chalk from 'chalk'; import { packagesInfoPath } from '../config/paths.mjs'; +import { getWorkdirPkgManager } from '../utils/get-workdir-pkg-manager.mjs'; const install = (packageName) => { const packagesInfo = JSON.parse(fs.readFileSync(packagesInfoPath, 'utf-8')); - const tarballPath = packagesInfo[packageName]?.tarballPath; + const packageInfo = packagesInfo[packageName]; + const packageManager = getWorkdirPkgManager(); - if (!tarballPath) { + if (!packageInfo?.tarballPath) { console.log(chalk.red(`Package ${packageName} not found in the local registry`)); process.exit(1); } - execSync(`npm install ${tarballPath}`, { stdio: 'inherit' }); + execSync(`${packageManager} add ${packageInfo.tarballPath}`, { stdio: 'inherit' }); console.log(chalk.green('Package installed successfully!')); }; diff --git a/packages/cli/package.json b/packages/cli/package.json index 51b0e2b..3c742d8 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -8,6 +8,7 @@ "type": "module", "repository": "github:edumudu/loctry", "homepage": "https://github.com/edumudu/loctry/tree/main/packages/cli#readme", + "packageManager": "yarn@1.22.17", "keywords": [ "cli", "local development", @@ -29,6 +30,7 @@ }, "dependencies": { "chalk": "^5.0.1", + "find-up": "^6.3.0", "yargs": "^17.3.1" }, "devDependencies": { @@ -41,4 +43,4 @@ "npm": ">=7.0.0", "yarn": ">=1.0.0" } -} \ No newline at end of file +} diff --git a/packages/cli/utils/get-workdir-pkg-manager.mjs b/packages/cli/utils/get-workdir-pkg-manager.mjs new file mode 100644 index 0000000..598576a --- /dev/null +++ b/packages/cli/utils/get-workdir-pkg-manager.mjs @@ -0,0 +1,11 @@ +import { resolvePkgJson } from './resolve-pkg-json.mjs'; + +/** + * Returns the package manager to use in the current working directory. + */ +export const getWorkdirPkgManager = () => { + const workdirPackageInfo = resolvePkgJson(); + const [packageManager] = workdirPackageInfo.packageManager.split('@'); + + return packageManager; +}; diff --git a/packages/cli/utils/resolve-pkg-json.mjs b/packages/cli/utils/resolve-pkg-json.mjs new file mode 100644 index 0000000..d8c6b62 --- /dev/null +++ b/packages/cli/utils/resolve-pkg-json.mjs @@ -0,0 +1,12 @@ +import { readFileSync } from 'fs'; +import { findUpSync } from 'find-up'; + +/** + * Resolve package.json info from the current working directory. + */ +export const resolvePkgJson = () => { + const packageJsonPath = findUpSync('package.json', { cwd: process.cwd() }); + const packageJson = JSON.parse(readFileSync(packageJsonPath)); + + return packageJson; +}; diff --git a/yarn.lock b/yarn.lock index 78d5256..8de1357 100644 --- a/yarn.lock +++ b/yarn.lock @@ -963,6 +963,14 @@ find-up@^5.0.0: locate-path "^6.0.0" path-exists "^4.0.0" +find-up@^6.3.0: + version "6.3.0" + resolved "https://registry.yarnpkg.com/find-up/-/find-up-6.3.0.tgz#2abab3d3280b2dc7ac10199ef324c4e002c8c790" + integrity sha512-v2ZsoEuVHYy8ZIlYqwPe/39Cy+cFDzp4dXPaxNvkEuouymu+2Jbz0PxpKarJHYJTmv2HWT3O382qY8l4jMWthw== + dependencies: + locate-path "^7.1.0" + path-exists "^5.0.0" + flat-cache@^3.0.4: version "3.0.4" resolved "https://registry.yarnpkg.com/flat-cache/-/flat-cache-3.0.4.tgz#61b0338302b2fe9f957dcc32fc2a87f1c3048b11" @@ -1453,6 +1461,13 @@ locate-path@^6.0.0: dependencies: p-locate "^5.0.0" +locate-path@^7.1.0: + version "7.1.0" + resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-7.1.0.tgz#241d62af60739f6097c055efe10329c88b798425" + integrity sha512-HNx5uOnYeK4SxEoid5qnhRfprlJeGMzFRKPLCf/15N3/B4AiofNwC/yq7VBKdVk9dx7m+PiYCJOGg55JYTAqoQ== + dependencies: + p-locate "^6.0.0" + lodash.merge@^4.6.2: version "4.6.2" resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a" @@ -1693,6 +1708,13 @@ p-limit@^3.0.2: dependencies: yocto-queue "^0.1.0" +p-limit@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-4.0.0.tgz#914af6544ed32bfa54670b061cafcbd04984b644" + integrity sha512-5b0R4txpzjPWVw/cXXUResoD4hb6U/x9BH08L7nw+GN1sezDzPdxeRvpc9c433fZhBan/wusjbCsqwqm4EIBIQ== + dependencies: + yocto-queue "^1.0.0" + p-locate@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-2.0.0.tgz#20a0103b222a70c8fd39cc2e580680f3dde5ec43" @@ -1714,6 +1736,13 @@ p-locate@^5.0.0: dependencies: p-limit "^3.0.2" +p-locate@^6.0.0: + version "6.0.0" + resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-6.0.0.tgz#3da9a49d4934b901089dca3302fa65dc5a05c04f" + integrity sha512-wPrq66Llhl7/4AGC6I+cqxT07LhXvWL08LNXz1fENOw0Ap4sRZZ/gZpTTJ5jpurzzzfS2W/Ge9BY3LgLjCShcw== + dependencies: + p-limit "^4.0.0" + p-map@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/p-map/-/p-map-4.0.0.tgz#bb2f95a5eda2ec168ec9274e06a747c3e2904d2b" @@ -1758,6 +1787,11 @@ path-exists@^4.0.0: resolved "https://registry.yarnpkg.com/path-exists/-/path-exists-4.0.0.tgz#513bdbe2d3b95d7762e8c1137efa195c6c61b5b3" integrity sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w== +path-exists@^5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/path-exists/-/path-exists-5.0.0.tgz#a6aad9489200b21fab31e49cf09277e5116fb9e7" + integrity sha512-RjhtfwJOxzcFmNOi6ltcbcu4Iu+FL3zEj83dk4kAS+fVpTxXLO1b38RvJgT/0QwvV/L3aY9TAnyv0EOqW4GoMQ== + path-is-absolute@^1.0.0: version "1.0.1" resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f" @@ -2441,3 +2475,8 @@ yocto-queue@^0.1.0: version "0.1.0" resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b" integrity sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q== + +yocto-queue@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-1.0.0.tgz#7f816433fb2cbc511ec8bf7d263c3b58a1a3c251" + integrity sha512-9bnSc/HEW2uRy67wc+T8UwauLuPJVn28jb+GtJY16iiKWyvmYJRXVT4UamsAEGQfPohgr2q4Tq0sQbQlxTfi1g==