td-barrage 0.1.0 → 0.1.1

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/client.js CHANGED
@@ -1,13 +1,22 @@
1
1
  export async function createOpenCodeClient(options = {}) {
2
2
  const importSdk = new Function("specifier", "return import(specifier)");
3
3
  const sdk = (await importSdkWithFallback(importSdk));
4
- if (typeof sdk.createOpencodeClient === "function") {
4
+ // A bare client only works when we already know where the server lives.
5
+ // Without a baseUrl it would issue requests against relative URLs like
6
+ // "/session?directory=..." and fail with "Failed to parse URL".
7
+ if (options.baseUrl && typeof sdk.createOpencodeClient === "function") {
5
8
  return wrapSdkClient(sdk.createOpencodeClient({ ...options, throwOnError: true }));
6
9
  }
10
+ // No baseUrl: spawn a local server and use the client it wires up for us.
7
11
  if (typeof sdk.createOpencode === "function") {
8
12
  const created = (await sdk.createOpencode(options));
9
- if (created.client)
13
+ if (created.client) {
14
+ registerServerShutdown(created.server);
10
15
  return wrapSdkClient(created.client);
16
+ }
17
+ }
18
+ if (typeof sdk.createOpencodeClient === "function") {
19
+ return wrapSdkClient(sdk.createOpencodeClient({ ...options, throwOnError: true }));
11
20
  }
12
21
  const Client = (sdk.OpenCode ?? sdk.Opencode ?? sdk.Client ?? sdk.default);
13
22
  if (!Client) {
@@ -15,6 +24,26 @@ export async function createOpenCodeClient(options = {}) {
15
24
  }
16
25
  return new Client(options);
17
26
  }
27
+ function registerServerShutdown(server) {
28
+ if (typeof server?.close !== "function")
29
+ return;
30
+ let closed = false;
31
+ const close = () => {
32
+ if (closed)
33
+ return;
34
+ closed = true;
35
+ server.close?.();
36
+ };
37
+ process.once("exit", close);
38
+ process.once("SIGINT", () => {
39
+ close();
40
+ process.exit(130);
41
+ });
42
+ process.once("SIGTERM", () => {
43
+ close();
44
+ process.exit(143);
45
+ });
46
+ }
18
47
  function wrapSdkClient(client) {
19
48
  const sessionDirs = new Map();
20
49
  const raw = client;
package/dist/index.js CHANGED
@@ -1,6 +1,8 @@
1
1
  #!/usr/bin/env node
2
2
  import fs from "node:fs/promises";
3
+ import { realpathSync } from "node:fs";
3
4
  import process from "node:process";
5
+ import { pathToFileURL } from "node:url";
4
6
  import { parseArgs } from "node:util";
5
7
  import { createOpenCodeClient } from "./client.js";
6
8
  import { topologicalOrder } from "./deps.js";
@@ -67,6 +69,19 @@ function isMissingFile(error) {
67
69
  (("code" in error && error.code === "ENOENT") ||
68
70
  (error instanceof Error && error.message.includes("ENOENT"))));
69
71
  }
70
- if (import.meta.url === `file://${process.argv[1]}`) {
72
+ function isMainModule() {
73
+ const entry = process.argv[1];
74
+ if (!entry)
75
+ return false;
76
+ try {
77
+ // Resolve symlinks (npm/npx link bins into node_modules/.bin) so the
78
+ // entry path matches import.meta.url, which is always the real path.
79
+ return import.meta.url === pathToFileURL(realpathSync(entry)).href;
80
+ }
81
+ catch {
82
+ return false;
83
+ }
84
+ }
85
+ if (isMainModule()) {
71
86
  process.exitCode = await main();
72
87
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "td-barrage",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "TypeScript queue runner that loads a JSON task file, resolves dependencies, runs tasks through the OpenCode SDK, and resumes interrupted work.",
5
5
  "type": "module",
6
6
  "engines": {