forked from conventional-changelog/commitlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnpm.ts
68 lines (54 loc) · 1.79 KB
/
npm.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import path from 'path';
import fs from 'fs-extra';
import resolvePkg from 'resolve-pkg';
import * as git from './git';
export async function installModules(cwd: string) {
const manifestPath = path.join(cwd, 'package.json');
const targetModulesPath = path.join(cwd, 'node_modules');
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);
})
);
}
}
export async function bootstrap(fixture: string, directory?: string) {
const cwd = await git.bootstrap(fixture, directory);
await installModules(cwd);
return cwd;
}
function findParentPath(
parentPath: string,
dirname: string
): string | undefined {
const rawFragments = parentPath.split(path.sep);
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(path.sep) : undefined;
}