/
index.ts
98 lines (83 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
96
97
98
import fs from 'fs'
import path from 'path'
import { assertProject, type Modules, type Project } from '@pnpm/assert-project'
import { type ProjectManifest } from '@pnpm/types'
import uniqueString from 'unique-string'
import { sync as writeJson5File } from 'write-json5-file'
import { sync as writeYamlFile } from 'write-yaml-file'
import writePkg from 'write-pkg'
export type { Modules, Project }
export type ManifestFormat = 'JSON' | 'JSON5' | 'YAML'
// The testing folder should be outside of the project to avoid lookup in the project's node_modules
// Not using the OS temp directory due to issues on Windows CI.
const tmpPath = path.join(__dirname, `../../../../pnpm_tmp/${uniqueString()}`)
let dirNumber = 0
export function tempDir (chdir: boolean = true) {
dirNumber++
const dirname = dirNumber.toString()
const tmpDir = path.join(tmpPath, dirname)
fs.mkdirSync(tmpDir, { recursive: true })
if (chdir) process.chdir(tmpDir)
return tmpDir
}
interface LocationAndManifest {
location: string
package: ProjectManifest
}
export function preparePackages (
pkgs: Array<LocationAndManifest | ProjectManifest>,
opts?: {
manifestFormat?: ManifestFormat
tempDir?: string
}
) {
const pkgTmpPath = opts?.tempDir ?? path.join(tempDir(), 'project')
const manifestFormat = opts?.manifestFormat
const dirname = path.dirname(pkgTmpPath)
const result: { [name: string]: Project } = {}
const cwd = process.cwd()
for (const aPkg of pkgs) {
if (typeof (aPkg as LocationAndManifest).location === 'string') {
result[(aPkg as LocationAndManifest).package.name!] = prepare((aPkg as LocationAndManifest).package, {
manifestFormat,
tempDir: path.join(dirname, (aPkg as LocationAndManifest).location),
})
} else {
result[(aPkg as ProjectManifest).name!] = prepare(aPkg as ProjectManifest, {
manifestFormat,
tempDir: path.join(dirname, (aPkg as ProjectManifest).name!),
})
}
}
process.chdir(cwd)
return result
}
export function prepare (
manifest?: ProjectManifest,
opts?: {
manifestFormat?: ManifestFormat
tempDir?: string
}
) {
const dir = opts?.tempDir ?? path.join(tempDir(), 'project')
fs.mkdirSync(dir, { recursive: true })
switch (opts?.manifestFormat ?? 'JSON') {
case 'JSON':
writePkg.sync(dir, { name: 'project', version: '0.0.0', ...manifest } as any) // eslint-disable-line
break
case 'JSON5':
writeJson5File(path.join(dir, 'package.json5'), { name: 'project', version: '0.0.0', ...manifest } as any) // eslint-disable-line
break
case 'YAML':
writeYamlFile(path.join(dir, 'package.yaml'), { name: 'project', version: '0.0.0', ...manifest } as any) // eslint-disable-line
break
}
process.chdir(dir)
return assertProject(dir)
}
export function prepareEmpty () {
const pkgTmpPath = path.join(tempDir(), 'project')
fs.mkdirSync(pkgTmpPath, { recursive: true })
process.chdir(pkgTmpPath)
return assertProject(pkgTmpPath)
}