/
yarn.ts
66 lines (61 loc) · 2.16 KB
/
yarn.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
import is from '@sindresorhus/is';
import { miscUtils, structUtils } from '@yarnpkg/core';
import { parseSyml } from '@yarnpkg/parsers';
import { logger } from '../../../logger';
import {
getSiblingFileName,
localPathExists,
readLocalFile,
} from '../../../util/fs';
import type { LockFile } from './types';
export async function getYarnLock(filePath: string): Promise<LockFile> {
const yarnLockRaw = await readLocalFile(filePath, 'utf8');
try {
const parsed = parseSyml(yarnLockRaw);
const lockedVersions: Record<string, string> = {};
let lockfileVersion: number;
for (const [key, val] of Object.entries(parsed)) {
if (key === '__metadata') {
// yarn 2
lockfileVersion = parseInt(val.cacheKey, 10);
} else {
for (const entry of key.split(', ')) {
const { scope, name, range } = structUtils.parseDescriptor(entry);
const packageName = scope ? `@${scope}/${name}` : name;
const { selector } = structUtils.parseRange(range);
logger.trace({ entry, version: val.version });
lockedVersions[packageName + '@' + selector] = parsed[key].version;
}
}
}
return {
isYarn1: !('__metadata' in parsed),
lockfileVersion,
lockedVersions,
};
} catch (err) {
logger.debug({ filePath, err }, 'Warning: Exception parsing yarn.lock');
return { isYarn1: true, lockedVersions: {} };
}
}
export function getZeroInstallPaths(yarnrcYml: string): string[] {
const conf = parseSyml(yarnrcYml);
const paths = [conf.cacheFolder || './.yarn/cache', '.pnp.cjs', '.pnp.js'];
if (miscUtils.tryParseOptionalBoolean(conf.pnpEnableInlining) === false) {
paths.push(conf.pnpDataPath || './.pnp.data.json');
}
return paths;
}
export async function isZeroInstall(yarnrcYmlPath: string): Promise<boolean> {
const yarnrcYml = await readLocalFile(yarnrcYmlPath, 'utf8');
if (is.string(yarnrcYml)) {
const paths = getZeroInstallPaths(yarnrcYml);
for (const p of paths) {
if (await localPathExists(getSiblingFileName(yarnrcYmlPath, p))) {
logger.debug(`Detected Yarn zero-install in ${p}`);
return true;
}
}
}
return false;
}