-
-
Notifications
You must be signed in to change notification settings - Fork 933
/
index.ts
95 lines (92 loc) · 2.94 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
import { promises as fs } from 'fs'
import path from 'path'
import {
createCafs,
getFilePathByModeInCafs,
} from '@pnpm/cafs'
import type { Cafs, PackageFilesResponse } from '@pnpm/cafs-types'
import { createIndexedPkgImporter } from '@pnpm/fs.indexed-pkg-importer'
import {
ImportIndexedPackage,
ImportPackageFunction,
PackageFileInfo,
} from '@pnpm/store-controller-types'
import memoize from 'mem'
import pathTemp from 'path-temp'
import mapValues from 'ramda/src/map'
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 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, {
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 } {
if (filesResponse.local) {
return {
filesMap: filesResponse.filesIndex,
isBuilt: false,
}
}
let isBuilt!: boolean
let filesIndex!: Record<string, PackageFileInfo>
if (targetEngine && ((filesResponse.sideEffects?.[targetEngine]) != null)) {
filesIndex = filesResponse.sideEffects?.[targetEngine]
isBuilt = true
} 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'
}
): 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?.ignoreFile),
cafsDir,
importPackage,
tempDir: async () => {
const tmpDir = pathTemp(baseTempDir)
await fs.mkdir(tmpDir, { recursive: true })
return tmpDir
},
}
}