-
-
Notifications
You must be signed in to change notification settings - Fork 942
/
index.ts
130 lines (125 loc) · 4.23 KB
/
index.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { promises as fs } from 'fs'
import path from 'path'
import {
type CafsLocker,
createCafs,
getFilePathByModeInCafs,
} from '@pnpm/store.cafs'
import type { Cafs, PackageFilesResponse } from '@pnpm/cafs-types'
import { createIndexedPkgImporter } from '@pnpm/fs.indexed-pkg-importer'
import {
type ImportIndexedPackage,
type ImportIndexedPackageAsync,
type ImportPackageFunction,
type ImportPackageFunctionAsync,
type PackageFileInfo,
} from '@pnpm/store-controller-types'
import memoize from 'mem'
import pathTemp from 'path-temp'
import mapValues from 'ramda/src/map'
export { type CafsLocker }
export function createPackageImporterAsync (
opts: {
importIndexedPackage?: ImportIndexedPackageAsync
packageImportMethod?: 'auto' | 'hardlink' | 'copy' | 'clone' | 'clone-or-copy'
cafsDir: string
}
): ImportPackageFunctionAsync {
const cachedImporterCreator = opts.importIndexedPackage
? () => opts.importIndexedPackage!
: memoize(createIndexedPkgImporter)
const packageImportMethod = opts.packageImportMethod
const gfm = getFlatMap.bind(null, opts.cafsDir)
return async (to, opts) => {
const { filesMap, isBuilt } = gfm(opts.filesResponse, opts.sideEffectsCacheKey)
const pkgImportMethod = (opts.requiresBuild && !isBuilt)
? 'clone-or-copy'
: (opts.filesResponse.packageImportMethod ?? packageImportMethod)
const impPkg = cachedImporterCreator(pkgImportMethod)
const importMethod = await impPkg(to, {
disableRelinkFromStore: opts.disableRelinkFromStore,
filesMap,
fromStore: opts.filesResponse.fromStore,
force: opts.force,
keepModulesDir: Boolean(opts.keepModulesDir),
})
return { importMethod, isBuilt }
}
}
function createPackageImporter (
opts: {
importIndexedPackage?: ImportIndexedPackage
packageImportMethod?: 'auto' | 'hardlink' | 'copy' | 'clone' | 'clone-or-copy'
cafsDir: string
}
): ImportPackageFunction {
const cachedImporterCreator = opts.importIndexedPackage
? () => opts.importIndexedPackage!
: memoize(createIndexedPkgImporter)
const packageImportMethod = opts.packageImportMethod
const gfm = getFlatMap.bind(null, opts.cafsDir)
return (to, opts) => {
const { filesMap, isBuilt } = gfm(opts.filesResponse, opts.sideEffectsCacheKey)
const pkgImportMethod = (opts.requiresBuild && !isBuilt)
? 'clone-or-copy'
: (opts.filesResponse.packageImportMethod ?? packageImportMethod)
const impPkg = cachedImporterCreator(pkgImportMethod)
const importMethod = impPkg(to, {
disableRelinkFromStore: opts.disableRelinkFromStore,
filesMap,
fromStore: opts.filesResponse.fromStore,
force: opts.force,
keepModulesDir: Boolean(opts.keepModulesDir),
})
return { importMethod, isBuilt }
}
}
function getFlatMap (
cafsDir: string,
filesResponse: PackageFilesResponse,
targetEngine?: string
): { filesMap: Record<string, string>, isBuilt: boolean } {
let isBuilt!: boolean
let filesIndex!: Record<string, PackageFileInfo>
if (targetEngine && ((filesResponse.sideEffects?.[targetEngine]) != null)) {
filesIndex = filesResponse.sideEffects?.[targetEngine]
isBuilt = true
} else if (!filesResponse.unprocessed) {
return {
filesMap: filesResponse.filesIndex,
isBuilt: false,
}
} else {
filesIndex = filesResponse.filesIndex
isBuilt = false
}
const filesMap = mapValues(({ integrity, mode }) => getFilePathByModeInCafs(cafsDir, integrity, mode), filesIndex)
return { filesMap, isBuilt }
}
export function createCafsStore (
storeDir: string,
opts?: {
ignoreFile?: (filename: string) => boolean
importPackage?: ImportIndexedPackage
packageImportMethod?: 'auto' | 'hardlink' | 'copy' | 'clone' | 'clone-or-copy'
cafsLocker?: CafsLocker
}
): Cafs {
const cafsDir = path.join(storeDir, 'files')
const baseTempDir = path.join(storeDir, 'tmp')
const importPackage = createPackageImporter({
importIndexedPackage: opts?.importPackage,
packageImportMethod: opts?.packageImportMethod,
cafsDir,
})
return {
...createCafs(cafsDir, opts),
cafsDir,
importPackage,
tempDir: async () => {
const tmpDir = pathTemp(baseTempDir)
await fs.mkdir(tmpDir, { recursive: true })
return tmpDir
},
}
}