Repository navigation
Expand file tree
/
Copy pathinstall.ts
More file actions
283 lines (267 loc) · 10 KB
/
Copy pathinstall.ts
File metadata and controls
283 lines (267 loc) · 10 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import { spawn } from 'node:child_process'
import { readFileSync, realpathSync } from 'node:fs'
import { homedir } from 'node:os'
import { dirname, isAbsolute, join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { getErrorMessage } from '@sim/utils/errors'
import { lock } from 'proper-lockfile'
import { upgradeCommand } from '#sim-cli/update/check'
import { cliVersion } from '#sim-cli/version'
export class CliUpdateError extends Error {}
const PACKAGE_MANAGERS = ['npm', 'pnpm', 'bun', 'yarn'] as const
export type PackageManager = (typeof PACKAGE_MANAGERS)[number]
interface PackageManagerOptions {
env: NodeJS.ProcessEnv
capture: boolean
}
type RunPackageManager = (
manager: PackageManager,
args: string[],
options: PackageManagerOptions
) => Promise<string>
interface InstallUpdateOptions {
modulePath?: string
env?: NodeJS.ProcessEnv
currentVersion?: string
packageManager?: PackageManager
run?: RunPackageManager
write?: (message: string) => void
}
/** Runs only fixed package-manager commands; installer output belongs on stderr. */
const runPackageManager: RunPackageManager = (manager, args, { env, capture }) =>
new Promise((resolve, reject) => {
const child = spawn(manager, args, {
cwd: homedir(),
env,
/** Windows package managers ship .cmd launchers. Arguments contain no user input. */
shell: process.platform === 'win32',
stdio: ['ignore', capture ? 'pipe' : process.stderr, process.stderr],
timeout: capture ? 10_000 : 5 * 60_000,
killSignal: 'SIGKILL',
windowsHide: true,
})
let output = ''
child.stdout?.setEncoding('utf8').on('data', (chunk: string) => {
output += chunk
if (Buffer.byteLength(output) > 64 * 1024) {
child.kill('SIGKILL')
reject(new CliUpdateError(`${manager} returned too much output while checking Sim.`))
}
})
child.once('error', (error) => {
reject(new CliUpdateError(`Could not run ${manager}: ${getErrorMessage(error)}`))
})
child.once('close', (code, signal) => {
if (code !== 0) {
reject(
new CliUpdateError(
`${manager} ${args.join(' ')} failed (${signal ?? `exit ${code}`}). Resolve the package-manager error and run sim update again.`
)
)
return
}
resolve(output.trim())
})
})
interface ReleaseVersion {
channel: 'latest' | 'staging' | 'dev'
precedence: bigint[]
}
/** Parses the release formats published by CI; build metadata has no precedence. */
function parseReleaseVersion(version: string): ReleaseVersion {
const match =
/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-(preview|dev)\.(0|[1-9]\d*)\.(0|[1-9]\d*))?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/.exec(
version
)
if (version.length > 256 || !match) {
throw new CliUpdateError(`Cannot determine the release channel for Sim ${version}.`)
}
const precedence = [BigInt(match[1]), BigInt(match[2]), BigInt(match[3])]
if (match[4]) precedence.push(BigInt(match[5]), BigInt(match[6]))
return {
channel: match[4] === 'preview' ? 'staging' : match[4] === 'dev' ? 'dev' : 'latest',
precedence,
}
}
/** Both versions have already been checked to belong to the same release channel. */
function compareReleases(candidate: ReleaseVersion, current: ReleaseVersion): number {
for (const [index, component] of candidate.precedence.entries()) {
if (component !== current.precedence[index])
return component > current.precedence[index] ? 1 : -1
}
return 0
}
function resolveInstallationPath(path: string): string {
try {
return realpathSync(path)
} catch (cause) {
throw new CliUpdateError(`Cannot access the Sim installation: ${getErrorMessage(cause)}`, {
cause,
})
}
}
function readInstalledVersion(entrypoint: string): string {
const manifestPath = join(dirname(dirname(resolveInstallationPath(entrypoint))), 'package.json')
let manifest: unknown
try {
manifest = JSON.parse(readFileSync(manifestPath, 'utf8'))
} catch (cause) {
throw new CliUpdateError(`Cannot read the installed Sim manifest: ${getErrorMessage(cause)}`, {
cause,
})
}
if (
typeof manifest !== 'object' ||
manifest === null ||
!('name' in manifest) ||
manifest.name !== 'sim' ||
!('version' in manifest) ||
typeof manifest.version !== 'string'
) {
throw new CliUpdateError(
'The installed Sim manifest must name the sim package and its version.'
)
}
return manifest.version
}
/** Yarn Classic wraps the selected field in an inspect event among JSON status lines. */
function parseRegistryVersion(output: string, manager: PackageManager): string {
let version: unknown
try {
if (manager === 'yarn') {
const events: unknown[] = output
.split(/\r?\n/)
.filter((line) => line.trim())
.map((line) => JSON.parse(line))
const inspections = events.filter(
(event): event is { type: 'inspect'; data: unknown } =>
typeof event === 'object' &&
event !== null &&
'type' in event &&
event.type === 'inspect' &&
'data' in event
)
if (inspections.length === 1) version = inspections[0].data
} else {
version = JSON.parse(output)
}
} catch (cause) {
throw new CliUpdateError(`${manager} returned invalid registry JSON.`, { cause })
}
if (typeof version !== 'string') {
throw new CliUpdateError(`${manager} did not resolve a single Sim release version.`)
}
return version
}
/** Updates the verified global installation and reports the version actually installed. */
export async function installUpdate(options: InstallUpdateOptions = {}): Promise<void> {
const modulePath = resolveInstallationPath(options.modulePath ?? fileURLToPath(import.meta.url))
const env: NodeJS.ProcessEnv = { ...(options.env ?? process.env), SIM_API_KEY: undefined }
const normalized = modulePath.replaceAll('\\', '/').toLowerCase()
if (
env.npm_command === 'exec' ||
normalized.includes('/_npx/') ||
normalized.includes('/bunx-') ||
!normalized.endsWith('/node_modules/sim/dist/index.js')
) {
throw new CliUpdateError(
'sim update requires a global installation. Update project dependencies with their package manager, or use sim@latest with your package runner.'
)
}
const manager = options.packageManager ?? upgradeCommand(modulePath, env).split(' ')[0]
if (!PACKAGE_MANAGERS.includes(manager as PackageManager)) {
throw new CliUpdateError('Cannot determine which package manager installed Sim.')
}
const packageManager = manager as PackageManager
const run = options.run ?? runPackageManager
const currentVersion = options.currentVersion ?? cliVersion()
const current = parseReleaseVersion(currentVersion)
const target = current.channel
const write = options.write ?? ((message: string) => void process.stderr.write(message))
env.SIM_NO_UPDATE_CHECK = '1'
const locateArgs =
packageManager === 'bun'
? ['pm', 'bin', '-g']
: packageManager === 'yarn'
? ['global', 'dir', '--silent']
: ['root', '-g']
const directory = await run(packageManager, locateArgs, { env, capture: true })
if (!isAbsolute(directory) || /[\r\n]/.test(directory)) {
throw new CliUpdateError(`${packageManager} did not return a valid global installation path.`)
}
const installedEntry =
packageManager === 'bun'
? join(directory, 'sim')
: join(directory, ...(packageManager === 'yarn' ? ['node_modules'] : []), 'sim/dist/index.js')
if (resolveInstallationPath(installedEntry) !== modulePath) {
throw new CliUpdateError(
`${packageManager} would update a different Sim installation. Use the package manager and global configuration that installed this copy, or select --package-manager.`
)
}
const release = await lock(dirname(dirname(modulePath)), { retries: 0, realpath: false }).catch(
(cause: unknown) => {
throw new CliUpdateError(`Cannot lock Sim for update: ${getErrorMessage(cause)}`, { cause })
}
)
let updateFailed = false
try {
if (readInstalledVersion(installedEntry) !== currentVersion) {
throw new CliUpdateError(
'The Sim installation changed while starting the update. Run sim update again.'
)
}
const version = parseRegistryVersion(
await run(
packageManager,
[
packageManager === 'bun' || packageManager === 'yarn' ? 'info' : 'view',
`sim@${target}`,
'version',
'--json',
],
{ env, capture: true }
),
packageManager
)
const candidate = parseReleaseVersion(version)
if (candidate.channel !== target) {
throw new CliUpdateError('The registry resolved Sim to a different release channel.')
}
const comparison = compareReleases(candidate, current)
if (comparison < 0) {
throw new CliUpdateError(
`Refusing to downgrade Sim ${currentVersion} to ${version}. Check your package-manager registry settings.`
)
}
if (comparison === 0) {
write(`Sim ${currentVersion} is already up to date.\n`)
return
}
write(`Updating Sim ${currentVersion} with ${packageManager} (sim@${version})…\n`)
const args =
packageManager === 'npm'
? ['install', '-g', `sim@${version}`]
: packageManager === 'yarn'
? ['global', 'add', `sim@${version}`]
: ['add', '-g', `sim@${version}`]
await run(packageManager, args, { env, capture: false })
if (readInstalledVersion(installedEntry) !== version) {
throw new CliUpdateError('The package manager did not install the expected Sim version.')
}
write(
`Updated Sim ${currentVersion} → ${version}. The next invocation will use the new version.\n`
)
} catch (error) {
updateFailed = true
throw error
} finally {
await release().catch((cause: unknown) => {
const message = `Cannot release the Sim update lock: ${getErrorMessage(cause)}`
if (updateFailed) {
write(`${message}\n`)
} else {
throw new CliUpdateError(message, { cause })
}
})
}
}