Skip to content

Commit

Permalink
[bugfix]: fix yarn data shape (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
kindoflew authored Sep 30, 2022
1 parent a1cc02a commit fa89694
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# main (unreleased)

# 0.1.2
- [[bugfix]: update shape of data returned from yarn](https://github.com/ombulabs/depngn/pull/5)

# 0.1.1

- [[bugfix]: specify depth in npm ls to avoid errors from uninstalled peerDeps](https://github.com/ombulabs/depngn/pull/1)
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "depngn",
"version": "0.1.1",
"version": "0.1.2",
"description": "Determine the compatibility of your packages with a given Node version",
"type": "module",
"main": "./dist/index.cjs",
Expand Down
30 changes: 25 additions & 5 deletions src/queries/getEngines.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { asyncExec, execWithLog } from "../cli/exec";
import { Managers, viewEnginesCommand } from "./getPackageManager";
import { PackageList } from "../types";
import { asyncExec, execWithLog } from '../cli/exec';
import { Managers, viewEnginesCommand } from './getPackageManager';
import { PackageList } from '../types';

export async function getEngines(deps: PackageList, manager: Managers) {
const command = viewEnginesCommand(manager);
const depsArray = Object.keys(deps).map(async (dep) => {
const engines = await asyncExec(
`${command} ${dep}@${deps[dep].version} engines --json`
);
const version = engines.stdout ? JSON.parse(engines.stdout) : '';
const range = parseEngines(engines, manager);
return {
package: dep,
range: version.node ? version.node.replace(' ', '') : '',
range,
};
});

Expand All @@ -20,3 +20,23 @@ export async function getEngines(deps: PackageList, manager: Managers) {
async () => await Promise.all(depsArray)
);
}

function parseEngines(
engines: { stdout: string; stderr: string },
manager: Managers
) {
const res = engines.stdout ? JSON.parse(engines.stdout) : {};
switch (manager) {
case 'npm':
return res.node ? res.node : '';

case 'yarn':
return res.data?.node ? res.data.node : '';

default:
const wrong = manager as never;
throw new Error(
`This error shouldn't happen, but somehow an invalid package manager made it through checks: ${wrong}.`
);
}
}
4 changes: 2 additions & 2 deletions src/queries/getPackageData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ function isCompatible(nodeVersion: string, depRange: string) {

const logicalOrRegEx = /||/;
if (depRange && logicalOrRegEx.test(depRange)) {
const rangeArray = depRange.split('||').map((range) => range.trim());
const rangeArray = depRange.split('||').map((range) => range.replaceAll(' ', ''));
compatible = rangeArray.some((range) => satisfies(nodeVersion, range));
} else {
compatible = satisfies(nodeVersion, depRange);
compatible = satisfies(nodeVersion, depRange.replaceAll(' ', ''));
}
return compatible;
}

0 comments on commit fa89694

Please sign in to comment.