-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathindex.ts
92 lines (77 loc) · 2.46 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
/* eslint-disable sonarjs/cognitive-complexity */
import { readWantedLockfile } from '@pnpm/lockfile-file'
import { LockfileFile } from '@pnpm/lockfile-file/lib/write'
import { ProjectManifest } from '@pnpm/types'
import { existsSync } from 'fs'
import normalizePath from 'normalize-path'
import path from 'path'
type UpdateFunc = (
data: Record<string, unknown>,
dir: string,
manifest: ProjectManifest,
) => Record<string, unknown> | Promise<Record<string, unknown> | null> | null
export default async (
workspaceDir: string,
): Promise<Record<string, UpdateFunc>> => {
const lockfile = await readWantedLockfile(workspaceDir, {
ignoreIncompatible: false,
})
if (lockfile == null) {
throw new Error('no lockfile found')
}
return {
'.releaserc.json': (releaseRc, _dir: string, manifest) => {
if (!manifest.name) {
return {}
}
return { tagFormat: `${manifest.name}@v\${version}`, ...releaseRc }
},
'package.json': updatePackageJson(workspaceDir, lockfile),
'tsconfig.build.json': updateTsConfig(workspaceDir, lockfile),
'tsconfig.json': updateTsConfig(workspaceDir, lockfile),
}
}
function updatePackageJson(_workspaceDir: string, _lockfile: LockfileFile) {
return (manifest: ProjectManifest, _dir: string) => {
return {
...manifest,
author: 'Philipp Busse',
}
}
}
function updateTsConfig(workspaceDir: string, lockfile: LockfileFile) {
return (
tsConfig: Record<string, unknown>,
dir: string,
manifest: ProjectManifest,
) => {
if (tsConfig == null || manifest.name?.includes('/tsconfig')) {
return tsConfig
}
const relative = normalizePath(path.relative(workspaceDir, dir))
const importer = lockfile.importers?.[relative]
if (!importer) {
return tsConfig
}
const deps = {
...importer.dependencies,
...importer.devDependencies,
}
const references = Object.values(deps)
.filter((dep) => dep.startsWith('link:'))
.filter((dep) => !dep.endsWith('tsconfig'))
.map((dep) => dep.slice('link:'.length))
.filter((relativePath) =>
existsSync(path.join(dir, relativePath, 'tsconfig.json')),
)
.map((path) => ({ path }))
console.log(`Updating tsconfig for ${dir}: ${JSON.stringify(references)}`)
return {
...tsConfig,
exclude: ['node_modules', 'dist'],
...(references && {
references: references.sort((r1, r2) => r1.path.localeCompare(r2.path)),
}),
}
}
}