Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support of lockfile v5 #65

Merged
merged 4 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@
"@npmcli/[email protected]": "patches/@[email protected]",
"[email protected]": "patches/[email protected]",
"@yarnpkg/[email protected]": "patches/@[email protected]",
"@pnpm/[email protected]": "patches/@[email protected]"
"@pnpm/[email protected]": "patches/@[email protected]",
"@pnpm/[email protected]": "patches/@[email protected]",
"@pnpm/[email protected]": "patches/@[email protected]"
}
},
"overrides": {
Expand Down
5 changes: 3 additions & 2 deletions packages/tools/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
"@nolyfill/internal": "workspace:*",
"@nolyfill/promise.any": "workspace:*",
"@npmcli/arborist": "^6.3.0",
"@pnpm/list": "^10.1.1",
"@pnpm/workspace.find-packages": "^2.0.4",
"@pnpm/list": "^10.1.2",
"@pnpm/list--old": "npm:@pnpm/list@^9.1.10",
"@pnpm/workspace.find-packages": "^2.0.5",
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^15.2.3",
Expand Down
12 changes: 12 additions & 0 deletions packages/tools/cli/src/lockfile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type { PackageManager } from '../package-manager';
import { buildPNPMDepTree } from './pnpm';
import { buildNPMDepTree } from './npm';
import { buildYarnDepTree } from './yarn';
import fsp from 'fs/promises';
import path from 'path';

export interface PackageLockDeps {
[depName: string]: PackageLockDep
Expand Down Expand Up @@ -29,3 +31,13 @@ export function buildDepTrees(packageManager: PackageManager, dir: string) {
throw new Error(`Unknown package manager: ${packageManager as string}`);
}
}

export async function getPNPMLockfileVersion(dir: string) {
const content = await fsp.readFile(path.resolve(dir, 'pnpm-lock.yaml'), 'utf-8');
const [, lockfileVersion] = content.match(/^lockfileVersion: '?(\d*(?:.\d*)?)'?$/m) ?? [];

if (!lockfileVersion)
throw new Error(`Can't detect lockfile version`)

return lockfileVersion;
}
23 changes: 20 additions & 3 deletions packages/tools/cli/src/lockfile/pnpm.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { searchForPackages } from '@pnpm/list';
import { searchForPackages as searchForPackages_Old } from '@pnpm/list--old';
import type { PackageNode } from '../types';
import path from 'path';
import { fileExists } from '@nolyfill/internal';
import { findWorkspacePackagesNoCheck } from '@pnpm/workspace.find-packages';
import { cache } from '../lib/cache';
import { getPNPMLockfileVersion } from '.';

export const buildPNPMDepTree = cache(async (dirPath: string): Promise<PackageNode[]> => {
const dirPaths = [dirPath];
Expand All @@ -13,15 +15,30 @@ export const buildPNPMDepTree = cache(async (dirPath: string): Promise<PackageNo
dirPaths.push(...allProjects.map((project) => project.dir));
}

const result = await searchForPackages(['*'], dirPaths, {
const searchNew = () => searchForPackages(['*'], dirPaths, {
depth: Infinity,
lockfileDir: dirPath,
include: {
dependencies: true,
devDependencies: true,
optionalDependencies: false
}
});
},
virtualStoreDirMaxLength: Infinity
})

const searchOld = () => searchForPackages_Old(['*'], dirPaths, {
depth: Infinity,
lockfileDir: dirPath,
include: {
dependencies: true,
devDependencies: true,
optionalDependencies: false
},
})

const lockfileVersion = await getPNPMLockfileVersion(dirPath)

const result = await ((+lockfileVersion) < 6 ? searchOld() : searchNew())

return result.flatMap((dep) => {
return [...(dep.dependencies ?? []), ...(dep.devDependencies ?? [])];
Expand Down
221 changes: 221 additions & 0 deletions patches/@[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
diff --git a/lib/index.js b/lib/index.js
index 0a3e9ff710e410973c1be1f30d69dba04e34e417..2c9de253ad710b83be002e3c912a06aa4ae7cb42 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -3,52 +3,53 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
-exports.list = exports.listForPackages = exports.searchForPackages = exports.flattenSearchedPackages = exports.renderTree = exports.renderParseable = exports.renderJson = void 0;
-const path_1 = __importDefault(require("path"));
+// exports.list = exports.listForPackages = exports.searchForPackages = exports.flattenSearchedPackages = exports.renderTree = exports.renderParseable = exports.renderJson = void 0;
+exports.searchForPackages = void 0;
+// const path_1 = __importDefault(require("path"));
const read_project_manifest_1 = require("@pnpm/read-project-manifest");
const reviewing_dependencies_hierarchy_1 = require("@pnpm/reviewing.dependencies-hierarchy");
-const renderJson_1 = require("./renderJson");
-Object.defineProperty(exports, "renderJson", { enumerable: true, get: function () { return renderJson_1.renderJson; } });
-const renderParseable_1 = require("./renderParseable");
-Object.defineProperty(exports, "renderParseable", { enumerable: true, get: function () { return renderParseable_1.renderParseable; } });
-const renderTree_1 = require("./renderTree");
-Object.defineProperty(exports, "renderTree", { enumerable: true, get: function () { return renderTree_1.renderTree; } });
-const pruneTree_1 = require("./pruneTree");
-const DEFAULTS = {
- alwaysPrintRootPackage: true,
- depth: 0,
- long: false,
- registries: undefined,
- reportAs: 'tree',
- showExtraneous: true,
-};
-function flattenSearchedPackages(pkgs, opts) {
- const flattedPkgs = [];
- for (const pkg of pkgs) {
- _walker([
- ...(pkg.optionalDependencies ?? []),
- ...(pkg.dependencies ?? []),
- ...(pkg.devDependencies ?? []),
- ...(pkg.unsavedDependencies ?? []),
- ], path_1.default.relative(opts.lockfileDir, pkg.path) || '.');
- }
- return flattedPkgs;
- function _walker(packages, depPath) {
- for (const pkg of packages) {
- const nextDepPath = `${depPath} > ${pkg.name}@${pkg.version}`;
- if (pkg.dependencies?.length) {
- _walker(pkg.dependencies, nextDepPath);
- }
- else {
- flattedPkgs.push({
- depPath: nextDepPath,
- ...pkg,
- });
- }
- }
- }
-}
-exports.flattenSearchedPackages = flattenSearchedPackages;
+// const renderJson_1 = require("./renderJson");
+// Object.defineProperty(exports, "renderJson", { enumerable: true, get: function () { return renderJson_1.renderJson; } });
+// const renderParseable_1 = require("./renderParseable");
+// Object.defineProperty(exports, "renderParseable", { enumerable: true, get: function () { return renderParseable_1.renderParseable; } });
+// const renderTree_1 = require("./renderTree");
+// Object.defineProperty(exports, "renderTree", { enumerable: true, get: function () { return renderTree_1.renderTree; } });
+// const pruneTree_1 = require("./pruneTree");
+// const DEFAULTS = {
+// alwaysPrintRootPackage: true,
+// depth: 0,
+// long: false,
+// registries: undefined,
+// reportAs: 'tree',
+// showExtraneous: true,
+// };
+// function flattenSearchedPackages(pkgs, opts) {
+// const flattedPkgs = [];
+// for (const pkg of pkgs) {
+// _walker([
+// ...(pkg.optionalDependencies ?? []),
+// ...(pkg.dependencies ?? []),
+// ...(pkg.devDependencies ?? []),
+// ...(pkg.unsavedDependencies ?? []),
+// ], path_1.default.relative(opts.lockfileDir, pkg.path) || '.');
+// }
+// return flattedPkgs;
+// function _walker(packages, depPath) {
+// for (const pkg of packages) {
+// const nextDepPath = `${depPath} > ${pkg.name}@${pkg.version}`;
+// if (pkg.dependencies?.length) {
+// _walker(pkg.dependencies, nextDepPath);
+// }
+// else {
+// flattedPkgs.push({
+// depPath: nextDepPath,
+// ...pkg,
+// });
+// }
+// }
+// }
+// }
+// exports.flattenSearchedPackages = flattenSearchedPackages;
async function searchForPackages(packages, projectPaths, opts) {
const search = (0, reviewing_dependencies_hierarchy_1.createPackagesSearcher)(packages);
return Promise.all(Object.entries(await (0, reviewing_dependencies_hierarchy_1.buildDependenciesHierarchy)(projectPaths, {
@@ -72,61 +73,61 @@ async function searchForPackages(packages, projectPaths, opts) {
}));
}
exports.searchForPackages = searchForPackages;
-async function listForPackages(packages, projectPaths, maybeOpts) {
- const opts = { ...DEFAULTS, ...maybeOpts };
- const pkgs = await searchForPackages(packages, projectPaths, opts);
- const prunedPkgs = (0, pruneTree_1.pruneDependenciesTrees)(pkgs ?? null, 10);
- const print = getPrinter(opts.reportAs);
- return print(prunedPkgs, {
- alwaysPrintRootPackage: opts.alwaysPrintRootPackage,
- depth: opts.depth,
- long: opts.long,
- search: Boolean(packages.length),
- showExtraneous: opts.showExtraneous,
- });
-}
-exports.listForPackages = listForPackages;
-async function list(projectPaths, maybeOpts) {
- const opts = { ...DEFAULTS, ...maybeOpts };
- const pkgs = await Promise.all(Object.entries(opts.depth === -1
- ? projectPaths.reduce((acc, projectPath) => {
- acc[projectPath] = {};
- return acc;
- }, {})
- : await (0, reviewing_dependencies_hierarchy_1.buildDependenciesHierarchy)(projectPaths, {
- depth: opts.depth,
- include: maybeOpts?.include,
- lockfileDir: maybeOpts?.lockfileDir,
- onlyProjects: maybeOpts?.onlyProjects,
- registries: opts.registries,
- modulesDir: opts.modulesDir,
- virtualStoreDirMaxLength: opts.virtualStoreDirMaxLength,
- }))
- .map(async ([projectPath, dependenciesHierarchy]) => {
- const entryPkg = await (0, read_project_manifest_1.safeReadProjectManifestOnly)(projectPath) ?? {};
- return {
- name: entryPkg.name,
- version: entryPkg.version,
- private: entryPkg.private,
- path: projectPath,
- ...dependenciesHierarchy,
- };
- }));
- const print = getPrinter(opts.reportAs);
- return print(pkgs, {
- alwaysPrintRootPackage: opts.alwaysPrintRootPackage,
- depth: opts.depth,
- long: opts.long,
- search: false,
- showExtraneous: opts.showExtraneous,
- });
-}
-exports.list = list;
-function getPrinter(reportAs) {
- switch (reportAs) {
- case 'parseable': return renderParseable_1.renderParseable;
- case 'json': return renderJson_1.renderJson;
- case 'tree': return renderTree_1.renderTree;
- }
-}
+// async function listForPackages(packages, projectPaths, maybeOpts) {
+// const opts = { ...DEFAULTS, ...maybeOpts };
+// const pkgs = await searchForPackages(packages, projectPaths, opts);
+// const prunedPkgs = (0, pruneTree_1.pruneDependenciesTrees)(pkgs ?? null, 10);
+// const print = getPrinter(opts.reportAs);
+// return print(prunedPkgs, {
+// alwaysPrintRootPackage: opts.alwaysPrintRootPackage,
+// depth: opts.depth,
+// long: opts.long,
+// search: Boolean(packages.length),
+// showExtraneous: opts.showExtraneous,
+// });
+// }
+// exports.listForPackages = listForPackages;
+// async function list(projectPaths, maybeOpts) {
+// const opts = { ...DEFAULTS, ...maybeOpts };
+// const pkgs = await Promise.all(Object.entries(opts.depth === -1
+// ? projectPaths.reduce((acc, projectPath) => {
+// acc[projectPath] = {};
+// return acc;
+// }, {})
+// : await (0, reviewing_dependencies_hierarchy_1.buildDependenciesHierarchy)(projectPaths, {
+// depth: opts.depth,
+// include: maybeOpts?.include,
+// lockfileDir: maybeOpts?.lockfileDir,
+// onlyProjects: maybeOpts?.onlyProjects,
+// registries: opts.registries,
+// modulesDir: opts.modulesDir,
+// virtualStoreDirMaxLength: opts.virtualStoreDirMaxLength,
+// }))
+// .map(async ([projectPath, dependenciesHierarchy]) => {
+// const entryPkg = await (0, read_project_manifest_1.safeReadProjectManifestOnly)(projectPath) ?? {};
+// return {
+// name: entryPkg.name,
+// version: entryPkg.version,
+// private: entryPkg.private,
+// path: projectPath,
+// ...dependenciesHierarchy,
+// };
+// }));
+// const print = getPrinter(opts.reportAs);
+// return print(pkgs, {
+// alwaysPrintRootPackage: opts.alwaysPrintRootPackage,
+// depth: opts.depth,
+// long: opts.long,
+// search: false,
+// showExtraneous: opts.showExtraneous,
+// });
+// }
+// exports.list = list;
+// function getPrinter(reportAs) {
+// switch (reportAs) {
+// case 'parseable': return renderParseable_1.renderParseable;
+// case 'json': return renderJson_1.renderJson;
+// case 'tree': return renderTree_1.renderTree;
+// }
+// }
//# sourceMappingURL=index.js.map
Loading
Loading