/
index.ts
240 lines (234 loc) · 7.67 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import fs from 'fs'
import path from 'path'
import { readWantedLockfile, Lockfile } from '@pnpm/lockfile-file'
import { ProjectManifest } from '@pnpm/types'
import isSubdir from 'is-subdir'
import loadJsonFile from 'load-json-file'
import normalizePath from 'normalize-path'
import exists from 'path-exists'
import writeJsonFile from 'write-json-file'
const NEXT_TAG = 'next-7'
export default async (workspaceDir: string) => {
const pnpmManifest = loadJsonFile.sync(path.join(workspaceDir, 'packages/pnpm/package.json'))
const pnpmVersion = pnpmManifest!['version'] // eslint-disable-line
const pnpmMajorKeyword = `pnpm${pnpmVersion.split('.')[0]}`
const pkgsDir = path.join(workspaceDir, 'packages')
const lockfile = await readWantedLockfile(workspaceDir, { ignoreIncompatible: false })
if (lockfile == null) {
throw new Error('no lockfile found')
}
return {
'package.json': (manifest: ProjectManifest & { keywords?: string[] }, dir: string) => {
if (manifest.name === 'monorepo-root') {
manifest.scripts!['release'] = `pnpm --filter=@pnpm/exe publish --tag=${NEXT_TAG} --access=public && pnpm publish --filter=!pnpm --filter=!@pnpm/exe --access=public && pnpm publish --filter=pnpm --tag=${NEXT_TAG} --access=public`
return manifest
}
if (!isSubdir(pkgsDir, dir)) {
if (manifest.name) {
manifest.devDependencies = {
...manifest.devDependencies,
[manifest.name]: `workspace:*`,
}
}
return manifest
}
if (manifest.name && manifest.name !== 'pnpm') {
manifest.devDependencies = {
...manifest.devDependencies,
[manifest.name]: `workspace:${manifest.version}`,
}
}
manifest.keywords = [
pnpmMajorKeyword,
...(manifest.keywords ?? []).filter((keyword) => !/^pnpm[0-9]+$/.test(keyword)),
]
if (dir.includes('artifacts') || manifest.name === '@pnpm/exe') {
manifest.version = pnpmVersion
if (manifest.name === '@pnpm/exe') {
for (const depName of ['@pnpm/linux-arm64', '@pnpm/linux-x64', '@pnpm/win-x64', '@pnpm/macos-x64', '@pnpm/macos-arm64']) {
manifest.optionalDependencies![depName] = `workspace:${pnpmVersion}`
}
}
return manifest
}
return updateManifest(workspaceDir, manifest, dir)
},
'tsconfig.json': updateTSConfig.bind(null, {
lockfile,
workspaceDir,
}),
}
}
async function updateTSConfig (
context: {
lockfile: Lockfile
workspaceDir: string
},
tsConfig: object,
dir: string,
manifest: ProjectManifest
) {
if (tsConfig == null) return tsConfig
if (manifest.name === '@pnpm/tsconfig') return tsConfig
const relative = normalizePath(path.relative(context.workspaceDir, dir))
const importer = context.lockfile.importers[relative]
if (!importer) return tsConfig
const deps = {
...importer.dependencies,
...importer.devDependencies,
}
const references = [] as Array<{ path: string }>
for (const [depName, spec] of Object.entries(deps)) {
if (!spec.startsWith('link:') || spec.length === 5) continue
const relativePath = spec.slice(5)
if (!await exists(path.join(dir, relativePath, 'tsconfig.json'))) continue
if (
depName === '@pnpm/package-store' && (
manifest.name === '@pnpm/git-fetcher' ||
manifest.name === '@pnpm/tarball-fetcher' ||
manifest.name === '@pnpm/package-requester'
)
) {
// This is to avoid a circular graph (which TypeScript references do not support.
continue
}
references.push({ path: relativePath })
}
await writeJsonFile(path.join(dir, 'tsconfig.lint.json'), {
extends: './tsconfig.json',
include: [
'src/**/*.ts',
'test/**/*.ts',
'../../typings/**/*.d.ts',
],
}, { indent: 2 })
return {
...tsConfig,
compilerOptions: {
...tsConfig['compilerOptions'],
rootDir: 'src',
},
references: references.sort((r1, r2) => r1.path.localeCompare(r2.path)),
}
}
let registryMockPort = 7769
async function updateManifest (workspaceDir: string, manifest: ProjectManifest, dir: string) {
const relative = normalizePath(path.relative(workspaceDir, dir))
let scripts: Record<string, string>
switch (manifest.name) {
case '@pnpm/lockfile-types':
scripts = { ...manifest.scripts }
break
case '@pnpm/headless':
case '@pnpm/outdated':
case '@pnpm/package-requester':
case '@pnpm/plugin-commands-import':
case '@pnpm/plugin-commands-installation':
case '@pnpm/plugin-commands-listing':
case '@pnpm/plugin-commands-outdated':
case '@pnpm/plugin-commands-publishing':
case '@pnpm/plugin-commands-rebuild':
case '@pnpm/plugin-commands-script-runners':
case '@pnpm/plugin-commands-store':
case 'pnpm':
case '@pnpm/core': {
// @pnpm/core tests currently works only with port 4873 due to the usage of
// the next package: pkg-with-tarball-dep-from-registry
const port = manifest.name === '@pnpm/core' ? 4873 : ++registryMockPort
scripts = {
...(manifest.scripts as Record<string, string>),
'registry-mock': 'registry-mock',
'test:jest': 'jest',
'test:e2e': 'registry-mock prepare && run-p -r registry-mock test:jest',
}
scripts.test = 'pnpm run compile && pnpm run _test'
scripts._test = `cross-env PNPM_REGISTRY_MOCK_PORT=${port} pnpm run test:e2e`
break
}
default:
if (await exists(path.join(dir, 'test'))) {
scripts = {
...(manifest.scripts as Record<string, string>),
_test: 'jest',
test: 'pnpm run compile && pnpm run _test',
}
} else {
scripts = {
...(manifest.scripts as Record<string, string>),
test: 'pnpm run compile',
}
}
break
}
if (manifest.name === 'pnpm') {
manifest.publishConfig!.tag = NEXT_TAG
}
if (scripts._test) {
if (scripts.pretest) {
scripts._test = `pnpm pretest && ${scripts._test}`
}
if (scripts.posttest) {
scripts._test = `${scripts._test} && pnpm posttest`
}
if (manifest.name === '@pnpm/server') {
scripts._test += ' --detectOpenHandles'
}
}
scripts.compile = 'tsc --build && pnpm run lint --fix'
delete scripts.tsc
let homepage: string
let repository: string | { type: 'git', url: string }
if (manifest.name === 'pnpm') {
homepage = 'https://pnpm.io'
repository = {
type: 'git',
url: 'git+https://github.com/pnpm/pnpm.git',
}
scripts.compile += ' && rimraf dist bin/nodes && pnpm run bundle \
&& shx cp -r node-gyp-bin dist/node-gyp-bin \
&& shx cp -r node_modules/@pnpm/tabtab/lib/scripts dist/scripts \
&& shx cp -r node_modules/ps-list/vendor dist/vendor \
&& shx cp pnpmrc dist/pnpmrc'
} else {
scripts.prepublishOnly = 'pnpm run compile'
homepage = `https://github.com/pnpm/pnpm/blob/main/${relative}#readme`
repository = `https://github.com/pnpm/pnpm/blob/main/${relative}`
}
if (scripts.lint) {
if (fs.existsSync(path.join(dir, 'test'))) {
scripts.lint = 'eslint src/**/*.ts test/**/*.ts'
} else {
scripts.lint = 'eslint src/**/*.ts'
}
}
const files: string[] = []
if (manifest.name === 'pnpm' || manifest.name?.endsWith('/pnpm')) {
files.push('dist')
files.push('bin')
} else {
// the order is important
files.push('lib')
files.push('!*.map')
if (manifest.bin) {
files.push('bin')
}
}
return {
...manifest,
bugs: {
url: 'https://github.com/pnpm/pnpm/issues',
},
engines: {
node: '>=14.19',
},
files,
funding: 'https://opencollective.com/pnpm',
homepage,
license: 'MIT',
repository,
scripts,
exports: {
'.': manifest.name === 'pnpm' ? './package.json' : './lib/index.js',
},
}
}