Skip to content

Commit

Permalink
chore: continue to improve types
Browse files Browse the repository at this point in the history
  • Loading branch information
fraxken committed May 8, 2024
1 parent f25ed3b commit 1e3ac65
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 38 deletions.
1 change: 1 addition & 0 deletions workspaces/tree-walker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"types": "./dist/index.d.ts",
"scripts": {
"build": "tsc",
"prepublishOnly": "npm run build",
"test-only": "glob -c \"tsx --test\" \"./test/**/*.spec.ts\"",
"test": "c8 -r html npm run test-only"
},
Expand Down
30 changes: 14 additions & 16 deletions workspaces/tree-walker/src/npm/walker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import combineAsyncIterators from "combine-async-iterators";
import * as iter from "itertools";
import pacote from "pacote";
import Arborist from "@npmcli/arborist";
import * as npm from "@npm/types";

// Import Internal Dependencies
import * as utils from "../utils/index.js";
Expand All @@ -17,19 +16,22 @@ import { Dependency } from "../Dependency.class.js";
// CONSTANTS
const { NPM_TOKEN } = utils;

export interface SearchOptions {
export interface DefaultSearchOptions {
exclude: Map<string, Set<string>>;
registry: string;
}

export interface SearchOptions extends DefaultSearchOptions {
parent: Dependency;
maxDepth: number;
registry: string;
exclude: Map<string, Set<string>>;
currDepth?: number;
}

export async function* searchDependencies(
packageName: string,
gitURL: string | null,
options: SearchOptions
) {
): AsyncIterableIterator<Dependency> {
const { exclude, currDepth = 0, parent, maxDepth, registry } = options;

const { name, version, deprecated, ...pkg } = await pacote.manifest(gitURL ?? packageName, {
Expand Down Expand Up @@ -94,10 +96,8 @@ export async function* searchDependencies(
yield current;
}

export interface SearchLockOptions {
export interface SearchLockOptions extends DefaultSearchOptions {
parent: Dependency;
exclude: Map<string, Set<string>>;
registry: string;
to: any;
includeDevDeps?: boolean;
fullLockMode?: boolean;
Expand All @@ -106,7 +106,7 @@ export interface SearchLockOptions {
export async function* searchLockDependencies(
currentPackageName: string,
options: SearchLockOptions
) {
): AsyncIterableIterator<Dependency> {
const { to, parent, exclude, fullLockMode, includeDevDeps, registry } = options;
const { version, integrity = to.integrity } = to.package;

Expand Down Expand Up @@ -151,21 +151,19 @@ export async function* searchLockDependencies(
yield current;
}

export interface WalkOptions {
export interface WalkOptions extends DefaultSearchOptions {
/**
* @default 4
*/
maxDepth?: number;
exclude: Map<string, Set<string>>;
fullLockMode: boolean;
usePackageLock: boolean;
includeDevDeps: boolean;
location: string;
registry: string;
}

export async function* walk(
manifest: npm.PackageJson,
manifest: pacote.AbbreviatedManifest & pacote.ManifestResult,
options: WalkOptions
): AsyncIterableIterator<Dependency> {
const {
Expand Down Expand Up @@ -195,14 +193,14 @@ export async function* walk(
parent.addFlag("hasCustomResolver", customResolvers.size > 0);
parent.addFlag("hasDependencies", dependencies.size > 0);

let iterators;
let iterators: AsyncIterableIterator<Dependency>[];
if (usePackageLock) {
const arb = new Arborist({
...NPM_TOKEN,
path: location,
registry
});
let tree;
let tree: any;
try {
await fs.access(path.join(location, "node_modules"));
tree = await arb.loadActual();
Expand Down Expand Up @@ -239,7 +237,7 @@ export async function* walk(
yield dep;
}

// Add root dependencies to the exclude Map (because the parent is not handled by searchDeepDependencies)
// Add root dependencies to the exclude Map (because the parent is not handled by searchDependencies)
// if we skip this the code will fail to re-link properly dependencies in the following steps
const depsName = await Promise.all(
iter.map(dependencies.entries(), utils.getCleanDependencyName)
Expand Down
16 changes: 11 additions & 5 deletions workspaces/tree-walker/src/utils/mergeDependencies.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
// Import Third-party Dependencies
// import * as npm from "@npm/types";
import pacote from "pacote";

export type mergeDependency = "devDependencies" | "dependencies";
export type NpmDependency =
"dependencies" |
"devDependencies" |
"optionalDependencies" |
"peerDependencies" |
"bundleDependencies" |
"bundledDependencies";

export function mergeDependencies(
manifest: any,
types: mergeDependency[] = ["dependencies"] as const
manifest: Partial<pacote.AbbreviatedManifest & pacote.ManifestResult>,
types: NpmDependency[] = ["dependencies"] as const
) {
const dependencies = new Map();
const customResolvers = new Map();
const alias = new Map();

for (const fieldName of types) {
if (!Reflect.has(manifest, fieldName)) {
if (!(fieldName in manifest)) {
continue;
}
const dep = manifest[fieldName] as Record<string, string>;
Expand Down
40 changes: 23 additions & 17 deletions workspaces/tree-walker/src/utils/semver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import { getLocalRegistryURL } from "@nodesecure/npm-registry-sdk";
import { NPM_TOKEN } from "./index.js";

/**
* @param {!string} version semver range
* @returns {string} semver version
*
* @example
* cleanRange(">=1.5.0"); // 1.5.0
* cleanRange("^2.0.0"); // 2.0.0
Expand All @@ -26,32 +23,41 @@ export function cleanRange(
return version;
}

/**
* @param {!string} depName dependency name (WITHOUT version/range)
* @param {!string} range semver range, ex: >=1.5.0
*/
export async function getExpectedSemVer(
depName: string,
dependencyName: string,
range: string
): Promise<[version: string, isLatest: boolean]> {
try {
const { versions, "dist-tags": { latest } } = await pacote.packument(depName, {
...NPM_TOKEN, registry: getLocalRegistryURL()
const { versions, "dist-tags": { latest } } = await pacote.packument(dependencyName, {
...NPM_TOKEN,
registry: getLocalRegistryURL()
});
const currVersion = semver.maxSatisfying(Object.keys(versions), range);
const currVersion = semver.maxSatisfying(
Object.keys(versions),
range
);

return currVersion === null ? [latest, true] : [currVersion, semver.eq(latest, currVersion)];
return currVersion === null ?
[latest, true] :
[currVersion, semver.eq(latest, currVersion)];
}
catch (err) {
catch {
return [cleanRange(range), true];
}
}

export async function getCleanDependencyName(
dependency: [string, string]
dependency: [name: string, range: string]
): Promise<[string, string, boolean]> {
const [depName, range] = dependency;
const [depVer, isLatest] = await getExpectedSemVer(depName, range);
const [dependencyName, semVerRange] = dependency;
const [dependencyVersion, isLatest] = await getExpectedSemVer(
dependencyName,
semVerRange
);

return [`${depName}@${range}`, `${depName}@${depVer}`, isLatest];
return [
`${dependencyName}@${semVerRange}`,
`${dependencyName}@${dependencyVersion}`,
isLatest
];
}

0 comments on commit 1e3ac65

Please sign in to comment.