-
-
Notifications
You must be signed in to change notification settings - Fork 955
/
read.ts
107 lines (102 loc) · 2.78 KB
/
read.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import {
LOCKFILE_VERSION,
WANTED_LOCKFILE,
} from '@pnpm/constants'
import { Lockfile } from '@pnpm/lockfile-types'
import { DEPENDENCIES_FIELDS } from '@pnpm/types'
import path = require('path')
import readYamlFile from 'read-yaml-file'
import { LockfileBreakingChangeError } from './errors'
import logger from './logger'
export async function readCurrentLockfile (
virtualStoreDir: string,
opts: {
wantedVersion?: number,
ignoreIncompatible: boolean,
},
): Promise<Lockfile | null> {
const lockfilePath = path.join(virtualStoreDir, 'lock.yaml')
return _read(lockfilePath, virtualStoreDir, opts)
}
export async function readWantedLockfile (
pkgPath: string,
opts: {
wantedVersion?: number,
ignoreIncompatible: boolean,
},
): Promise<Lockfile | null> {
const lockfilePath = path.join(pkgPath, WANTED_LOCKFILE)
return _read(lockfilePath, pkgPath, opts)
}
async function _read (
lockfilePath: string,
prefix: string,
opts: {
wantedVersion?: number,
ignoreIncompatible: boolean,
},
): Promise<Lockfile | null> {
let lockfile
try {
lockfile = await readYamlFile<Lockfile>(lockfilePath)
} catch (err) {
if ((err as NodeJS.ErrnoException).code !== 'ENOENT') {
throw err
}
return null
}
// tslint:disable:no-string-literal
if (typeof lockfile?.['specifiers'] !== 'undefined') {
lockfile.importers = {
'.': {
specifiers: lockfile['specifiers'],
},
}
delete lockfile['specifiers']
for (const depType of DEPENDENCIES_FIELDS) {
if (lockfile[depType]) {
lockfile.importers['.'][depType] = lockfile[depType]
delete lockfile[depType]
}
}
}
if (lockfile) {
// tslint:enable:no-string-literal
if (typeof opts.wantedVersion !== 'number' || Math.floor(lockfile.lockfileVersion) === Math.floor(opts.wantedVersion)) {
if (typeof opts.wantedVersion === 'number' && lockfile.lockfileVersion > opts.wantedVersion) {
logger.warn({
message: `Your ${WANTED_LOCKFILE} was generated by a newer version of pnpm. ` +
`It is a compatible version but it might get downgraded to version ${opts.wantedVersion}`,
prefix,
})
}
return lockfile
}
}
if (opts.ignoreIncompatible) {
logger.warn({
message: `Ignoring not compatible lockfile at ${lockfilePath}`,
prefix,
})
return null
}
throw new LockfileBreakingChangeError(lockfilePath)
}
export function createLockfileObject (
importerIds: string[],
opts: {
lockfileVersion: number,
},
) {
const importers = importerIds.reduce((acc, importerId) => {
acc[importerId] = {
dependencies: {},
specifiers: {},
}
return acc
}, {})
return {
importers,
lockfileVersion: opts.lockfileVersion || LOCKFILE_VERSION,
}
}