-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprepack.ts
More file actions
117 lines (93 loc) · 3.09 KB
/
Copy pathprepack.ts
File metadata and controls
117 lines (93 loc) · 3.09 KB
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
import { mkdir, readFile, writeFile } from 'node:fs/promises'
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { $ } from 'execa'
import {
completionFileNames,
completionShells,
renderCompletionStub,
} from './src/lib/render/completion/index.js'
const versionFile = './src/lib/version.ts'
const completionsDirectory = './completions'
const main = async (): Promise<void> => {
const version = await injectVersion(resolveFile(versionFile))
// eslint-disable-next-line no-console
console.log(`✓ Version ${version} injected into ${versionFile}`)
const blueprintVersion = await injectBlueprintVersion(
resolveFile(versionFile),
)
// eslint-disable-next-line no-console
console.log(
`✓ Blueprint version ${blueprintVersion} injected into ${versionFile}`,
)
await writeCompletions(resolveFile(completionsDirectory))
// eslint-disable-next-line no-console
console.log(`✓ Shell completion loaders written to ${completionsDirectory}`)
const { command } = await $`tsc --project tsconfig.prepack.json`
// eslint-disable-next-line no-console
console.log(`✓ Rebuilt with '${command}'`)
}
const writeCompletions = async (path: string): Promise<void> => {
await mkdir(path, { recursive: true })
await Promise.all(
completionShells.map(async (shell) => {
await writeFile(
join(path, completionFileNames[shell]),
renderCompletionStub(shell),
'utf8',
)
}),
)
}
const injectVersion = async (path: string): Promise<string> => {
const { version } = await readPackageJson()
if (version == null) {
throw new Error('Missing version in package.json')
}
await replaceInFile(
path,
"const seamapiCliVersion = '0.0.0'",
`const seamapiCliVersion = '${version}'`,
)
return version
}
const injectBlueprintVersion = async (path: string): Promise<string> => {
const { dependencies } = await readPackageJson()
const version = dependencies?.['@seamapi/blueprint']
if (version == null) {
throw new Error('Missing @seamapi/blueprint in package.json dependencies')
}
if (!/^\d+\.\d+\.\d+$/.test(version)) {
throw new Error(
`The @seamapi/blueprint dependency must be pinned to an exact version in package.json, got '${version}'`,
)
}
await replaceInFile(
path,
"const seamapiBlueprintVersion = '0.0.0'",
`const seamapiBlueprintVersion = '${version}'`,
)
return version
}
const replaceInFile = async (
path: string,
placeholder: string,
replacement: string,
): Promise<void> => {
const source = await readFile(path, 'utf8')
if (!source.includes(placeholder)) {
throw new Error(`Missing generated-value placeholder in ${path}`)
}
await writeFile(path, source.replace(placeholder, replacement), 'utf8')
}
const resolveFile = (path: string): string =>
fileURLToPath(new URL(path, import.meta.url))
const readPackageJson = async (): Promise<{
version?: string
dependencies?: Record<string, string>
}> =>
JSON.parse(await readFile(resolveFile('package.json'), 'utf8')) as {
version?: string
dependencies?: Record<string, string>
}
await main()