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

peerDep correctness #50

Merged
merged 14 commits into from
Sep 13, 2021
30 changes: 26 additions & 4 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import tmp = require('tmp');
import fs = require('fs-extra');
import path = require('path');
import resolvePackagePath = require('resolve-package-path');
import CacheGroup = require('resolve-package-path/lib/cache-group');
import { PackageJson } from 'type-fest';
import { readdirSync, statSync } from 'fs';

Expand Down Expand Up @@ -127,6 +128,12 @@ export class Project {

private dependencyLinks: Map<string, { dir: string; requestedRange: string }> = new Map();
private linkIsDevDependency: Set<string> = new Set();
private usingHardLinks = true;

// we keep our own package resolution cache because the default global one
// could get polluted by us resolving test-specific things that will change on
// subsequent tests.
private resolutionCache = new CacheGroup();

constructor(
name?: string,
Expand Down Expand Up @@ -262,7 +269,7 @@ export class Project {
}

private writeLinkedPackage(name: string, target: string) {
let targetPkg = require(path.join(target, 'package.json'));
let targetPkg = fs.readJsonSync(`${target}/package.json`);
let peers = new Set(Object.keys(targetPkg.peerDependencies ?? {}));
let destination = path.join(this.baseDir, 'node_modules', name);

Expand All @@ -281,7 +288,7 @@ export class Project {
if (peers.has(depName)) {
continue;
}
let depTarget = resolvePackagePath(depName, target);
let depTarget = resolvePackagePath(depName, target, this.resolutionCache);
if (!depTarget) {
throw new Error(`package ${name} in ${target} depends on ${depName} but we could not resolve it`);
ef4 marked this conversation as resolved.
Show resolved Hide resolved
}
Expand All @@ -303,9 +310,24 @@ export class Project {
if (stat.isDirectory()) {
this.hardLinkContents(path.join(target, name), path.join(destination, name));
stefanpenner marked this conversation as resolved.
Show resolved Hide resolved
ef4 marked this conversation as resolved.
Show resolved Hide resolved
} else {
fs.ensureLinkSync(path.join(target, name), path.join(destination, name));
this.hardLinkFile(path.join(target, name), path.join(destination, name));
}
}
}

private hardLinkFile(source: string, destination: string) {
if (this.usingHardLinks) {
try {
fs.ensureLinkSync(source, destination);
return;
} catch (err: any) {
if (err.code !== 'EXDEV') {
throw err;
}
this.usingHardLinks = false;
}
}
fs.copySync(source, destination);
ef4 marked this conversation as resolved.
Show resolved Hide resolved
}

static fromDir(root: string, opts?: ReadDirOpts): Project {
Expand Down Expand Up @@ -482,7 +504,7 @@ export class Project {
this.removeDevDependency(name);
let dir: string;
if ('baseDir' in opts) {
let pkgJSONPath = resolvePackagePath(opts.resolveName || name, opts.baseDir);
let pkgJSONPath = resolvePackagePath(opts.resolveName || name, opts.baseDir, this.resolutionCache);
if (!pkgJSONPath) {
throw new Error(`failed to locate ${opts.resolveName || name} in ${opts.baseDir}`);
}
Expand Down