@aiusage/cli 0.4.5 → 0.4.6

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.
Files changed (2) hide show
  1. package/dist/cli.js +28 -8
  2. package/package.json +1 -1
package/dist/cli.js CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  // src/cli.ts
4
4
  import { existsSync } from "node:fs";
5
+ import { createInterface } from "node:readline";
5
6
  import { hostname as hostname3 } from "node:os";
6
7
 
7
8
  // src/scanners/claude.ts
@@ -2107,9 +2108,15 @@ async function runHealth(flags) {
2107
2108
  console.log(JSON.stringify(health, null, 2));
2108
2109
  }
2109
2110
  async function runEnroll(flags) {
2110
- let config = await readConfig(), apiBaseUrl = resolveServer(flags.server, void 0), siteId = resolveRequiredString(flags["site-id"] ?? flags.siteId, void 0, "\u7F3A\u5C11 --site-id"), enrollToken = resolveRequiredString(flags["enroll-token"], void 0, "\u7F3A\u5C11 --enroll-token"), deviceId = resolveOptionalString(flags["device-id"], config.deviceId) ?? detectDeviceId(), deviceAlias = resolveOptionalString(flags["device-name"] ?? flags["device-alias"], config.deviceAlias), targetName = resolveOptionalString(flags.target ?? flags.name, void 0) ?? deriveTargetName(apiBaseUrl), response = await enrollDevice(apiBaseUrl, { siteId, deviceId, deviceAlias, enrollToken }), target = {
2111
+ let config = await readConfig(), existingTarget = config.targets?.[0], apiBaseUrl = resolveOptionalString(flags.server, existingTarget?.apiBaseUrl) ?? await prompt("Server URL: ");
2112
+ if (!apiBaseUrl) throw new Error("\u7F3A\u5C11\u670D\u52A1\u7AEF\u5730\u5740");
2113
+ let normalizedUrl = normalizeServerUrl(apiBaseUrl), siteId = resolveOptionalString(flags["site-id"] ?? flags.siteId, existingTarget?.siteId) ?? await prompt("Site ID: ");
2114
+ if (!siteId) throw new Error("\u7F3A\u5C11 site-id");
2115
+ let enrollToken = resolveOptionalString(flags["enroll-token"], void 0) ?? await prompt("Enroll Token: ");
2116
+ if (!enrollToken) throw new Error("\u7F3A\u5C11 enroll-token");
2117
+ let deviceId = resolveOptionalString(flags["device-id"], config.deviceId) ?? detectDeviceId(), deviceAlias = resolveOptionalString(flags["device-name"] ?? flags["device-alias"], config.deviceAlias) ?? (await prompt(`Device Name [${hostname3()}]: `) || hostname3()), targetName = resolveOptionalString(flags.target ?? flags.name, void 0) ?? deriveTargetName(normalizedUrl), response = await enrollDevice(normalizedUrl, { siteId, deviceId, deviceAlias, enrollToken }), target = {
2111
2118
  name: targetName,
2112
- apiBaseUrl,
2119
+ apiBaseUrl: normalizedUrl,
2113
2120
  siteId,
2114
2121
  deviceToken: response.deviceToken,
2115
2122
  lastSuccessfulUploadAt: void 0
@@ -2122,6 +2129,14 @@ async function runEnroll(flags) {
2122
2129
  configPath: getConfigPath()
2123
2130
  }, null, 2));
2124
2131
  }
2132
+ function prompt(question) {
2133
+ let rl = createInterface({ input: process.stdin, output: process.stdout });
2134
+ return new Promise((resolve3) => {
2135
+ rl.question(question, (answer) => {
2136
+ rl.close(), resolve3(answer.trim());
2137
+ });
2138
+ });
2139
+ }
2125
2140
  async function runSync(flags) {
2126
2141
  let config = await readConfig(), allTargets = config.targets ?? [];
2127
2142
  if (allTargets.length === 0)
@@ -2177,7 +2192,17 @@ async function runInit(flags) {
2177
2192
  deviceId: resolveOptionalString(flags["device-id"], config.deviceId) ?? detectDeviceId(),
2178
2193
  deviceAlias: resolveOptionalString(flags["device-name"] ?? flags["device-alias"], config.deviceAlias) ?? hostname3(),
2179
2194
  lookbackDays: typeof flags.lookback == "string" ? parsePositiveInt2(flags.lookback, "--lookback") : config.lookbackDays ?? 7
2180
- };
2195
+ }, serverUrl = resolveOptionalString(flags.server, void 0), siteId = resolveOptionalString(flags["site-id"] ?? flags.siteId, void 0);
2196
+ if (serverUrl || siteId) {
2197
+ let existing = next.targets?.[0], target = {
2198
+ name: existing?.name ?? "default",
2199
+ apiBaseUrl: serverUrl ? normalizeServerUrl(serverUrl) : existing?.apiBaseUrl ?? "",
2200
+ siteId: siteId ?? existing?.siteId,
2201
+ deviceToken: existing?.deviceToken,
2202
+ lastSuccessfulUploadAt: existing?.lastSuccessfulUploadAt
2203
+ }, targets = next.targets ?? [], idx = targets.findIndex((t2) => t2.name === target.name);
2204
+ idx >= 0 ? targets[idx] = target : targets.push(target), next.targets = targets;
2205
+ }
2181
2206
  await writeConfig(next), console.log(JSON.stringify({ configPath: getConfigPath(), config: next }, null, 2));
2182
2207
  }
2183
2208
  async function runSchedule(sub, flags) {
@@ -2267,11 +2292,6 @@ function deriveTargetName(url) {
2267
2292
  return "default";
2268
2293
  }
2269
2294
  }
2270
- function resolveServer(flagValue, configValue) {
2271
- let value = resolveOptionalString(flagValue, configValue);
2272
- if (!value) throw new Error("\u7F3A\u5C11\u670D\u52A1\u7AEF\u5730\u5740\uFF0C\u8BF7\u4F20 --server \u6216\u5148\u6267\u884C init");
2273
- return normalizeServerUrl(value);
2274
- }
2275
2295
  function resolveRequiredString(flagValue, configValue, message) {
2276
2296
  let value = resolveOptionalString(flagValue, configValue);
2277
2297
  if (!value) throw new Error(message);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiusage/cli",
3
- "version": "0.4.5",
3
+ "version": "0.4.6",
4
4
  "description": "Track and sync AI tool token usage across devices.",
5
5
  "license": "MIT",
6
6
  "private": false,