Skip to content

Commit

Permalink
feat: add findPossibleResolution function
Browse files Browse the repository at this point in the history
  • Loading branch information
motea927 committed Feb 16, 2024
1 parent 3699d3b commit f157dc7
Showing 1 changed file with 42 additions and 4 deletions.
46 changes: 42 additions & 4 deletions src/solution/solution.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { execSync } from "node:child_process";
import semver from "semver";
import type { Dependency } from "../packageUtils/types";

export interface Resolution {
Expand Down Expand Up @@ -28,12 +30,48 @@ export function determineResolutionType(problem: Dependency): string {
return "install";
}

function semverReverseSort(a: string, b: string) {
const lt = semver.lt(a, b);
const gt = semver.gt(a, b);
if (!lt && !gt) {
return 0;
} else if (lt) {
return 1;
}
return -1;
}

function findPossibleResolution(
_packageName: string,
_allPeerDeps: Dependency[],
packageName: string,
allPeerDeps: Dependency[],
) {
// todo
return "";
const requiredPeerVersions = allPeerDeps.filter(
(dep) => dep.name === packageName,
);
// todo: skip this step if only one required peer version and it's an exact version
const command = `npm view ${packageName} versions`;
let rawVersionsInfo;
try {
rawVersionsInfo = execSync(command, { stdio: "pipe" }).toString();

const availableVersions = JSON.parse(
rawVersionsInfo.replace(/'/g, '"'),
).sort(semverReverseSort);

return availableVersions.find((ver: string) =>
requiredPeerVersions.every((peerVer) => {
return semver.satisfies(ver, peerVer.version, {
includePrerelease: true,
});
}),
);
} catch (error) {
console.error(`Error while running command: '${command}'`);
console.error(error);
console.error();
console.error("npm output:");
console.error(rawVersionsInfo);
}
}

export function determineResolutionVersion(
Expand Down

0 comments on commit f157dc7

Please sign in to comment.