Skip to content

test: speed up npm bootstrap #954

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

Merged
merged 1 commit into from
Feb 5, 2020
Merged
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
53 changes: 50 additions & 3 deletions @packages/test/src/npm.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,62 @@
import execa from 'execa';
import path from 'path';
import fs from 'fs-extra';
import resolvePkg from 'resolve-pkg';

import * as git from './git';

export async function bootstrap(fixture: string, directory?: string) {
const cwd = await git.bootstrap(fixture, directory);
const manifestPath = path.join(cwd, 'package.json');
const targetModulesPath = path.join(cwd, 'node_modules');

if (await fs.pathExists(path.join(cwd, 'package.json'))) {
await execa('npm', ['install'], {cwd});
if (await fs.pathExists(manifestPath)) {
const {dependencies = {}, devDependencies = {}} = await fs.readJson(
manifestPath
);
const deps = Object.keys({...dependencies, ...devDependencies});
await Promise.all(
deps.map(async (dependency: any) => {
const sourcePath = resolvePkg(dependency);

if (!sourcePath) {
throw new Error(`Could not resolve dependency ${dependency}`);
}

const sourceModulesPath = findParentPath(sourcePath, 'node_modules');

if (!sourceModulesPath) {
throw new Error(`Could not determine node_modules for ${sourcePath}`);
}

const relativePath = path.relative(sourceModulesPath, sourcePath);
const targetPath = path.join(targetModulesPath, relativePath);

await fs.mkdirp(path.join(targetPath, '..'));
await fs.symlink(sourcePath, targetPath);
})
);
}

return cwd;
}

function findParentPath(path: string, dirname: string): string | undefined {
const rawFragments = path.split('/');

const {matched, fragments} = rawFragments.reduceRight(
({fragments, matched}, item) => {
if (item === dirname && !matched) {
return {fragments, matched: true};
}

if (!matched && fragments.length > 0) {
fragments.pop();
}

return {fragments, matched};
},
{fragments: rawFragments, matched: false}
);

return matched ? fragments.join('/') : undefined;
}