@elmohq/cli 0.2.12 → 0.2.14

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/index.js CHANGED
@@ -7,1125 +7,1367 @@ import path from "node:path";
7
7
  import { fileURLToPath } from "node:url";
8
8
  import * as p from "@clack/prompts";
9
9
  import { Command } from "commander";
10
- import { parse as parseDotenv } from "dotenv";
10
+ import { parse } from "dotenv";
11
11
  import pc from "picocolors";
12
12
  import semver from "semver";
13
- import { submitNewsletterSignup, trackCliEvent } from "./telemetry.js";
14
- // ── Constants ────────────────────────────────────────────────────────────────
13
+ import { PostHog } from "posthog-node";
14
+ //#region ../../packages/config/src/scrape-targets.ts
15
+ /**
16
+ * Parse the SCRAPE_TARGETS env var into structured ModelConfig objects.
17
+ *
18
+ * Parsing: split on ":". First = model, second = provider. If last segment is "online",
19
+ * pop it (websearch = true). Remaining middle segments rejoined with ":" = version slug.
20
+ * This naturally handles OpenRouter variant suffixes like ":free".
21
+ */
22
+ function parseScrapeTargets(envValue) {
23
+ if (!envValue || !envValue.trim()) throw new Error("SCRAPE_TARGETS environment variable is required. Set it to configure which AI models to track. Example:\n SCRAPE_TARGETS=chatgpt:olostep:online,google-ai-mode:olostep:online,copilot:olostep:online\nSee https://docs.elmohq.com/docs/user-guide/providers for details.");
24
+ return envValue.split(",").map((raw) => {
25
+ const trimmed = raw.trim();
26
+ if (!trimmed) throw new Error("Invalid SCRAPE_TARGETS: empty entry (check for trailing commas)");
27
+ const parts = trimmed.split(":");
28
+ if (parts.length < 2) throw new Error(`Invalid SCRAPE_TARGETS entry: "${trimmed}" (need at least model:provider)`);
29
+ const model = parts[0];
30
+ const provider = parts[1];
31
+ const webSearch = parts[parts.length - 1] === "online";
32
+ const versionParts = parts.slice(2, webSearch ? -1 : void 0);
33
+ return {
34
+ model,
35
+ provider,
36
+ version: versionParts.length > 0 ? versionParts.join(":") : void 0,
37
+ webSearch
38
+ };
39
+ });
40
+ }
41
+ /**
42
+ * Inverse of parseScrapeTargets for a single entry: build the
43
+ * model:provider[:version][:online] string from a ModelConfig.
44
+ */
45
+ function formatScrapeTarget(config) {
46
+ const parts = [config.model, config.provider];
47
+ if (config.version) parts.push(config.version);
48
+ if (config.webSearch) parts.push("online");
49
+ return parts.join(":");
50
+ }
51
+ //#endregion
52
+ //#region src/compose-pin.ts
53
+ function renderedByHeader(version) {
54
+ return [`# Rendered by elmo ${version} on ${(/* @__PURE__ */ new Date()).toISOString()}`, "# Run `elmo upgrade` after upgrading the CLI to refresh this file."].join("\n");
55
+ }
56
+ function parseRenderedVersion(contents) {
57
+ const match = contents.match(/^# Rendered by elmo (\S+) on /m);
58
+ return match ? match[1] : null;
59
+ }
60
+ function refreshHeaderVersion(contents, version) {
61
+ if (!/^# Rendered by elmo \S+ on /m.test(contents)) return `${renderedByHeader(version)}\n${contents}`;
62
+ return contents.replace(/^# Rendered by elmo \S+ on .*$/m, `# Rendered by elmo ${version} on ${(/* @__PURE__ */ new Date()).toISOString()}`);
63
+ }
64
+ function repinImages(contents, version) {
65
+ return contents.replace(/(image:\s*elmohq\/elmo-[a-z-]+):\S+/g, `$1:${version}`);
66
+ }
67
+ //#endregion
68
+ //#region src/migrations/planner.ts
69
+ function planMigrations(from, to, all) {
70
+ if (!semver.valid(from)) throw new Error(`Invalid from version: ${from}`);
71
+ if (!semver.valid(to)) throw new Error(`Invalid to version: ${to}`);
72
+ if (semver.gt(from, to)) throw new Error(`Downgrade not supported: ${from} -> ${to}`);
73
+ if (semver.eq(from, to)) return [];
74
+ for (const m of all) {
75
+ if (!semver.valid(m.from)) throw new Error(`Migration has invalid from: ${m.from}`);
76
+ if (!semver.valid(m.to)) throw new Error(`Migration has invalid to: ${m.to}`);
77
+ if (!semver.gt(m.to, m.from)) throw new Error(`Migration ${m.from} -> ${m.to}: 'to' must be greater than 'from'`);
78
+ }
79
+ const applicable = all.filter((m) => semver.gte(m.from, from) && semver.lt(m.from, to)).slice().sort((a, b) => semver.compare(a.from, b.from));
80
+ const seen = /* @__PURE__ */ new Set();
81
+ for (const m of applicable) {
82
+ if (seen.has(m.from)) throw new Error(`Multiple migrations registered with from=${m.from}`);
83
+ seen.add(m.from);
84
+ }
85
+ return applicable;
86
+ }
87
+ async function runMigrations(plan, ctx) {
88
+ for (const migration of plan) {
89
+ ctx.log.step(`Migrating ${migration.from} → ${migration.to}: ${migration.description}`);
90
+ await migration.run(ctx);
91
+ }
92
+ }
93
+ //#endregion
94
+ //#region src/migrations/index.ts
95
+ const MIGRATIONS = [];
96
+ //#endregion
97
+ //#region src/telemetry.ts
98
+ const POSTHOG_PUBLIC_KEY = "phc_Jhx9LnI9cTDFHpQmpOzJSDTW127qD9pFU65KRnYym6z";
99
+ const POSTHOG_HOST = "https://us.i.posthog.com";
100
+ function envDisabled() {
101
+ return Boolean(process.env.DISABLE_TELEMETRY);
102
+ }
103
+ async function readEnvKey(configDir, key) {
104
+ try {
105
+ return parse(await fs.readFile(path.join(configDir, ".env"), "utf8"))[key];
106
+ } catch {
107
+ return;
108
+ }
109
+ }
110
+ async function isTelemetryDisabled(configDir) {
111
+ if (envDisabled()) return true;
112
+ return Boolean(await readEnvKey(configDir, "DISABLE_TELEMETRY"));
113
+ }
114
+ async function trackCliEvent(configDir, eventName, properties, personProperties) {
115
+ if (await isTelemetryDisabled(configDir)) return;
116
+ const distinctId = await readEnvKey(configDir, "DEPLOYMENT_ID");
117
+ if (!distinctId) return;
118
+ try {
119
+ const client = new PostHog(POSTHOG_PUBLIC_KEY, { host: POSTHOG_HOST });
120
+ client.capture({
121
+ distinctId,
122
+ event: eventName,
123
+ properties: {
124
+ ...properties,
125
+ ...personProperties ? { $set: personProperties } : {}
126
+ }
127
+ });
128
+ await client.shutdown();
129
+ } catch {}
130
+ }
131
+ async function submitNewsletterSignup(configDir, email) {
132
+ try {
133
+ const distinctId = (await isTelemetryDisabled(configDir) ? null : await readEnvKey(configDir, "DEPLOYMENT_ID")) ?? email;
134
+ const client = new PostHog(POSTHOG_PUBLIC_KEY, { host: POSTHOG_HOST });
135
+ client.capture({
136
+ distinctId,
137
+ event: "newsletter_signup",
138
+ properties: {
139
+ source: "cli_init",
140
+ $set: {
141
+ email,
142
+ wants_updates: true
143
+ }
144
+ }
145
+ });
146
+ await client.shutdown();
147
+ } catch {}
148
+ }
149
+ //#endregion
150
+ //#region src/index.ts
15
151
  const CONFIG_HOME = path.join(os.homedir(), ".elmo");
16
152
  const DEFAULT_APP_NAME = "Elmo";
17
153
  const DEFAULT_APP_ICON = "/icons/elmo-icon.svg";
18
154
  const DEFAULT_APP_PORT = 1515;
19
155
  const LOCAL_DATABASE_URL = "postgres://postgres:postgres@postgres:5432/elmo";
20
156
  const TELEMETRY_DOC_URL = "https://elmohq.com/docs/developer-guide/telemetry";
21
- // ── Banner ───────────────────────────────────────────────────────────────────
22
157
  const ELMO_ASCII = [
23
- "",
24
- " ▄▄ ",
25
- " ██ ",
26
- "▄█▀█▄ ██ ███▄███▄ ▄███▄ ",
27
- "██▄█▀ ██ ██ ██ ██ ██ ██ ",
28
- "▀█▄▄▄ ██ ██ ██ ██ ▀███▀ ",
29
- "",
158
+ "",
159
+ " ▄▄ ",
160
+ " ██ ",
161
+ "▄█▀█▄ ██ ███▄███▄ ▄███▄ ",
162
+ "██▄█▀ ██ ██ ██ ██ ██ ██ ",
163
+ "▀█▄▄▄ ██ ██ ██ ██ ▀███▀ ",
164
+ ""
30
165
  ].join("\n");
31
166
  function printBanner() {
32
- // text-blue-600 ≈ #2563EB → RGB(37, 99, 235)
33
- const blue = "\x1b[38;2;37;99;235m";
34
- const reset = "\x1b[0m";
35
- console.log(`${blue}${ELMO_ASCII}${reset}`);
167
+ console.log(`${ELMO_ASCII}`);
36
168
  }
37
- // ── Logging ──────────────────────────────────────────────────────────────────
38
169
  const log = {
39
- info: (msg) => p.log.info(msg),
40
- warn: (msg) => p.log.warn(msg),
41
- error: (msg) => p.log.error(msg),
42
- success: (msg) => p.log.success(msg),
43
- step: (msg) => p.log.step(msg),
170
+ info: (msg) => p.log.info(msg),
171
+ warn: (msg) => p.log.warn(msg),
172
+ error: (msg) => p.log.error(msg),
173
+ success: (msg) => p.log.success(msg),
174
+ step: (msg) => p.log.step(msg)
44
175
  };
45
- // ── Helpers ──────────────────────────────────────────────────────────────────
46
176
  function assertNotCancelled(value) {
47
- if (p.isCancel(value)) {
48
- p.cancel("Setup cancelled.");
49
- process.exit(0);
50
- }
177
+ if (p.isCancel(value)) {
178
+ p.cancel("Setup cancelled.");
179
+ process.exit(0);
180
+ }
51
181
  }
52
182
  function generateSecret(bytes = 32) {
53
- return crypto.randomBytes(bytes).toString("base64url");
183
+ return crypto.randomBytes(bytes).toString("base64url");
54
184
  }
55
185
  function link(text, url) {
56
- // OSC 8 hyperlink: clickable in iTerm2, Windows Terminal, GNOME Terminal, etc.
57
- // Falls back to plain text in unsupported terminals.
58
- return `\x1b]8;;${url}\x07${text}\x1b]8;;\x07`;
186
+ return `\x1b]8;;${url}\x07${text}\x1b]8;;\x07`;
59
187
  }
60
- // ── Main ─────────────────────────────────────────────────────────────────────
61
188
  async function main() {
62
- const version = await getPackageVersion();
63
- const program = new Command();
64
- program
65
- .name("elmo")
66
- .version(version)
67
- .option("--dir <path>", "Config directory")
68
- .configureHelp({ showGlobalOptions: true })
69
- .action(() => {
70
- printBanner();
71
- program.outputHelp();
72
- });
73
- program
74
- .command("init")
75
- .description("set up local Elmo instance")
76
- .option("--dev", "Use local build context (repo only)")
77
- .option("--docker-dir <path>", "Path to Docker build context (dev mode)")
78
- .action(async (_opts, cmd) => {
79
- await withVersionCheck(version, () => runInit(cmd.optsWithGlobals(), version));
80
- });
81
- program
82
- .command("compose")
83
- .description("run Docker Compose commands using your Elmo config")
84
- .allowUnknownOption(true)
85
- .argument("[args...]", "Arguments passed to Docker Compose")
86
- .action(async (args, _opts, cmd) => {
87
- await withVersionCheck(version, () => runCompose(args, cmd.optsWithGlobals()));
88
- });
89
- program
90
- .command("edit")
91
- .description("change API keys, scrape targets, or the Docker Compose YAML")
92
- .argument("<env|compose>", "which config file to edit")
93
- .action(async (target, _opts, cmd) => {
94
- await runEdit(target, cmd.optsWithGlobals());
95
- });
96
- await program.parseAsync(process.argv);
189
+ const version = await getPackageVersion();
190
+ const program = new Command();
191
+ program.name("elmo").version(version).option("--dir <path>", "Config directory").configureHelp({ showGlobalOptions: true }).action(() => {
192
+ printBanner();
193
+ program.outputHelp();
194
+ });
195
+ program.command("init").description("set up local Elmo instance").option("--dev", "Use local build context (repo only)").option("--docker-dir <path>", "Path to Docker build context (dev mode)").action(async (_opts, cmd) => {
196
+ await withVersionCheck(version, () => runInit(cmd.optsWithGlobals(), version));
197
+ });
198
+ program.command("compose").description("run Docker Compose commands using your Elmo config").allowUnknownOption(true).argument("[args...]", "Arguments passed to Docker Compose").action(async (args, _opts, cmd) => {
199
+ await withVersionCheck(version, () => runCompose(args, cmd.optsWithGlobals()));
200
+ });
201
+ program.command("edit").description("change API keys, scrape targets, or the Docker Compose YAML").argument("<env|compose>", "which config file to edit").action(async (target, _opts, cmd) => {
202
+ await runEdit(target, cmd.optsWithGlobals());
203
+ });
204
+ program.command("upgrade").description("upgrade your Elmo deployment to this CLI's version").option("--yes", "skip confirmation prompts").action(async (_opts, cmd) => {
205
+ await runUpgrade(cmd.optsWithGlobals(), version);
206
+ });
207
+ await program.parseAsync(process.argv);
97
208
  }
98
209
  async function withVersionCheck(version, fn) {
99
- const notifyPromise = maybeNotifyNewVersion(version);
100
- await fn();
101
- await notifyPromise.catch(() => undefined);
210
+ const notifyPromise = maybeNotifyNewVersion(version);
211
+ await fn();
212
+ await notifyPromise.catch(() => void 0);
102
213
  }
103
- // ── Command: init ────────────────────────────────────────────────────────────
104
214
  async function runInit(options, version) {
105
- printBanner();
106
- p.intro(pc.bold("Setting up Elmo"));
107
- const cwd = process.cwd();
108
- // ── Resolve config directory ─────────────────────────────────────────
109
- const configDir = options.dir ? path.resolve(cwd, options.dir) : CONFIG_HOME;
110
- // ── .env safety check ────────────────────────────────────────────────
111
- const existingEnvPath = path.join(configDir, ".env");
112
- let preservedDeploymentId;
113
- if (await fileExists(existingEnvPath)) {
114
- const contents = await fs.readFile(existingEnvPath, "utf8");
115
- const isElmoEnv = contents.startsWith("# Rendered by elmo") || contents.startsWith("# Generated by elmo");
116
- if (!isElmoEnv) {
117
- p.log.warn(`A .env file already exists in ${configDir} and was NOT created by Elmo.`);
118
- const overwrite = await p.confirm({
119
- message: "Overwrite the existing .env file? This cannot be undone.",
120
- initialValue: false,
121
- });
122
- assertNotCancelled(overwrite);
123
- if (!overwrite) {
124
- p.cancel("Setup cancelled. Choose a different directory with --dir.");
125
- process.exit(0);
126
- }
127
- }
128
- else {
129
- p.log.warn(`An existing Elmo config was found at ${configDir}.`);
130
- const overwrite = await p.confirm({
131
- message: "Overwrite it with new values? Existing secrets (DATABASE_URL, API keys, etc.) will be replaced.",
132
- initialValue: false,
133
- });
134
- assertNotCancelled(overwrite);
135
- if (!overwrite) {
136
- p.cancel("Setup cancelled. Use `elmo edit env` to change individual values.");
137
- process.exit(0);
138
- }
139
- preservedDeploymentId = parseDotenv(contents).DEPLOYMENT_ID;
140
- }
141
- }
142
- // ── Dev mode: resolve docker directory ───────────────────────────────
143
- let dockerDir;
144
- let repoRoot;
145
- if (options.dev) {
146
- if (options.dockerDir) {
147
- dockerDir = path.resolve(cwd, options.dockerDir);
148
- if (!(await fileExists(path.join(dockerDir, "Dockerfile")))) {
149
- p.log.error(`Dockerfile not found in ${dockerDir}`);
150
- process.exit(1);
151
- }
152
- }
153
- else {
154
- dockerDir = await resolveDockerDirInteractive(cwd);
155
- }
156
- repoRoot = path.resolve(dockerDir, "..");
157
- }
158
- else {
159
- repoRoot = cwd;
160
- }
161
- // ── Data stores ──────────────────────────────────────────────────────
162
- const postgresMode = await p.select({
163
- message: "PostgreSQL connection",
164
- options: [
165
- {
166
- value: "docker",
167
- label: "Run Postgres in Docker",
168
- },
169
- {
170
- value: "external",
171
- label: "Use existing Postgres (provide DATABASE_URL)",
172
- },
173
- ],
174
- initialValue: "docker",
175
- });
176
- assertNotCancelled(postgresMode);
177
- const env = {};
178
- env.DEPLOYMENT_MODE = "local";
179
- env.VITE_DEPLOYMENT_MODE = "local";
180
- env.DEPLOYMENT_ID = preservedDeploymentId ?? crypto.randomUUID();
181
- env.BETTER_AUTH_SECRET = generateSecret();
182
- env.APP_NAME = DEFAULT_APP_NAME;
183
- env.APP_ICON = DEFAULT_APP_ICON;
184
- env.VITE_APP_NAME = DEFAULT_APP_NAME;
185
- env.VITE_APP_ICON = DEFAULT_APP_ICON;
186
- if (postgresMode === "external") {
187
- p.note("Must be an IPv4-compatible direct connection or database pooler.", "DATABASE_URL");
188
- const url = await p.password({
189
- message: "DATABASE_URL",
190
- validate: (v) => (!v ? "Required" : undefined),
191
- });
192
- assertNotCancelled(url);
193
- env.DATABASE_URL = url;
194
- }
195
- else {
196
- env.DATABASE_URL = LOCAL_DATABASE_URL;
197
- }
198
- // ── AI providers ─────────────────────────────────────────────────────
199
- const setupMode = await configureProvidersInteractive(env);
200
- // ── Telemetry ───────────────────────────────────────────────────────
201
- p.note([
202
- "Elmo is open source and maintained by a small team. Telemetry",
203
- "from both the CLI and your local deployment (web + worker)",
204
- "tells us things like which CLI versions are still in use, where",
205
- "`elmo init` drops off, which providers people pick, and whether",
206
- "new features actually get used. Without it we are flying blind",
207
- "on what to fix or build next.",
208
- "",
209
- pc.bold("What we send:"),
210
- " • deployment ID (random UUID stored as DEPLOYMENT_ID in your .env)",
211
- " • CLI/app version, OS, arch, Node version, deployment mode",
212
- " • command/event names + non-secret options (e.g. postgres mode)",
213
- " • feature counts (prompts edited, brands created — never the names or text)",
214
- " • IP address (recorded on each event by PostHog, used for geolocation)",
215
- "",
216
- pc.bold("What we never send:"),
217
- " API keys, .env contents, brand names, prompt text, and scraped responses.",
218
- "",
219
- `Full breakdown: ${link(pc.cyan(TELEMETRY_DOC_URL), TELEMETRY_DOC_URL)}`,
220
- "Toggle later by editing DISABLE_TELEMETRY in .env (`elmo edit env`).",
221
- ].join("\n"), "Telemetry");
222
- const telemetryEnabled = await p.confirm({
223
- message: "Share telemetry?",
224
- initialValue: true,
225
- });
226
- assertNotCancelled(telemetryEnabled);
227
- if (!telemetryEnabled) {
228
- env.DISABLE_TELEMETRY = "1";
229
- }
230
- // ── Product updates ─────────────────────────────────────────────────
231
- const updatesEmail = await p.text({
232
- message: "Enter your work email to receive product updates (optional)",
233
- placeholder: "you@example.com",
234
- });
235
- const email = p.isCancel(updatesEmail) ? undefined : updatesEmail || undefined;
236
- // ── Web app port ────────────────────────────────────────────────────
237
- const portInput = await p.text({
238
- message: "Web app port",
239
- placeholder: String(DEFAULT_APP_PORT),
240
- defaultValue: String(DEFAULT_APP_PORT),
241
- validate: (v) => {
242
- if (!v)
243
- return undefined;
244
- const n = Number(v);
245
- if (!Number.isInteger(n) || n < 1 || n > 65535) {
246
- return "Must be an integer between 1 and 65535";
247
- }
248
- return undefined;
249
- },
250
- });
251
- assertNotCancelled(portInput);
252
- const port = Number(portInput);
253
- env.APP_URL = `http://localhost:${port}`;
254
- env.VITE_APP_URL = env.APP_URL;
255
- // ── Write config ─────────────────────────────────────────────────────
256
- const composeYaml = buildComposeYaml({
257
- dev: Boolean(options.dev),
258
- postgresMode,
259
- repoRoot,
260
- dockerDir,
261
- port,
262
- version,
263
- });
264
- await ensureDir(configDir);
265
- await writeConfigFiles(configDir, {
266
- env,
267
- composeYaml,
268
- postgresMode,
269
- dev: Boolean(options.dev),
270
- version,
271
- });
272
- p.log.success(`Config written to ${configDir}`);
273
- p.log.warn("Your generated .env file contains secrets — do not commit it to version control.");
274
- if (options.dev) {
275
- p.log.info("Dev mode enabled. Run `elmo compose build` before starting.");
276
- }
277
- const shouldStart = await p.confirm({
278
- message: "Start the stack now?",
279
- initialValue: true,
280
- });
281
- assertNotCancelled(shouldStart);
282
- if (shouldStart) {
283
- await doStart(configDir);
284
- }
285
- else {
286
- p.log.info("You can start later with `elmo compose up -d`.");
287
- }
288
- // CLI telemetry — silently dropped if the user opted out above.
289
- await trackCliEvent(configDir, "cli_init", {
290
- version,
291
- os: process.platform,
292
- arch: process.arch,
293
- node_version: process.version,
294
- postgres_mode: postgresMode,
295
- dev_mode: Boolean(options.dev),
296
- setup_mode: setupMode,
297
- has_scraper: Boolean(env.BRIGHTDATA_API_TOKEN || env.OLOSTEP_API_KEY),
298
- has_direct_api: hasDirectApiConfigured(env),
299
- });
300
- // Newsletter signup is a separate, explicit opt-in and runs even when
301
- // telemetry is disabled.
302
- if (email) {
303
- await submitNewsletterSignup(configDir, email);
304
- }
305
- p.log.message(`If you find Elmo useful, star us on GitHub!\n ${link(pc.cyan("https://github.com/elmohq/elmo"), "https://github.com/elmohq/elmo")}`);
306
- p.outro(pc.green("Setup complete!"));
307
- }
308
- // ── Provider Configuration ───────────────────────────────────────────────────
215
+ printBanner();
216
+ p.intro(pc.bold("Setting up Elmo"));
217
+ const cwd = process.cwd();
218
+ const configDir = options.dir ? path.resolve(cwd, options.dir) : CONFIG_HOME;
219
+ const existingEnvPath = path.join(configDir, ".env");
220
+ let preservedDeploymentId;
221
+ if (await fileExists(existingEnvPath)) {
222
+ const contents = await fs.readFile(existingEnvPath, "utf8");
223
+ if (!(contents.startsWith("# Rendered by elmo") || contents.startsWith("# Generated by elmo"))) {
224
+ p.log.warn(`A .env file already exists in ${configDir} and was NOT created by Elmo.`);
225
+ const overwrite = await p.confirm({
226
+ message: "Overwrite the existing .env file? This cannot be undone.",
227
+ initialValue: false
228
+ });
229
+ assertNotCancelled(overwrite);
230
+ if (!overwrite) {
231
+ p.cancel("Setup cancelled. Choose a different directory with --dir.");
232
+ process.exit(0);
233
+ }
234
+ } else {
235
+ p.log.warn(`An existing Elmo config was found at ${configDir}.`);
236
+ const overwrite = await p.confirm({
237
+ message: "Overwrite it with new values? Existing secrets (DATABASE_URL, API keys, etc.) will be replaced.",
238
+ initialValue: false
239
+ });
240
+ assertNotCancelled(overwrite);
241
+ if (!overwrite) {
242
+ p.cancel("Setup cancelled. Use `elmo edit env` to change individual values.");
243
+ process.exit(0);
244
+ }
245
+ preservedDeploymentId = parse(contents).DEPLOYMENT_ID;
246
+ }
247
+ }
248
+ let dockerDir;
249
+ let repoRoot;
250
+ if (options.dev) {
251
+ if (options.dockerDir) {
252
+ dockerDir = path.resolve(cwd, options.dockerDir);
253
+ if (!await fileExists(path.join(dockerDir, "Dockerfile"))) {
254
+ p.log.error(`Dockerfile not found in ${dockerDir}`);
255
+ process.exit(1);
256
+ }
257
+ } else dockerDir = await resolveDockerDirInteractive(cwd);
258
+ repoRoot = path.resolve(dockerDir, "..");
259
+ } else repoRoot = cwd;
260
+ const postgresMode = await p.select({
261
+ message: "PostgreSQL connection",
262
+ options: [{
263
+ value: "docker",
264
+ label: "Run Postgres in Docker"
265
+ }, {
266
+ value: "external",
267
+ label: "Use existing Postgres (provide DATABASE_URL)"
268
+ }],
269
+ initialValue: "docker"
270
+ });
271
+ assertNotCancelled(postgresMode);
272
+ const env = {};
273
+ env.DEPLOYMENT_MODE = "local";
274
+ env.VITE_DEPLOYMENT_MODE = "local";
275
+ env.DEPLOYMENT_ID = preservedDeploymentId ?? crypto.randomUUID();
276
+ env.BETTER_AUTH_SECRET = generateSecret();
277
+ env.APP_NAME = DEFAULT_APP_NAME;
278
+ env.APP_ICON = DEFAULT_APP_ICON;
279
+ env.VITE_APP_NAME = DEFAULT_APP_NAME;
280
+ env.VITE_APP_ICON = DEFAULT_APP_ICON;
281
+ if (postgresMode === "external") {
282
+ p.note("Must be an IPv4-compatible direct connection or database pooler.", "DATABASE_URL");
283
+ const url = await p.password({
284
+ message: "DATABASE_URL",
285
+ validate: (v) => !v ? "Required" : void 0
286
+ });
287
+ assertNotCancelled(url);
288
+ env.DATABASE_URL = url;
289
+ } else env.DATABASE_URL = LOCAL_DATABASE_URL;
290
+ const setupMode = await configureProvidersInteractive(env);
291
+ p.note([
292
+ "Elmo is open source and maintained by a small team. Telemetry",
293
+ "from both the CLI and your local deployment (web + worker)",
294
+ "tells us things like which CLI versions are still in use, where",
295
+ "`elmo init` drops off, which providers people pick, and whether",
296
+ "new features actually get used. Without it we are flying blind",
297
+ "on what to fix or build next.",
298
+ "",
299
+ pc.bold("What we send:"),
300
+ " • deployment ID (random UUID stored as DEPLOYMENT_ID in your .env)",
301
+ " • CLI/app version, OS, arch, Node version, deployment mode",
302
+ " • command/event names + non-secret options (e.g. postgres mode)",
303
+ " • feature counts (prompts edited, brands created — never the names or text)",
304
+ " • IP address (recorded on each event by PostHog, used for geolocation)",
305
+ "",
306
+ pc.bold("What we never send:"),
307
+ " API keys, .env contents, brand names, prompt text, and scraped responses.",
308
+ "",
309
+ `Full breakdown: ${link(pc.cyan(TELEMETRY_DOC_URL), TELEMETRY_DOC_URL)}`,
310
+ "Toggle later by editing DISABLE_TELEMETRY in .env (`elmo edit env`)."
311
+ ].join("\n"), "Telemetry");
312
+ const telemetryEnabled = await p.confirm({
313
+ message: "Share telemetry?",
314
+ initialValue: true
315
+ });
316
+ assertNotCancelled(telemetryEnabled);
317
+ if (!telemetryEnabled) env.DISABLE_TELEMETRY = "1";
318
+ const updatesEmail = await p.text({
319
+ message: "Enter your work email to receive product updates (optional)",
320
+ placeholder: "you@example.com"
321
+ });
322
+ const email = p.isCancel(updatesEmail) ? void 0 : updatesEmail || void 0;
323
+ const portInput = await p.text({
324
+ message: "Web app port",
325
+ placeholder: String(DEFAULT_APP_PORT),
326
+ defaultValue: String(DEFAULT_APP_PORT),
327
+ validate: (v) => {
328
+ if (!v) return void 0;
329
+ const n = Number(v);
330
+ if (!Number.isInteger(n) || n < 1 || n > 65535) return "Must be an integer between 1 and 65535";
331
+ }
332
+ });
333
+ assertNotCancelled(portInput);
334
+ const port = Number(portInput);
335
+ env.APP_URL = `http://localhost:${port}`;
336
+ env.VITE_APP_URL = env.APP_URL;
337
+ const composeYaml = buildComposeYaml({
338
+ dev: Boolean(options.dev),
339
+ postgresMode,
340
+ repoRoot,
341
+ dockerDir,
342
+ port,
343
+ version
344
+ });
345
+ await ensureDir(configDir);
346
+ await writeConfigFiles(configDir, {
347
+ env,
348
+ composeYaml,
349
+ postgresMode,
350
+ dev: Boolean(options.dev),
351
+ version
352
+ });
353
+ p.log.success(`Config written to ${configDir}`);
354
+ p.log.warn("Your generated .env file contains secrets — do not commit it to version control.");
355
+ if (options.dev) p.log.info("Dev mode enabled. Run `elmo compose build` before starting.");
356
+ const shouldStart = await p.confirm({
357
+ message: "Start the stack now?",
358
+ initialValue: true
359
+ });
360
+ assertNotCancelled(shouldStart);
361
+ if (shouldStart) await doStart(configDir);
362
+ else p.log.info("You can start later with `elmo compose up -d`.");
363
+ await trackCliEvent(configDir, "cli_init", {
364
+ version,
365
+ os: process.platform,
366
+ arch: process.arch,
367
+ node_version: process.version,
368
+ postgres_mode: postgresMode,
369
+ dev_mode: Boolean(options.dev),
370
+ setup_mode: setupMode,
371
+ has_scraper: Boolean(env.BRIGHTDATA_API_TOKEN || env.OLOSTEP_API_KEY || env.OXYLABS_USERNAME),
372
+ has_direct_api: hasDirectApiConfigured(env)
373
+ });
374
+ if (email) await submitNewsletterSignup(configDir, email);
375
+ p.log.message(`If you find Elmo useful, star us on GitHub!\n ${link(pc.cyan("https://github.com/elmohq/elmo"), "https://github.com/elmohq/elmo")}`);
376
+ p.outro(pc.green("Setup complete!"));
377
+ }
309
378
  const BRIGHTDATA_AFFILIATE = "https://get.brightdata.com/67h1b7h0shcn";
310
379
  const OLOSTEP_AFFILIATE = "https://olostep.com/?ref=elmo";
380
+ const OXYLABS_AFFILIATE = "https://oxylabs.go2cloud.org/aff_c?offer_id=7&aff_id=2263&url_id=32";
311
381
  const PROVIDERS_DOC_URL = "https://docs.elmohq.com/docs/user-guide/providers";
312
- // Surfaces each scraper can track — the first two are the "recommended starter" set.
313
- const BRIGHTDATA_MODELS = ["chatgpt", "google-ai-mode", "perplexity", "copilot", "gemini", "grok"];
382
+ const BRIGHTDATA_MODELS = [
383
+ "chatgpt",
384
+ "google-ai-mode",
385
+ "perplexity",
386
+ "copilot",
387
+ "gemini",
388
+ "grok"
389
+ ];
314
390
  const OLOSTEP_MODELS = [
315
- "chatgpt",
316
- "google-ai-mode",
317
- "google-ai-overview",
318
- "perplexity",
319
- "copilot",
320
- "gemini",
321
- "grok",
391
+ "chatgpt",
392
+ "google-ai-mode",
393
+ "google-ai-overview",
394
+ "perplexity",
395
+ "copilot",
396
+ "gemini",
397
+ "grok"
398
+ ];
399
+ const OXYLABS_MODELS = [
400
+ "chatgpt",
401
+ "google-ai-mode",
402
+ "perplexity"
322
403
  ];
323
404
  const DEFAULT_SCRAPER_MODELS = ["chatgpt", "google-ai-mode"];
405
+ const DATAFORSEO_MODELS = [
406
+ "google-ai-mode",
407
+ "chatgpt",
408
+ "perplexity",
409
+ "gemini"
410
+ ];
324
411
  const DEFAULT_OPENAI_MODEL = "gpt-5-mini";
325
- const DEFAULT_ANTHROPIC_MODEL = "claude-sonnet-4-20250514";
412
+ const DEFAULT_ANTHROPIC_MODEL = "claude-sonnet-4-6";
326
413
  const DEFAULT_OPENROUTER_MODEL = "anthropic/claude-sonnet-4.6";
327
414
  const DEFAULT_MISTRAL_MODEL = "mistral-medium-latest";
328
415
  async function configureProvidersInteractive(env) {
329
- p.note([
330
- "Elmo needs two kinds of providers:",
331
- "",
332
- pc.bold("1. A scraper") + " — to track ChatGPT and Google AI Mode (no public APIs):",
333
- ` • ${pc.cyan("BrightData")} — cheap solid option, ~$0.45/mo per prompt`,
334
- ` • ${pc.cyan("Olostep")} — premium option, powers Peec/AirOps, ~$2.25/mo per prompt`,
335
- "",
336
- pc.bold("2. A direct LLM API") + " — for low-latency tasks (onboarding analysis, sentiment scoring,",
337
- " ad-hoc LLM calls). Required:",
338
- ` • ${pc.cyan("OpenRouter")} — one key, all major models (recommended)`,
339
- ` • ${pc.cyan("Anthropic / OpenAI / Mistral")} — direct provider keys`,
340
- "",
341
- "Pricing assumes Elmo's default cadence (5 runs/day × 2 surfaces).",
342
- ].join("\n"), "AI providers");
343
- const mode = await p.select({
344
- message: "Setup mode",
345
- options: [
346
- { value: "recommended", label: "Recommended — one scraper + one direct API" },
347
- { value: "custom", label: "Custom — pick each provider individually" },
348
- ],
349
- initialValue: "recommended",
350
- });
351
- assertNotCancelled(mode);
352
- if (mode === "recommended") {
353
- await configureProvidersRecommended(env);
354
- }
355
- else {
356
- await configureProvidersCustom(env);
357
- }
358
- return mode;
416
+ p.note([
417
+ "Elmo needs two kinds of providers:",
418
+ "",
419
+ pc.bold("1. A scraper") + " — to track ChatGPT and Google AI Mode (no public APIs):",
420
+ ` • ${pc.cyan("BrightData")} — cheap solid option, ~$0.45/mo per prompt`,
421
+ ` • ${pc.cyan("Oxylabs")} — sync realtime API, pay-as-you-go`,
422
+ ` • ${pc.cyan("Olostep")} — premium option, powers Peec/AirOps, ~$2.25/mo per prompt`,
423
+ "",
424
+ pc.bold("2. A direct LLM API") + " — for low-latency tasks (onboarding analysis, sentiment scoring,",
425
+ " ad-hoc LLM calls). Required:",
426
+ ` • ${pc.cyan("OpenRouter")} — one key, all major models (recommended)`,
427
+ ` • ${pc.cyan("Anthropic / OpenAI / Mistral")} — direct provider keys`,
428
+ "",
429
+ "Pricing assumes Elmo's default cadence (5 runs/day × 2 surfaces)."
430
+ ].join("\n"), "AI providers");
431
+ const mode = await p.select({
432
+ message: "Setup mode",
433
+ options: [{
434
+ value: "recommended",
435
+ label: "Recommended — one scraper + one direct API"
436
+ }, {
437
+ value: "custom",
438
+ label: "Custom — pick each provider individually"
439
+ }],
440
+ initialValue: "recommended"
441
+ });
442
+ assertNotCancelled(mode);
443
+ if (mode === "recommended") await configureProvidersRecommended(env);
444
+ else await configureProvidersCustom(env);
445
+ return mode;
359
446
  }
360
447
  async function configureProvidersRecommended(env) {
361
- const targets = [];
362
- // ── Scraper ─────────────────────────────────────────────────────────────
363
- const scraper = await p.select({
364
- message: "Scraper (tracks ChatGPT + Google AI Mode)",
365
- options: [
366
- { value: "brightdata", label: "BrightData — ~$0.45/mo per prompt (cheaper)" },
367
- { value: "olostep", label: "Olostep — ~$2.25/mo per prompt (premium)" },
368
- ],
369
- initialValue: "brightdata",
370
- });
371
- assertNotCancelled(scraper);
372
- await collectScraperKey(scraper, env);
373
- for (const model of DEFAULT_SCRAPER_MODELS) {
374
- targets.push(`${model}:${scraper}:online`);
375
- }
376
- // ── Direct API ──────────────────────────────────────────────────────────
377
- const direct = await p.select({
378
- message: "Direct LLM API (powers onboarding analysis + sentiment scoring)",
379
- options: [
380
- { value: "openrouter", label: "OpenRouter — one key, all major models (recommended)" },
381
- { value: "anthropic", label: "Anthropic — direct Claude" },
382
- { value: "openai", label: "OpenAI — direct GPT-* models" },
383
- { value: "mistral", label: "Mistral — direct Mistral models" },
384
- ],
385
- initialValue: "openrouter",
386
- });
387
- assertNotCancelled(direct);
388
- await collectDirectApiQuick(direct, env);
389
- await finalizeScrapeTargets(env, targets, { skipEdit: true });
448
+ const targets = [];
449
+ const scraper = await p.select({
450
+ message: "Scraper (tracks ChatGPT + Google AI Mode)",
451
+ options: [
452
+ {
453
+ value: "brightdata",
454
+ label: "BrightData — ~$0.45/mo per prompt (cheaper)"
455
+ },
456
+ {
457
+ value: "oxylabs",
458
+ label: "Oxylabs — sync realtime API, pay-as-you-go"
459
+ },
460
+ {
461
+ value: "olostep",
462
+ label: "Olostep — ~$2.25/mo per prompt (premium)"
463
+ }
464
+ ],
465
+ initialValue: "brightdata"
466
+ });
467
+ assertNotCancelled(scraper);
468
+ await collectScraperKey(scraper, env);
469
+ for (const model of DEFAULT_SCRAPER_MODELS) targets.push(formatScrapeTarget({
470
+ model,
471
+ provider: scraper,
472
+ webSearch: true
473
+ }));
474
+ const direct = await p.select({
475
+ message: "Direct LLM API (powers onboarding analysis + sentiment scoring)",
476
+ options: [
477
+ {
478
+ value: "openrouter",
479
+ label: "OpenRouter — one key, all major models (recommended)"
480
+ },
481
+ {
482
+ value: "anthropic",
483
+ label: "Anthropic — direct Claude"
484
+ },
485
+ {
486
+ value: "openai",
487
+ label: "OpenAI — direct GPT-* models"
488
+ },
489
+ {
490
+ value: "mistral",
491
+ label: "Mistral — direct Mistral models"
492
+ }
493
+ ],
494
+ initialValue: "openrouter"
495
+ });
496
+ assertNotCancelled(direct);
497
+ await collectDirectApiQuick(direct, env);
498
+ await finalizeScrapeTargets(env, targets, { skipEdit: true });
390
499
  }
391
500
  async function configureProvidersCustom(env) {
392
- const targets = [];
393
- p.log.step(pc.bold("Step 1 of 2 — Direct LLM API (at least one is required)"));
394
- // Order matches the auto-pick preference in onboarding/llm.ts so the first
395
- // provider asked is the one onboarding will reach for by default.
396
- while (!hasDirectApiConfigured(env)) {
397
- await collectOpenRouter(env, targets);
398
- await collectAnthropic(env, targets);
399
- await collectOpenAI(env, targets);
400
- await collectMistral(env, targets);
401
- if (!hasDirectApiConfigured(env)) {
402
- p.log.warn("Onboarding analysis and other low-latency LLM tasks require a direct API. Configure at least one before continuing.");
403
- }
404
- }
405
- p.log.step(pc.bold("Step 2 of 2 — Scrapers (optional, but needed to track ChatGPT / Google AI Mode)"));
406
- await collectBrightData(env, targets);
407
- await collectOlostep(env, targets);
408
- await collectDataForSEO(env, targets);
409
- await finalizeScrapeTargets(env, targets);
501
+ const targets = [];
502
+ p.log.step(pc.bold("Step 1 of 2 — Direct LLM API (at least one is required)"));
503
+ while (!hasDirectApiConfigured(env)) {
504
+ await collectOpenRouter(env, targets);
505
+ await collectAnthropic(env, targets);
506
+ await collectOpenAI(env, targets);
507
+ await collectMistral(env, targets);
508
+ if (!hasDirectApiConfigured(env)) p.log.warn("Onboarding analysis and other low-latency LLM tasks require a direct API. Configure at least one before continuing.");
509
+ }
510
+ p.log.step(pc.bold("Step 2 of 2 — Scrapers (optional, but needed to track ChatGPT / Google AI Mode)"));
511
+ await collectBrightData(env, targets);
512
+ await collectOxylabs(env, targets);
513
+ await collectOlostep(env, targets);
514
+ await collectDataForSEO(env, targets);
515
+ await finalizeScrapeTargets(env, targets);
410
516
  }
411
517
  function hasDirectApiConfigured(env) {
412
- return Boolean(env.ANTHROPIC_API_KEY || env.OPENAI_API_KEY || env.MISTRAL_API_KEY || env.OPENROUTER_API_KEY);
518
+ return Boolean(env.ANTHROPIC_API_KEY || env.OPENAI_API_KEY || env.MISTRAL_API_KEY || env.OPENROUTER_API_KEY);
413
519
  }
414
520
  async function collectScraperKey(scraper, env) {
415
- if (scraper === "brightdata") {
416
- p.log.info(`Sign up: ${link(pc.cyan(BRIGHTDATA_AFFILIATE), BRIGHTDATA_AFFILIATE)}`);
417
- const key = await p.password({
418
- message: "BrightData API token",
419
- validate: (v) => (!v ? "Required" : undefined),
420
- });
421
- assertNotCancelled(key);
422
- env.BRIGHTDATA_API_TOKEN = key;
423
- }
424
- else {
425
- p.log.info(`Sign up: ${link(pc.cyan(OLOSTEP_AFFILIATE), OLOSTEP_AFFILIATE)}`);
426
- const key = await p.password({
427
- message: "Olostep API key",
428
- validate: (v) => (!v ? "Required" : undefined),
429
- });
430
- assertNotCancelled(key);
431
- env.OLOSTEP_API_KEY = key;
432
- }
521
+ if (scraper === "brightdata") {
522
+ p.log.info(`Sign up: ${link(pc.cyan(BRIGHTDATA_AFFILIATE), BRIGHTDATA_AFFILIATE)}`);
523
+ const key = await p.password({
524
+ message: "BrightData API token",
525
+ validate: (v) => !v ? "Required" : void 0
526
+ });
527
+ assertNotCancelled(key);
528
+ env.BRIGHTDATA_API_TOKEN = key;
529
+ } else if (scraper === "oxylabs") {
530
+ p.log.info(`Sign up: ${link(pc.cyan(OXYLABS_AFFILIATE), OXYLABS_AFFILIATE)}`);
531
+ const username = await p.text({
532
+ message: "Oxylabs username",
533
+ validate: (v) => !v ? "Required" : void 0
534
+ });
535
+ assertNotCancelled(username);
536
+ env.OXYLABS_USERNAME = username;
537
+ const password = await p.password({
538
+ message: "Oxylabs password",
539
+ validate: (v) => !v ? "Required" : void 0
540
+ });
541
+ assertNotCancelled(password);
542
+ env.OXYLABS_PASSWORD = password;
543
+ } else {
544
+ p.log.info(`Sign up: ${link(pc.cyan(OLOSTEP_AFFILIATE), OLOSTEP_AFFILIATE)}`);
545
+ const key = await p.password({
546
+ message: "Olostep API key",
547
+ validate: (v) => !v ? "Required" : void 0
548
+ });
549
+ assertNotCancelled(key);
550
+ env.OLOSTEP_API_KEY = key;
551
+ }
433
552
  }
434
553
  async function collectDirectApiQuick(kind, env) {
435
- if (kind === "openrouter") {
436
- const key = await p.password({
437
- message: "OpenRouter API key",
438
- validate: (v) => (!v ? "Required" : undefined),
439
- });
440
- assertNotCancelled(key);
441
- env.OPENROUTER_API_KEY = key;
442
- }
443
- else if (kind === "anthropic") {
444
- const key = await p.password({
445
- message: "Anthropic API key",
446
- validate: (v) => (!v ? "Required" : undefined),
447
- });
448
- assertNotCancelled(key);
449
- env.ANTHROPIC_API_KEY = key;
450
- }
451
- else if (kind === "openai") {
452
- const key = await p.password({
453
- message: "OpenAI API key",
454
- validate: (v) => (!v ? "Required" : undefined),
455
- });
456
- assertNotCancelled(key);
457
- env.OPENAI_API_KEY = key;
458
- }
459
- else {
460
- const key = await p.password({
461
- message: "Mistral API key",
462
- validate: (v) => (!v ? "Required" : undefined),
463
- });
464
- assertNotCancelled(key);
465
- env.MISTRAL_API_KEY = key;
466
- }
554
+ if (kind === "openrouter") {
555
+ const key = await p.password({
556
+ message: "OpenRouter API key",
557
+ validate: (v) => !v ? "Required" : void 0
558
+ });
559
+ assertNotCancelled(key);
560
+ env.OPENROUTER_API_KEY = key;
561
+ } else if (kind === "anthropic") {
562
+ const key = await p.password({
563
+ message: "Anthropic API key",
564
+ validate: (v) => !v ? "Required" : void 0
565
+ });
566
+ assertNotCancelled(key);
567
+ env.ANTHROPIC_API_KEY = key;
568
+ } else if (kind === "openai") {
569
+ const key = await p.password({
570
+ message: "OpenAI API key",
571
+ validate: (v) => !v ? "Required" : void 0
572
+ });
573
+ assertNotCancelled(key);
574
+ env.OPENAI_API_KEY = key;
575
+ } else {
576
+ const key = await p.password({
577
+ message: "Mistral API key",
578
+ validate: (v) => !v ? "Required" : void 0
579
+ });
580
+ assertNotCancelled(key);
581
+ env.MISTRAL_API_KEY = key;
582
+ }
467
583
  }
468
584
  async function collectBrightData(env, targets) {
469
- const enable = await p.confirm({
470
- message: `Configure ${pc.bold("BrightData")}? (~$0.45/mo per prompt)`,
471
- initialValue: true,
472
- });
473
- assertNotCancelled(enable);
474
- if (!enable)
475
- return;
476
- p.log.info(`Sign up and generate an API token: ${link(pc.cyan(BRIGHTDATA_AFFILIATE), BRIGHTDATA_AFFILIATE)}`);
477
- const key = await p.password({
478
- message: "BrightData API token",
479
- validate: (v) => (!v ? "Required" : undefined),
480
- });
481
- assertNotCancelled(key);
482
- env.BRIGHTDATA_API_TOKEN = key;
483
- await pickScraperTargets({
484
- providerLabel: "BrightData",
485
- providerId: "brightdata",
486
- allModels: BRIGHTDATA_MODELS,
487
- targets,
488
- });
585
+ const enable = await p.confirm({
586
+ message: `Configure ${pc.bold("BrightData")}? (~$0.45/mo per prompt)`,
587
+ initialValue: true
588
+ });
589
+ assertNotCancelled(enable);
590
+ if (!enable) return;
591
+ p.log.info(`Sign up and generate an API token: ${link(pc.cyan(BRIGHTDATA_AFFILIATE), BRIGHTDATA_AFFILIATE)}`);
592
+ const key = await p.password({
593
+ message: "BrightData API token",
594
+ validate: (v) => !v ? "Required" : void 0
595
+ });
596
+ assertNotCancelled(key);
597
+ env.BRIGHTDATA_API_TOKEN = key;
598
+ await pickScraperTargets({
599
+ providerLabel: "BrightData",
600
+ providerId: "brightdata",
601
+ allModels: BRIGHTDATA_MODELS,
602
+ targets
603
+ });
489
604
  }
490
605
  async function collectOlostep(env, targets) {
491
- const enable = await p.confirm({
492
- message: `Configure ${pc.bold("Olostep")}? (~$2.25/mo per prompt)`,
493
- initialValue: false,
494
- });
495
- assertNotCancelled(enable);
496
- if (!enable)
497
- return;
498
- p.log.info(`Grab an API key: ${link(pc.cyan(OLOSTEP_AFFILIATE), OLOSTEP_AFFILIATE)}`);
499
- const key = await p.password({
500
- message: "Olostep API key",
501
- validate: (v) => (!v ? "Required" : undefined),
502
- });
503
- assertNotCancelled(key);
504
- env.OLOSTEP_API_KEY = key;
505
- await pickScraperTargets({
506
- providerLabel: "Olostep",
507
- providerId: "olostep",
508
- allModels: OLOSTEP_MODELS,
509
- targets,
510
- });
606
+ const enable = await p.confirm({
607
+ message: `Configure ${pc.bold("Olostep")}? (~$2.25/mo per prompt)`,
608
+ initialValue: false
609
+ });
610
+ assertNotCancelled(enable);
611
+ if (!enable) return;
612
+ p.log.info(`Grab an API key: ${link(pc.cyan(OLOSTEP_AFFILIATE), OLOSTEP_AFFILIATE)}`);
613
+ const key = await p.password({
614
+ message: "Olostep API key",
615
+ validate: (v) => !v ? "Required" : void 0
616
+ });
617
+ assertNotCancelled(key);
618
+ env.OLOSTEP_API_KEY = key;
619
+ await pickScraperTargets({
620
+ providerLabel: "Olostep",
621
+ providerId: "olostep",
622
+ allModels: OLOSTEP_MODELS,
623
+ targets
624
+ });
625
+ }
626
+ async function collectOxylabs(env, targets) {
627
+ const enable = await p.confirm({
628
+ message: `Configure ${pc.bold("Oxylabs")}? (sync realtime API, pay-as-you-go)`,
629
+ initialValue: false
630
+ });
631
+ assertNotCancelled(enable);
632
+ if (!enable) return;
633
+ p.log.info(`Sign up and create Web Scraper API credentials: ${link(pc.cyan(OXYLABS_AFFILIATE), OXYLABS_AFFILIATE)}`);
634
+ const username = await p.text({
635
+ message: "Oxylabs username",
636
+ validate: (v) => !v ? "Required" : void 0
637
+ });
638
+ assertNotCancelled(username);
639
+ env.OXYLABS_USERNAME = username;
640
+ const password = await p.password({
641
+ message: "Oxylabs password",
642
+ validate: (v) => !v ? "Required" : void 0
643
+ });
644
+ assertNotCancelled(password);
645
+ env.OXYLABS_PASSWORD = password;
646
+ await pickScraperTargets({
647
+ providerLabel: "Oxylabs",
648
+ providerId: "oxylabs",
649
+ allModels: OXYLABS_MODELS,
650
+ targets
651
+ });
511
652
  }
512
653
  async function pickScraperTargets(args) {
513
- const selected = (await p.multiselect({
514
- message: `LLM Providers to track via ${args.providerLabel}`,
515
- options: args.allModels.map((model) => ({ value: model, label: model })),
516
- required: true,
517
- initialValues: [...DEFAULT_SCRAPER_MODELS],
518
- }));
519
- assertNotCancelled(selected);
520
- for (const model of selected) {
521
- args.targets.push(`${model}:${args.providerId}:online`);
522
- }
654
+ const selected = await p.multiselect({
655
+ message: `LLM Providers to track via ${args.providerLabel}`,
656
+ options: args.allModels.map((model) => ({
657
+ value: model,
658
+ label: model
659
+ })),
660
+ required: true,
661
+ initialValues: [...DEFAULT_SCRAPER_MODELS]
662
+ });
663
+ assertNotCancelled(selected);
664
+ for (const model of selected) args.targets.push(formatScrapeTarget({
665
+ model,
666
+ provider: args.providerId,
667
+ webSearch: true
668
+ }));
523
669
  }
524
670
  async function collectAnthropic(env, targets) {
525
- const enable = await p.confirm({
526
- message: `Configure ${pc.bold("Anthropic API")}? (direct Claude — ~$4–5/mo per prompt per model)`,
527
- initialValue: false,
528
- });
529
- assertNotCancelled(enable);
530
- if (!enable)
531
- return;
532
- const key = await p.password({
533
- message: "Anthropic API key",
534
- validate: (v) => (!v ? "Required" : undefined),
535
- });
536
- assertNotCancelled(key);
537
- env.ANTHROPIC_API_KEY = key;
538
- const model = await p.text({
539
- message: "Claude model",
540
- placeholder: DEFAULT_ANTHROPIC_MODEL,
541
- defaultValue: DEFAULT_ANTHROPIC_MODEL,
542
- });
543
- assertNotCancelled(model);
544
- const slug = model || DEFAULT_ANTHROPIC_MODEL;
545
- const webSearch = await p.confirm({
546
- message: "Enable web search? (recommended, but more expensive)",
547
- initialValue: true,
548
- });
549
- assertNotCancelled(webSearch);
550
- targets.push(webSearch ? `claude:anthropic-api:${slug}:online` : `claude:anthropic-api:${slug}`);
671
+ const enable = await p.confirm({
672
+ message: `Configure ${pc.bold("Anthropic API")}? (direct Claude — ~$4–5/mo per prompt per model)`,
673
+ initialValue: false
674
+ });
675
+ assertNotCancelled(enable);
676
+ if (!enable) return;
677
+ const key = await p.password({
678
+ message: "Anthropic API key",
679
+ validate: (v) => !v ? "Required" : void 0
680
+ });
681
+ assertNotCancelled(key);
682
+ env.ANTHROPIC_API_KEY = key;
683
+ const model = await p.text({
684
+ message: "Claude model",
685
+ placeholder: DEFAULT_ANTHROPIC_MODEL,
686
+ defaultValue: DEFAULT_ANTHROPIC_MODEL
687
+ });
688
+ assertNotCancelled(model);
689
+ const slug = model || DEFAULT_ANTHROPIC_MODEL;
690
+ const webSearch = await p.confirm({
691
+ message: "Enable web search? (recommended, but more expensive)",
692
+ initialValue: true
693
+ });
694
+ assertNotCancelled(webSearch);
695
+ targets.push(formatScrapeTarget({
696
+ model: "claude",
697
+ provider: "anthropic-api",
698
+ version: slug,
699
+ webSearch
700
+ }));
551
701
  }
552
702
  async function collectOpenAI(env, targets) {
553
- const enable = await p.confirm({
554
- message: `Configure ${pc.bold("OpenAI API")}? (gpt-* with web search — not the real ChatGPT UI)`,
555
- initialValue: false,
556
- });
557
- assertNotCancelled(enable);
558
- if (!enable)
559
- return;
560
- const key = await p.password({
561
- message: "OpenAI API key",
562
- validate: (v) => (!v ? "Required" : undefined),
563
- });
564
- assertNotCancelled(key);
565
- env.OPENAI_API_KEY = key;
566
- const model = await p.text({
567
- message: "OpenAI model",
568
- placeholder: DEFAULT_OPENAI_MODEL,
569
- defaultValue: DEFAULT_OPENAI_MODEL,
570
- });
571
- assertNotCancelled(model);
572
- const slug = model || DEFAULT_OPENAI_MODEL;
573
- const webSearch = await p.confirm({
574
- message: "Enable web search? (recommended, but more expensive)",
575
- initialValue: true,
576
- });
577
- assertNotCancelled(webSearch);
578
- targets.push(webSearch ? `chatgpt:openai-api:${slug}:online` : `chatgpt:openai-api:${slug}`);
703
+ const enable = await p.confirm({
704
+ message: `Configure ${pc.bold("OpenAI API")}? (gpt-* with web search — not the real ChatGPT UI)`,
705
+ initialValue: false
706
+ });
707
+ assertNotCancelled(enable);
708
+ if (!enable) return;
709
+ const key = await p.password({
710
+ message: "OpenAI API key",
711
+ validate: (v) => !v ? "Required" : void 0
712
+ });
713
+ assertNotCancelled(key);
714
+ env.OPENAI_API_KEY = key;
715
+ const model = await p.text({
716
+ message: "OpenAI model",
717
+ placeholder: DEFAULT_OPENAI_MODEL,
718
+ defaultValue: DEFAULT_OPENAI_MODEL
719
+ });
720
+ assertNotCancelled(model);
721
+ const slug = model || DEFAULT_OPENAI_MODEL;
722
+ const webSearch = await p.confirm({
723
+ message: "Enable web search? (recommended, but more expensive)",
724
+ initialValue: true
725
+ });
726
+ assertNotCancelled(webSearch);
727
+ targets.push(formatScrapeTarget({
728
+ model: "chatgpt",
729
+ provider: "openai-api",
730
+ version: slug,
731
+ webSearch
732
+ }));
579
733
  }
580
734
  async function collectMistral(env, targets) {
581
- const enable = await p.confirm({
582
- message: `Configure ${pc.bold("Mistral API")}? (direct Mistral models)`,
583
- initialValue: false,
584
- });
585
- assertNotCancelled(enable);
586
- if (!enable)
587
- return;
588
- const key = await p.password({
589
- message: "Mistral API key",
590
- validate: (v) => (!v ? "Required" : undefined),
591
- });
592
- assertNotCancelled(key);
593
- env.MISTRAL_API_KEY = key;
594
- const model = await p.text({
595
- message: "Mistral model",
596
- placeholder: DEFAULT_MISTRAL_MODEL,
597
- defaultValue: DEFAULT_MISTRAL_MODEL,
598
- });
599
- assertNotCancelled(model);
600
- const slug = model || DEFAULT_MISTRAL_MODEL;
601
- const webSearch = await p.confirm({
602
- message: "Enable web search? (recommended, but more expensive)",
603
- initialValue: true,
604
- });
605
- assertNotCancelled(webSearch);
606
- targets.push(webSearch ? `mistral:mistral-api:${slug}:online` : `mistral:mistral-api:${slug}`);
735
+ const enable = await p.confirm({
736
+ message: `Configure ${pc.bold("Mistral API")}? (direct Mistral models)`,
737
+ initialValue: false
738
+ });
739
+ assertNotCancelled(enable);
740
+ if (!enable) return;
741
+ const key = await p.password({
742
+ message: "Mistral API key",
743
+ validate: (v) => !v ? "Required" : void 0
744
+ });
745
+ assertNotCancelled(key);
746
+ env.MISTRAL_API_KEY = key;
747
+ const model = await p.text({
748
+ message: "Mistral model",
749
+ placeholder: DEFAULT_MISTRAL_MODEL,
750
+ defaultValue: DEFAULT_MISTRAL_MODEL
751
+ });
752
+ assertNotCancelled(model);
753
+ const slug = model || DEFAULT_MISTRAL_MODEL;
754
+ const webSearch = await p.confirm({
755
+ message: "Enable web search? (recommended, but more expensive)",
756
+ initialValue: true
757
+ });
758
+ assertNotCancelled(webSearch);
759
+ targets.push(formatScrapeTarget({
760
+ model: "mistral",
761
+ provider: "mistral-api",
762
+ version: slug,
763
+ webSearch
764
+ }));
607
765
  }
608
766
  async function collectOpenRouter(env, targets) {
609
- const enable = await p.confirm({
610
- message: `Configure ${pc.bold("OpenRouter")}? (one key, many hosted models)`,
611
- initialValue: false,
612
- });
613
- assertNotCancelled(enable);
614
- if (!enable)
615
- return;
616
- const key = await p.password({
617
- message: "OpenRouter API key",
618
- validate: (v) => (!v ? "Required" : undefined),
619
- });
620
- assertNotCancelled(key);
621
- env.OPENROUTER_API_KEY = key;
622
- const model = await p.text({
623
- message: "OpenRouter model slug",
624
- placeholder: DEFAULT_OPENROUTER_MODEL,
625
- defaultValue: DEFAULT_OPENROUTER_MODEL,
626
- });
627
- assertNotCancelled(model);
628
- const slug = model || DEFAULT_OPENROUTER_MODEL;
629
- const webSearch = await p.confirm({
630
- message: "Enable web search? (recommended, but more expensive)",
631
- initialValue: true,
632
- });
633
- assertNotCancelled(webSearch);
634
- targets.push(webSearch ? `claude:openrouter:${slug}:online` : `claude:openrouter:${slug}`);
767
+ const enable = await p.confirm({
768
+ message: `Configure ${pc.bold("OpenRouter")}? (one key, many hosted models)`,
769
+ initialValue: false
770
+ });
771
+ assertNotCancelled(enable);
772
+ if (!enable) return;
773
+ const key = await p.password({
774
+ message: "OpenRouter API key",
775
+ validate: (v) => !v ? "Required" : void 0
776
+ });
777
+ assertNotCancelled(key);
778
+ env.OPENROUTER_API_KEY = key;
779
+ const model = await p.text({
780
+ message: "OpenRouter model slug",
781
+ placeholder: DEFAULT_OPENROUTER_MODEL,
782
+ defaultValue: DEFAULT_OPENROUTER_MODEL
783
+ });
784
+ assertNotCancelled(model);
785
+ const slug = model || DEFAULT_OPENROUTER_MODEL;
786
+ const webSearch = await p.confirm({
787
+ message: "Enable web search? (recommended, but more expensive)",
788
+ initialValue: true
789
+ });
790
+ assertNotCancelled(webSearch);
791
+ targets.push(formatScrapeTarget({
792
+ model: "claude",
793
+ provider: "openrouter",
794
+ version: slug,
795
+ webSearch
796
+ }));
635
797
  }
636
798
  async function collectDataForSEO(env, targets) {
637
- const enable = await p.confirm({
638
- message: `Configure ${pc.bold("DataForSEO")}? (Google AI Mode scraping)`,
639
- initialValue: false,
640
- });
641
- assertNotCancelled(enable);
642
- if (!enable)
643
- return;
644
- const login = await p.text({
645
- message: "DataForSEO login",
646
- validate: (v) => (!v ? "Required" : undefined),
647
- });
648
- assertNotCancelled(login);
649
- env.DATAFORSEO_LOGIN = login;
650
- const pwd = await p.password({
651
- message: "DataForSEO password",
652
- validate: (v) => (!v ? "Required" : undefined),
653
- });
654
- assertNotCancelled(pwd);
655
- env.DATAFORSEO_PASSWORD = pwd;
656
- const addTarget = await p.confirm({
657
- message: "Also scrape Google AI Mode via DataForSEO? (google-ai-mode:dataforseo:online)",
658
- initialValue: false,
659
- });
660
- assertNotCancelled(addTarget);
661
- if (addTarget) {
662
- targets.push("google-ai-mode:dataforseo:online");
663
- }
799
+ const enable = await p.confirm({
800
+ message: `Configure ${pc.bold("DataForSEO")}? (Google AI Mode + LLM Responses)`,
801
+ initialValue: false
802
+ });
803
+ assertNotCancelled(enable);
804
+ if (!enable) return;
805
+ const login = await p.text({
806
+ message: "DataForSEO login",
807
+ validate: (v) => !v ? "Required" : void 0
808
+ });
809
+ assertNotCancelled(login);
810
+ env.DATAFORSEO_LOGIN = login;
811
+ const pwd = await p.password({
812
+ message: "DataForSEO password",
813
+ validate: (v) => !v ? "Required" : void 0
814
+ });
815
+ assertNotCancelled(pwd);
816
+ env.DATAFORSEO_PASSWORD = pwd;
817
+ const selected = await p.multiselect({
818
+ message: "LLM Providers to track via DataForSEO",
819
+ options: DATAFORSEO_MODELS.map((model) => ({
820
+ value: model,
821
+ label: model
822
+ })),
823
+ required: false,
824
+ initialValues: ["google-ai-mode"]
825
+ });
826
+ assertNotCancelled(selected);
827
+ for (const model of selected) targets.push(formatScrapeTarget({
828
+ model,
829
+ provider: "dataforseo",
830
+ webSearch: true
831
+ }));
664
832
  }
665
833
  async function finalizeScrapeTargets(env, targets, options = {}) {
666
- const deduped = dedupeTargets(targets);
667
- if (!deduped) {
668
- p.log.warn("No SCRAPE_TARGETS configured. Elmo will not run scheduled checks until you set them.");
669
- p.log.info(`Reference: ${link(pc.cyan(PROVIDERS_DOC_URL), PROVIDERS_DOC_URL)}`);
670
- const addManual = await p.confirm({
671
- message: "Enter SCRAPE_TARGETS manually now?",
672
- initialValue: false,
673
- });
674
- assertNotCancelled(addManual);
675
- if (addManual) {
676
- const manual = await p.text({
677
- message: "SCRAPE_TARGETS (model:provider[:version][:online], comma-separated)",
678
- placeholder: "chatgpt:brightdata:online,google-ai-mode:brightdata:online",
679
- validate: (v) => (!v ? "Required" : undefined),
680
- });
681
- assertNotCancelled(manual);
682
- env.SCRAPE_TARGETS = manual;
683
- }
684
- return;
685
- }
686
- if (options.skipEdit) {
687
- env.SCRAPE_TARGETS = deduped;
688
- return;
689
- }
690
- const customize = await p.confirm({
691
- message: "Edit SCRAPE_TARGETS before saving?",
692
- initialValue: false,
693
- });
694
- assertNotCancelled(customize);
695
- if (customize) {
696
- p.log.info(`Reference: ${link(pc.cyan(PROVIDERS_DOC_URL), PROVIDERS_DOC_URL)}`);
697
- const manual = await p.text({
698
- message: "SCRAPE_TARGETS",
699
- initialValue: deduped,
700
- validate: (v) => (!v ? "Required" : undefined),
701
- });
702
- assertNotCancelled(manual);
703
- env.SCRAPE_TARGETS = manual;
704
- p.log.step(`SCRAPE_TARGETS:\n ${pc.cyan(manual)}`);
705
- }
706
- else {
707
- env.SCRAPE_TARGETS = deduped;
708
- }
834
+ const deduped = dedupeTargets(targets);
835
+ if (!deduped) {
836
+ p.log.warn("No SCRAPE_TARGETS configured. Elmo will not run scheduled checks until you set them.");
837
+ p.log.info(`Reference: ${link(pc.cyan(PROVIDERS_DOC_URL), PROVIDERS_DOC_URL)}`);
838
+ const addManual = await p.confirm({
839
+ message: "Enter SCRAPE_TARGETS manually now?",
840
+ initialValue: false
841
+ });
842
+ assertNotCancelled(addManual);
843
+ if (addManual) {
844
+ const manual = await p.text({
845
+ message: "SCRAPE_TARGETS (model:provider[:version][:online], comma-separated)",
846
+ placeholder: "chatgpt:brightdata:online,google-ai-mode:brightdata:online",
847
+ validate: validateScrapeTargetsInput
848
+ });
849
+ assertNotCancelled(manual);
850
+ env.SCRAPE_TARGETS = manual;
851
+ }
852
+ return;
853
+ }
854
+ if (options.skipEdit) {
855
+ env.SCRAPE_TARGETS = deduped;
856
+ return;
857
+ }
858
+ const customize = await p.confirm({
859
+ message: "Edit SCRAPE_TARGETS before saving?",
860
+ initialValue: false
861
+ });
862
+ assertNotCancelled(customize);
863
+ if (customize) {
864
+ p.log.info(`Reference: ${link(pc.cyan(PROVIDERS_DOC_URL), PROVIDERS_DOC_URL)}`);
865
+ const manual = await p.text({
866
+ message: "SCRAPE_TARGETS",
867
+ initialValue: deduped,
868
+ validate: validateScrapeTargetsInput
869
+ });
870
+ assertNotCancelled(manual);
871
+ env.SCRAPE_TARGETS = manual;
872
+ p.log.step(`SCRAPE_TARGETS:\n ${pc.cyan(manual)}`);
873
+ } else env.SCRAPE_TARGETS = deduped;
874
+ }
875
+ function validateScrapeTargetsInput(value) {
876
+ if (!value) return "Required";
877
+ try {
878
+ parseScrapeTargets(value);
879
+ } catch (error) {
880
+ return error instanceof Error ? error.message.split("\n")[0] : String(error);
881
+ }
709
882
  }
710
883
  function dedupeTargets(targets) {
711
- const seen = new Set();
712
- const out = [];
713
- for (const t of targets) {
714
- if (seen.has(t))
715
- continue;
716
- seen.add(t);
717
- out.push(t);
718
- }
719
- return out.join(",");
720
- }
721
- // ── Start helper (used by init) ──────────────────────────────────────────────
884
+ const seen = /* @__PURE__ */ new Set();
885
+ const out = [];
886
+ for (const t of targets) {
887
+ if (seen.has(t)) continue;
888
+ seen.add(t);
889
+ out.push(t);
890
+ }
891
+ return out.join(",");
892
+ }
722
893
  async function doStart(configDir) {
723
- assertDockerRunning();
724
- log.step("Starting Docker Compose stack...");
725
- await runDockerCompose(configDir, ["up", "-d"]);
726
- const s = p.spinner();
727
- s.start("Waiting for services to become healthy...");
728
- const ok = await waitForHealthy(configDir, 180_000);
729
- if (ok) {
730
- s.stop("All services healthy!");
731
- }
732
- else {
733
- s.stop("Health check timed out.");
734
- p.log.warn("Some services did not report healthy status.");
735
- }
736
- log.info("Examples:");
737
- console.log(` ${pc.bold("elmo compose logs -f")}`);
738
- console.log(` ${pc.bold("elmo compose logs -f web")}`);
739
- console.log(` ${pc.bold("elmo compose ps")}`);
740
- console.log(` ${pc.bold("elmo compose down")}`);
741
- }
742
- // ── Command: compose ─────────────────────────────────────────────────────────
894
+ assertDockerRunning();
895
+ log.step("Starting Docker Compose stack...");
896
+ await runDockerCompose(configDir, ["up", "-d"]);
897
+ const s = p.spinner();
898
+ s.start("Waiting for services to become healthy...");
899
+ if (await waitForHealthy(configDir, 18e4)) s.stop("All services healthy!");
900
+ else {
901
+ s.stop("Health check timed out.");
902
+ p.log.warn("Some services did not report healthy status.");
903
+ }
904
+ log.info("Examples:");
905
+ console.log(` ${pc.bold("elmo compose logs -f")}`);
906
+ console.log(` ${pc.bold("elmo compose logs -f web")}`);
907
+ console.log(` ${pc.bold("elmo compose ps")}`);
908
+ console.log(` ${pc.bold("elmo compose down")}`);
909
+ }
743
910
  async function runCompose(args, options) {
744
- const configDir = await resolveConfigDir(options.dir);
745
- assertDockerRunning();
746
- await runDockerCompose(configDir, args);
911
+ const configDir = await resolveConfigDir(options.dir);
912
+ assertDockerRunning();
913
+ await runDockerCompose(configDir, args);
747
914
  }
748
- // ── Command: edit ────────────────────────────────────────────────────────────
749
915
  async function runEdit(target, options) {
750
- const configDir = await resolveConfigDir(options.dir);
751
- let filePath;
752
- if (target === "env") {
753
- filePath = path.join(configDir, ".env");
754
- }
755
- else if (target === "compose") {
756
- filePath = path.join(configDir, "elmo.yaml");
757
- }
758
- else {
759
- throw new Error(`Unknown edit target: ${target}. Use \`env\` or \`compose\`.`);
760
- }
761
- if (!(await fileExists(filePath))) {
762
- throw new Error(`File not found: ${filePath}`);
763
- }
764
- const editorEnv = process.env.VISUAL || process.env.EDITOR || "nano";
765
- const parts = editorEnv.split(/\s+/).filter(Boolean);
766
- const cmd = parts[0] ?? "nano";
767
- const args = [...parts.slice(1), filePath];
768
- await new Promise((resolve, reject) => {
769
- const child = spawn(cmd, args, { stdio: "inherit" });
770
- child.on("close", (code) => {
771
- if (code === 0)
772
- resolve();
773
- else
774
- reject(new Error(`${cmd} exited with code ${code}`));
775
- });
776
- child.on("error", (err) => reject(err));
777
- });
778
- log.info("Restart the stack with `elmo compose up -d` to apply changes.");
779
- }
780
- // ── Compose YAML Builder ─────────────────────────────────────────────────────
916
+ const configDir = await resolveConfigDir(options.dir);
917
+ let filePath;
918
+ if (target === "env") filePath = path.join(configDir, ".env");
919
+ else if (target === "compose") filePath = path.join(configDir, "elmo.yaml");
920
+ else throw new Error(`Unknown edit target: ${target}. Use \`env\` or \`compose\`.`);
921
+ if (!await fileExists(filePath)) throw new Error(`File not found: ${filePath}`);
922
+ const parts = (process.env.VISUAL || process.env.EDITOR || "nano").split(/\s+/).filter(Boolean);
923
+ const cmd = parts[0] ?? "nano";
924
+ const args = [...parts.slice(1), filePath];
925
+ await new Promise((resolve, reject) => {
926
+ const child = spawn(cmd, args, { stdio: "inherit" });
927
+ child.on("close", (code) => {
928
+ if (code === 0) resolve();
929
+ else reject(/* @__PURE__ */ new Error(`${cmd} exited with code ${code}`));
930
+ });
931
+ child.on("error", (err) => reject(err));
932
+ });
933
+ log.info("Restart the stack with `elmo compose up -d` to apply changes.");
934
+ }
935
+ async function runUpgrade(options, cliVersion) {
936
+ printBanner();
937
+ p.intro(pc.bold("Upgrading Elmo"));
938
+ const latestCli = await fetchLatestCliVersion();
939
+ if (latestCli && semver.valid(cliVersion) && semver.lt(cliVersion, latestCli)) {
940
+ log.warn(`Your CLI (${cliVersion}) is behind the latest published version (${latestCli}).`);
941
+ log.info("Recommended: upgrade the CLI first, then rerun this command:");
942
+ console.log(` ${pc.bold("npm install -g @elmohq/cli@latest")}`);
943
+ const proceed = options.yes ? true : await p.confirm({
944
+ message: `Continue upgrading the stack with CLI ${cliVersion} anyway?`,
945
+ initialValue: false
946
+ });
947
+ assertNotCancelled(proceed);
948
+ if (!proceed) {
949
+ p.cancel("Upgrade cancelled. Upgrade the CLI and rerun `elmo upgrade`.");
950
+ process.exit(0);
951
+ }
952
+ }
953
+ const configDir = await resolveConfigDir(options.dir);
954
+ const composePath = path.join(configDir, "elmo.yaml");
955
+ const detectedVersion = await readRenderedVersion(composePath);
956
+ const fromVersion = detectedVersion ?? cliVersion;
957
+ if (!semver.valid(fromVersion)) throw new Error(`Could not determine the installed version from ${composePath}.`);
958
+ if (semver.gt(fromVersion, cliVersion)) {
959
+ log.warn(`Your deployment (${fromVersion}) is newer than this CLI (${cliVersion}).`);
960
+ log.info("Upgrade the CLI to match, then rerun:");
961
+ console.log(` ${pc.bold("npm install -g @elmohq/cli@latest")}`);
962
+ process.exit(1);
963
+ }
964
+ if (detectedVersion !== null && semver.eq(detectedVersion, cliVersion)) {
965
+ log.success(`Already at ${cliVersion}.`);
966
+ const pull = options.yes ? true : await p.confirm({
967
+ message: "Pull images for this version anyway?",
968
+ initialValue: false
969
+ });
970
+ assertNotCancelled(pull);
971
+ if (pull) {
972
+ assertDockerRunning();
973
+ const wasRunning = await stackHasRunningServices(configDir);
974
+ log.step("Pulling images...");
975
+ await runDockerCompose(configDir, ["pull"]);
976
+ if (wasRunning) {
977
+ log.step("Restarting services...");
978
+ await runDockerCompose(configDir, ["up", "-d"]);
979
+ }
980
+ }
981
+ p.outro(pc.green("Nothing to upgrade."));
982
+ return;
983
+ }
984
+ const plan = detectedVersion === null ? [] : planMigrations(fromVersion, cliVersion, MIGRATIONS);
985
+ if (detectedVersion === null) log.warn(`Couldn't detect the deployment's version — re-pinning images to ${pc.cyan(cliVersion)}.`);
986
+ else log.info(`Upgrading from ${pc.cyan(fromVersion)} → ${pc.cyan(cliVersion)}`);
987
+ if (plan.length === 0) log.step("No migrations to run (docker images will be re-pinned and pulled).");
988
+ else {
989
+ log.step(`Migrations to apply: ${plan.length}`);
990
+ for (const m of plan) console.log(` • ${pc.bold(`${m.from} → ${m.to}`)} ${m.description}`);
991
+ }
992
+ const confirm = options.yes ? true : await p.confirm({
993
+ message: "Proceed with upgrade?",
994
+ initialValue: true
995
+ });
996
+ assertNotCancelled(confirm);
997
+ if (!confirm) {
998
+ p.cancel("Upgrade cancelled.");
999
+ process.exit(0);
1000
+ }
1001
+ assertDockerRunning();
1002
+ const wasRunning = await stackHasRunningServices(configDir);
1003
+ if (wasRunning) {
1004
+ log.step("Stopping services...");
1005
+ await runDockerCompose(configDir, ["down"]);
1006
+ }
1007
+ const ctx = buildMigrationContext(configDir, cliVersion);
1008
+ try {
1009
+ await runMigrations(plan, ctx);
1010
+ } catch (error) {
1011
+ const msg = error instanceof Error ? error.message : String(error);
1012
+ log.error(`Migration failed: ${msg}`);
1013
+ log.info("Your config version was left unchanged. Fix the issue and rerun `elmo upgrade`.");
1014
+ if (wasRunning) log.info("The stack was stopped for the upgrade. Restart with `elmo compose up -d` after fixing.");
1015
+ process.exit(1);
1016
+ }
1017
+ const isDev = await composeUsesBuild(composePath);
1018
+ await repinComposeImages(composePath, cliVersion);
1019
+ await refreshRenderedVersion(path.join(configDir, ".env"), cliVersion);
1020
+ log.success(`Pinned config to ${cliVersion}.`);
1021
+ if (isDev) log.info("Dev mode detected — rebuild with `elmo compose build` to apply the new version.");
1022
+ else {
1023
+ log.step("Pulling images...");
1024
+ await runDockerCompose(configDir, ["pull"]);
1025
+ }
1026
+ if (wasRunning) {
1027
+ log.step("Starting services...");
1028
+ await runDockerCompose(configDir, ["up", "-d"]);
1029
+ const s = p.spinner();
1030
+ s.start("Waiting for services to become healthy...");
1031
+ if (await waitForHealthy(configDir, 18e4)) s.stop("All services healthy!");
1032
+ else {
1033
+ s.stop("Health check timed out.");
1034
+ log.warn("Some services did not report healthy status.");
1035
+ }
1036
+ } else log.info("Stack was stopped before upgrade — leaving it stopped. Start with `elmo compose up -d`.");
1037
+ await trackCliEvent(configDir, "cli_upgrade", {
1038
+ from_version: fromVersion,
1039
+ to_version: cliVersion,
1040
+ migrations_run: plan.length,
1041
+ was_running: wasRunning,
1042
+ dev_mode: isDev
1043
+ });
1044
+ p.outro(pc.green(`Upgraded to ${cliVersion}.`));
1045
+ }
1046
+ async function stackHasRunningServices(configDir) {
1047
+ try {
1048
+ return (await getComposeServices(configDir)).some((s) => s.State?.startsWith("running") ?? false);
1049
+ } catch {
1050
+ return false;
1051
+ }
1052
+ }
1053
+ async function readRenderedVersion(filePath) {
1054
+ try {
1055
+ return parseRenderedVersion(await fs.readFile(filePath, "utf8"));
1056
+ } catch {
1057
+ return null;
1058
+ }
1059
+ }
1060
+ async function composeUsesBuild(composePath) {
1061
+ try {
1062
+ const contents = await fs.readFile(composePath, "utf8");
1063
+ return /^\s*build:/m.test(contents);
1064
+ } catch {
1065
+ return false;
1066
+ }
1067
+ }
1068
+ async function repinComposeImages(composePath, version) {
1069
+ const contents = await fs.readFile(composePath, "utf8");
1070
+ await fs.writeFile(composePath, refreshHeaderVersion(repinImages(contents, version), version), "utf8");
1071
+ }
1072
+ async function refreshRenderedVersion(filePath, version) {
1073
+ try {
1074
+ const contents = await fs.readFile(filePath, "utf8");
1075
+ await fs.writeFile(filePath, refreshHeaderVersion(contents, version), "utf8");
1076
+ } catch {}
1077
+ }
1078
+ function buildMigrationContext(configDir, version) {
1079
+ const envPath = path.join(configDir, ".env");
1080
+ return {
1081
+ configDir,
1082
+ log: {
1083
+ info: (msg) => log.info(msg),
1084
+ warn: (msg) => log.warn(msg),
1085
+ step: (msg) => log.step(msg)
1086
+ },
1087
+ readEnv: async () => {
1088
+ try {
1089
+ return parse(await fs.readFile(envPath, "utf8"));
1090
+ } catch {
1091
+ return {};
1092
+ }
1093
+ },
1094
+ writeEnv: async (env) => {
1095
+ await fs.writeFile(envPath, buildEnvFile(env, version), "utf8");
1096
+ }
1097
+ };
1098
+ }
781
1099
  function buildComposeYaml(options) {
782
- const services = [];
783
- const volumes = new Set();
784
- const dependsOnWeb = [];
785
- const dependsOnWorker = [];
786
- const dependencyConditions = {
787
- postgres: "service_healthy",
788
- "db-migrate": "service_completed_successfully",
789
- };
790
- const dockerfilePath = options.dockerDir
791
- ? path.relative(options.repoRoot, path.join(options.dockerDir, "Dockerfile"))
792
- : "docker/Dockerfile";
793
- if (options.postgresMode === "docker") {
794
- services.push(buildPostgresService());
795
- services.push(buildDbMigrateService({
796
- dev: options.dev,
797
- dockerfilePath,
798
- repoRoot: options.repoRoot,
799
- version: options.version,
800
- }));
801
- dependsOnWeb.push("db-migrate");
802
- dependsOnWorker.push("db-migrate");
803
- volumes.add("postgres_data");
804
- }
805
- services.push(buildWebService({
806
- dev: options.dev,
807
- dependsOn: dependsOnWeb,
808
- dependencyConditions,
809
- repoRoot: options.repoRoot,
810
- dockerfilePath,
811
- port: options.port,
812
- version: options.version,
813
- }));
814
- services.push(buildWorkerService({
815
- dev: options.dev,
816
- dependsOn: dependsOnWorker,
817
- dependencyConditions,
818
- repoRoot: options.repoRoot,
819
- dockerfilePath,
820
- version: options.version,
821
- }));
822
- const lines = [renderedByHeader(options.version), "", "name: elmo", "", "services:"];
823
- lines.push(...services.map((service) => indentBlock(service, 2)));
824
- if (volumes.size > 0) {
825
- lines.push("", "volumes:");
826
- for (const volume of volumes) {
827
- lines.push(` ${volume}:`);
828
- }
829
- }
830
- return `${lines.join("\n")}\n`;
1100
+ const services = [];
1101
+ const volumes = /* @__PURE__ */ new Set();
1102
+ const dependsOnWeb = [];
1103
+ const dependsOnWorker = [];
1104
+ const dependencyConditions = {
1105
+ postgres: "service_healthy",
1106
+ "db-migrate": "service_completed_successfully"
1107
+ };
1108
+ const dockerfilePath = options.dockerDir ? path.relative(options.repoRoot, path.join(options.dockerDir, "Dockerfile")) : "docker/Dockerfile";
1109
+ if (options.postgresMode === "docker") {
1110
+ services.push(buildPostgresService());
1111
+ services.push(buildDbMigrateService({
1112
+ dev: options.dev,
1113
+ dockerfilePath,
1114
+ repoRoot: options.repoRoot,
1115
+ version: options.version
1116
+ }));
1117
+ dependsOnWeb.push("db-migrate");
1118
+ dependsOnWorker.push("db-migrate");
1119
+ volumes.add("postgres_data");
1120
+ }
1121
+ services.push(buildWebService({
1122
+ dev: options.dev,
1123
+ dependsOn: dependsOnWeb,
1124
+ dependencyConditions,
1125
+ repoRoot: options.repoRoot,
1126
+ dockerfilePath,
1127
+ port: options.port,
1128
+ version: options.version
1129
+ }));
1130
+ services.push(buildWorkerService({
1131
+ dev: options.dev,
1132
+ dependsOn: dependsOnWorker,
1133
+ dependencyConditions,
1134
+ repoRoot: options.repoRoot,
1135
+ dockerfilePath,
1136
+ version: options.version
1137
+ }));
1138
+ const lines = [
1139
+ renderedByHeader(options.version),
1140
+ "",
1141
+ "name: elmo",
1142
+ "",
1143
+ "services:"
1144
+ ];
1145
+ lines.push(...services.map((service) => indentBlock(service, 2)));
1146
+ if (volumes.size > 0) {
1147
+ lines.push("", "volumes:");
1148
+ for (const volume of volumes) lines.push(` ${volume}:`);
1149
+ }
1150
+ return `${lines.join("\n")}\n`;
831
1151
  }
832
1152
  function buildPostgresService() {
833
- return [
834
- "postgres:",
835
- " image: postgres:16-alpine",
836
- " environment:",
837
- " POSTGRES_USER: postgres",
838
- " POSTGRES_PASSWORD: postgres",
839
- " POSTGRES_DB: elmo",
840
- " volumes:",
841
- " - postgres_data:/var/lib/postgresql/data",
842
- " ports:",
843
- ' - "5432:5432"',
844
- " healthcheck:",
845
- ' test: ["CMD-SHELL", "pg_isready -U postgres"]',
846
- " interval: 5s",
847
- " timeout: 5s",
848
- " retries: 5",
849
- " start_period: 30s",
850
- ].join("\n");
1153
+ return [
1154
+ "postgres:",
1155
+ " image: postgres:16-alpine",
1156
+ " environment:",
1157
+ " POSTGRES_USER: postgres",
1158
+ " POSTGRES_PASSWORD: postgres",
1159
+ " POSTGRES_DB: elmo",
1160
+ " volumes:",
1161
+ " - postgres_data:/var/lib/postgresql/data",
1162
+ " ports:",
1163
+ " - \"5432:5432\"",
1164
+ " healthcheck:",
1165
+ " test: [\"CMD-SHELL\", \"pg_isready -U postgres\"]",
1166
+ " interval: 5s",
1167
+ " timeout: 5s",
1168
+ " retries: 5",
1169
+ " start_period: 30s"
1170
+ ].join("\n");
851
1171
  }
852
1172
  function buildDbMigrateService(options) {
853
- const lines = ["db-migrate:"];
854
- if (options.dev) {
855
- lines.push(" build:", ` context: ${options.repoRoot}`, ` dockerfile: ${options.dockerfilePath}`, " target: migrate");
856
- }
857
- else {
858
- lines.push(` image: elmohq/elmo-db-migrate:${options.version}`);
859
- }
860
- lines.push(" environment:", " - DATABASE_URL=postgres://postgres:postgres@postgres:5432/elmo", " depends_on:", " postgres:", " condition: service_healthy");
861
- return lines.join("\n");
1173
+ const lines = ["db-migrate:"];
1174
+ if (options.dev) lines.push(" build:", ` context: ${options.repoRoot}`, ` dockerfile: ${options.dockerfilePath}`, " target: migrate");
1175
+ else lines.push(` image: elmohq/elmo-db-migrate:${options.version}`);
1176
+ lines.push(" environment:", " - DATABASE_URL=postgres://postgres:postgres@postgres:5432/elmo", " depends_on:", " postgres:", " condition: service_healthy");
1177
+ return lines.join("\n");
862
1178
  }
863
1179
  function buildWebService(options) {
864
- const lines = ["web:"];
865
- if (options.dev) {
866
- lines.push(" build:", ` context: ${options.repoRoot}`, ` dockerfile: ${options.dockerfilePath}`, " target: web", " args:", " DEPLOYMENT_MODE: local");
867
- }
868
- else {
869
- lines.push(` image: elmohq/elmo-web:${options.version}`);
870
- }
871
- lines.push(" env_file:", " - path: .env", " required: true", " ports:", ` - "${options.port}:3000"`);
872
- if (options.dependsOn.length > 0) {
873
- lines.push(" depends_on:");
874
- for (const service of options.dependsOn) {
875
- const condition = options.dependencyConditions[service] ?? "service_started";
876
- lines.push(` ${service}:`, ` condition: ${condition}`);
877
- }
878
- }
879
- return lines.join("\n");
1180
+ const lines = ["web:"];
1181
+ if (options.dev) lines.push(" build:", ` context: ${options.repoRoot}`, ` dockerfile: ${options.dockerfilePath}`, " target: web", " args:", " DEPLOYMENT_MODE: local");
1182
+ else lines.push(` image: elmohq/elmo-web:${options.version}`);
1183
+ lines.push(" env_file:", " - path: .env", " required: true", " ports:", ` - "${options.port}:3000"`);
1184
+ if (options.dependsOn.length > 0) {
1185
+ lines.push(" depends_on:");
1186
+ for (const service of options.dependsOn) {
1187
+ const condition = options.dependencyConditions[service] ?? "service_started";
1188
+ lines.push(` ${service}:`, ` condition: ${condition}`);
1189
+ }
1190
+ }
1191
+ return lines.join("\n");
880
1192
  }
881
1193
  function buildWorkerService(options) {
882
- const lines = ["worker:"];
883
- if (options.dev) {
884
- lines.push(" build:", ` context: ${options.repoRoot}`, ` dockerfile: ${options.dockerfilePath}`, " target: worker", " args:", " DEPLOYMENT_MODE: local");
885
- }
886
- else {
887
- lines.push(` image: elmohq/elmo-worker:${options.version}`);
888
- }
889
- lines.push(" env_file:", " - path: .env", " required: true");
890
- if (options.dependsOn.length > 0) {
891
- lines.push(" depends_on:");
892
- for (const service of options.dependsOn) {
893
- const condition = options.dependencyConditions[service] ?? "service_started";
894
- lines.push(` ${service}:`, ` condition: ${condition}`);
895
- }
896
- }
897
- return lines.join("\n");
1194
+ const lines = ["worker:"];
1195
+ if (options.dev) lines.push(" build:", ` context: ${options.repoRoot}`, ` dockerfile: ${options.dockerfilePath}`, " target: worker", " args:", " DEPLOYMENT_MODE: local");
1196
+ else lines.push(` image: elmohq/elmo-worker:${options.version}`);
1197
+ lines.push(" env_file:", " - path: .env", " required: true");
1198
+ if (options.dependsOn.length > 0) {
1199
+ lines.push(" depends_on:");
1200
+ for (const service of options.dependsOn) {
1201
+ const condition = options.dependencyConditions[service] ?? "service_started";
1202
+ lines.push(` ${service}:`, ` condition: ${condition}`);
1203
+ }
1204
+ }
1205
+ return lines.join("\n");
898
1206
  }
899
1207
  function indentBlock(block, spaces) {
900
- const indent = " ".repeat(spaces);
901
- return block
902
- .split("\n")
903
- .map((line) => `${indent}${line}`)
904
- .join("\n");
1208
+ const indent = " ".repeat(spaces);
1209
+ return block.split("\n").map((line) => `${indent}${line}`).join("\n");
905
1210
  }
906
- // ── Docker Helpers ───────────────────────────────────────────────────────────
907
1211
  async function getComposeServices(configDir) {
908
- const output = await runDockerComposeCapture(configDir, ["ps", "--format", "json"]);
909
- if (!output.trim()) {
910
- return [];
911
- }
912
- try {
913
- const trimmed = output.trim();
914
- const parsed = JSON.parse(trimmed);
915
- if (Array.isArray(parsed)) {
916
- return parsed;
917
- }
918
- if (typeof parsed === "object" && parsed !== null) {
919
- return [parsed];
920
- }
921
- return [];
922
- }
923
- catch {
924
- try {
925
- return output
926
- .trim()
927
- .split("\n")
928
- .filter((line) => line.trim())
929
- .map((line) => JSON.parse(line));
930
- }
931
- catch {
932
- log.warn("Unable to parse docker compose status.");
933
- return [];
934
- }
935
- }
1212
+ const output = await runDockerComposeCapture(configDir, [
1213
+ "ps",
1214
+ "--format",
1215
+ "json"
1216
+ ]);
1217
+ if (!output.trim()) return [];
1218
+ try {
1219
+ const trimmed = output.trim();
1220
+ const parsed = JSON.parse(trimmed);
1221
+ if (Array.isArray(parsed)) return parsed;
1222
+ if (typeof parsed === "object" && parsed !== null) return [parsed];
1223
+ return [];
1224
+ } catch {
1225
+ try {
1226
+ return output.trim().split("\n").filter((line) => line.trim()).map((line) => JSON.parse(line));
1227
+ } catch {
1228
+ log.warn("Unable to parse docker compose status.");
1229
+ return [];
1230
+ }
1231
+ }
936
1232
  }
937
1233
  function isServiceReady(service) {
938
- if (service.Health) {
939
- return service.Health === "healthy";
940
- }
941
- if (service.State?.startsWith("running")) {
942
- return true;
943
- }
944
- return false;
1234
+ if (service.Health) return service.Health === "healthy";
1235
+ if (service.State?.startsWith("running")) return true;
1236
+ return false;
945
1237
  }
946
1238
  async function waitForHealthy(configDir, timeoutMs) {
947
- const start = Date.now();
948
- while (Date.now() - start < timeoutMs) {
949
- const services = await getComposeServices(configDir);
950
- if (services.length > 0 && services.every(isServiceReady)) {
951
- return true;
952
- }
953
- await sleep(3000);
954
- }
955
- return false;
1239
+ const start = Date.now();
1240
+ while (Date.now() - start < timeoutMs) {
1241
+ const services = await getComposeServices(configDir);
1242
+ if (services.length > 0 && services.every(isServiceReady)) return true;
1243
+ await sleep(3e3);
1244
+ }
1245
+ return false;
956
1246
  }
957
1247
  function sleep(ms) {
958
- return new Promise((resolve) => setTimeout(resolve, ms));
1248
+ return new Promise((resolve) => setTimeout(resolve, ms));
959
1249
  }
960
1250
  function runDockerCompose(configDir, args) {
961
- return new Promise((resolve, reject) => {
962
- const composeFile = path.join(configDir, "elmo.yaml");
963
- const commandArgs = ["compose", "-f", composeFile, ...args];
964
- const child = spawn("docker", commandArgs, {
965
- stdio: "inherit",
966
- });
967
- child.on("close", (code) => {
968
- if (code === 0) {
969
- resolve();
970
- }
971
- else {
972
- reject(new Error(`docker compose exited with code ${code}`));
973
- }
974
- });
975
- });
1251
+ return new Promise((resolve, reject) => {
1252
+ spawn("docker", [
1253
+ "compose",
1254
+ "-f",
1255
+ path.join(configDir, "elmo.yaml"),
1256
+ ...args
1257
+ ], { stdio: "inherit" }).on("close", (code) => {
1258
+ if (code === 0) resolve();
1259
+ else reject(/* @__PURE__ */ new Error(`docker compose exited with code ${code}`));
1260
+ });
1261
+ });
976
1262
  }
977
1263
  function runDockerComposeCapture(configDir, args) {
978
- return new Promise((resolve, reject) => {
979
- const composeFile = path.join(configDir, "elmo.yaml");
980
- const commandArgs = ["compose", "-f", composeFile, ...args];
981
- const child = spawn("docker", commandArgs);
982
- let stdout = "";
983
- let stderr = "";
984
- child.stdout.on("data", (data) => {
985
- stdout += data.toString();
986
- });
987
- child.stderr.on("data", (data) => {
988
- stderr += data.toString();
989
- });
990
- child.on("close", (code) => {
991
- if (code === 0) {
992
- resolve(stdout);
993
- }
994
- else {
995
- reject(new Error(stderr || `docker compose exited with code ${code}`));
996
- }
997
- });
998
- });
1264
+ return new Promise((resolve, reject) => {
1265
+ const child = spawn("docker", [
1266
+ "compose",
1267
+ "-f",
1268
+ path.join(configDir, "elmo.yaml"),
1269
+ ...args
1270
+ ]);
1271
+ let stdout = "";
1272
+ let stderr = "";
1273
+ child.stdout.on("data", (data) => {
1274
+ stdout += data.toString();
1275
+ });
1276
+ child.stderr.on("data", (data) => {
1277
+ stderr += data.toString();
1278
+ });
1279
+ child.on("close", (code) => {
1280
+ if (code === 0) resolve(stdout);
1281
+ else reject(new Error(stderr || `docker compose exited with code ${code}`));
1282
+ });
1283
+ });
999
1284
  }
1000
1285
  function assertDockerRunning() {
1001
- const result = spawnSync("docker", ["info"], {
1002
- stdio: "ignore",
1003
- });
1004
- if (result.status !== 0) {
1005
- throw new Error("Docker does not appear to be running. Start Docker and try again.");
1006
- }
1007
- }
1008
- // ── Docker Dir Resolution ────────────────────────────────────────────────────
1286
+ if (spawnSync("docker", ["info"], { stdio: "ignore" }).status !== 0) throw new Error("Docker does not appear to be running. Start Docker and try again.");
1287
+ }
1009
1288
  async function resolveDockerDirInteractive(cwd) {
1010
- const inCwd = await fileExists(path.join(cwd, "Dockerfile"));
1011
- const inDockerDir = await fileExists(path.join(cwd, "docker", "Dockerfile"));
1012
- const defaultDir = inCwd ? "." : inDockerDir ? "docker" : ".";
1013
- const dir = await p.text({
1014
- message: "Path to docker directory (contains Dockerfile)",
1015
- defaultValue: defaultDir,
1016
- });
1017
- assertNotCancelled(dir);
1018
- const resolved = path.resolve(cwd, dir);
1019
- if (!(await fileExists(path.join(resolved, "Dockerfile")))) {
1020
- p.log.error(`Dockerfile not found in ${resolved}. Provide the directory that contains Dockerfile.`);
1021
- process.exit(1);
1022
- }
1023
- return resolved;
1024
- }
1025
- async function resolveDockerDirAuto(cwd, explicitDir) {
1026
- if (explicitDir) {
1027
- const resolved = path.resolve(cwd, explicitDir);
1028
- if (!(await fileExists(path.join(resolved, "Dockerfile")))) {
1029
- throw new Error(`Dockerfile not found in ${resolved}`);
1030
- }
1031
- return resolved;
1032
- }
1033
- // Auto-detect
1034
- if (await fileExists(path.join(cwd, "docker", "Dockerfile"))) {
1035
- return path.resolve(cwd, "docker");
1036
- }
1037
- if (await fileExists(path.join(cwd, "Dockerfile"))) {
1038
- return cwd;
1039
- }
1040
- throw new Error("Could not find Dockerfile. Specify --docker-dir or set ELMO_DOCKER_DIR.");
1041
- }
1042
- // ── Config Dir Resolution ────────────────────────────────────────────────────
1289
+ const inCwd = await fileExists(path.join(cwd, "Dockerfile"));
1290
+ const inDockerDir = await fileExists(path.join(cwd, "docker", "Dockerfile"));
1291
+ const defaultDir = inCwd ? "." : inDockerDir ? "docker" : ".";
1292
+ const dir = await p.text({
1293
+ message: "Path to docker directory (contains Dockerfile)",
1294
+ defaultValue: defaultDir
1295
+ });
1296
+ assertNotCancelled(dir);
1297
+ const resolved = path.resolve(cwd, dir);
1298
+ if (!await fileExists(path.join(resolved, "Dockerfile"))) {
1299
+ p.log.error(`Dockerfile not found in ${resolved}. Provide the directory that contains Dockerfile.`);
1300
+ process.exit(1);
1301
+ }
1302
+ return resolved;
1303
+ }
1043
1304
  async function resolveConfigDir(explicitDir) {
1044
- const resolved = explicitDir ? path.resolve(process.cwd(), explicitDir) : CONFIG_HOME;
1045
- const composePath = path.join(resolved, "elmo.yaml");
1046
- if (!(await fileExists(composePath))) {
1047
- if (explicitDir) {
1048
- throw new Error(`Config directory does not contain elmo.yaml: ${resolved}\nRun \`elmo init --dir ${explicitDir}\` to create it.`);
1049
- }
1050
- throw new Error(`No config found at ${resolved}. Run \`elmo init\` to create one, or specify --dir.`);
1051
- }
1052
- return resolved;
1053
- }
1054
- // ── File & Config Helpers ────────────────────────────────────────────────────
1055
- async function writeConfigFiles(configDir, initConfig) {
1056
- const envPath = path.join(configDir, ".env");
1057
- const composePath = path.join(configDir, "elmo.yaml");
1058
- await ensureDir(configDir);
1059
- await fs.writeFile(envPath, buildEnvFile(initConfig.env, initConfig.version), "utf8");
1060
- await fs.writeFile(composePath, initConfig.composeYaml, "utf8");
1305
+ const resolved = explicitDir ? path.resolve(process.cwd(), explicitDir) : CONFIG_HOME;
1306
+ if (!await fileExists(path.join(resolved, "elmo.yaml"))) {
1307
+ if (explicitDir) throw new Error(`Config directory does not contain elmo.yaml: ${resolved}\nRun \`elmo init --dir ${explicitDir}\` to create it.`);
1308
+ throw new Error(`No config found at ${resolved}. Run \`elmo init\` to create one, or specify --dir.`);
1309
+ }
1310
+ return resolved;
1061
1311
  }
1062
- function renderedByHeader(version) {
1063
- return [
1064
- `# Rendered by elmo ${version} on ${new Date().toISOString()}`,
1065
- "# Re-run `elmo init` after upgrading the CLI to refresh this file.",
1066
- ].join("\n");
1312
+ async function writeConfigFiles(configDir, initConfig) {
1313
+ const envPath = path.join(configDir, ".env");
1314
+ const composePath = path.join(configDir, "elmo.yaml");
1315
+ await ensureDir(configDir);
1316
+ await fs.writeFile(envPath, buildEnvFile(initConfig.env, initConfig.version), "utf8");
1317
+ await fs.writeFile(composePath, initConfig.composeYaml, "utf8");
1067
1318
  }
1068
1319
  function buildEnvFile(env, version) {
1069
- const lines = [renderedByHeader(version), "# WARNING: contains secrets. Do not commit.", ""];
1070
- for (const [key, rawValue] of Object.entries(env)) {
1071
- if (rawValue === undefined) {
1072
- continue;
1073
- }
1074
- lines.push(`${key}=${formatEnvValue(rawValue)}`);
1075
- }
1076
- return `${lines.join("\n")}\n`;
1320
+ const lines = [
1321
+ renderedByHeader(version),
1322
+ "# WARNING: contains secrets. Do not commit.",
1323
+ ""
1324
+ ];
1325
+ for (const [key, rawValue] of Object.entries(env)) {
1326
+ if (rawValue === void 0) continue;
1327
+ lines.push(`${key}=${formatEnvValue(rawValue)}`);
1328
+ }
1329
+ return `${lines.join("\n")}\n`;
1077
1330
  }
1078
1331
  function formatEnvValue(value) {
1079
- if (value === "") {
1080
- return '""';
1081
- }
1082
- if (/[\s#"']/u.test(value)) {
1083
- const escaped = value.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
1084
- return `"${escaped}"`;
1085
- }
1086
- return value;
1332
+ if (value === "") return "\"\"";
1333
+ if (/[\s#"']/u.test(value)) return `"${value.replace(/\\/g, "\\\\").replace(/"/g, "\\\"")}"`;
1334
+ return value;
1087
1335
  }
1088
1336
  async function fileExists(target) {
1089
- try {
1090
- await fs.access(target);
1091
- return true;
1092
- }
1093
- catch {
1094
- return false;
1095
- }
1337
+ try {
1338
+ await fs.access(target);
1339
+ return true;
1340
+ } catch {
1341
+ return false;
1342
+ }
1096
1343
  }
1097
1344
  async function ensureDir(dir) {
1098
- await fs.mkdir(dir, { recursive: true });
1345
+ await fs.mkdir(dir, { recursive: true });
1099
1346
  }
1100
- // ── Version Helpers ──────────────────────────────────────────────────────────
1101
1347
  async function getPackageVersion() {
1102
- const selfDir = path.dirname(fileURLToPath(import.meta.url));
1103
- const packagePath = path.resolve(selfDir, "..", "package.json");
1104
- const contents = await fs.readFile(packagePath, "utf8");
1105
- const json = JSON.parse(contents);
1106
- return json.version;
1348
+ const selfDir = path.dirname(fileURLToPath(import.meta.url));
1349
+ const packagePath = path.resolve(selfDir, "..", "package.json");
1350
+ const contents = await fs.readFile(packagePath, "utf8");
1351
+ return JSON.parse(contents).version;
1352
+ }
1353
+ async function fetchLatestCliVersion() {
1354
+ try {
1355
+ const response = await fetch("https://registry.npmjs.org/@elmohq/cli/latest");
1356
+ if (!response.ok) return null;
1357
+ return (await response.json()).version ?? null;
1358
+ } catch {
1359
+ return null;
1360
+ }
1107
1361
  }
1108
1362
  async function maybeNotifyNewVersion(currentVersion) {
1109
- try {
1110
- const response = await fetch("https://registry.npmjs.org/@elmohq/cli/latest");
1111
- if (!response.ok) {
1112
- return;
1113
- }
1114
- const data = (await response.json());
1115
- if (!data.version) {
1116
- return;
1117
- }
1118
- if (semver.valid(currentVersion) && semver.lt(currentVersion, data.version)) {
1119
- log.warn(`New CLI version available (${data.version}). Run: npm install -g @elmohq/cli@latest`);
1120
- }
1121
- }
1122
- catch {
1123
- // Ignore update errors
1124
- }
1125
- }
1126
- // ── Entry Point ──────────────────────────────────────────────────────────────
1363
+ const latest = await fetchLatestCliVersion();
1364
+ if (!latest) return;
1365
+ if (semver.valid(currentVersion) && semver.lt(currentVersion, latest)) log.warn(`New CLI version available (${latest}). Run: npm install -g @elmohq/cli@latest`);
1366
+ }
1127
1367
  main().catch((error) => {
1128
- const msg = error instanceof Error ? error.message : String(error);
1129
- console.error(`\n${pc.red("Error:")} ${msg}`);
1130
- process.exit(1);
1368
+ const msg = error instanceof Error ? error.message : String(error);
1369
+ console.error(`\n${pc.red("Error:")} ${msg}`);
1370
+ process.exit(1);
1131
1371
  });
1372
+ //#endregion
1373
+ export {};