@tokenboard/cli 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/cli.js CHANGED
@@ -587,7 +587,7 @@ function _getBuiltinFlags(long, short, userNames, userAliases) {
587
587
 
588
588
  // src/commands/preview.ts
589
589
  init_esm_shims();
590
- import { homedir } from "os";
590
+ import { homedir as homedir2 } from "os";
591
591
 
592
592
  // src/config/home.ts
593
593
  init_esm_shims();
@@ -1680,42 +1680,95 @@ function renderLocalPreview(summary, style) {
1680
1680
  }
1681
1681
  return lines.join("\n");
1682
1682
  }
1683
- var CLAIM_CTA = "\u2192 Sign in with GitHub to claim your spot \u2192 tokenboard claim";
1683
+ var CLAIM_PROMPT = "Sign in with GitHub to claim your spot?";
1684
+ var CLAIM_HINT = "\u2192 Claim your spot anytime \u2192 tokenboard claim";
1684
1685
 
1685
- // src/commands/preview.ts
1686
- async function collectLocalRecords() {
1687
- const tz = resolveTimeZone();
1688
- const claudeLines = dedupeByMessageId(collectClaudeCodeLines(resolveProjectsRoot(homedir())));
1689
- const claudeRecords = claudeLines.map((l) => parsedLineToRecord(l, tz));
1690
- const ccusage = await collectCcusage();
1691
- const records = aggregateByKey([...claudeRecords, ...ccusage.records]);
1692
- return { records, ccusageSkipped: ccusage.skipped, npxAvailable: ccusage.npxAvailable };
1686
+ // src/config/auth-store.ts
1687
+ init_esm_shims();
1688
+ import { homedir, platform } from "os";
1689
+ import { join as join3, isAbsolute } from "path";
1690
+ import { mkdir, writeFile, chmod, rename, unlink, readFile } from "fs/promises";
1691
+ import { randomBytes } from "crypto";
1692
+ function resolveConfigDir(env = process.env) {
1693
+ if (platform() === "win32") {
1694
+ const appData = env.APPDATA && env.APPDATA.trim() !== "" ? env.APPDATA : join3(homedir(), "AppData", "Roaming");
1695
+ return join3(appData, "tokenboard");
1696
+ }
1697
+ const xdg = env.XDG_CONFIG_HOME;
1698
+ const base = xdg && isAbsolute(xdg) ? xdg : join3(homedir(), ".config");
1699
+ return join3(base, "tokenboard");
1693
1700
  }
1694
- async function runPreview(args) {
1695
- const prices = loadPriceTable();
1696
- const { records } = await collectLocalRecords();
1697
- const summary = summarize(records, prices);
1698
- if (args.json) {
1699
- process.stdout.write(JSON.stringify(summary, null, 2) + "\n");
1700
- return;
1701
+ function resolveAuthPath(env = process.env) {
1702
+ return join3(resolveConfigDir(env), "auth.json");
1703
+ }
1704
+ async function writeAuthFile(auth, env = process.env) {
1705
+ const dir = resolveConfigDir(env);
1706
+ const finalPath = join3(dir, "auth.json");
1707
+ await mkdir(dir, { recursive: true, mode: 448 });
1708
+ const tmpPath = join3(dir, `.auth.json.${randomBytes(6).toString("hex")}.tmp`);
1709
+ const body = `${JSON.stringify(auth, null, 2)}
1710
+ `;
1711
+ try {
1712
+ await writeFile(tmpPath, body, { mode: 384, flag: "wx" });
1713
+ await chmod(tmpPath, 384);
1714
+ await rename(tmpPath, finalPath);
1715
+ await chmod(finalPath, 384);
1716
+ } catch (err) {
1717
+ await unlink(tmpPath).catch(() => {
1718
+ });
1719
+ throw err;
1701
1720
  }
1702
- const style = resolveStyle({
1703
- isTTY: Boolean(process.stdout.isTTY),
1704
- // Per the NO_COLOR spec (no-color.org), color is disabled when the var is PRESENT
1705
- // regardless of value — so `NO_COLOR=` (empty) still counts. Test presence, not truthiness.
1706
- noColorEnv: process.env["NO_COLOR"] !== void 0,
1707
- noColorFlag: args.noColor,
1708
- asciiFlag: args.ascii,
1709
- columns: process.stdout.columns
1710
- });
1711
- process.stdout.write(renderLocalPreview(summary, style) + "\n");
1712
- if (process.stdout.isTTY) {
1713
- const c = styler(style);
1714
- process.stdout.write("\n" + c.cyan(CLAIM_CTA) + "\n");
1721
+ return finalPath;
1722
+ }
1723
+ async function readAuthFile(env = process.env) {
1724
+ try {
1725
+ return JSON.parse(await readFile(resolveAuthPath(env), "utf8"));
1726
+ } catch (err) {
1727
+ if (err.code === "ENOENT") return null;
1728
+ throw err;
1715
1729
  }
1716
1730
  }
1717
1731
 
1718
- // src/commands/show-data.ts
1732
+ // src/prompt/confirm.ts
1733
+ init_esm_shims();
1734
+ import { createInterface } from "readline/promises";
1735
+ function parseYesNo(answer) {
1736
+ const a = answer.trim().toLowerCase();
1737
+ return a === "y" || a === "yes";
1738
+ }
1739
+ async function confirm(question) {
1740
+ if (!process.stdin.isTTY || !process.stdout.isTTY) return false;
1741
+ const rl = createInterface({ input: process.stdin, output: process.stdout });
1742
+ try {
1743
+ const answer = await rl.question(`${question} [y/N] `);
1744
+ return parseYesNo(answer);
1745
+ } finally {
1746
+ rl.close();
1747
+ }
1748
+ }
1749
+
1750
+ // src/commands/claim.ts
1751
+ init_esm_shims();
1752
+
1753
+ // src/claim/api-base.ts
1754
+ init_esm_shims();
1755
+ var DEFAULT_API_BASE = "https://tokenboard.sh";
1756
+ function resolveApiBase(env = process.env) {
1757
+ const raw = env.TOKENBOARD_API_URL?.trim();
1758
+ const base = raw && raw !== "" ? raw : DEFAULT_API_BASE;
1759
+ let parsed;
1760
+ try {
1761
+ parsed = new URL(base);
1762
+ } catch {
1763
+ throw new Error(`TOKENBOARD_API_URL is not a valid URL: ${base}`);
1764
+ }
1765
+ if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
1766
+ throw new Error(`TOKENBOARD_API_URL must be http(s): ${base}`);
1767
+ }
1768
+ return base.replace(/\/+$/, "");
1769
+ }
1770
+
1771
+ // src/claim/transport.ts
1719
1772
  init_esm_shims();
1720
1773
 
1721
1774
  // ../packages/contracts/src/index.ts
@@ -1983,65 +2036,7 @@ var profileUpdateResponseSchema = z8.object({
1983
2036
  social_links: z8.record(z8.string(), z8.string())
1984
2037
  });
1985
2038
 
1986
- // src/commands/show-data.ts
1987
- async function runShowData() {
1988
- const { records, ccusageSkipped, npxAvailable } = await collectLocalRecords();
1989
- const failures = [];
1990
- for (const rec of records) {
1991
- const result = normalizedRecordSchema.safeParse(rec);
1992
- if (!result.success) failures.push({ record: rec, issues: result.error.issues });
1993
- }
1994
- process.stdout.write(
1995
- " tokenboard show-data \u2014 dry run. This is EXACTLY what a future `sync` would upload.\n Counts only \u2014 no prompts, code, file paths, or repo names. NOTHING is uploaded in this release.\n\n"
1996
- );
1997
- if (failures.length > 0) {
1998
- process.stderr.write(
1999
- `show-data: ${failures.length} record(s) failed contract validation (parser bug):
2000
- ` + JSON.stringify(failures, null, 2) + "\n"
2001
- );
2002
- process.exitCode = 1;
2003
- return;
2004
- }
2005
- process.stdout.write(JSON.stringify(records, null, 2) + "\n");
2006
- if (!npxAvailable) {
2007
- process.stdout.write("\n (npx not found \u2014 ccusage long-tail skipped; Claude Code data shown above)\n");
2008
- } else if (ccusageSkipped.length > 0) {
2009
- process.stdout.write(`
2010
- (ccusage sources skipped: ${ccusageSkipped.join(", ")})
2011
- `);
2012
- }
2013
- }
2014
-
2015
- // src/commands/stub.ts
2016
- init_esm_shims();
2017
- function notAvailableYet(name, phase) {
2018
- process.stdout.write(` tokenboard ${name} \u2014 coming in a later release (${phase}).
2019
- `);
2020
- }
2021
-
2022
- // src/commands/claim.ts
2023
- init_esm_shims();
2024
-
2025
- // src/claim/api-base.ts
2026
- init_esm_shims();
2027
- var DEFAULT_API_BASE = "https://tokenboard.sh";
2028
- function resolveApiBase(env = process.env) {
2029
- const raw = env.TOKENBOARD_API_URL?.trim();
2030
- const base = raw && raw !== "" ? raw : DEFAULT_API_BASE;
2031
- let parsed;
2032
- try {
2033
- parsed = new URL(base);
2034
- } catch {
2035
- throw new Error(`TOKENBOARD_API_URL is not a valid URL: ${base}`);
2036
- }
2037
- if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
2038
- throw new Error(`TOKENBOARD_API_URL must be http(s): ${base}`);
2039
- }
2040
- return base.replace(/\/+$/, "");
2041
- }
2042
-
2043
2039
  // src/claim/transport.ts
2044
- init_esm_shims();
2045
2040
  var REQUEST_TIMEOUT_MS = 15e3;
2046
2041
  async function postJson(url, body) {
2047
2042
  const res = await fetch(url, {
@@ -2074,14 +2069,14 @@ async function pollDeviceGrant(base, deviceCode) {
2074
2069
  // src/claim/browser.ts
2075
2070
  init_esm_shims();
2076
2071
  import { spawn as spawn2 } from "child_process";
2077
- import { platform } from "os";
2072
+ import { platform as platform2 } from "os";
2078
2073
  function openInBrowser(url) {
2079
2074
  let command;
2080
2075
  let args;
2081
- if (platform() === "darwin") {
2076
+ if (platform2() === "darwin") {
2082
2077
  command = "open";
2083
2078
  args = [url];
2084
- } else if (platform() === "win32") {
2079
+ } else if (platform2() === "win32") {
2085
2080
  command = "cmd";
2086
2081
  args = ["/c", "start", "", url];
2087
2082
  } else {
@@ -2099,7 +2094,7 @@ function openInBrowser(url) {
2099
2094
 
2100
2095
  // src/claim/machine.ts
2101
2096
  init_esm_shims();
2102
- import { hostname, platform as platform2, arch, userInfo } from "os";
2097
+ import { hostname, platform as platform3, arch, userInfo } from "os";
2103
2098
  import { createHash } from "crypto";
2104
2099
  function machineHash() {
2105
2100
  let user = "";
@@ -2107,57 +2102,11 @@ function machineHash() {
2107
2102
  user = userInfo().username;
2108
2103
  } catch {
2109
2104
  }
2110
- const stable = [hostname(), platform2(), arch(), user].join(" ");
2105
+ const stable = [hostname(), platform3(), arch(), user].join(" ");
2111
2106
  return createHash("sha256").update(stable).digest("hex");
2112
2107
  }
2113
2108
  function clientLabel() {
2114
- return `${hostname()} (${platform2()})`;
2115
- }
2116
-
2117
- // src/config/auth-store.ts
2118
- init_esm_shims();
2119
- import { homedir as homedir2, platform as platform3 } from "os";
2120
- import { join as join3, isAbsolute } from "path";
2121
- import { mkdir, writeFile, chmod, rename, unlink, readFile } from "fs/promises";
2122
- import { randomBytes } from "crypto";
2123
- function resolveConfigDir(env = process.env) {
2124
- if (platform3() === "win32") {
2125
- const appData = env.APPDATA && env.APPDATA.trim() !== "" ? env.APPDATA : join3(homedir2(), "AppData", "Roaming");
2126
- return join3(appData, "tokenboard");
2127
- }
2128
- const xdg = env.XDG_CONFIG_HOME;
2129
- const base = xdg && isAbsolute(xdg) ? xdg : join3(homedir2(), ".config");
2130
- return join3(base, "tokenboard");
2131
- }
2132
- function resolveAuthPath(env = process.env) {
2133
- return join3(resolveConfigDir(env), "auth.json");
2134
- }
2135
- async function writeAuthFile(auth, env = process.env) {
2136
- const dir = resolveConfigDir(env);
2137
- const finalPath = join3(dir, "auth.json");
2138
- await mkdir(dir, { recursive: true, mode: 448 });
2139
- const tmpPath = join3(dir, `.auth.json.${randomBytes(6).toString("hex")}.tmp`);
2140
- const body = `${JSON.stringify(auth, null, 2)}
2141
- `;
2142
- try {
2143
- await writeFile(tmpPath, body, { mode: 384, flag: "wx" });
2144
- await chmod(tmpPath, 384);
2145
- await rename(tmpPath, finalPath);
2146
- await chmod(finalPath, 384);
2147
- } catch (err) {
2148
- await unlink(tmpPath).catch(() => {
2149
- });
2150
- throw err;
2151
- }
2152
- return finalPath;
2153
- }
2154
- async function readAuthFile(env = process.env) {
2155
- try {
2156
- return JSON.parse(await readFile(resolveAuthPath(env), "utf8"));
2157
- } catch (err) {
2158
- if (err.code === "ENOENT") return null;
2159
- throw err;
2160
- }
2109
+ return `${hostname()} (${platform3()})`;
2161
2110
  }
2162
2111
 
2163
2112
  // src/commands/claim.ts
@@ -2205,6 +2154,88 @@ async function runClaim() {
2205
2154
  throw new Error("This claim request expired. Run `tokenboard claim` again.");
2206
2155
  }
2207
2156
 
2157
+ // src/commands/preview.ts
2158
+ async function collectLocalRecords() {
2159
+ const tz = resolveTimeZone();
2160
+ const claudeLines = dedupeByMessageId(collectClaudeCodeLines(resolveProjectsRoot(homedir2())));
2161
+ const claudeRecords = claudeLines.map((l) => parsedLineToRecord(l, tz));
2162
+ const ccusage = await collectCcusage();
2163
+ const records = aggregateByKey([...claudeRecords, ...ccusage.records]);
2164
+ return { records, ccusageSkipped: ccusage.skipped, npxAvailable: ccusage.npxAvailable };
2165
+ }
2166
+ async function runPreview(args) {
2167
+ const prices = loadPriceTable();
2168
+ const { records } = await collectLocalRecords();
2169
+ const summary = summarize(records, prices);
2170
+ if (args.json) {
2171
+ process.stdout.write(JSON.stringify(summary, null, 2) + "\n");
2172
+ return;
2173
+ }
2174
+ const style = resolveStyle({
2175
+ isTTY: Boolean(process.stdout.isTTY),
2176
+ // Per the NO_COLOR spec (no-color.org), color is disabled when the var is PRESENT
2177
+ // regardless of value — so `NO_COLOR=` (empty) still counts. Test presence, not truthiness.
2178
+ noColorEnv: process.env["NO_COLOR"] !== void 0,
2179
+ noColorFlag: args.noColor,
2180
+ asciiFlag: args.ascii,
2181
+ columns: process.stdout.columns
2182
+ });
2183
+ process.stdout.write(renderLocalPreview(summary, style) + "\n");
2184
+ await offerClaim(style);
2185
+ }
2186
+ async function offerClaim(style) {
2187
+ if (!process.stdin.isTTY || !process.stdout.isTTY) return;
2188
+ const c = styler(style);
2189
+ const auth = await readAuthFile();
2190
+ if (auth) {
2191
+ process.stdout.write("\n" + c.dim(`\u2713 claimed as @${auth.handle} \xB7 run \`tokenboard sync\` to upload`) + "\n");
2192
+ return;
2193
+ }
2194
+ process.stdout.write("\n");
2195
+ if (await confirm(c.cyan(CLAIM_PROMPT))) {
2196
+ await runClaim();
2197
+ return;
2198
+ }
2199
+ process.stdout.write(c.dim(CLAIM_HINT) + "\n");
2200
+ }
2201
+
2202
+ // src/commands/show-data.ts
2203
+ init_esm_shims();
2204
+ async function runShowData() {
2205
+ const { records, ccusageSkipped, npxAvailable } = await collectLocalRecords();
2206
+ const failures = [];
2207
+ for (const rec of records) {
2208
+ const result = normalizedRecordSchema.safeParse(rec);
2209
+ if (!result.success) failures.push({ record: rec, issues: result.error.issues });
2210
+ }
2211
+ process.stdout.write(
2212
+ " tokenboard show-data \u2014 dry run. This is EXACTLY what a future `sync` would upload.\n Counts only \u2014 no prompts, code, file paths, or repo names. NOTHING is uploaded in this release.\n\n"
2213
+ );
2214
+ if (failures.length > 0) {
2215
+ process.stderr.write(
2216
+ `show-data: ${failures.length} record(s) failed contract validation (parser bug):
2217
+ ` + JSON.stringify(failures, null, 2) + "\n"
2218
+ );
2219
+ process.exitCode = 1;
2220
+ return;
2221
+ }
2222
+ process.stdout.write(JSON.stringify(records, null, 2) + "\n");
2223
+ if (!npxAvailable) {
2224
+ process.stdout.write("\n (npx not found \u2014 ccusage long-tail skipped; Claude Code data shown above)\n");
2225
+ } else if (ccusageSkipped.length > 0) {
2226
+ process.stdout.write(`
2227
+ (ccusage sources skipped: ${ccusageSkipped.join(", ")})
2228
+ `);
2229
+ }
2230
+ }
2231
+
2232
+ // src/commands/stub.ts
2233
+ init_esm_shims();
2234
+ function notAvailableYet(name, phase) {
2235
+ process.stdout.write(` tokenboard ${name} \u2014 coming in a later release (${phase}).
2236
+ `);
2237
+ }
2238
+
2208
2239
  // src/commands/sync.ts
2209
2240
  init_esm_shims();
2210
2241