@marktiderman/genesis-cli 1.10.0 → 1.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/doctor.d.ts CHANGED
@@ -26,7 +26,7 @@ declare function parseRulesMarkdown(markdown: string): PatternRule[];
26
26
  declare function defineDoctorPlugin(plugin: DoctorPlugin): DoctorPlugin;
27
27
 
28
28
  /** Ids of the built-in rules, for `rules` overrides and `allow` entries. */
29
- declare const BUILTIN_RULES: readonly ["local-primitives", "starter-leftover", "forbidden-import", "raw-tag", "palette-color", "arbitrary-color", "raw-color", "undefined-token", "shadowed-export", "version-skew", "duplicate-genesis-package", "peer-range-mismatch", "hand-rolled-sub-nav", "local-genesis-copy", "resource-workspace", "page-layout", "iso-date-slice", "native-raw-primitive", "native-literal-color", "native-custom-header", "native-screen-layout", "store-readiness", "migration-grants", "migration-rls", "migration-definer-search-path", "migration-edited", "migration-testing-kit"];
29
+ declare const BUILTIN_RULES: readonly ["local-primitives", "starter-leftover", "forbidden-import", "raw-tag", "palette-color", "arbitrary-color", "raw-color", "undefined-token", "shadowed-export", "version-skew", "duplicate-genesis-package", "peer-range-mismatch", "hand-rolled-sub-nav", "local-genesis-copy", "resource-workspace", "page-layout", "iso-date-slice", "native-raw-primitive", "native-literal-color", "native-custom-header", "native-screen-layout", "store-readiness", "migration-grants", "migration-rls", "migration-definer-search-path", "migration-edited", "migration-testing-kit", "migration-view-rls"];
30
30
 
31
31
  /**
32
32
  * Minimal globs for config: `*` matches within a folder, `**` across folders,
@@ -89,12 +89,12 @@ interface GenesisInstall {
89
89
  * where a package's dependencies are its siblings in the store entry. Copies
90
90
  * nothing resolves to are left out: they never load.
91
91
  *
92
- * Each copy records which roots reach it: the app's declared runtime
93
- * dependencies (`runtime`), hoisted packages nobody declared (they could be
94
- * anyone's), and the dev roots (declared devDependencies, and the Genesis CLI
95
- * wherever it's listed). Only a copy reached from dev roots alone is `devOnly`.
96
- * An app with no runtime dependencies at all (everything in devDependencies, as
97
- * a bundled SPA may list them) has its devDependencies taken as runtime.
92
+ * Each copy records which roots reach it: the app's declared dependencies
93
+ * (`runtime`; devDependencies included, since a bundler ships what the app
94
+ * imports from them), hoisted packages nobody declared (they could be anyone's),
95
+ * and the dev roots: declared tooling (test runners, type packages, linters,
96
+ * bundlers, the Genesis CLI wherever it's listed), whose trees never ship. Only
97
+ * a copy reached from dev roots alone is `devOnly`.
98
98
  */
99
99
  declare function genesisInstalls(root: string): Map<string, GenesisInstall[]>;
100
100
 
package/dist/doctor.js CHANGED
@@ -966,7 +966,8 @@ var BUILTIN_RULES = [
966
966
  "migration-rls",
967
967
  "migration-definer-search-path",
968
968
  "migration-edited",
969
- "migration-testing-kit"
969
+ "migration-testing-kit",
970
+ "migration-view-rls"
970
971
  ];
971
972
  var DEFAULT_LOCAL_PRIMITIVE_DIRS = ["src/components/ui"];
972
973
  var STARTER_LEFTOVERS = ["components.json", "src/lib/utils.ts"];
@@ -1795,6 +1796,7 @@ function checkSupabasePack(ctx) {
1795
1796
  checkGrants(ctx, migrations.filter((m) => isNew(m.file)), exposed);
1796
1797
  }
1797
1798
  checkRls(ctx, migrations, exposed);
1799
+ checkViewInvoker(ctx, migrations, exposed);
1798
1800
  checkDefinerSearchPath(ctx, migrations);
1799
1801
  checkTestingKit(ctx, migrations);
1800
1802
  }
@@ -1924,6 +1926,50 @@ function checkRls(ctx, migrations, exposed) {
1924
1926
  });
1925
1927
  }
1926
1928
  }
1929
+ function checkViewInvoker(ctx, migrations, exposed) {
1930
+ const views = /* @__PURE__ */ new Map();
1931
+ const name = String.raw`([\w$.]+)`;
1932
+ const invokerOn = /\bsecurity_invoker\s*(?:=\s*(?:true|on|yes|1|'true'|'on'))?\s*(?:,|\))/;
1933
+ for (const { file, statements } of migrations) {
1934
+ for (const { sql, line } of statements) {
1935
+ const create = new RegExp(String.raw`^create (?:or replace )?(?:temp |temporary )?(?:recursive )?view ${name}`).exec(sql);
1936
+ if (create) {
1937
+ if (/^create (?:or replace )?(?:temp|temporary) /.test(sql)) continue;
1938
+ const options = /^[^(]*?\bwith\s*(\([^)]*\))\s*as\b/.exec(sql)?.[1] ?? "";
1939
+ views.set(qualify(create[1]), { invoker: invokerOn.test(options), file, line });
1940
+ continue;
1941
+ }
1942
+ const alter = new RegExp(String.raw`^alter view (?:if exists )?${name} (set|reset) (\([^)]*\))`).exec(sql);
1943
+ if (alter && /\bsecurity_invoker\b/.test(alter[3])) {
1944
+ const state = views.get(qualify(alter[1]));
1945
+ if (state) state.invoker = alter[2] === "set" && invokerOn.test(alter[3]);
1946
+ continue;
1947
+ }
1948
+ const rename = new RegExp(String.raw`^alter view (?:if exists )?${name} rename to ([\w$]+)$`).exec(sql);
1949
+ if (rename) {
1950
+ const from = qualify(rename[1]);
1951
+ const state = views.get(from);
1952
+ if (state) {
1953
+ views.delete(from);
1954
+ views.set(`${schemaOf(from)}.${rename[2]}`, state);
1955
+ }
1956
+ continue;
1957
+ }
1958
+ const drop = /^drop view (?:if exists )?(.+?)(?: cascade| restrict)?$/.exec(sql);
1959
+ if (drop) for (const n of drop[1].split(",")) views.delete(qualify(n));
1960
+ }
1961
+ }
1962
+ for (const [v, at] of views) {
1963
+ if (at.invoker || !exposed.has(schemaOf(v))) continue;
1964
+ ctx.report({
1965
+ rule: "migration-view-rls",
1966
+ file: at.file,
1967
+ line: at.line,
1968
+ subject: v,
1969
+ message: `${v} runs as its owner, so it reads past row level security; create it "with (security_invoker = true)" or add "alter view ${v} set (security_invoker = true);"`
1970
+ });
1971
+ }
1972
+ }
1927
1973
  function checkDefinerSearchPath(ctx, migrations) {
1928
1974
  const fns = /* @__PURE__ */ new Map();
1929
1975
  const keysFor = (sql, nameEnd, fnName) => {
@@ -2134,6 +2180,7 @@ function satisfies(version, range) {
2134
2180
  // src/doctor/installs.ts
2135
2181
  var GENESIS = /^@marktiderman\/genesis(?:-[\w.-]+)?$/;
2136
2182
  var DEV_TOOLS = /* @__PURE__ */ new Set(["@marktiderman/genesis-cli"]);
2183
+ var NON_SHIPPING = /^(?:@marktiderman\/genesis-cli|vitest|@vitest\/.+|jest|jest-.+|@jest\/.+|ts-jest|babel-jest|mocha|ava|c8|nyc|@playwright\/test|playwright|cypress|@testing-library\/.+|happy-dom|jsdom|msw|@types\/.+|typescript|tsx|ts-node|eslint|eslint-.+|@eslint\/.+|@typescript-eslint\/.+|typescript-eslint|prettier|prettier-.+|@biomejs\/.+|oxlint|vite|@vitejs\/.+|vite-plugin-.+|webpack|webpack-.+|rollup|@rollup\/.+|esbuild|tsup|turbo|@storybook\/.+|storybook|@changesets\/.+|husky|lint-staged|wrangler|knip|concurrently|npm-run-all2?|rimraf|cross-env|dotenv-cli)$/;
2137
2184
  var readManifest = (dir) => {
2138
2185
  try {
2139
2186
  return JSON.parse(readFileSync(path14.join(dir, "package.json"), "utf8"));
@@ -2148,6 +2195,11 @@ var real = (p) => {
2148
2195
  return p;
2149
2196
  }
2150
2197
  };
2198
+ var LOCKFILES = ["bun.lock", "bun.lockb", "package-lock.json", "npm-shrinkwrap.json", "yarn.lock", "pnpm-lock.yaml"];
2199
+ function nodeModulesOf(dir) {
2200
+ const parent = path14.dirname(dir);
2201
+ return path14.basename(parent).startsWith("@") ? path14.dirname(parent) : parent;
2202
+ }
2151
2203
  function packagesIn(nodeModules) {
2152
2204
  if (!existsSync(nodeModules)) return [];
2153
2205
  const out = [];
@@ -2196,24 +2248,41 @@ function genesisInstalls(root) {
2196
2248
  for (const r of roots) visit(r.dir, r.by, r.range);
2197
2249
  while (queue.length) {
2198
2250
  const { real: from, manifest, label } = queue.shift();
2199
- const deps = { ...manifest.peerDependencies, ...manifest.optionalDependencies, ...manifest.dependencies };
2251
+ const deps = { ...manifest.optionalDependencies, ...manifest.dependencies };
2200
2252
  for (const [name, range] of Object.entries(deps)) {
2201
2253
  const dir = findPackageDir(from, name);
2202
2254
  if (dir) visit(dir, label, range);
2203
2255
  }
2256
+ for (const [name, range] of Object.entries(manifest.peerDependencies ?? {})) {
2257
+ if (name in deps) continue;
2258
+ const dir = peerOf(from, name);
2259
+ if (dir) visit(dir, label, range);
2260
+ }
2204
2261
  }
2205
2262
  };
2263
+ const peerOf = (from, name) => {
2264
+ const dir = findPackageDir(from, name);
2265
+ if (!dir) return null;
2266
+ if (!ownInstall || name in declared) return dir;
2267
+ const realDir = real(dir);
2268
+ if (realDir.startsWith(realRoot + path14.sep)) return dir;
2269
+ return nodeModulesOf(realDir) === nodeModulesOf(from) ? dir : null;
2270
+ };
2206
2271
  const rootsOf = (deps, by) => Object.entries(deps).flatMap(([name, range]) => {
2207
2272
  const dir = findPackageDir(root, name);
2208
2273
  return dir ? [{ dir, by, ...range ? { range } : {} }] : [];
2209
2274
  });
2210
2275
  const app = readManifest(root);
2211
- let runtimeDeps = { ...app?.peerDependencies, ...app?.optionalDependencies, ...app?.dependencies };
2212
- let devDeps = Object.fromEntries(Object.entries(app?.devDependencies ?? {}).filter(([n]) => !(n in runtimeDeps)));
2213
- if (Object.keys(runtimeDeps).length === 0) {
2214
- runtimeDeps = Object.fromEntries(Object.entries(devDeps).filter(([n]) => !DEV_TOOLS.has(n)));
2215
- devDeps = Object.fromEntries(Object.entries(devDeps).filter(([n]) => DEV_TOOLS.has(n)));
2216
- }
2276
+ const ownInstall = LOCKFILES.some((f) => existsSync(path14.join(root, f)));
2277
+ const declared = { ...app?.devDependencies, ...app?.peerDependencies, ...app?.optionalDependencies, ...app?.dependencies };
2278
+ const declaredDev = Object.entries(app?.devDependencies ?? {});
2279
+ const runtimeDeps = {
2280
+ ...Object.fromEntries(declaredDev.filter(([n]) => !NON_SHIPPING.test(n))),
2281
+ ...app?.peerDependencies,
2282
+ ...app?.optionalDependencies,
2283
+ ...app?.dependencies
2284
+ };
2285
+ const devDeps = Object.fromEntries(declaredDev.filter(([n]) => NON_SHIPPING.test(n) && !(n in runtimeDeps)));
2217
2286
  walk("runtime", rootsOf(runtimeDeps, "the app"));
2218
2287
  const top = path14.join(root, "node_modules");
2219
2288
  walk(
@@ -2224,7 +2293,7 @@ function genesisInstalls(root) {
2224
2293
  }).map((dir) => ({ dir, by: "hoisted, not a direct dependency" }))
2225
2294
  );
2226
2295
  const devRoots = { ...devDeps };
2227
- for (const tool of DEV_TOOLS) if (!(tool in devRoots)) devRoots[tool] = runtimeDeps[tool];
2296
+ for (const tool of DEV_TOOLS) if (!(tool in devRoots) && tool in runtimeDeps) devRoots[tool] = runtimeDeps[tool];
2228
2297
  walk("dev", rootsOf(devRoots, "the app (dev)"));
2229
2298
  for (const [key2, copy] of copies) {
2230
2299
  const kinds = via.get(key2);
@@ -2296,11 +2365,21 @@ function stripForImports(code) {
2296
2365
  const templates = [];
2297
2366
  let depth = 0;
2298
2367
  let i = 0;
2368
+ let sure = true;
2299
2369
  const isSpecifier = () => /(?:\bfrom|(?:^|[;{}\n])\s*import|\bimport\s*\(|\brequire\s*\()\s*$/.test(out.slice(-40).join(""));
2300
- const opensString = () => {
2301
- const before = out.slice(-12).join("");
2302
- if (!/[\w$]$/.test(before)) return true;
2303
- return /(?:^|[^\w$])(?:return|from|import|case|typeof|in|of|else|yield|await|export|default|void|require)$/.test(before);
2370
+ const before = () => out.slice(-24).join("");
2371
+ const KEYWORD = /(?:^|[^\w$.])(?:return|typeof|instanceof|case|do|else|in|of|new|delete|void|throw|yield|await|from|import|export|default|require)\s*$/;
2372
+ const startsExpression = (b) => {
2373
+ const t = b.replace(/\s+$/, "");
2374
+ if (!t) return true;
2375
+ if (KEYWORD.test(b)) return true;
2376
+ return /[([{,;:=!&|?+\-*%<>~^}]$/.test(t);
2377
+ };
2378
+ const exprStart = () => startsExpression(before());
2379
+ const taggedTemplate = () => {
2380
+ const b = out.slice(-48).join("");
2381
+ const m = /([A-Za-z_$][\w$.]*)$/.exec(b);
2382
+ return m !== null && startsExpression(b.slice(0, b.length - m[1].length));
2304
2383
  };
2305
2384
  const templateBody = () => {
2306
2385
  while (i < n) {
@@ -2323,8 +2402,9 @@ function stripForImports(code) {
2323
2402
  i++;
2324
2403
  }
2325
2404
  }
2405
+ sure = false;
2326
2406
  };
2327
- while (i < n) {
2407
+ while (i < n && sure) {
2328
2408
  const ch = code[i];
2329
2409
  const next = code[i + 1];
2330
2410
  if (ch === "/" && next === "/") {
@@ -2343,7 +2423,45 @@ function stripForImports(code) {
2343
2423
  out.push(" ");
2344
2424
  i += 2;
2345
2425
  }
2346
- } else if ((ch === '"' || ch === "'") && opensString()) {
2426
+ } else if (ch === "/" && next !== ">" && code[i - 1] !== "<" && exprStart()) {
2427
+ const afterBrace = /[}>]\s*$/.test(before());
2428
+ const start = i;
2429
+ const mark = out.length;
2430
+ out.push("/");
2431
+ i++;
2432
+ let inClass = false;
2433
+ for (; ; ) {
2434
+ const c = code[i];
2435
+ if (c === void 0 || c === "\n") {
2436
+ if (afterBrace) {
2437
+ out.length = mark;
2438
+ out.push("/");
2439
+ i = start + 1;
2440
+ } else sure = false;
2441
+ break;
2442
+ }
2443
+ if (c === "\\") {
2444
+ out.push(" ", blank(code[i + 1] ?? ""));
2445
+ i += 2;
2446
+ continue;
2447
+ }
2448
+ i++;
2449
+ if (c === "[") inClass = true;
2450
+ else if (c === "]") inClass = false;
2451
+ else if (c === "/" && !inClass) {
2452
+ if (afterBrace && /[<>]/.test(code.slice(start + 1, i - 1))) {
2453
+ out.length = mark;
2454
+ out.push("/");
2455
+ i = start + 1;
2456
+ break;
2457
+ }
2458
+ out.push("/");
2459
+ break;
2460
+ }
2461
+ out.push(" ");
2462
+ }
2463
+ if (out.length > mark + 1 || i > start + 1) while (i < n && /[a-z]/.test(code[i])) out.push(code[i++]);
2464
+ } else if ((ch === '"' || ch === "'") && exprStart()) {
2347
2465
  const keep = isSpecifier();
2348
2466
  out.push(ch);
2349
2467
  i++;
@@ -2356,11 +2474,12 @@ function stripForImports(code) {
2356
2474
  i++;
2357
2475
  }
2358
2476
  }
2359
- if (i < n) {
2477
+ if (code[i] !== ch) sure = false;
2478
+ else {
2360
2479
  out.push(code[i]);
2361
2480
  i++;
2362
2481
  }
2363
- } else if (ch === "`") {
2482
+ } else if (ch === "`" && (exprStart() || taggedTemplate())) {
2364
2483
  out.push("`");
2365
2484
  i++;
2366
2485
  templateBody();
@@ -2377,7 +2496,7 @@ function stripForImports(code) {
2377
2496
  i++;
2378
2497
  }
2379
2498
  }
2380
- return out.join("");
2499
+ return sure && templates.length === 0 ? out.join("") : null;
2381
2500
  }
2382
2501
  function bindings(clause) {
2383
2502
  const out = [];
@@ -2491,6 +2610,11 @@ function createTrail(ctx) {
2491
2610
  const files = [];
2492
2611
  let lost = false;
2493
2612
  const code = stripped(file);
2613
+ if (code === null) {
2614
+ const out2 = { files: [], lost: true };
2615
+ importsCache.set(file.absolutePath, out2);
2616
+ return out2;
2617
+ }
2494
2618
  const used = new Set(renderedElements(code).map((e) => e.split(".")[0]));
2495
2619
  for (const m of code.matchAll(/\b\w*[cC]omponent\s*:\s*([A-Za-z_$][\w$]*)/g)) used.add(m[1]);
2496
2620
  for (const m of code.matchAll(/\bexport\s+default\s+([A-Za-z_$][\w$]*)/g)) used.add(m[1]);
@@ -2527,20 +2651,23 @@ function createTrail(ctx) {
2527
2651
  };
2528
2652
  const strippedCache = /* @__PURE__ */ new Map();
2529
2653
  const stripped = (file) => {
2530
- let c = strippedCache.get(file.absolutePath);
2531
- if (c === void 0) strippedCache.set(file.absolutePath, c = stripForImports(file.code));
2532
- return c;
2654
+ if (!strippedCache.has(file.absolutePath)) strippedCache.set(file.absolutePath, stripForImports(file.code));
2655
+ return strippedCache.get(file.absolutePath);
2533
2656
  };
2534
2657
  const memo = /* @__PURE__ */ new WeakMap();
2535
2658
  const reach = (file, test2, depth, seen) => {
2536
2659
  let cache = memo.get(test2);
2537
- if (!cache) memo.set(test2, cache = /* @__PURE__ */ new Map());
2660
+ if (!cache) memo.set(test2, cache = { free: /* @__PURE__ */ new Map(), cut: /* @__PURE__ */ new Map() });
2538
2661
  const k = `${file.absolutePath}\0${depth}`;
2539
- const cached = cache.get(k);
2540
- if (cached) return [cached, false];
2662
+ const free = cache.free.get(k);
2663
+ if (free) return [free, /* @__PURE__ */ new Set()];
2664
+ const reusable = cache.cut.get(k)?.find((e) => e.cut.every((m) => seen.has(m)));
2665
+ if (reusable) return [reusable.result, new Set(reusable.cut)];
2541
2666
  let result;
2542
- let cut = false;
2543
- if (test2(stripped(file))) result = "yes";
2667
+ const cut = /* @__PURE__ */ new Set();
2668
+ const code = stripped(file);
2669
+ if (code === null) result = "unknown";
2670
+ else if (test2(code)) result = "yes";
2544
2671
  else {
2545
2672
  const { files, lost } = importsOf(file);
2546
2673
  let unknown = lost && depth > 0;
@@ -2549,7 +2676,7 @@ function createTrail(ctx) {
2549
2676
  seen.add(file.absolutePath);
2550
2677
  for (const abs of files) {
2551
2678
  if (seen.has(abs)) {
2552
- cut = true;
2679
+ cut.add(abs);
2553
2680
  continue;
2554
2681
  }
2555
2682
  const target = read(abs);
@@ -2558,7 +2685,7 @@ function createTrail(ctx) {
2558
2685
  continue;
2559
2686
  }
2560
2687
  const [r, c] = reach(target, test2, depth - 1, seen);
2561
- if (c) cut = true;
2688
+ for (const m of c) cut.add(m);
2562
2689
  if (r === "yes") {
2563
2690
  result = "yes";
2564
2691
  break;
@@ -2566,11 +2693,18 @@ function createTrail(ctx) {
2566
2693
  if (r === "unknown") unknown = true;
2567
2694
  }
2568
2695
  seen.delete(file.absolutePath);
2696
+ cut.delete(file.absolutePath);
2569
2697
  }
2570
2698
  if (result === "no" && unknown) result = "unknown";
2571
2699
  }
2572
- if (result === "yes" || !cut) cache.set(k, result);
2573
- return [result, cut && result !== "yes"];
2700
+ if (result === "yes" || cut.size === 0) {
2701
+ cache.free.set(k, result);
2702
+ return [result, /* @__PURE__ */ new Set()];
2703
+ }
2704
+ const entries = cache.cut.get(k) ?? [];
2705
+ entries.push({ cut: [...cut], result });
2706
+ cache.cut.set(k, entries);
2707
+ return [result, cut];
2574
2708
  };
2575
2709
  return { reach: (file, test2, depth = TRAIL_DEPTH) => reach(file, test2, depth, /* @__PURE__ */ new Set())[0] };
2576
2710
  }