@project-ajax/sdk 0.0.50 → 0.0.52
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/cli/api/client.d.ts +0 -8
- package/dist/cli/api/client.d.ts.map +1 -1
- package/dist/cli/api/client.js +4 -31
- package/dist/cli/commands/auth.d.ts.map +1 -1
- package/dist/cli/commands/auth.impl.d.ts +4 -9
- package/dist/cli/commands/auth.impl.d.ts.map +1 -1
- package/dist/cli/commands/auth.impl.js +4 -10
- package/dist/cli/commands/auth.impl.test.d.ts +2 -0
- package/dist/cli/commands/auth.impl.test.d.ts.map +1 -0
- package/dist/cli/commands/auth.js +1 -15
- package/dist/cli/commands/bundle.impl.test.d.ts +2 -0
- package/dist/cli/commands/bundle.impl.test.d.ts.map +1 -0
- package/dist/cli/commands/deploy.impl.test.d.ts +2 -0
- package/dist/cli/commands/deploy.impl.test.d.ts.map +1 -0
- package/dist/cli/commands/utils/testing.d.ts +25 -0
- package/dist/cli/commands/utils/testing.d.ts.map +1 -0
- package/dist/cli/commands/utils/testing.js +51 -0
- package/dist/cli/config.d.ts +8 -8
- package/dist/cli/config.d.ts.map +1 -1
- package/dist/cli/config.js +56 -19
- package/dist/cli/config.test.d.ts +2 -0
- package/dist/cli/config.test.d.ts.map +1 -0
- package/dist/cli/flags.d.ts +5 -0
- package/dist/cli/flags.d.ts.map +1 -1
- package/dist/cli/flags.js +25 -0
- package/dist/cli/handler.d.ts.map +1 -1
- package/dist/cli/handler.js +3 -5
- package/package.json +2 -2
- package/src/capabilities/tool.test.ts +51 -1
- package/src/cli/api/client.ts +3 -44
- package/src/cli/commands/.cursor/rules/testing-commands.mdc +212 -0
- package/src/cli/commands/auth.impl.test.ts +206 -0
- package/src/cli/commands/auth.impl.ts +5 -17
- package/src/cli/commands/auth.ts +2 -15
- package/src/cli/commands/bundle.impl.test.ts +137 -0
- package/src/cli/commands/deploy.impl.test.ts +239 -0
- package/src/cli/commands/utils/testing.ts +60 -0
- package/src/cli/config.test.ts +114 -0
- package/src/cli/config.ts +71 -29
- package/src/cli/flags.ts +29 -0
- package/src/cli/handler.ts +2 -5
package/src/cli/config.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import * as fs from "node:fs";
|
|
2
2
|
import * as path from "node:path";
|
|
3
|
+
import type { GlobalFlags } from "./flags.js";
|
|
3
4
|
|
|
4
5
|
export const Environments = ["local", "staging", "dev", "prod"] as const;
|
|
5
6
|
export type Environment = (typeof Environments)[number];
|
|
6
7
|
|
|
7
|
-
interface
|
|
8
|
-
environment
|
|
9
|
-
baseUrl
|
|
8
|
+
interface ConfigMap {
|
|
9
|
+
environment: Environment;
|
|
10
|
+
baseUrl: string;
|
|
10
11
|
token: string | null;
|
|
11
12
|
workerId: string | null;
|
|
12
13
|
}
|
|
@@ -41,66 +42,56 @@ export class TokenNotSetError extends Error {
|
|
|
41
42
|
* | WORKERS_BASE_URL | baseUrl | The base URL to use |
|
|
42
43
|
*/
|
|
43
44
|
export class Config {
|
|
44
|
-
readonly #
|
|
45
|
+
readonly #configMap: ConfigMap;
|
|
45
46
|
readonly #configFilePath: string;
|
|
46
|
-
readonly #env: NodeJS.ProcessEnv;
|
|
47
47
|
|
|
48
48
|
constructor(opts: {
|
|
49
49
|
configFilePath: string;
|
|
50
|
-
configFile:
|
|
51
|
-
processEnv: NodeJS.ProcessEnv;
|
|
50
|
+
configFile: ConfigMap;
|
|
52
51
|
}) {
|
|
53
52
|
this.#configFilePath = opts.configFilePath;
|
|
54
|
-
this.#
|
|
55
|
-
this.#env = opts.processEnv;
|
|
53
|
+
this.#configMap = opts.configFile;
|
|
56
54
|
}
|
|
57
55
|
|
|
58
56
|
// Getters read from the environment variables, and then the config file.
|
|
59
57
|
|
|
60
58
|
get baseUrl() {
|
|
61
|
-
return this.#
|
|
59
|
+
return this.#configMap.baseUrl;
|
|
62
60
|
}
|
|
63
61
|
|
|
64
62
|
get token() {
|
|
65
|
-
return this.#
|
|
63
|
+
return this.#configMap.token;
|
|
66
64
|
}
|
|
67
65
|
|
|
68
66
|
get environment() {
|
|
69
|
-
return this.#
|
|
70
|
-
? parseEnvironment(this.#env.WORKERS_ENVIRONMENT)
|
|
71
|
-
: this.#configFile.environment;
|
|
67
|
+
return this.#configMap.environment;
|
|
72
68
|
}
|
|
73
69
|
|
|
74
70
|
get workerId() {
|
|
75
|
-
return this.#
|
|
71
|
+
return this.#configMap.workerId;
|
|
76
72
|
}
|
|
77
73
|
|
|
78
74
|
// Setters update the config file, and then write it to disk.
|
|
79
75
|
|
|
80
76
|
async setEnvironment(environment: Environment) {
|
|
81
|
-
this.#
|
|
82
|
-
await this.#writeConfigFile();
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
async setBaseUrl(baseUrl: string) {
|
|
86
|
-
this.#configFile.baseUrl = baseUrl;
|
|
77
|
+
this.#configMap.environment = environment;
|
|
87
78
|
await this.#writeConfigFile();
|
|
88
79
|
}
|
|
89
80
|
|
|
90
81
|
async setToken(token: string | null) {
|
|
91
|
-
this.#
|
|
82
|
+
this.#configMap.token = token;
|
|
92
83
|
await this.#writeConfigFile();
|
|
93
84
|
}
|
|
94
85
|
|
|
95
86
|
async setWorkerId(workerId: string | null) {
|
|
96
|
-
this.#
|
|
87
|
+
this.#configMap.workerId = workerId;
|
|
97
88
|
await this.#writeConfigFile();
|
|
98
89
|
}
|
|
99
90
|
|
|
100
91
|
async #writeConfigFile() {
|
|
101
92
|
await fs.promises.writeFile(
|
|
102
93
|
this.#configFilePath,
|
|
103
|
-
JSON.stringify(this.#
|
|
94
|
+
JSON.stringify(this.#configMap, null, 2),
|
|
104
95
|
"utf-8",
|
|
105
96
|
);
|
|
106
97
|
}
|
|
@@ -118,28 +109,66 @@ export class Config {
|
|
|
118
109
|
static async load(opts: {
|
|
119
110
|
configFilePath: string;
|
|
120
111
|
processEnv: NodeJS.ProcessEnv;
|
|
112
|
+
flags: GlobalFlags;
|
|
121
113
|
}) {
|
|
122
114
|
const absConfigFilePath = path.resolve(process.cwd(), opts.configFilePath);
|
|
123
115
|
const configFile = await Config.#readConfigFile(absConfigFilePath);
|
|
116
|
+
|
|
117
|
+
if (opts.processEnv.WORKERS_TOKEN) {
|
|
118
|
+
configFile.token = opts.processEnv.WORKERS_TOKEN;
|
|
119
|
+
}
|
|
120
|
+
if (opts.processEnv.WORKERS_ENVIRONMENT) {
|
|
121
|
+
configFile.environment = parseEnvironment(
|
|
122
|
+
opts.processEnv.WORKERS_ENVIRONMENT,
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
if (opts.processEnv.WORKERS_WORKER_ID) {
|
|
126
|
+
configFile.workerId = opts.processEnv.WORKERS_WORKER_ID;
|
|
127
|
+
}
|
|
128
|
+
if (opts.processEnv.WORKERS_BASE_URL) {
|
|
129
|
+
configFile.baseUrl = opts.processEnv.WORKERS_BASE_URL;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (opts.flags.token) {
|
|
133
|
+
configFile.token = opts.flags.token;
|
|
134
|
+
}
|
|
135
|
+
if (opts.flags.env) {
|
|
136
|
+
configFile.environment = parseEnvironment(opts.flags.env);
|
|
137
|
+
}
|
|
138
|
+
if (opts.flags["base-url"]) {
|
|
139
|
+
configFile.baseUrl = opts.flags["base-url"];
|
|
140
|
+
}
|
|
141
|
+
if (opts.flags["worker-id"]) {
|
|
142
|
+
configFile.workerId = opts.flags["worker-id"];
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
configFile.baseUrl ??= baseUrl(configFile.environment ?? "prod");
|
|
146
|
+
|
|
124
147
|
return new Config({
|
|
125
148
|
configFilePath: absConfigFilePath,
|
|
126
|
-
configFile,
|
|
127
|
-
processEnv: opts.processEnv,
|
|
149
|
+
configFile: configFile as ConfigMap,
|
|
128
150
|
});
|
|
129
151
|
}
|
|
130
152
|
|
|
131
153
|
static async #readConfigFile(configFilePath: string) {
|
|
132
|
-
let configFile:
|
|
154
|
+
let configFile: Partial<ConfigMap>;
|
|
133
155
|
try {
|
|
134
156
|
const configContents = await fs.promises.readFile(
|
|
135
157
|
configFilePath,
|
|
136
158
|
"utf-8",
|
|
137
159
|
);
|
|
138
160
|
|
|
139
|
-
configFile = JSON.parse(configContents) as
|
|
161
|
+
configFile = JSON.parse(configContents) as Partial<ConfigMap>;
|
|
140
162
|
} catch (error) {
|
|
141
163
|
if (isENOENT(error)) {
|
|
142
|
-
|
|
164
|
+
// The default, non-logged in configuration before overrides are
|
|
165
|
+
// applied from environment variables, and then flags.
|
|
166
|
+
configFile = {
|
|
167
|
+
token: null,
|
|
168
|
+
workerId: null,
|
|
169
|
+
environment: "prod",
|
|
170
|
+
baseUrl: baseUrl("prod"),
|
|
171
|
+
};
|
|
143
172
|
} else {
|
|
144
173
|
throw error;
|
|
145
174
|
}
|
|
@@ -196,3 +225,16 @@ export function extractPayloadFromToken(token: string): {
|
|
|
196
225
|
throw new Error("Failed to parse token payload.");
|
|
197
226
|
}
|
|
198
227
|
}
|
|
228
|
+
|
|
229
|
+
function baseUrl(environment: Environment): string {
|
|
230
|
+
switch (environment) {
|
|
231
|
+
case "local":
|
|
232
|
+
return "http://localhost:3000";
|
|
233
|
+
case "staging":
|
|
234
|
+
return "https://staging.notion.so";
|
|
235
|
+
case "dev":
|
|
236
|
+
return "https://dev.notion.so";
|
|
237
|
+
case "prod":
|
|
238
|
+
return "https://www.notion.so";
|
|
239
|
+
}
|
|
240
|
+
}
|
package/src/cli/flags.ts
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
import type { TypedFlagParameter } from "@stricli/core";
|
|
2
|
+
import { type Environment, Environments } from "./config.js";
|
|
2
3
|
import type { LocalContext } from "./context.js";
|
|
3
4
|
|
|
4
5
|
export interface GlobalFlags {
|
|
5
6
|
config?: string;
|
|
6
7
|
debug: boolean;
|
|
8
|
+
env?: Environment;
|
|
9
|
+
"base-url"?: string;
|
|
10
|
+
token?: string;
|
|
11
|
+
"worker-id"?: string;
|
|
7
12
|
}
|
|
8
13
|
|
|
9
14
|
export const globalFlags: {
|
|
@@ -23,6 +28,30 @@ export const globalFlags: {
|
|
|
23
28
|
brief: "Enable debug logging",
|
|
24
29
|
default: false,
|
|
25
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
|
+
},
|
|
26
55
|
};
|
|
27
56
|
|
|
28
57
|
export interface FormatFlags {
|
package/src/cli/handler.ts
CHANGED
|
@@ -25,13 +25,14 @@ export function buildHandler<const FLAGS, const ARGS extends BaseArgs = []>(
|
|
|
25
25
|
...args: ARGS
|
|
26
26
|
) {
|
|
27
27
|
const configFilePath =
|
|
28
|
-
this.process.env.WORKERS_CONFIG_FILE_PATH ??
|
|
29
28
|
flags.config ??
|
|
29
|
+
this.process.env.WORKERS_CONFIG_FILE_PATH ??
|
|
30
30
|
"./workers.json";
|
|
31
31
|
|
|
32
32
|
const config = await Config.load({
|
|
33
33
|
configFilePath,
|
|
34
34
|
processEnv: process.env,
|
|
35
|
+
flags,
|
|
35
36
|
});
|
|
36
37
|
|
|
37
38
|
this.writer.debugEnabled = flags.debug;
|
|
@@ -53,11 +54,7 @@ export function buildAuthedHandler<
|
|
|
53
54
|
return buildHandler(function (flags: FLAGS, ...args: ARGS) {
|
|
54
55
|
// Check token first - this will throw TokenNotSetError with helpful message if not authenticated
|
|
55
56
|
const { token, cellId } = this.config.tokenInfo;
|
|
56
|
-
|
|
57
57
|
const environment = this.config.environment;
|
|
58
|
-
if (!environment) {
|
|
59
|
-
throw new Error("Unexpected error: Environment not set");
|
|
60
|
-
}
|
|
61
58
|
|
|
62
59
|
const client = new ApiClient({
|
|
63
60
|
token,
|