/
monorepo.ts
68 lines (65 loc) · 2.37 KB
/
monorepo.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 is from '@sindresorhus/is';
import minimatch from 'minimatch';
import upath from 'upath';
import { logger } from '../../../logger';
import { SkipReason } from '../../../types';
import { PackageFile } from '../../common';
function matchesAnyPattern(val: string, patterns: string[]): boolean {
const res = patterns.some(
(pattern) => pattern === val + '/' || minimatch(val, pattern, { dot: true })
);
logger.trace({ val, patterns, res }, `matchesAnyPattern`);
return res;
}
export function detectMonorepos(packageFiles: Partial<PackageFile>[]): void {
logger.debug('Detecting Lerna and Yarn Workspaces');
for (const p of packageFiles) {
const {
packageFile,
npmLock,
yarnLock,
lernaDir,
lernaClient,
lernaPackages,
yarnWorkspacesPackages,
} = p;
const basePath = path.dirname(packageFile);
const packages = yarnWorkspacesPackages || lernaPackages;
if (packages && packages.length) {
logger.debug(
{ packageFile, yarnWorkspacesPackages, lernaPackages },
'Found monorepo packages with base path ' + JSON.stringify(basePath)
);
const internalPackagePatterns = (is.array(packages)
? packages
: [packages]
).map((pattern) => upath.join(basePath, pattern));
const internalPackageFiles = packageFiles.filter((sp) =>
matchesAnyPattern(path.dirname(sp.packageFile), internalPackagePatterns)
);
const internalPackageNames = internalPackageFiles
.map((sp) => sp.packageJsonName)
.filter(Boolean);
p.deps?.forEach((dep) => {
if (internalPackageNames.includes(dep.depName)) {
dep.skipReason = SkipReason.InternalPackage; // eslint-disable-line no-param-reassign
}
});
for (const subPackage of internalPackageFiles) {
subPackage.lernaDir = lernaDir;
subPackage.lernaClient = lernaClient;
subPackage.yarnLock = subPackage.yarnLock || yarnLock;
subPackage.npmLock = subPackage.npmLock || npmLock;
if (subPackage.yarnLock) {
subPackage.hasYarnWorkspaces = !!yarnWorkspacesPackages;
}
subPackage.deps?.forEach((dep) => {
if (internalPackageNames.includes(dep.depName)) {
dep.skipReason = SkipReason.InternalPackage; // eslint-disable-line no-param-reassign
}
});
}
}
}
}