Skip to content

Commit

Permalink
feat: add support to others package mangers
Browse files Browse the repository at this point in the history
  • Loading branch information
edumudu committed Apr 9, 2022
1 parent e199fa7 commit 273175f
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 4 deletions.
8 changes: 5 additions & 3 deletions packages/cli/commands/install.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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!'));
};

Expand Down
4 changes: 3 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -29,6 +30,7 @@
},
"dependencies": {
"chalk": "^5.0.1",
"find-up": "^6.3.0",
"yargs": "^17.3.1"
},
"devDependencies": {
Expand All @@ -41,4 +43,4 @@
"npm": ">=7.0.0",
"yarn": ">=1.0.0"
}
}
}
11 changes: 11 additions & 0 deletions packages/cli/utils/get-workdir-pkg-manager.mjs
Original file line number Diff line number Diff line change
@@ -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;
};
12 changes: 12 additions & 0 deletions packages/cli/utils/resolve-pkg-json.mjs
Original file line number Diff line number Diff line change
@@ -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;
};
39 changes: 39 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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==

0 comments on commit 273175f

Please sign in to comment.