Repository navigation
Expand file tree
/
Copy pathexecute.ts
More file actions
43 lines (41 loc) · 1.42 KB
/
Copy pathexecute.ts
File metadata and controls
43 lines (41 loc) · 1.42 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
import {
LAUNCH_PROTOCOL_VERSION,
LAUNCH_PROTOCOL_VERSION_VARIABLE,
POST_UPDATE_SERVICE_START_EXIT_CODE,
RELAUNCH_EXIT_CODE,
updateCommandString,
type UpdateAction,
} from "@magnitudedev/release"
import { Effect } from "effect"
import type { CliUpdaterShape } from "./updater"
const launcherSpeaksRelaunch = (): boolean =>
process.env[LAUNCH_PROTOCOL_VERSION_VARIABLE] === String(LAUNCH_PROTOCOL_VERSION)
export const executeUpdate = (
updater: CliUpdaterShape,
action: UpdateAction,
options: { readonly relaunch: boolean } = { relaunch: false },
): Effect.Effect<number> => {
const command = updateCommandString(action)
return Effect.gen(function* () {
yield* Effect.sync(() => {
process.stdout.write(`\nUpdating Magnitude via \`${command}\`...\n`)
})
yield* updater.runUpdate(action)
return yield* Effect.sync(() => {
if (options.relaunch && launcherSpeaksRelaunch()) {
// The launcher re-runs its pipeline and starts the new version.
return RELAUNCH_EXIT_CODE
}
if (!options.relaunch && launcherSpeaksRelaunch()) {
return POST_UPDATE_SERVICE_START_EXIT_CODE
}
process.stdout.write("\nUpdate ran successfully. Run `magnitude service start` to activate it.\n")
return 0
})
}).pipe(
Effect.catchAll((error) => Effect.sync(() => {
process.stderr.write(`\n\`${command}\` failed: ${error.reason}\n`)
return 1
})),
)
}