@pocketenv/cli 0.3.2 → 0.3.3

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.js CHANGED
@@ -22,7 +22,7 @@ import relativeTime from 'dayjs/plugin/relativeTime.js';
22
22
  import { password, editor, input } from '@inquirer/prompts';
23
23
  import sodium from 'libsodium-wrappers';
24
24
 
25
- var version = "0.3.2";
25
+ var version = "0.3.3";
26
26
 
27
27
  async function getAccessToken() {
28
28
  const tokenPath = path.join(os.homedir(), ".pocketenv", "token.json");
@@ -418,10 +418,29 @@ function expandRepo(repo) {
418
418
  return repo;
419
419
  }
420
420
 
421
+ async function waitUntilRunning(name, authToken, timeoutMs = 6e4, intervalMs = 2e3) {
422
+ const deadline = Date.now() + timeoutMs;
423
+ while (Date.now() < deadline) {
424
+ const response = await client.get(
425
+ "/xrpc/io.pocketenv.sandbox.getSandbox",
426
+ {
427
+ params: { id: name },
428
+ headers: { Authorization: `Bearer ${authToken}` }
429
+ }
430
+ );
431
+ if (response.data.sandbox?.status === "RUNNING") return;
432
+ await new Promise((resolve) => setTimeout(resolve, intervalMs));
433
+ }
434
+ throw new Error(
435
+ `Sandbox ${name} did not reach RUNNING state within ${timeoutMs / 1e3}s`
436
+ );
437
+ }
438
+
421
439
  async function start(name, { ssh: ssh$1, repo }) {
422
440
  const token = await getAccessToken();
423
441
  if (repo) repo = expandRepo(repo);
424
442
  try {
443
+ const authToken = env$1.POCKETENV_TOKEN || token;
425
444
  await client.post(
426
445
  "/xrpc/io.pocketenv.sandbox.startSandbox",
427
446
  {
@@ -432,11 +451,12 @@ async function start(name, { ssh: ssh$1, repo }) {
432
451
  id: name
433
452
  },
434
453
  headers: {
435
- Authorization: `Bearer ${env$1.POCKETENV_TOKEN || token}`
454
+ Authorization: `Bearer ${authToken}`
436
455
  }
437
456
  }
438
457
  );
439
458
  if (ssh$1) {
459
+ await waitUntilRunning(name, authToken);
440
460
  await ssh(name);
441
461
  return;
442
462
  }
@@ -619,6 +639,7 @@ async function createSandbox(name, {
619
639
  );
620
640
  return;
621
641
  }
642
+ await waitUntilRunning(sandbox.data.name, token);
622
643
  await ssh(sandbox.data.name);
623
644
  } catch (error) {
624
645
  consola.error(`Failed to create sandbox: ${error}`);
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "bin": {
5
5
  "pocketenv": "dist/index.js"
6
6
  },
7
- "version": "0.3.2",
7
+ "version": "0.3.3",
8
8
  "type": "module",
9
9
  "keywords": [
10
10
  "sandbox",
package/src/cmd/create.ts CHANGED
@@ -5,6 +5,7 @@ import type { Sandbox } from "../types/sandbox";
5
5
  import connectToSandbox from "./ssh";
6
6
  import { c } from "../theme";
7
7
  import { expandRepo } from "../lib/expandRepo";
8
+ import waitUntilRunning from "../lib/waitUntilRunning";
8
9
 
9
10
  async function createSandbox(
10
11
  name: string,
@@ -52,6 +53,7 @@ async function createSandbox(
52
53
  );
53
54
  return;
54
55
  }
56
+ await waitUntilRunning(sandbox.data.name, token);
55
57
  await connectToSandbox(sandbox.data.name);
56
58
  } catch (error) {
57
59
  consola.error(`Failed to create sandbox: ${error}`);
package/src/cmd/start.ts CHANGED
@@ -5,6 +5,7 @@ import { client } from "../client";
5
5
  import { env } from "../lib/env";
6
6
  import connectToSandbox from "./ssh";
7
7
  import { expandRepo } from "../lib/expandRepo";
8
+ import waitUntilRunning from "../lib/waitUntilRunning";
8
9
 
9
10
  async function start(
10
11
  name: string,
@@ -14,6 +15,8 @@ async function start(
14
15
  if (repo) repo = expandRepo(repo);
15
16
 
16
17
  try {
18
+ const authToken = env.POCKETENV_TOKEN || token;
19
+
17
20
  await client.post(
18
21
  "/xrpc/io.pocketenv.sandbox.startSandbox",
19
22
  {
@@ -24,12 +27,13 @@ async function start(
24
27
  id: name,
25
28
  },
26
29
  headers: {
27
- Authorization: `Bearer ${env.POCKETENV_TOKEN || token}`,
30
+ Authorization: `Bearer ${authToken}`,
28
31
  },
29
32
  },
30
33
  );
31
34
 
32
35
  if (ssh) {
36
+ await waitUntilRunning(name, authToken);
33
37
  await connectToSandbox(name);
34
38
  return;
35
39
  }
@@ -0,0 +1,27 @@
1
+ import { client } from "../client";
2
+ import type { Sandbox } from "../types/sandbox";
3
+
4
+ async function waitUntilRunning(
5
+ name: string,
6
+ authToken: string,
7
+ timeoutMs = 60_000,
8
+ intervalMs = 2_000,
9
+ ): Promise<void> {
10
+ const deadline = Date.now() + timeoutMs;
11
+ while (Date.now() < deadline) {
12
+ const response = await client.get<{ sandbox: Sandbox | null }>(
13
+ "/xrpc/io.pocketenv.sandbox.getSandbox",
14
+ {
15
+ params: { id: name },
16
+ headers: { Authorization: `Bearer ${authToken}` },
17
+ },
18
+ );
19
+ if (response.data.sandbox?.status === "RUNNING") return;
20
+ await new Promise((resolve) => setTimeout(resolve, intervalMs));
21
+ }
22
+ throw new Error(
23
+ `Sandbox ${name} did not reach RUNNING state within ${timeoutMs / 1000}s`,
24
+ );
25
+ }
26
+
27
+ export default waitUntilRunning;