@project-ajax/cli 0.0.6 → 0.0.7

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.
@@ -1 +1 @@
1
- {"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/commands/auth.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,YAAY,wEA2DvB,CAAC"}
1
+ {"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/commands/auth.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,YAAY,wEAkEvB,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"auth.impl.d.ts","sourceRoot":"","sources":["../../src/commands/auth.impl.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAI/C,eAAO,MAAM,KAAK,+GAsChB,CAAC;AAEH,eAAO,MAAM,IAAI,mFAEf,CAAC;AAEH,eAAO,MAAM,MAAM,mFAIjB,CAAC"}
1
+ {"version":3,"file":"auth.impl.d.ts","sourceRoot":"","sources":["../../src/commands/auth.impl.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AA0E/C,eAAO,MAAM,KAAK,+GA8FhB,CAAC;AAEH,eAAO,MAAM,IAAI,mFAEf,CAAC;AAEH,eAAO,MAAM,MAAM,mFAIjB,CAAC"}
@@ -1,33 +1,104 @@
1
1
  import { buildHandler } from "../handler.js";
2
2
  import { openNotionUrl } from "../utils/openUrl.js";
3
+ async function loginFetch(baseUrl, endpoint, body) {
4
+ const url = `${baseUrl}/api/v3${endpoint}`;
5
+ const response = await fetch(url, {
6
+ method: "POST",
7
+ headers: {
8
+ "Content-Type": "application/json"
9
+ },
10
+ body: JSON.stringify(body)
11
+ });
12
+ if (!response.ok) {
13
+ const text = await response.text();
14
+ throw new Error(`Login request failed: ${response.status} ${text}`);
15
+ }
16
+ return await response.json();
17
+ }
18
+ async function pollLoginRedeem(baseUrl, sessionId, timeoutMs = 3e5, intervalMs = 2e3) {
19
+ const startTime = Date.now();
20
+ while (Date.now() - startTime < timeoutMs) {
21
+ const result = await loginFetch(
22
+ baseUrl,
23
+ "/workersCliLoginRedeem",
24
+ { sessionId }
25
+ );
26
+ if (result.status === "confirmed" || result.status === "expired") {
27
+ return result;
28
+ }
29
+ await new Promise((resolve) => setTimeout(resolve, intervalMs));
30
+ }
31
+ return { status: "expired" };
32
+ }
3
33
  const login = buildHandler(async function(_, token) {
4
34
  const environment = this.config.environment;
5
- if (!token) {
6
- const url = `${this.config.baseUrl}/__workers__?createToken=true`;
7
- this.io.writeErr(`Opening browser to create a token...
8
- ${url}
9
- `);
10
- this.io.writeErr("After creating a token, run:");
11
- this.io.writeErr(" npx workers auth login <token>");
12
- try {
13
- await openNotionUrl(environment, url);
14
- } catch (_error) {
15
- this.io.writeErr(
16
- `Failed to open browser automatically. Please visit:
17
- ${url}`
18
- );
35
+ const baseUrl = this.config.baseUrl;
36
+ if (token) {
37
+ const update2 = {
38
+ token,
39
+ workerId: null
40
+ };
41
+ if (environment !== "prod") {
42
+ update2.environment = environment;
19
43
  }
44
+ await this.config.update(update2);
45
+ this.io.writeErr("Successfully logged in!");
20
46
  return;
21
47
  }
48
+ this.io.writeErr("Starting browser login...\n");
49
+ let initResponse;
50
+ try {
51
+ initResponse = await loginFetch(
52
+ baseUrl,
53
+ "/workersCliLoginInit",
54
+ {}
55
+ );
56
+ } catch (error) {
57
+ this.io.writeErr(
58
+ `Failed to start login: ${error instanceof Error ? error.message : String(error)}`
59
+ );
60
+ process.exit(1);
61
+ }
62
+ const { sessionId, verificationCode, browserUrl } = initResponse;
63
+ this.io.writeErr(
64
+ "Copy this verification code and paste it in your browser:\n"
65
+ );
66
+ this.io.writeErr(` ${verificationCode}
67
+ `);
68
+ await this.io.input({
69
+ message: "Press Enter to open browser...",
70
+ noTTY: `Please visit this URL to authenticate:
71
+ ${browserUrl}`
72
+ });
73
+ try {
74
+ await openNotionUrl(environment, browserUrl);
75
+ } catch (_error) {
76
+ this.io.writeErr(
77
+ `Could not open browser automatically. Please visit:
78
+ ${browserUrl}`
79
+ );
80
+ }
81
+ this.io.writeErr("");
82
+ this.io.writeErr("Waiting for browser confirmation...");
83
+ this.io.writeErr("(Press Ctrl+C to cancel)\n");
84
+ const redeemResponse = await pollLoginRedeem(baseUrl, sessionId);
85
+ if (redeemResponse.status === "expired") {
86
+ this.io.writeErr("Login session expired. Please try again.");
87
+ process.exit(1);
88
+ }
89
+ if (redeemResponse.status !== "confirmed") {
90
+ this.io.writeErr("Login failed. Please try again.");
91
+ process.exit(1);
92
+ }
22
93
  const update = {
23
- token,
94
+ token: redeemResponse.token,
24
95
  workerId: null
25
96
  };
26
97
  if (environment !== "prod") {
27
98
  update.environment = environment;
28
99
  }
29
100
  await this.config.update(update);
30
- this.io.writeErr("Successfully logged in!");
101
+ this.io.writeErr("\u2713 Successfully logged in!");
31
102
  });
32
103
  const show = buildHandler(function() {
33
104
  this.io.writeOut(`${this.config.token ?? ""}`);
@@ -7,14 +7,22 @@ const authCommands = buildRouteMap({
7
7
  routes: {
8
8
  login: buildCommand({
9
9
  docs: {
10
- brief: "Login to the Project Ajax platform using a Workers API token; will clear existing token, environment, and worker ID"
10
+ brief: "Login to the Project Ajax platform via browser authentication",
11
+ fullDescription: `
12
+ Opens a browser window for you to authenticate and select a workspace.
13
+ A verification code will be displayed in the terminal - make sure it matches
14
+ what you see in your browser before confirming.
15
+
16
+ Alternatively, you can pass a token directly (for automation or CI environments):
17
+ workers auth login <token>
18
+ `.trim()
11
19
  },
12
20
  parameters: {
13
21
  positional: {
14
22
  kind: "tuple",
15
23
  parameters: [
16
24
  {
17
- brief: "A Workers API token",
25
+ brief: "A Workers API token (optional, for CI/automation)",
18
26
  parse: String,
19
27
  placeholder: "api-token",
20
28
  optional: true
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@project-ajax/cli",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "description": "A CLI for the Project Ajax platform",
5
5
  "license": "UNLICENSED",
6
6
  "type": "module",