Repository navigation
Expand file tree
/
Copy pathself-update-bootstrap.ts
More file actions
215 lines (193 loc) · 6.12 KB
/
Copy pathself-update-bootstrap.ts
File metadata and controls
215 lines (193 loc) · 6.12 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
import { spawn } from "node:child_process";
import { readFileSync } from "node:fs";
import { join } from "node:path";
import {
APP_NAME,
getAgentDir,
getSelfUpdateCommand,
getSelfUpdateUnavailableInstruction,
PACKAGE_NAME,
VERSION,
} from "./config.ts";
import { brandProfile } from "./core/brand.ts";
import { getLatestPiRelease, isNewerPackageVersion, type LatestPiRelease } from "./utils/version-check.ts";
export interface SelfUpdateBootstrapCommand {
command: string;
args: string[];
display: string;
steps?: SelfUpdateBootstrapCommand[];
}
interface BootstrapSelfUpdateArgs {
force: boolean;
}
interface BootstrapSelfUpdateDependencies {
getLatestRelease?: (currentVersion: string) => Promise<LatestPiRelease | undefined>;
getSelfUpdateCommand?: (
packageName: string,
npmCommand?: string[],
updatePackageName?: string,
) => SelfUpdateBootstrapCommand | undefined;
getUnavailableInstruction?: (packageName: string, npmCommand?: string[], updatePackageName?: string) => string;
readNpmCommand?: () => string[] | undefined;
runCommand?: (step: SelfUpdateBootstrapCommand) => Promise<void>;
writeStdout?: (line: string) => void;
writeStderr?: (line: string) => void;
}
function parseBootstrapSelfUpdateArgs(args: readonly string[]): BootstrapSelfUpdateArgs | undefined {
const [command, ...rest] = args;
if (command !== "update") {
return undefined;
}
let force = false;
let selfFlag = false;
let extensionsFlag = false;
let extensionFlag = false;
let source: string | undefined;
for (let index = 0; index < rest.length; index++) {
const arg = rest[index];
if (arg === "-h" || arg === "--help") {
return undefined;
}
if (arg === "--force") {
force = true;
continue;
}
if (arg === "--self") {
selfFlag = true;
continue;
}
if (arg === "--extensions") {
extensionsFlag = true;
continue;
}
if (arg === "--extension") {
extensionFlag = true;
index++;
continue;
}
if (arg.startsWith("-")) {
return undefined;
}
if (source) {
return undefined;
}
source = arg;
}
if (extensionFlag) {
return undefined;
}
const sourceIsSelf = source === "self" || source === "pi" || source === APP_NAME;
if (source && !sourceIsSelf) {
return undefined;
}
if (!selfFlag && !sourceIsSelf && extensionsFlag) {
return undefined;
}
return { force };
}
function getObjectProperty(value: unknown, key: string): unknown {
if (!value || typeof value !== "object") {
return undefined;
}
return Object.getOwnPropertyDescriptor(value, key)?.value;
}
function readConfiguredNpmCommand(): string[] | undefined {
try {
const parsed: unknown = JSON.parse(readFileSync(join(getAgentDir(), "settings.json"), "utf-8"));
const npmCommand = getObjectProperty(parsed, "npmCommand");
if (!Array.isArray(npmCommand) || npmCommand.some((part) => typeof part !== "string")) {
return undefined;
}
return npmCommand.length > 0 ? npmCommand : undefined;
} catch {
return undefined;
}
}
async function runCommand(step: SelfUpdateBootstrapCommand): Promise<void> {
await new Promise<void>((resolve, reject) => {
const child = spawn(step.command, step.args, { stdio: "inherit" });
child.on("error", (error) => {
reject(error);
});
child.on("close", (code, signal) => {
if (code === 0) {
resolve();
} else if (signal) {
reject(new Error(`${step.display} terminated by signal ${signal}`));
} else {
reject(new Error(`${step.display} exited with code ${code ?? "unknown"}`));
}
});
});
}
export async function handleBootstrapSelfUpdate(
args: readonly string[],
dependencies: BootstrapSelfUpdateDependencies = {},
): Promise<boolean> {
const parsed = parseBootstrapSelfUpdateArgs(args);
if (!parsed) {
return false;
}
const writeStdout = dependencies.writeStdout ?? ((line: string) => console.log(line));
const writeStderr = dependencies.writeStderr ?? ((line: string) => console.error(line));
const readNpmCommand = dependencies.readNpmCommand ?? readConfiguredNpmCommand;
const resolveSelfUpdateCommand = dependencies.getSelfUpdateCommand ?? getSelfUpdateCommand;
const resolveUnavailableInstruction = dependencies.getUnavailableInstruction ?? getSelfUpdateUnavailableInstruction;
const executeCommand = dependencies.runCommand ?? runCommand;
const brandUpdate = brandProfile()?.update;
if (brandUpdate) {
writeStdout(`${APP_NAME} ships inside ${brandUpdate.packageName}. Update it with: ${brandUpdate.command}`);
return true;
}
const loadLatestRelease = dependencies.getLatestRelease ?? getLatestPiRelease;
let latestRelease: LatestPiRelease | undefined;
if (!parsed.force) {
try {
latestRelease = await loadLatestRelease(VERSION);
} catch {
latestRelease = undefined;
}
}
const updatePackageName = latestRelease?.packageName ?? PACKAGE_NAME;
const shouldRun =
parsed.force ||
!latestRelease ||
updatePackageName !== PACKAGE_NAME ||
isNewerPackageVersion(latestRelease.version, VERSION);
if (!shouldRun) {
writeStdout(`${APP_NAME} is already up to date (v${VERSION})`);
return true;
}
const npmCommand = readNpmCommand();
const selfUpdateCommand = resolveSelfUpdateCommand(PACKAGE_NAME, npmCommand, updatePackageName);
if (!selfUpdateCommand) {
writeStderr(`error: ${APP_NAME} cannot self-update this installation.`);
writeStderr(resolveUnavailableInstruction(PACKAGE_NAME, npmCommand, updatePackageName));
if (process.argv[1]) {
writeStderr("");
writeStderr(`Location of ${APP_NAME} executable: ${process.argv[1]}`);
}
process.exitCode = 1;
return true;
}
if (latestRelease?.note?.trim()) {
writeStdout("");
writeStdout("Update note");
writeStdout(latestRelease.note.trim());
writeStdout("");
}
writeStdout(`Updating ${APP_NAME} with ${selfUpdateCommand.display}...`);
for (const step of selfUpdateCommand.steps ?? [selfUpdateCommand]) {
try {
await executeCommand(step);
} catch (error: unknown) {
const message = error instanceof Error ? error.message : "Unknown package command error";
writeStderr(`Error: ${message}`);
writeStderr(`If this keeps failing, run this command yourself: ${selfUpdateCommand.display}`);
process.exitCode = 1;
return true;
}
}
writeStdout(`Updated ${APP_NAME}`);
return true;
}