Skip to content

Commit

Permalink
Discover pkgs from volta and corepack
Browse files Browse the repository at this point in the history
And discover yarn and pnpm from engines.
  • Loading branch information
felipecrs authored and mxcl committed Sep 6, 2024
1 parent 6806170 commit 0bb7d75
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 8 deletions.
4 changes: 3 additions & 1 deletion fixtures/package.json/engines/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"engines": {
"node": "~16.16.1",
"npm": "~9.7.1"
"npm": "~9.7.1",
"yarn": "~1.22.10",
"pnpm": "~7.33.7"
}
}

4 changes: 4 additions & 0 deletions fixtures/package.json/packageManager/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"packageManager": "[email protected]+sha256.d1581d46ed10f54ff0cbdd94a2373b1f070202b0fbff29f27c2ce01460427043"
}

9 changes: 9 additions & 0 deletions fixtures/package.json/volta/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"volta": {
"node": "16.16.1",
"npm": "9.7.1",
"yarn": "1.22.10",
"pnpm": "7.33.7"
}
}

14 changes: 14 additions & 0 deletions src/utils/devenv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ Deno.test("devenv.ts", async runner => {
'package.json/engines/package.json',
'nodejs.org~16.16.1',
'npmjs.com~9.7.1',
'yarnpkg.com~1.22.10',
'pnpm.io~7.33.7',
],
[
'package.json/packageManager/package.json',
'[email protected]',
'nodejs.org'
],
[
'package.json/volta/package.json',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
],
[".node-version", "[email protected]"],
["python-version/std/.python-version", "python.org~3.10"],
Expand Down
60 changes: 53 additions & 7 deletions src/utils/devenv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,59 @@ export default async function(dir: Path) {
const json = JSON.parse(await path.read());
let node = json?.pkgx;
if (isString(node) || isArray(node)) node = { dependencies: node }
if (!node && json?.engines) {
node = {
dependencies: {
...(json.engines.node && { 'nodejs.org': json.engines.node }),
...(json.engines.npm && { 'npmjs.com': json.engines.npm }),
},
};
if (!node) {
if (json?.engines) {
node = {
dependencies: {
...(json.engines.node && { 'nodejs.org': json.engines.node }),
...(json.engines.npm && { 'npmjs.com': json.engines.npm }),
...(json.engines.yarn && { 'yarnpkg.com': json.engines.yarn }),
...(json.engines.pnpm && { 'pnpm.io': json.engines.pnpm }),
},
};
}
if (json?.packageManager) { // corepack
// example: "[email protected]+sha256.d1581d46ed10f54ff0cbdd94a2373b1f070202b0fbff29f27c2ce01460427043"
const match = json.packageManager.match(/^(?<pkg>[^@]+)@(?<version>[^+]+)/);

if (match) {
const { pkg, version } = match.groups as { pkg: string, version: string };

switch (pkg) {
case 'npm':
node = {
dependencies: {
'npmjs.com': version,
},
};
break;
case 'yarn':
node = {
dependencies: {
'yarnpkg.com': version,
},
};
break;
case 'pnpm':
node = {
dependencies: {
'pnpm.io': version,
},
};
break;
};
}
}
if (json?.volta) {
node = {
dependencies: {
...(json.volta.node && { 'nodejs.org': json.volta.node }),
...(json.volta.npm && { 'npmjs.com': json.volta.npm }),
...(json.volta.yarn && { 'yarnpkg.com': json.volta.yarn }),
...(json.volta.pnpm && { 'pnpm.io': json.volta.pnpm }),
}
}
}
}
await parse_well_formatted_node(node)
has_package_json = true
Expand Down

0 comments on commit 0bb7d75

Please sign in to comment.