@aiusage/cli 0.4.4 → 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 +55 -25
  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
@@ -1892,6 +1893,10 @@ import { homedir as homedir14 } from "node:os";
1892
1893
  import { join as join14 } from "node:path";
1893
1894
  var msgs = {
1894
1895
  en: {
1896
+ groupConfig: "Configuration",
1897
+ groupSync: "Sync Targets",
1898
+ groupTools: "Tools",
1899
+ groupSchedule: "Schedule",
1895
1900
  config: "Config",
1896
1901
  configMissing: "Not found, run aiusage init first",
1897
1902
  deviceId: "Device ID",
@@ -1912,6 +1917,10 @@ var msgs = {
1912
1917
  scheduleOff: "Not enabled, run aiusage schedule on"
1913
1918
  },
1914
1919
  zh: {
1920
+ groupConfig: "\u57FA\u672C\u914D\u7F6E",
1921
+ groupSync: "\u540C\u6B65\u76EE\u6807",
1922
+ groupTools: "\u5DE5\u5177\u68C0\u6D4B",
1923
+ groupSchedule: "\u5B9A\u65F6\u4EFB\u52A1",
1915
1924
  config: "\u914D\u7F6E\u6587\u4EF6",
1916
1925
  configMissing: "\u672A\u627E\u5230\uFF0C\u8BF7\u5148\u6267\u884C aiusage init",
1917
1926
  deviceId: "\u8BBE\u5907 ID",
@@ -1950,29 +1959,29 @@ async function countFiles(dir, exts, cap = 1e3) {
1950
1959
  return count;
1951
1960
  }
1952
1961
  async function runDoctor(lang = "zh") {
1953
- let s = msgs[lang], checks = [], config = await readConfig(), configPath = getConfigPath();
1962
+ let s = msgs[lang], checks = [], config = await readConfig(), g1 = s.groupConfig, configPath = getConfigPath();
1954
1963
  try {
1955
- await stat2(configPath), checks.push({ name: s.config, status: "ok", message: configPath });
1964
+ await stat2(configPath), checks.push({ group: g1, name: s.config, status: "ok", message: configPath });
1956
1965
  } catch {
1957
- checks.push({ name: s.config, status: "fail", message: s.configMissing });
1966
+ checks.push({ group: g1, name: s.config, status: "fail", message: s.configMissing });
1958
1967
  }
1959
- config.deviceId ? checks.push({ name: s.deviceId, status: "ok", message: config.deviceId }) : checks.push({ name: s.deviceId, status: "warn", message: s.deviceIdMissing });
1960
- let targets = config.targets ?? [];
1968
+ config.deviceId ? checks.push({ group: g1, name: s.deviceId, status: "ok", message: config.deviceId }) : checks.push({ group: g1, name: s.deviceId, status: "warn", message: s.deviceIdMissing });
1969
+ let g2 = s.groupSync, targets = config.targets ?? [];
1961
1970
  if (targets.length === 0)
1962
- checks.push({ name: s.targets, status: "warn", message: s.targetMissing });
1971
+ checks.push({ group: g2, name: s.targets, status: "warn", message: s.targetMissing });
1963
1972
  else
1964
1973
  for (let target of targets) {
1965
1974
  let prefix = `[${target.name}]`;
1966
- target.deviceToken ? checks.push({ name: `${prefix} ${s.deviceToken}`, status: "ok", message: `${target.deviceToken.slice(0, 12)}\u2026` }) : checks.push({ name: `${prefix} ${s.deviceToken}`, status: "fail", message: s.tokenMissing });
1975
+ target.deviceToken ? checks.push({ group: g2, name: `${prefix} ${s.deviceToken}`, status: "ok", message: `${target.deviceToken.slice(0, 12)}\u2026` }) : checks.push({ group: g2, name: `${prefix} ${s.deviceToken}`, status: "fail", message: s.tokenMissing });
1967
1976
  try {
1968
1977
  let health = await fetchHealth(target.apiBaseUrl);
1969
- checks.push({ name: `${prefix} ${s.server}`, status: "ok", message: health.siteId });
1978
+ checks.push({ group: g2, name: `${prefix} ${s.server}`, status: "ok", message: health.siteId });
1970
1979
  } catch (err) {
1971
- checks.push({ name: `${prefix} ${s.server}`, status: "fail", message: err instanceof Error ? err.message : String(err) });
1980
+ checks.push({ group: g2, name: `${prefix} ${s.server}`, status: "fail", message: err instanceof Error ? err.message : String(err) });
1972
1981
  }
1973
- target.lastSuccessfulUploadAt ? checks.push({ name: `${prefix} ${s.lastSync}`, status: "ok", message: target.lastSuccessfulUploadAt }) : checks.push({ name: `${prefix} ${s.lastSync}`, status: "warn", message: s.lastSyncNone });
1982
+ target.lastSuccessfulUploadAt ? checks.push({ group: g2, name: `${prefix} ${s.lastSync}`, status: "ok", message: target.lastSuccessfulUploadAt }) : checks.push({ group: g2, name: `${prefix} ${s.lastSync}`, status: "warn", message: s.lastSyncNone });
1974
1983
  }
1975
- let home = homedir14(), tools = [
1984
+ let g3 = s.groupTools, home = homedir14(), tools = [
1976
1985
  { dir: join14(home, ".claude", "projects"), label: "Claude Code", exts: [".jsonl"] },
1977
1986
  { dir: join14(home, ".codex"), label: "Codex CLI", exts: [".jsonl"] },
1978
1987
  { dir: join14(home, ".copilot", "session-state"), label: "Copilot CLI", exts: [".jsonl"] },
@@ -1988,18 +1997,19 @@ async function runDoctor(lang = "zh") {
1988
1997
  try {
1989
1998
  await stat2(tool.dir);
1990
1999
  } catch {
1991
- checks.push({ name: tool.label, status: "warn", message: s.notInstalled });
2000
+ checks.push({ group: g3, name: tool.label, status: "warn", message: s.notInstalled });
1992
2001
  continue;
1993
2002
  }
1994
2003
  let n = await countFiles(tool.dir, tool.exts);
1995
- n > 0 ? checks.push({ name: tool.label, status: "ok", message: s.hasData(n) }) : checks.push({ name: tool.label, status: "warn", message: s.installedNoData });
2004
+ n > 0 ? checks.push({ group: g3, name: tool.label, status: "ok", message: s.hasData(n) }) : checks.push({ group: g3, name: tool.label, status: "warn", message: s.installedNoData });
1996
2005
  }
1997
- let schedule = await getScheduleStatus();
2006
+ let g4 = s.groupSchedule, schedule = await getScheduleStatus();
1998
2007
  return schedule.enabled ? checks.push({
2008
+ group: g4,
1999
2009
  name: s.schedule,
2000
2010
  status: "ok",
2001
2011
  message: schedule.intervalLabel ? `${s.scheduleEvery} ${schedule.intervalLabel}` : s.scheduleEnabled
2002
- }) : checks.push({ name: s.schedule, status: "warn", message: s.scheduleOff }), checks;
2012
+ }) : checks.push({ group: g4, name: s.schedule, status: "warn", message: s.scheduleOff }), checks;
2003
2013
  }
2004
2014
 
2005
2015
  // src/cli.ts
@@ -2098,9 +2108,15 @@ async function runHealth(flags) {
2098
2108
  console.log(JSON.stringify(health, null, 2));
2099
2109
  }
2100
2110
  async function runEnroll(flags) {
2101
- 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 = {
2102
2118
  name: targetName,
2103
- apiBaseUrl,
2119
+ apiBaseUrl: normalizedUrl,
2104
2120
  siteId,
2105
2121
  deviceToken: response.deviceToken,
2106
2122
  lastSuccessfulUploadAt: void 0
@@ -2113,6 +2129,14 @@ async function runEnroll(flags) {
2113
2129
  configPath: getConfigPath()
2114
2130
  }, null, 2));
2115
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
+ }
2116
2140
  async function runSync(flags) {
2117
2141
  let config = await readConfig(), allTargets = config.targets ?? [];
2118
2142
  if (allTargets.length === 0)
@@ -2168,7 +2192,17 @@ async function runInit(flags) {
2168
2192
  deviceId: resolveOptionalString(flags["device-id"], config.deviceId) ?? detectDeviceId(),
2169
2193
  deviceAlias: resolveOptionalString(flags["device-name"] ?? flags["device-alias"], config.deviceAlias) ?? hostname3(),
2170
2194
  lookbackDays: typeof flags.lookback == "string" ? parsePositiveInt2(flags.lookback, "--lookback") : config.lookbackDays ?? 7
2171
- };
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
+ }
2172
2206
  await writeConfig(next), console.log(JSON.stringify({ configPath: getConfigPath(), config: next }, null, 2));
2173
2207
  }
2174
2208
  async function runSchedule(sub, flags) {
@@ -2183,10 +2217,11 @@ async function runSchedule(sub, flags) {
2183
2217
  }
2184
2218
  }
2185
2219
  async function runDoctorCommand(flags) {
2186
- let config = await readConfig(), lang = (typeof flags.lang == "string" ? flags.lang : config.lang) || "en", checks = await runDoctor(lang);
2220
+ let config = await readConfig(), lang = (typeof flags.lang == "string" ? flags.lang : config.lang) || "en", checks = await runDoctor(lang), lastGroup = "";
2187
2221
  for (let check of checks) {
2222
+ check.group !== lastGroup && (lastGroup && console.log(""), console.log(`\u2500\u2500 ${check.group} \u2500\u2500`), lastGroup = check.group);
2188
2223
  let icon = check.status === "ok" ? "\u2713" : check.status === "warn" ? "\u26A0" : "\u2717";
2189
- console.log(`${icon} ${check.name}: ${check.message}`);
2224
+ console.log(` ${icon} ${check.name}: ${check.message}`);
2190
2225
  }
2191
2226
  checks.filter((c) => c.status === "fail").length > 0 && (process.exitCode = 1);
2192
2227
  }
@@ -2257,11 +2292,6 @@ function deriveTargetName(url) {
2257
2292
  return "default";
2258
2293
  }
2259
2294
  }
2260
- function resolveServer(flagValue, configValue) {
2261
- let value = resolveOptionalString(flagValue, configValue);
2262
- if (!value) throw new Error("\u7F3A\u5C11\u670D\u52A1\u7AEF\u5730\u5740\uFF0C\u8BF7\u4F20 --server \u6216\u5148\u6267\u884C init");
2263
- return normalizeServerUrl(value);
2264
- }
2265
2295
  function resolveRequiredString(flagValue, configValue, message) {
2266
2296
  let value = resolveOptionalString(flagValue, configValue);
2267
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.4",
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,