cli: refactory versioning mapDependencies to use get-packages

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2022-01-07 11:59:13 +01:00
parent 0555c594e4
commit f202e2a90b
2 changed files with 16 additions and 24 deletions
@@ -17,7 +17,6 @@
import mockFs from 'mock-fs';
import path from 'path';
import * as runObj from '../run';
import { paths } from '../paths';
import { fetchPackageInfo, mapDependencies } from './packages';
import { NotFoundError } from '../errors';
@@ -58,22 +57,19 @@ describe('mapDependencies', () => {
});
it('should read dependencies', async () => {
// Make sure all modules involved in package discovery are in the module cache before we mock fs
const { Project } = require('@lerna/project');
const project = new Project(paths.targetDir);
await project.getPackages();
mockFs({
'lerna.json': JSON.stringify({
packages: ['pkgs/*'],
'/root/package.json': JSON.stringify({
workspaces: {
packages: ['pkgs/*'],
},
}),
'pkgs/a/package.json': JSON.stringify({
'/root/pkgs/a/package.json': JSON.stringify({
name: 'a',
dependencies: {
'@backstage/core': '1 || 2',
},
}),
'pkgs/b/package.json': JSON.stringify({
'/root/pkgs/b/package.json': JSON.stringify({
name: 'b',
dependencies: {
'@backstage/core': '3',
@@ -82,10 +78,7 @@ describe('mapDependencies', () => {
}),
});
const dependencyMap = await mapDependencies(
paths.targetDir,
'@backstage/*',
);
const dependencyMap = await mapDependencies('/root', '@backstage/*');
expect(Array.from(dependencyMap)).toEqual([
[
'@backstage/core',
@@ -93,12 +86,12 @@ describe('mapDependencies', () => {
{
name: 'a',
range: '1 || 2',
location: path.resolve('pkgs', 'a'),
location: path.normalize('/root/pkgs/a'),
},
{
name: 'b',
range: '3',
location: path.resolve('pkgs', 'b'),
location: path.normalize('/root/pkgs/b'),
},
],
],
@@ -108,7 +101,7 @@ describe('mapDependencies', () => {
{
name: 'b',
range: '^0',
location: path.resolve('pkgs', 'b'),
location: path.normalize('/root/pkgs/b'),
},
],
],
+6 -7
View File
@@ -14,6 +14,7 @@
* limitations under the License.
*/
import minimatch from 'minimatch';
import { getPackages } from '@manypkg/get-packages';
import { runPlain } from '../../lib/run';
import { NotFoundError } from '../errors';
@@ -22,7 +23,7 @@ const DEP_TYPES = [
'devDependencies',
'peerDependencies',
'optionalDependencies',
];
] as const;
// Package data as returned by `yarn info`
export type YarnInfoInspectData = {
@@ -66,14 +67,12 @@ export async function mapDependencies(
targetDir: string,
pattern: string,
): Promise<Map<string, PkgVersionInfo[]>> {
const { Project } = require('@lerna/project');
const project = new Project(targetDir);
const packages = await project.getPackages();
const { packages } = await getPackages(targetDir);
const dependencyMap = new Map<string, PkgVersionInfo[]>();
for (const pkg of packages) {
const deps = DEP_TYPES.flatMap(
t => Object.entries(pkg.get(t) ?? {}) as [string, string][],
t => Object.entries(pkg.packageJson[t] ?? {}) as [string, string][],
);
for (const [name, range] of deps) {
@@ -82,8 +81,8 @@ export async function mapDependencies(
name,
(dependencyMap.get(name) ?? []).concat({
range,
name: pkg.name,
location: pkg.location,
name: pkg.packageJson.name,
location: pkg.dir,
}),
);
}