create-insight 0.25.4 → 0.26.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/create-insight.mjs +62 -19
  2. package/package.json +1 -1
@@ -6,7 +6,7 @@ import { createHash, randomBytes as randomBytes2 } from "node:crypto";
6
6
  import { appendFileSync, copyFileSync, createWriteStream, existsSync as existsSync3, mkdirSync, readFileSync as readFileSync3, readdirSync, renameSync, rmSync, statSync, writeFileSync as writeFileSync2, realpathSync } from "node:fs";
7
7
  import { createServer } from "node:net";
8
8
  import { arch as hostArch, homedir, userInfo } from "node:os";
9
- import { basename as basename2, join as join3, relative, resolve } from "node:path";
9
+ import { basename as basename2, join as join3, relative as relative2, resolve } from "node:path";
10
10
  import { createInterface } from "node:readline";
11
11
  import { Readable } from "node:stream";
12
12
  import { pipeline } from "node:stream/promises";
@@ -72,7 +72,7 @@ function makeKeys(dir2, options = {}) {
72
72
 
73
73
  // src/init/compose.ts
74
74
  import { existsSync as existsSync2, readFileSync as readFileSync2 } from "node:fs";
75
- import { basename, dirname, join as join2 } from "node:path";
75
+ import { basename, dirname, isAbsolute, join as join2, relative, sep } from "node:path";
76
76
  function hostFromContainer(engine2) {
77
77
  return engine2 === "podman" ? "host.containers.internal" : "host.docker.internal";
78
78
  }
@@ -80,11 +80,43 @@ function defaultAppUrl(engine2, port2 = 3e3) {
80
80
  return `http://${hostFromContainer(engine2)}:${port2}`;
81
81
  }
82
82
  var INSIGHT_COMPOSE = "docker-compose.insight.yml";
83
+ var COMPOSE_NAMES = ["compose.yaml", "compose.yml", "docker-compose.yaml", "docker-compose.yml"];
84
+ function appComposeFile(dir2) {
85
+ let at = dir2;
86
+ for (let up2 = 0; up2 < 20; up2++) {
87
+ for (const name2 of COMPOSE_NAMES) {
88
+ const path = join2(at, name2);
89
+ if (existsSync2(path) && !isInsights(path)) return up2 ? join2(...Array(up2).fill(".."), name2) : name2;
90
+ }
91
+ const parent = dirname(at);
92
+ if (existsSync2(join2(at, ".git")) || parent === at || !insideGit(parent)) return null;
93
+ at = parent;
94
+ }
95
+ return null;
96
+ }
97
+ var isInsights = (path) => {
98
+ try {
99
+ return /\/insight:\S+/.test(readFileSync2(path, "utf8"));
100
+ } catch {
101
+ return false;
102
+ }
103
+ };
104
+ function insideGit(dir2) {
105
+ for (let at = dir2; ; at = dirname(at)) {
106
+ if (existsSync2(join2(at, ".git"))) return true;
107
+ if (dirname(at) === at) return false;
108
+ }
109
+ }
83
110
  function composeFile(dir2) {
84
111
  if (existsSync2(join2(dir2, INSIGHT_COMPOSE))) return INSIGHT_COMPOSE;
85
112
  const main = join2(dir2, "docker-compose.yml");
86
- if (!existsSync2(main)) return "docker-compose.yml";
87
- return /\/insight:\S+/.test(readFileSync2(main, "utf8")) ? "docker-compose.yml" : INSIGHT_COMPOSE;
113
+ if (existsSync2(main) && isInsights(main)) return "docker-compose.yml";
114
+ return appComposeFile(dir2) ? INSIGHT_COMPOSE : "docker-compose.yml";
115
+ }
116
+ function composeNote(dir2, tool2, file) {
117
+ const app = appComposeFile(dir2);
118
+ if (file !== INSIGHT_COMPOSE || !app) return null;
119
+ return `Your app has its own compose file (${app}), left as it is. Insight's is ${INSIGHT_COMPOSE}, in this folder: start it with ${composeCommand(tool2, file)} up -d`;
88
120
  }
89
121
  function appPort(dir2) {
90
122
  const ok = (n) => Number.isInteger(n) && n > 0 && n < 65536;
@@ -100,12 +132,20 @@ function appPort(dir2) {
100
132
  for (const file of [".env.local", ".env"]) {
101
133
  try {
102
134
  const m = /^\s*PORT\s*=\s*["']?(\d{2,5})/m.exec(readFileSync2(join2(dir2, file), "utf8"));
103
- if (m && ok(Number(m[1]))) return { port: Number(m[1]), from: file };
135
+ if (m && ok(Number(m[1]))) return { port: Number(m[1]), from: `${file}'s PORT` };
104
136
  } catch {
105
137
  }
106
138
  }
107
139
  return null;
108
140
  }
141
+ function appPortLine(found2, appUrl2) {
142
+ const said = found2 ? `Your app's port, ${found2.port}, from ${found2.from}` : "Your app's port: nothing in package.json or .env says, so the usual 3000";
143
+ return `${said}. Insight reads your app at ${appUrl2.replace(/\/+$/, "")}; if that's wrong, change INSIGHT_READ_URL in insight.env.`;
144
+ }
145
+ function keysDirSetting(appDir, keysDir2) {
146
+ const rel = relative(appDir, keysDir2);
147
+ return rel && !rel.startsWith("..") && !isAbsolute(rel) ? rel.split(sep).join("/") : keysDir2;
148
+ }
109
149
  function containerNames(project2) {
110
150
  return { insight: `${project2}-insight`, db: `${project2}-db` };
111
151
  }
@@ -207,7 +247,9 @@ var { values } = cliArgs(
207
247
  --no-open print the sign-in link instead of opening it
208
248
 
209
249
  Writes docker-compose.yml, insight.env (secrets, added to .gitignore), insight-config/, keys/ (the app's signing key,
210
- added to .gitignore) and vendor/kaleyla-insight-<version>.tgz (commit this one: your CI installs from it).`,
250
+ added to .gitignore) and vendor/kaleyla-insight-<version>.tgz (commit this one: your CI installs from it).
251
+ When your app has a compose file of its own (compose.yaml or docker-compose.yml, here or in a folder above up to the
252
+ git repository's root), Insight's is docker-compose.insight.yml in this folder instead, and its commands use -f.`,
211
253
  {
212
254
  key: { type: "string" },
213
255
  release: { type: "string" },
@@ -241,15 +283,20 @@ var key = (values.key ?? process.env.KALEYLA_KEY ?? saved.INSIGHT_LICENCE_KEY ??
241
283
  if (!key && !values.yes && process.stdin.isTTY) key = (await askHidden("Your Kaleyla key (KLY-\u2026): ")).trim();
242
284
  if (!key) stop("Insight needs your Kaleyla key: give it with --key, or run this again and paste it when asked.");
243
285
  if (!key.startsWith("KLY-")) stop("That isn\u2019t a Kaleyla key: those start with KLY-.");
286
+ if (!upgrading && values.project !== void 0 && !/^[A-Za-z0-9][A-Za-z0-9_.-]{0,50}$/.test(values.project)) stop(`--project ${values.project}: use letters, numbers, - and _ only.`);
244
287
  if (!upgrading && !values["no-package"]) {
245
288
  for (const d of [dir, ...(values.also ?? []).map((a) => resolve(dir, a))]) {
246
- if (!existsSync3(join3(d, "package.json"))) stop(`${relative(dir, d) || "This folder"} has no package.json. Run this from your app\u2019s folder, or add --no-package for an app that isn\u2019t on Node.js.`);
289
+ if (!existsSync3(join3(d, "package.json"))) stop(`${relative2(dir, d) || "This folder"} has no package.json. Run this from your app\u2019s folder, or add --no-package for an app that isn\u2019t on Node.js.`);
247
290
  }
248
291
  }
249
292
  var engine = detectEngine();
250
293
  var cli = engine.cli;
251
294
  var tool = engine.compose.join(" ");
252
295
  done(`${engine.name === "podman" ? "Podman" : "Docker"} is running (${tool}${engine.selinux ? ", SELinux on" : ""})`);
296
+ var project = upgrading ? null : projectName(values.project ?? appName(dir));
297
+ var names = project ? containerNames(project) : null;
298
+ var taken = project ? takenBy(project, names) : void 0;
299
+ if (taken) stop(`${taken}, so this app's Insight can't be called ${project}: it would take over that one's containers and database. Choose another name with --project <name> (this app's own name, say).`);
253
300
  var release;
254
301
  try {
255
302
  const r = await fetch(`${DOWNLOADS}/v1/releases/${encodeURIComponent(values.release ?? "latest")}`, { headers: { authorization: `Bearer ${key}` } });
@@ -307,11 +354,6 @@ ${(up2.stderr ?? "").trim()}`);
307
354
  var port = values.port ? Number(values.port) : await freePort(3120);
308
355
  if (!Number.isInteger(port) || port < 1 || port > 65535) stop(`--port ${values.port} isn\u2019t a port.`);
309
356
  var name = values.name ?? (run("git", ["config", "user.name"]).stdout?.trim() || userInfo().username);
310
- if (values.project !== void 0 && !/^[A-Za-z0-9][A-Za-z0-9_.-]{0,50}$/.test(values.project)) stop(`--project ${values.project}: use letters, numbers, - and _ only.`);
311
- var project = projectName(values.project ?? appName(dir));
312
- var names = containerNames(project);
313
- var taken = takenBy(project, names);
314
- if (taken) stop(`${taken}, so this app's Insight can't be called ${project}: it would take over that one's containers and database. Choose another name with --project <name> (this app's own name, say).`);
315
357
  var found = values["app-url"] ? null : appPort(dir);
316
358
  var appUrl = values["app-url"] ?? defaultAppUrl(engine.name, found?.port ?? 3e3);
317
359
  var composeName = composeFile(dir);
@@ -342,7 +384,7 @@ writeFileSync2(
342
384
  // A new key, when you renew: the console, Settings › Licence (it then wins over this one).
343
385
  `INSIGHT_LICENCE_KEY=${key}`,
344
386
  // Where create-insight upgrade puts the new app package.
345
- `INSIGHT_APP_PACKAGES=${packageDirs.map((d) => relative(dir, d) || ".").join(",")}`,
387
+ `INSIGHT_APP_PACKAGES=${packageDirs.map((d) => relative2(dir, d) || ".").join(",")}`,
346
388
  `INSIGHT_STORE_URL=postgres://insight:${dbPassword}@insight-db:5432/insight`,
347
389
  "# Insight's own database (the insight-db service)",
348
390
  "POSTGRES_USER=insight",
@@ -359,8 +401,9 @@ if (add.length) appendFileSync(ignore, `${ignored.length && ignored.at(-1) !== "
359
401
  ${add.join("\n")}
360
402
  `);
361
403
  done(`Wrote ${composeName}, insight.env (secrets, kept out of git) and insight-config/`);
362
- if (composeName !== "docker-compose.yml") say(` Your app has its own docker-compose.yml, left as it is. Insight's is ${composeName}: start it with ${composeCommand(tool, composeName)} up -d`);
363
- if (!values["app-url"]) say(` Insight reads your app at port ${found?.port ?? 3e3} (${found ? `from ${found.from}` : "nothing in package.json or .env said, so the usual 3000"}). If that's wrong, change INSIGHT_READ_URL in insight.env.`);
404
+ var note = composeNote(dir, tool, composeName);
405
+ if (note) say(` ${note}`);
406
+ if (!values["app-url"]) say(` ${appPortLine(found, appUrl)}`);
364
407
  var keysDir = join3(dir, "keys");
365
408
  mkdirSync(keysDir, { recursive: true });
366
409
  var existingKey = join3(keysDir, "insight-signing-key.json");
@@ -382,7 +425,7 @@ done(opened ? "Opened the console, signed in as its Owner" : `Sign in to the con
382
425
  say();
383
426
  say("Your app\u2019s server needs two settings (in its .env, say):");
384
427
  say(` INSIGHT_URL=${consoleUrl}`);
385
- say(` INSIGHT_KEYS_DIR=${keysDir} (deploy this folder with the app: its signing key and watch secret)`);
428
+ say(` INSIGHT_KEYS_DIR=${keysDirSetting(dir, keysDir)} (relative to the folder your app's server runs from, this one; deploy keys/ with the app: its signing key and watch secret)`);
386
429
  if (packageDirs.length) say(`Commit vendor/kaleyla-insight-${version}.tgz with package.json: your CI installs Insight\u2019s package from it, with no key.`);
387
430
  say("Next, in the console: Your app \u203A Connection. Choose how people will ask and which AI model answers (nothing reaches a model before that), then give the prompt to your coding agent.");
388
431
  for (const line of afterInstallLines({ cli, tool, file: composeName, container: names.insight })) say(line);
@@ -442,7 +485,7 @@ function sha256(path) {
442
485
  return createHash("sha256").update(readFileSync3(path)).digest("hex");
443
486
  }
444
487
  function addPackage(target) {
445
- if (!existsSync3(join3(target, "package.json"))) stop(`${relative(dir, target) || "This folder"} has no package.json. Run this from your app\u2019s folder, or add --no-package for an app that isn\u2019t on Node.js.`);
488
+ if (!existsSync3(join3(target, "package.json"))) stop(`${relative2(dir, target) || "This folder"} has no package.json. Run this from your app\u2019s folder, or add --no-package for an app that isn\u2019t on Node.js.`);
446
489
  const vendor = join3(target, "vendor");
447
490
  mkdirSync(vendor, { recursive: true });
448
491
  for (const old of readdirSync(vendor).filter((n) => /^kaleyla-insight-.+\.tgz$/.test(n))) rmSync(join3(vendor, old));
@@ -452,9 +495,9 @@ function addPackage(target) {
452
495
  const lock = (n) => existsSync3(join3(target, n));
453
496
  const [pm, args] = lock("pnpm-lock.yaml") ? ["pnpm", ["add", `@kaleyla/insight@${spec}`]] : lock("yarn.lock") ? ["yarn", ["add", `@kaleyla/insight@${spec}`]] : lock("bun.lockb") || lock("bun.lock") ? ["bun", ["add", `@kaleyla/insight@${spec}`]] : ["npm", ["install", "--save-exact", `@kaleyla/insight@${spec}`]];
454
497
  const r = spawnSync(pm, args, { cwd: target, encoding: "utf8", shell: process.platform === "win32" });
455
- if (r.status !== 0) stop(`${pm} couldn\u2019t add @kaleyla/insight in ${relative(dir, target) || "this folder"}:
498
+ if (r.status !== 0) stop(`${pm} couldn\u2019t add @kaleyla/insight in ${relative2(dir, target) || "this folder"}:
456
499
  ${(r.stderr || r.stdout || "").trim().split("\n").slice(-8).join("\n")}`);
457
- done(`Added @kaleyla/insight ${version} to ${relative(dir, target) || "the app"} (vendor/${file}, with ${pm})`);
500
+ done(`Added @kaleyla/insight ${version} to ${relative2(dir, target) || "the app"} (vendor/${file}, with ${pm})`);
458
501
  }
459
502
  function askHidden(question) {
460
503
  return new Promise((resolveAnswer) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-insight",
3
- "version": "0.25.4",
3
+ "version": "0.26.0",
4
4
  "description": "Sets Insight up beside your app, with your Kaleyla key: npx create-insight",
5
5
  "license": "UNLICENSED",
6
6
  "type": "module",