@project-ajax/sdk 0.0.60 → 0.0.62
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +0 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -2
- package/package.json +4 -16
- package/src/index.ts +0 -1
- package/dist/capabilities/slashCommand.d.ts +0 -56
- package/dist/capabilities/slashCommand.d.ts.map +0 -1
- package/dist/capabilities/slashCommand.js +0 -32
- package/dist/cli/api/client.d.ts +0 -212
- package/dist/cli/api/client.d.ts.map +0 -1
- package/dist/cli/api/client.js +0 -330
- package/dist/cli/api/result.d.ts +0 -43
- package/dist/cli/api/result.d.ts.map +0 -1
- package/dist/cli/api/result.js +0 -43
- package/dist/cli/bin/cli.d.ts +0 -3
- package/dist/cli/bin/cli.d.ts.map +0 -1
- package/dist/cli/bin/cli.js +0 -5
- package/dist/cli/commands/auth.d.ts +0 -2
- package/dist/cli/commands/auth.d.ts.map +0 -1
- package/dist/cli/commands/auth.impl.d.ts +0 -5
- package/dist/cli/commands/auth.impl.d.ts.map +0 -1
- package/dist/cli/commands/auth.impl.js +0 -45
- package/dist/cli/commands/auth.impl.test.d.ts +0 -2
- package/dist/cli/commands/auth.impl.test.d.ts.map +0 -1
- package/dist/cli/commands/auth.js +0 -56
- package/dist/cli/commands/bundle.d.ts +0 -2
- package/dist/cli/commands/bundle.d.ts.map +0 -1
- package/dist/cli/commands/bundle.impl.d.ts +0 -2
- package/dist/cli/commands/bundle.impl.d.ts.map +0 -1
- package/dist/cli/commands/bundle.impl.js +0 -21
- package/dist/cli/commands/bundle.impl.test.d.ts +0 -2
- package/dist/cli/commands/bundle.impl.test.d.ts.map +0 -1
- package/dist/cli/commands/bundle.js +0 -23
- package/dist/cli/commands/capabilities.d.ts +0 -2
- package/dist/cli/commands/capabilities.d.ts.map +0 -1
- package/dist/cli/commands/capabilities.impl.d.ts +0 -3
- package/dist/cli/commands/capabilities.impl.d.ts.map +0 -1
- package/dist/cli/commands/capabilities.impl.js +0 -40
- package/dist/cli/commands/capabilities.js +0 -24
- package/dist/cli/commands/connect.d.ts +0 -2
- package/dist/cli/commands/connect.d.ts.map +0 -1
- package/dist/cli/commands/connect.impl.d.ts +0 -6
- package/dist/cli/commands/connect.impl.d.ts.map +0 -1
- package/dist/cli/commands/connect.impl.js +0 -116
- package/dist/cli/commands/connect.js +0 -78
- package/dist/cli/commands/deploy.d.ts +0 -3
- package/dist/cli/commands/deploy.d.ts.map +0 -1
- package/dist/cli/commands/deploy.impl.d.ts +0 -6
- package/dist/cli/commands/deploy.impl.d.ts.map +0 -1
- package/dist/cli/commands/deploy.impl.js +0 -60
- package/dist/cli/commands/deploy.impl.test.d.ts +0 -2
- package/dist/cli/commands/deploy.impl.test.d.ts.map +0 -1
- package/dist/cli/commands/deploy.js +0 -22
- package/dist/cli/commands/env.d.ts +0 -2
- package/dist/cli/commands/env.d.ts.map +0 -1
- package/dist/cli/commands/env.impl.d.ts +0 -11
- package/dist/cli/commands/env.impl.d.ts.map +0 -1
- package/dist/cli/commands/env.impl.js +0 -62
- package/dist/cli/commands/env.js +0 -39
- package/dist/cli/commands/exec.d.ts +0 -3
- package/dist/cli/commands/exec.d.ts.map +0 -1
- package/dist/cli/commands/exec.impl.d.ts +0 -7
- package/dist/cli/commands/exec.impl.d.ts.map +0 -1
- package/dist/cli/commands/exec.impl.js +0 -123
- package/dist/cli/commands/exec.js +0 -30
- package/dist/cli/commands/runs.d.ts +0 -2
- package/dist/cli/commands/runs.d.ts.map +0 -1
- package/dist/cli/commands/runs.impl.d.ts +0 -4
- package/dist/cli/commands/runs.impl.d.ts.map +0 -1
- package/dist/cli/commands/runs.impl.js +0 -71
- package/dist/cli/commands/runs.js +0 -45
- package/dist/cli/commands/secrets.d.ts +0 -2
- package/dist/cli/commands/secrets.d.ts.map +0 -1
- package/dist/cli/commands/secrets.impl.d.ts +0 -5
- package/dist/cli/commands/secrets.impl.d.ts.map +0 -1
- package/dist/cli/commands/secrets.impl.js +0 -99
- package/dist/cli/commands/secrets.js +0 -64
- package/dist/cli/commands/utils/testing.d.ts +0 -13
- package/dist/cli/commands/utils/testing.d.ts.map +0 -1
- package/dist/cli/commands/utils/testing.js +0 -58
- package/dist/cli/config.d.ts +0 -63
- package/dist/cli/config.d.ts.map +0 -1
- package/dist/cli/config.js +0 -194
- package/dist/cli/config.test.d.ts +0 -2
- package/dist/cli/config.test.d.ts.map +0 -1
- package/dist/cli/context.d.ts +0 -15
- package/dist/cli/context.d.ts.map +0 -1
- package/dist/cli/context.js +0 -16
- package/dist/cli/deploy.d.ts +0 -37
- package/dist/cli/deploy.d.ts.map +0 -1
- package/dist/cli/deploy.js +0 -100
- package/dist/cli/flags.d.ts +0 -21
- package/dist/cli/flags.d.ts.map +0 -1
- package/dist/cli/flags.js +0 -49
- package/dist/cli/handler.d.ts +0 -14
- package/dist/cli/handler.d.ts.map +0 -1
- package/dist/cli/handler.js +0 -32
- package/dist/cli/io.d.ts +0 -55
- package/dist/cli/io.d.ts.map +0 -1
- package/dist/cli/io.js +0 -96
- package/dist/cli/routes.d.ts +0 -2
- package/dist/cli/routes.d.ts.map +0 -1
- package/dist/cli/routes.js +0 -62
- package/dist/cli/utils/array.d.ts +0 -2
- package/dist/cli/utils/array.d.ts.map +0 -1
- package/dist/cli/utils/array.js +0 -10
- package/dist/cli/utils/openUrl.d.ts +0 -4
- package/dist/cli/utils/openUrl.d.ts.map +0 -1
- package/dist/cli/utils/openUrl.js +0 -43
- package/dist/cli/utils/string.d.ts +0 -2
- package/dist/cli/utils/string.d.ts.map +0 -1
- package/dist/cli/utils/string.js +0 -12
- package/src/capabilities/slashCommand.ts +0 -71
- package/src/cli/api/client.ts +0 -628
- package/src/cli/api/result.ts +0 -71
- package/src/cli/bin/cli.ts +0 -7
- package/src/cli/commands/.cursor/rules/testing-commands.mdc +0 -212
- package/src/cli/commands/auth.impl.test.ts +0 -228
- package/src/cli/commands/auth.impl.ts +0 -56
- package/src/cli/commands/auth.ts +0 -63
- package/src/cli/commands/bundle.impl.test.ts +0 -143
- package/src/cli/commands/bundle.impl.ts +0 -21
- package/src/cli/commands/bundle.ts +0 -23
- package/src/cli/commands/capabilities.impl.ts +0 -47
- package/src/cli/commands/capabilities.ts +0 -25
- package/src/cli/commands/connect.impl.ts +0 -149
- package/src/cli/commands/connect.ts +0 -80
- package/src/cli/commands/deploy.impl.test.ts +0 -254
- package/src/cli/commands/deploy.impl.ts +0 -73
- package/src/cli/commands/deploy.ts +0 -22
- package/src/cli/commands/env.impl.ts +0 -88
- package/src/cli/commands/env.ts +0 -38
- package/src/cli/commands/exec.impl.ts +0 -171
- package/src/cli/commands/exec.ts +0 -32
- package/src/cli/commands/runs.impl.ts +0 -87
- package/src/cli/commands/runs.ts +0 -49
- package/src/cli/commands/secrets.impl.ts +0 -130
- package/src/cli/commands/secrets.ts +0 -73
- package/src/cli/commands/utils/testing.ts +0 -66
- package/src/cli/config.test.ts +0 -108
- package/src/cli/config.ts +0 -265
- package/src/cli/context.ts +0 -26
- package/src/cli/deploy.ts +0 -190
- package/src/cli/flags.ts +0 -72
- package/src/cli/handler.ts +0 -68
- package/src/cli/io.ts +0 -132
- package/src/cli/routes.ts +0 -61
- package/src/cli/utils/array.ts +0 -7
- package/src/cli/utils/openUrl.ts +0 -53
- package/src/cli/utils/string.ts +0 -9
package/src/cli/config.ts
DELETED
|
@@ -1,265 +0,0 @@
|
|
|
1
|
-
import * as fs from "node:fs";
|
|
2
|
-
import * as path from "node:path";
|
|
3
|
-
import type { GlobalFlags } from "./flags.js";
|
|
4
|
-
|
|
5
|
-
export const Environments = ["local", "staging", "dev", "prod"] as const;
|
|
6
|
-
export type Environment = (typeof Environments)[number];
|
|
7
|
-
|
|
8
|
-
export interface ConfigMap {
|
|
9
|
-
environment: Environment;
|
|
10
|
-
baseUrl: string;
|
|
11
|
-
token: string | null;
|
|
12
|
-
workerId: string | null;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export function parseEnvironment(name: string): Environment {
|
|
16
|
-
if (["local", "staging", "dev", "prod"].includes(name)) {
|
|
17
|
-
return name as Environment;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
throw new Error(`Invalid environment name: ${name}`);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export class TokenNotSetError extends Error {
|
|
24
|
-
constructor(
|
|
25
|
-
message: string = "Not authenticated. Run 'workers auth login' first.",
|
|
26
|
-
) {
|
|
27
|
-
super(message);
|
|
28
|
-
this.name = "TokenNotSetError";
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Manages configuration for the CLI.
|
|
34
|
-
*
|
|
35
|
-
* Environment variables take precedence over the config file.
|
|
36
|
-
*
|
|
37
|
-
* | Environment Variable | Config File | Description |
|
|
38
|
-
* |----------------------|-------------|-------------|
|
|
39
|
-
* | WORKERS_TOKEN | token | The token to use for authentication |
|
|
40
|
-
* | WORKERS_ENVIRONMENT | environment | The environment to use |
|
|
41
|
-
* | WORKERS_WORKER_ID | workerId | The worker ID to use |
|
|
42
|
-
* | WORKERS_BASE_URL | baseUrl | The base URL to use |
|
|
43
|
-
*/
|
|
44
|
-
export class Config {
|
|
45
|
-
readonly #configMap: ConfigMap;
|
|
46
|
-
readonly #configFilePath: string;
|
|
47
|
-
|
|
48
|
-
constructor(opts: {
|
|
49
|
-
configMap: ConfigMap;
|
|
50
|
-
configFilePath: string;
|
|
51
|
-
}) {
|
|
52
|
-
this.#configMap = opts.configMap;
|
|
53
|
-
this.#configFilePath = opts.configFilePath;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// Getters read from the environment variables, and then the config file.
|
|
57
|
-
|
|
58
|
-
get baseUrl() {
|
|
59
|
-
return this.#configMap.baseUrl;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
get token() {
|
|
63
|
-
return this.#configMap.token;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
get environment() {
|
|
67
|
-
return this.#configMap.environment;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
get workerId() {
|
|
71
|
-
return this.#configMap.workerId;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
get tokenInfo(): { token: string; spaceId: string; cellId: string } {
|
|
75
|
-
const token = this.token;
|
|
76
|
-
if (!token) {
|
|
77
|
-
throw new TokenNotSetError();
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const { spaceId, cellId } = extractPayloadFromToken(token);
|
|
81
|
-
return { token, spaceId, cellId };
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Update the config with a partial config map.
|
|
86
|
-
*
|
|
87
|
-
* This will write only the updated keys in the config file on disk. Not all
|
|
88
|
-
* keys are written, since some current keys in the Config object may have
|
|
89
|
-
* come from e.g. environment variables, rather than the original config
|
|
90
|
-
* file.
|
|
91
|
-
*
|
|
92
|
-
* @param config The config update.
|
|
93
|
-
*/
|
|
94
|
-
async update(config: Partial<ConfigMap>) {
|
|
95
|
-
Object.assign(this.#configMap, config);
|
|
96
|
-
|
|
97
|
-
const currentConfigFile = await Config.#readConfigFile(
|
|
98
|
-
this.#configFilePath,
|
|
99
|
-
);
|
|
100
|
-
Object.assign(currentConfigFile, config);
|
|
101
|
-
await fs.promises.writeFile(
|
|
102
|
-
this.#configFilePath,
|
|
103
|
-
JSON.stringify(currentConfigFile, null, 2),
|
|
104
|
-
"utf-8",
|
|
105
|
-
);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
static async load(opts: {
|
|
109
|
-
configFilePath: string;
|
|
110
|
-
processEnv: NodeJS.ProcessEnv;
|
|
111
|
-
flags: GlobalFlags;
|
|
112
|
-
}) {
|
|
113
|
-
const absConfigFilePath = path.resolve(process.cwd(), opts.configFilePath);
|
|
114
|
-
const partialConfig = await Config.#readConfigFile(absConfigFilePath);
|
|
115
|
-
|
|
116
|
-
if (opts.processEnv.WORKERS_TOKEN) {
|
|
117
|
-
partialConfig.token = opts.processEnv.WORKERS_TOKEN;
|
|
118
|
-
}
|
|
119
|
-
if (opts.processEnv.WORKERS_ENVIRONMENT) {
|
|
120
|
-
partialConfig.environment = parseEnvironment(
|
|
121
|
-
opts.processEnv.WORKERS_ENVIRONMENT,
|
|
122
|
-
);
|
|
123
|
-
}
|
|
124
|
-
if (opts.processEnv.WORKERS_WORKER_ID) {
|
|
125
|
-
partialConfig.workerId = opts.processEnv.WORKERS_WORKER_ID;
|
|
126
|
-
}
|
|
127
|
-
if (opts.processEnv.WORKERS_BASE_URL) {
|
|
128
|
-
partialConfig.baseUrl = opts.processEnv.WORKERS_BASE_URL;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
if (opts.flags.token) {
|
|
132
|
-
partialConfig.token = opts.flags.token;
|
|
133
|
-
}
|
|
134
|
-
if (opts.flags.env) {
|
|
135
|
-
partialConfig.environment = parseEnvironment(opts.flags.env);
|
|
136
|
-
}
|
|
137
|
-
if (opts.flags["base-url"]) {
|
|
138
|
-
partialConfig.baseUrl = opts.flags["base-url"];
|
|
139
|
-
}
|
|
140
|
-
if (opts.flags["worker-id"]) {
|
|
141
|
-
partialConfig.workerId = opts.flags["worker-id"];
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
partialConfig.environment ??= "prod";
|
|
145
|
-
partialConfig.baseUrl ??= baseUrlForEnvironment(partialConfig.environment);
|
|
146
|
-
|
|
147
|
-
const environment = partialConfig.environment;
|
|
148
|
-
if (!environment) {
|
|
149
|
-
throw new Error("Environment is required");
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
const baseUrl = partialConfig.baseUrl;
|
|
153
|
-
if (!baseUrl) {
|
|
154
|
-
throw new Error("Base URL is required");
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
const token = partialConfig.token;
|
|
158
|
-
if (token === undefined) {
|
|
159
|
-
throw new Error("Token is required");
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
const workerId = partialConfig.workerId;
|
|
163
|
-
if (workerId === undefined) {
|
|
164
|
-
throw new Error("Worker ID is required");
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
const configMap: ConfigMap = {
|
|
168
|
-
environment,
|
|
169
|
-
baseUrl,
|
|
170
|
-
token,
|
|
171
|
-
workerId,
|
|
172
|
-
};
|
|
173
|
-
|
|
174
|
-
return new Config({
|
|
175
|
-
configFilePath: absConfigFilePath,
|
|
176
|
-
configMap,
|
|
177
|
-
});
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
static async #readConfigFile(configFilePath: string) {
|
|
181
|
-
let configFile: Partial<ConfigMap>;
|
|
182
|
-
try {
|
|
183
|
-
const configContents = await fs.promises.readFile(
|
|
184
|
-
configFilePath,
|
|
185
|
-
"utf-8",
|
|
186
|
-
);
|
|
187
|
-
|
|
188
|
-
configFile = JSON.parse(configContents) as Partial<ConfigMap>;
|
|
189
|
-
} catch (error) {
|
|
190
|
-
if (isENOENT(error)) {
|
|
191
|
-
// The default, non-logged in configuration before overrides are
|
|
192
|
-
// applied from environment variables, and then flags.
|
|
193
|
-
configFile = {
|
|
194
|
-
token: null,
|
|
195
|
-
workerId: null,
|
|
196
|
-
};
|
|
197
|
-
} else {
|
|
198
|
-
throw error;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
return configFile;
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
function isENOENT(
|
|
207
|
-
error: unknown,
|
|
208
|
-
): error is NodeJS.ErrnoException & { code: "ENOENT" } {
|
|
209
|
-
return error instanceof Error && "code" in error && error.code === "ENOENT";
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
export function extractPayloadFromToken(token: string): {
|
|
213
|
-
spaceId: string;
|
|
214
|
-
userId: string;
|
|
215
|
-
cellId: string;
|
|
216
|
-
} {
|
|
217
|
-
try {
|
|
218
|
-
// Token format: <version>.<type>.<encoded-payload-json>.<encoded-signature>
|
|
219
|
-
const parts = token.split(".");
|
|
220
|
-
if (parts.length !== 4 || !parts[2]) {
|
|
221
|
-
throw new Error("Invalid token format.");
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
// Decode the payload (third part)
|
|
225
|
-
const payloadBase64 = parts[2];
|
|
226
|
-
const payloadJson = Buffer.from(payloadBase64, "base64url").toString(
|
|
227
|
-
"utf-8",
|
|
228
|
-
);
|
|
229
|
-
const payload = JSON.parse(payloadJson) as {
|
|
230
|
-
spaceId?: string;
|
|
231
|
-
userId?: string;
|
|
232
|
-
cellId?: string;
|
|
233
|
-
};
|
|
234
|
-
|
|
235
|
-
if (!payload.spaceId || !payload.userId || !payload.cellId) {
|
|
236
|
-
throw new Error(
|
|
237
|
-
"Token payload missing required fields (spaceId, userId, cellId).",
|
|
238
|
-
);
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
return {
|
|
242
|
-
spaceId: payload.spaceId,
|
|
243
|
-
userId: payload.userId,
|
|
244
|
-
cellId: payload.cellId,
|
|
245
|
-
};
|
|
246
|
-
} catch (error) {
|
|
247
|
-
if (error instanceof Error && error.message.startsWith("Token")) {
|
|
248
|
-
throw error;
|
|
249
|
-
}
|
|
250
|
-
throw new Error("Failed to parse token payload.");
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
function baseUrlForEnvironment(environment: Environment): string {
|
|
255
|
-
switch (environment) {
|
|
256
|
-
case "local":
|
|
257
|
-
return "http://localhost:3000";
|
|
258
|
-
case "staging":
|
|
259
|
-
return "https://staging.notion.so";
|
|
260
|
-
case "dev":
|
|
261
|
-
return "https://dev.notion.so";
|
|
262
|
-
case "prod":
|
|
263
|
-
return "https://www.notion.so";
|
|
264
|
-
}
|
|
265
|
-
}
|
package/src/cli/context.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import * as fs from "node:fs";
|
|
2
|
-
import * as os from "node:os";
|
|
3
|
-
import * as path from "node:path";
|
|
4
|
-
import type { StricliAutoCompleteContext } from "@stricli/auto-complete";
|
|
5
|
-
import type { CommandContext } from "@stricli/core";
|
|
6
|
-
import { IO } from "./io.js";
|
|
7
|
-
|
|
8
|
-
export interface LocalContext
|
|
9
|
-
extends CommandContext,
|
|
10
|
-
StricliAutoCompleteContext {
|
|
11
|
-
readonly fs: typeof fs;
|
|
12
|
-
readonly io: IO;
|
|
13
|
-
readonly os: typeof os;
|
|
14
|
-
readonly path: typeof path;
|
|
15
|
-
readonly process: NodeJS.Process;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export function buildContext(process: NodeJS.Process): LocalContext {
|
|
19
|
-
return {
|
|
20
|
-
fs,
|
|
21
|
-
io: new IO({ debugEnabled: false }),
|
|
22
|
-
os,
|
|
23
|
-
path,
|
|
24
|
-
process,
|
|
25
|
-
};
|
|
26
|
-
}
|
package/src/cli/deploy.ts
DELETED
|
@@ -1,190 +0,0 @@
|
|
|
1
|
-
// AI-Generated: Significantly generated by model: Claude Sonnet 4.5; agent: AI Agents.
|
|
2
|
-
|
|
3
|
-
import * as childProcess from "node:child_process";
|
|
4
|
-
import * as fs from "node:fs";
|
|
5
|
-
import * as path from "node:path";
|
|
6
|
-
import * as esbuild from "esbuild";
|
|
7
|
-
import type { ApiError, Environment } from "./api/client.js";
|
|
8
|
-
import { Result } from "./api/result.js";
|
|
9
|
-
import type { LocalContext } from "./context.js";
|
|
10
|
-
import type { AuthedContext } from "./handler.js";
|
|
11
|
-
|
|
12
|
-
export interface BaseDeployOptions {
|
|
13
|
-
/**
|
|
14
|
-
* Path to the worker directory to deploy
|
|
15
|
-
*/
|
|
16
|
-
workerPath: string;
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* API token for authentication
|
|
20
|
-
*/
|
|
21
|
-
token: string;
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Environment to deploy to
|
|
25
|
-
*/
|
|
26
|
-
environment: Environment;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export interface CreateDeployOptions extends BaseDeployOptions {
|
|
30
|
-
/**
|
|
31
|
-
* Name of the worker (if it has not yet been created)
|
|
32
|
-
*/
|
|
33
|
-
name: string;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export interface UpdateDeployOptions extends BaseDeployOptions {
|
|
37
|
-
/**
|
|
38
|
-
* ID of the worker to update
|
|
39
|
-
*/
|
|
40
|
-
workerId: string;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export type DeployOptions = CreateDeployOptions | UpdateDeployOptions;
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Deploy a worker to the specified environment
|
|
47
|
-
*/
|
|
48
|
-
export async function deployWorker(
|
|
49
|
-
context: AuthedContext,
|
|
50
|
-
options: DeployOptions,
|
|
51
|
-
): Promise<Result<{ workerId: string }, ApiError>> {
|
|
52
|
-
const { workerPath } = options;
|
|
53
|
-
|
|
54
|
-
// Resolve absolute path
|
|
55
|
-
const absPath = path.resolve(process.cwd(), workerPath);
|
|
56
|
-
context.io.debug(`Deploying worker from: ${absPath}`);
|
|
57
|
-
|
|
58
|
-
// Create API client
|
|
59
|
-
const client = context.apiClient;
|
|
60
|
-
|
|
61
|
-
let uploadUrl: string;
|
|
62
|
-
let uploadFields: Record<string, string>;
|
|
63
|
-
let workerId: string;
|
|
64
|
-
|
|
65
|
-
if ("workerId" in options) {
|
|
66
|
-
context.io.writeErr(`Updating worker...`);
|
|
67
|
-
|
|
68
|
-
workerId = options.workerId;
|
|
69
|
-
|
|
70
|
-
const updateResult = await client.updateWorkerBundle(workerId);
|
|
71
|
-
if (Result.isSuccess(updateResult)) {
|
|
72
|
-
const res = Result.unwrap(updateResult);
|
|
73
|
-
uploadUrl = res.url;
|
|
74
|
-
uploadFields = res.fields;
|
|
75
|
-
} else {
|
|
76
|
-
context.io.writeErr(
|
|
77
|
-
"Failed to generate pre-signed upload URL for worker bundle:",
|
|
78
|
-
);
|
|
79
|
-
throw new Error(updateResult.error.message);
|
|
80
|
-
}
|
|
81
|
-
} else {
|
|
82
|
-
context.io.writeErr(`Creating worker...`);
|
|
83
|
-
|
|
84
|
-
const createResult = await client.createWorker(options.name);
|
|
85
|
-
if (Result.isSuccess(createResult)) {
|
|
86
|
-
const res = Result.unwrap(createResult);
|
|
87
|
-
uploadUrl = res.url;
|
|
88
|
-
uploadFields = res.fields;
|
|
89
|
-
workerId = res.worker.workerId;
|
|
90
|
-
} else {
|
|
91
|
-
console.error("Failed to create worker:");
|
|
92
|
-
throw new Error(createResult.error.message);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
context.io.debug(`Generated upload URL: ${uploadUrl}`);
|
|
97
|
-
|
|
98
|
-
// Build the worker bundle
|
|
99
|
-
context.io.writeErr("Building worker bundle...");
|
|
100
|
-
const archivePath = await buildWorkerBundle(context, absPath);
|
|
101
|
-
context.io.debug(`Created bundle at: ${archivePath}`);
|
|
102
|
-
|
|
103
|
-
// Upload the bundle
|
|
104
|
-
context.io.writeErr("Uploading bundle...");
|
|
105
|
-
await uploadBundle(uploadUrl, uploadFields, archivePath);
|
|
106
|
-
context.io.debug("Upload complete");
|
|
107
|
-
|
|
108
|
-
// Fetch and save capabilities
|
|
109
|
-
context.io.writeErr("Fetching and saving worker capabilities...");
|
|
110
|
-
const capabilitiesResult = await client.fetchAndSaveCapabilities(workerId);
|
|
111
|
-
|
|
112
|
-
if (Result.isSuccess(capabilitiesResult)) {
|
|
113
|
-
return Result.success({ workerId });
|
|
114
|
-
} else {
|
|
115
|
-
return capabilitiesResult;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Build the worker bundle using esbuild and create a tar.gz archive
|
|
121
|
-
*/
|
|
122
|
-
async function buildWorkerBundle(
|
|
123
|
-
context: LocalContext,
|
|
124
|
-
workerPath: string,
|
|
125
|
-
): Promise<string> {
|
|
126
|
-
const entrypoint = "src/index.ts";
|
|
127
|
-
|
|
128
|
-
if (!fs.existsSync(path.join(workerPath, entrypoint))) {
|
|
129
|
-
throw new Error(`Entrypoint not found: ${entrypoint}`);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
// Create temporary output directory
|
|
133
|
-
const outdir = fs.mkdtempSync("/tmp/workers-build-");
|
|
134
|
-
|
|
135
|
-
// Build with esbuild
|
|
136
|
-
esbuild.buildSync({
|
|
137
|
-
entryPoints: [entrypoint],
|
|
138
|
-
absWorkingDir: workerPath,
|
|
139
|
-
bundle: true,
|
|
140
|
-
outdir,
|
|
141
|
-
platform: "node",
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
context.io.debug(`Built bundle to: ${outdir}`);
|
|
145
|
-
|
|
146
|
-
// Create tar.gz archive
|
|
147
|
-
const archiveDir = fs.mkdtempSync("/tmp/workers-archive-");
|
|
148
|
-
const archivePath = path.join(archiveDir, "archive.tar.gz");
|
|
149
|
-
|
|
150
|
-
childProcess.execSync(`tar -czf ${archivePath} -C ${outdir} .`);
|
|
151
|
-
|
|
152
|
-
return archivePath;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Upload the worker bundle to the pre-signed URL
|
|
157
|
-
*/
|
|
158
|
-
async function uploadBundle(
|
|
159
|
-
url: string,
|
|
160
|
-
fields: Record<string, string>,
|
|
161
|
-
archivePath: string,
|
|
162
|
-
): Promise<void> {
|
|
163
|
-
const formData = new FormData();
|
|
164
|
-
|
|
165
|
-
// Add all the pre-signed fields
|
|
166
|
-
for (const [key, value] of Object.entries(fields)) {
|
|
167
|
-
formData.append(key, value);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
// Add the file
|
|
171
|
-
const fileBuffer = fs.readFileSync(archivePath);
|
|
172
|
-
formData.append(
|
|
173
|
-
"file",
|
|
174
|
-
new Blob([new Uint8Array(fileBuffer)]),
|
|
175
|
-
"archive.tar.gz",
|
|
176
|
-
);
|
|
177
|
-
|
|
178
|
-
// Upload
|
|
179
|
-
const response = await fetch(url, {
|
|
180
|
-
method: "POST",
|
|
181
|
-
body: formData,
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
if (!response.ok) {
|
|
185
|
-
const errorText = await response.text();
|
|
186
|
-
throw new Error(
|
|
187
|
-
`Failed to upload bundle: ${response.status} ${response.statusText}\n${errorText}`,
|
|
188
|
-
);
|
|
189
|
-
}
|
|
190
|
-
}
|
package/src/cli/flags.ts
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import type { TypedFlagParameter } from "@stricli/core";
|
|
2
|
-
import { type Environment, Environments } from "./config.js";
|
|
3
|
-
import type { LocalContext } from "./context.js";
|
|
4
|
-
|
|
5
|
-
export interface GlobalFlags {
|
|
6
|
-
config?: string;
|
|
7
|
-
debug: boolean;
|
|
8
|
-
env?: Environment;
|
|
9
|
-
"base-url"?: string;
|
|
10
|
-
token?: string;
|
|
11
|
-
"worker-id"?: string;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export const globalFlags: {
|
|
15
|
-
[K in keyof Required<GlobalFlags>]: TypedFlagParameter<
|
|
16
|
-
GlobalFlags[K],
|
|
17
|
-
LocalContext
|
|
18
|
-
>;
|
|
19
|
-
} = {
|
|
20
|
-
config: {
|
|
21
|
-
kind: "parsed",
|
|
22
|
-
parse: String,
|
|
23
|
-
brief: "The path to the config file to use",
|
|
24
|
-
optional: true,
|
|
25
|
-
},
|
|
26
|
-
debug: {
|
|
27
|
-
kind: "boolean",
|
|
28
|
-
brief: "Enable debug logging",
|
|
29
|
-
default: false,
|
|
30
|
-
},
|
|
31
|
-
env: {
|
|
32
|
-
kind: "enum",
|
|
33
|
-
values: Environments,
|
|
34
|
-
brief: "The environment to use for the command",
|
|
35
|
-
optional: true,
|
|
36
|
-
},
|
|
37
|
-
"base-url": {
|
|
38
|
-
kind: "parsed",
|
|
39
|
-
parse: String,
|
|
40
|
-
brief: "The base URL to use for all API requests",
|
|
41
|
-
optional: true,
|
|
42
|
-
},
|
|
43
|
-
token: {
|
|
44
|
-
kind: "parsed",
|
|
45
|
-
parse: String,
|
|
46
|
-
brief: "The token to use for authentication",
|
|
47
|
-
optional: true,
|
|
48
|
-
},
|
|
49
|
-
"worker-id": {
|
|
50
|
-
kind: "parsed",
|
|
51
|
-
parse: String,
|
|
52
|
-
brief: "The ID of the worker to use for the command",
|
|
53
|
-
optional: true,
|
|
54
|
-
},
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
export interface FormatFlags {
|
|
58
|
-
plain: boolean;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export const formatFlags: {
|
|
62
|
-
[K in keyof Required<FormatFlags>]: TypedFlagParameter<
|
|
63
|
-
FormatFlags[K],
|
|
64
|
-
LocalContext
|
|
65
|
-
>;
|
|
66
|
-
} = {
|
|
67
|
-
plain: {
|
|
68
|
-
kind: "boolean",
|
|
69
|
-
brief: "Output the results in plain text format",
|
|
70
|
-
default: false,
|
|
71
|
-
},
|
|
72
|
-
};
|
package/src/cli/handler.ts
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import type { BaseArgs } from "@stricli/core";
|
|
2
|
-
import { ApiClient } from "./api/client.js";
|
|
3
|
-
import { Config } from "./config.js";
|
|
4
|
-
import type { LocalContext } from "./context.js";
|
|
5
|
-
import type { GlobalFlags } from "./flags.js";
|
|
6
|
-
|
|
7
|
-
export interface HandlerContext extends LocalContext {
|
|
8
|
-
config: Config;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export interface AuthedContext extends HandlerContext {
|
|
12
|
-
apiClient: ApiClient;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export function buildHandler<const FLAGS, const ARGS extends BaseArgs = []>(
|
|
16
|
-
handler: (
|
|
17
|
-
this: HandlerContext,
|
|
18
|
-
flags: FLAGS,
|
|
19
|
-
...args: ARGS
|
|
20
|
-
) => Promise<void> | void,
|
|
21
|
-
) {
|
|
22
|
-
return async function (
|
|
23
|
-
this: LocalContext,
|
|
24
|
-
flags: GlobalFlags & FLAGS,
|
|
25
|
-
...args: ARGS
|
|
26
|
-
) {
|
|
27
|
-
const configFilePath =
|
|
28
|
-
flags.config ??
|
|
29
|
-
this.process.env.WORKERS_CONFIG_FILE_PATH ??
|
|
30
|
-
"./workers.json";
|
|
31
|
-
|
|
32
|
-
const config = await Config.load({
|
|
33
|
-
configFilePath,
|
|
34
|
-
processEnv: process.env,
|
|
35
|
-
flags,
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
this.io.debugEnabled = flags.debug;
|
|
39
|
-
|
|
40
|
-
await handler.call({ ...this, config }, flags, ...args);
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export function buildAuthedHandler<
|
|
45
|
-
const FLAGS,
|
|
46
|
-
const ARGS extends BaseArgs = [],
|
|
47
|
-
>(
|
|
48
|
-
handler: (
|
|
49
|
-
this: AuthedContext,
|
|
50
|
-
flags: FLAGS,
|
|
51
|
-
...args: ARGS
|
|
52
|
-
) => Promise<void> | void,
|
|
53
|
-
) {
|
|
54
|
-
return buildHandler(function (flags: FLAGS, ...args: ARGS) {
|
|
55
|
-
// Check token first - this will throw TokenNotSetError with helpful message if not authenticated
|
|
56
|
-
const { token, cellId } = this.config.tokenInfo;
|
|
57
|
-
const environment = this.config.environment;
|
|
58
|
-
|
|
59
|
-
const client = new ApiClient({
|
|
60
|
-
token,
|
|
61
|
-
environment,
|
|
62
|
-
baseUrl: this.config.baseUrl,
|
|
63
|
-
cellId,
|
|
64
|
-
writer: this.io,
|
|
65
|
-
});
|
|
66
|
-
return handler.call({ ...this, apiClient: client }, flags, ...args);
|
|
67
|
-
});
|
|
68
|
-
}
|