@letterstory/cli 0.177.61 → 0.177.62

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/lib/cli.mjs CHANGED
@@ -90,7 +90,7 @@ import {
90
90
  import { fetchParkedSections, withoutParked } from "./parked.mjs";
91
91
 
92
92
  // Matches cli/package.json; publish-cli.yml rewrites both to the published version.
93
- export const VERSION = "0.177.61";
93
+ export const VERSION = "0.177.62";
94
94
 
95
95
  /** The exit code for a gate refusal (CARD_REQUIRED, PAYMENT_FAILED, OUT_OF_TOKENS): 1 is a plain error. */
96
96
  export const EXIT_PAYMENT_REQUIRED = 3;
@@ -2121,6 +2121,10 @@ Refs (Visual Refs: other companies' brands to design something new after, apart
2121
2121
  Usage data (what signed-in people do in your own app):
2122
2122
  usage [--days 7|30|90] [--site <id>] [--limit <n>] People, companies and the features they use
2123
2123
  usage --host <app host> What to add to the app before anyone signed in
2124
+ usage posthog connect --project <id> --key <phx_…> [--posthog-host <h>] [--host <app host>]
2125
+ Connect your PostHog: 90 days now, then every day
2126
+ usage posthog [status] | sync [--days 7] | disconnect
2127
+ The connection, a sync now, or remove it
2124
2128
  usage case-study [--days 7|30|90] The patterns in your usage and the latest case study
2125
2129
  usage case-study generate [--patterns <id,id>] [--named [--companies <a.com,b.com>]]
2126
2130
  Write a case study (no company named unless --named)
@@ -5,6 +5,9 @@
5
5
  // letterstory usage --host app.acme.com what to add to the app before anyone signed in
6
6
  // letterstory usage import-posthog --project <id> --key <phx_…> [--days 90] [--host app.acme.com]
7
7
  // [--feature-urls map.json] [--dry-run] history from your PostHog (import_posthog_usage)
8
+ // letterstory usage posthog connect --project <id> --key <phx_…> [--posthog-host eu.posthog.com] [--host app.acme.com]
9
+ // connect PostHog for good: 90 days now, then daily
10
+ // letterstory usage posthog [status]|sync [--days 7]|disconnect the connection (never its key), sync now, remove it
8
11
  // letterstory usage case-study [--days 7|30|90] the patterns and the latest case study
9
12
  // letterstory usage case-study generate [--days] [--patterns a,b] [--named [--companies x.com,y.com]]
10
13
  // letterstory usage case-study publish|unpublish <id>
@@ -63,6 +66,23 @@ export const ARGS = {
63
66
  },
64
67
  dry_run: { flag: "dry-run", type: "bool" },
65
68
  },
69
+ connect_posthog_usage: {
70
+ project_id: { flag: "project" },
71
+ // --key, or POSTHOG_PERSONAL_API_KEY so the key stays out of the shell's history.
72
+ personal_api_key: {
73
+ value: (flags) => {
74
+ const key = typeof flags.key === "string" ? flags.key : process.env.POSTHOG_PERSONAL_API_KEY;
75
+ if (!key) throw new CliError("posthog connect needs --key (or POSTHOG_PERSONAL_API_KEY).");
76
+ return key;
77
+ },
78
+ },
79
+ posthog_host: { flag: "posthog-host" },
80
+ site_id: { flag: "site" },
81
+ host: { flag: "host" },
82
+ },
83
+ get_posthog_usage_connection: {},
84
+ sync_posthog_usage: { days: { flag: "days", type: "num" } },
85
+ disconnect_posthog_usage: {},
66
86
  get_case_study: {
67
87
  id: { flag: "id" },
68
88
  days: { flag: "days", type: "num" },
@@ -133,6 +153,53 @@ async function cmdCaseStudy(ctx) {
133
153
  throw new CliError(`Unknown: usage case-study ${sub}. Try generate, publish, unpublish or pr.`);
134
154
  }
135
155
 
156
+ function printConnection(c) {
157
+ if (!c)
158
+ return "PostHog isn't connected. Connect it: letterstory usage posthog connect --project <id> --key <phx_…>";
159
+ const lines = [
160
+ `PostHog project ${c.project_id} on ${c.posthog_host}, people signed in on ${c.host ?? "the app"}`,
161
+ ` Connected ${c.connected_at}`,
162
+ ` Last sync ${c.last_synced_at ?? "not yet"}${c.last_people !== null ? `, ${c.last_people} people` : ""}`,
163
+ ];
164
+ if (c.last_error) lines.push(` Last import stopped: ${c.last_error}`);
165
+ return lines.join("\n");
166
+ }
167
+
168
+ async function cmdPostHog(ctx) {
169
+ const { client, flags, io } = ctx;
170
+ const sub = ctx.positionals[1] ?? "status";
171
+ if (sub === "status") {
172
+ const result = await client.callTool("get_posthog_usage_connection", {});
173
+ printResult(io, flags, result, (r) => printConnection(r.connection));
174
+ return 0;
175
+ }
176
+ if (sub === "connect" || sub === "sync") {
177
+ if (sub === "connect" && !flags.project) throw new CliError("posthog connect needs --project.");
178
+ const result =
179
+ sub === "connect"
180
+ ? await client.callTool("connect_posthog_usage", argsFromFlags(ARGS.connect_posthog_usage, flags, []))
181
+ : await client.callTool("sync_posthog_usage", argsFromFlags(ARGS.sync_posthog_usage, flags, []));
182
+ printResult(io, flags, result, (r) =>
183
+ [
184
+ r.imported
185
+ ? `Imported from PostHog (${r.imported.host}, since ${r.imported.since}): ${r.imported.people} people, ${r.imported.companies} companies, ${r.imported.features} features`
186
+ : "The import didn't finish.",
187
+ "",
188
+ printConnection(r.connection),
189
+ ].join("\n")
190
+ );
191
+ return 0;
192
+ }
193
+ if (sub === "disconnect") {
194
+ const result = await client.callTool("disconnect_posthog_usage", {});
195
+ printResult(io, flags, result, (r) =>
196
+ r.disconnected ? "PostHog disconnected. What was imported stays." : "PostHog wasn't connected."
197
+ );
198
+ return 0;
199
+ }
200
+ throw new CliError(`Unknown: usage posthog ${sub}. Try connect, status, sync or disconnect.`);
201
+ }
202
+
136
203
  const STEP = {
137
204
  identify: "Letter's script is on the app. Add the identify line to signed-in pages:",
138
205
  add_app: "Put this tag and the identify line on the app:",
@@ -143,6 +210,7 @@ const STEP = {
143
210
  export async function cmdUsage(ctx) {
144
211
  const { client, flags, io } = ctx;
145
212
  if (ctx.positionals[0] === "case-study") return cmdCaseStudy(ctx);
213
+ if (ctx.positionals[0] === "posthog") return cmdPostHog(ctx);
146
214
  if (ctx.positionals[0] === "import-posthog") {
147
215
  if (!flags.project || !flags.key) throw new CliError("import-posthog needs --project and --key.");
148
216
  const result = await client.callTool(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@letterstory/cli",
3
- "version": "0.177.61",
3
+ "version": "0.177.62",
4
4
  "description": "Spin up and manage Letter phantom blogs from your terminal.",
5
5
  "type": "module",
6
6
  "license": "UNLICENSED",