@ingram-tech/nk-dev 0.20.0 → 0.21.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/README.md CHANGED
@@ -89,6 +89,9 @@ tsc), so versions stay under your control. nk only orchestrates.
89
89
  silently 405s the endpoint, a page or layout whose `openGraph` carries no
90
90
  image — its own object replaces the inherited one, so the card unfurls as a
91
91
  blank placeholder); `--fix` applies the auto-fixable findings.
92
+ In a workspace it judges each package for its role: the root may fan
93
+ `type-check`/`ci` out to members, a member inherits nk-dev and `lint`/`format`
94
+ from the root, and only a Next app is asked for `nk type-check`.
92
95
  - **`nk dev`** — start the Next dev server on the golden-path local database
93
96
  (see [`db-package.md`](https://github.com/ingram-technologies/nextkit/blob/main/docs/db-package.md)):
94
97
  - **PGlite** — if `@ingram-tech/nk-db`'s `nk-pglite-dev` bin resolves, hand off
package/lib/doctor.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { existsSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs";
2
- import { resolve } from "node:path";
2
+ import { dirname, relative, resolve, sep } from "node:path";
3
3
  import { checkAgentGuideImport, ensureGuideImport } from "./agent-guide.js";
4
4
  import { appDirFindings } from "./app-dir.js";
5
5
  import { authNextFindings } from "./auth-next.js";
@@ -56,6 +56,59 @@ function readJson(file) {
56
56
  }
57
57
  }
58
58
 
59
+ /** A package.json `workspaces` field as a list of patterns. */
60
+ function workspacePatterns(pkg) {
61
+ const ws = pkg?.workspaces;
62
+ if (Array.isArray(ws)) return ws;
63
+ if (Array.isArray(ws?.packages)) return ws.packages;
64
+ return [];
65
+ }
66
+
67
+ /** True if `rel` (a `/`-separated path) matches a workspace pattern: a `*`
68
+ * segment matches any one segment, `**` any depth. */
69
+ function matchesWorkspace(rel, pattern) {
70
+ const want = pattern.replace(/^\.\//, "").replace(/\/$/, "").split("/");
71
+ const have = rel.split("/");
72
+ if (want.at(-1) === "**") {
73
+ return want.slice(0, -1).every((seg, i) => seg === "*" || seg === have[i]);
74
+ }
75
+ return (
76
+ want.length === have.length &&
77
+ want.every((seg, i) => seg === "*" || seg === have[i])
78
+ );
79
+ }
80
+
81
+ /**
82
+ * The workspace root that lists `cwd` as a member, or null. Walks up from the
83
+ * parent directory: the first package.json declaring `workspaces` decides.
84
+ */
85
+ function workspaceRootOf(cwd) {
86
+ let dir = dirname(cwd);
87
+ for (;;) {
88
+ const pkg = readJson(resolve(dir, "package.json"));
89
+ if (pkg && workspacePatterns(pkg).length > 0) {
90
+ const rel = relative(dir, cwd).split(sep).join("/");
91
+ return workspacePatterns(pkg).some((p) => matchesWorkspace(rel, p))
92
+ ? { dir, pkg }
93
+ : null;
94
+ }
95
+ const parent = dirname(dir);
96
+ if (parent === dir) return null;
97
+ dir = parent;
98
+ }
99
+ }
100
+
101
+ const NEXT_CONFIGS = [
102
+ "next.config.ts",
103
+ "next.config.mjs",
104
+ "next.config.js",
105
+ "next.config.cjs",
106
+ ];
107
+
108
+ /** A script that runs the same script in every workspace member. */
109
+ const DELEGATES_RE =
110
+ /(^|\s)(--filter|-F|--recursive|-r|--workspaces|-ws)(\s|=|$)|\bturbo\s+run\b/;
111
+
59
112
  /** Write JSON in the house style (tabs, trailing newline). */
60
113
  function writeJson(file, value) {
61
114
  writeFileSync(file, `${JSON.stringify(value, null, "\t")}\n`);
@@ -74,8 +127,22 @@ export function findings(cwd) {
74
127
  if (!pkg) return out; // not a package — nothing to reconcile
75
128
 
76
129
  // 1. Scripts point at `nk`. Accept either `type-check` or `typecheck` key.
130
+ // In a workspace: the root may fan `type-check` out to its members; a
131
+ // member leaves `lint`/`format` to a root that runs them; and only a Next
132
+ // app wants `nk type-check` (it runs `next typegen`), a library runs tsc.
77
133
  const scripts = pkg.scripts ?? {};
134
+ const isWorkspaceRoot = workspacePatterns(pkg).length > 0;
135
+ const delegates = (cmd) => isWorkspaceRoot && DELEGATES_RE.test(cmd ?? "");
136
+ const root = workspaceRootOf(cwd);
137
+ const isNextApp = NEXT_CONFIGS.some((f) => existsSync(resolve(cwd, f)));
138
+ const runsTsc = (cmd) => root !== null && !isNextApp && /\btsc\b/.test(cmd ?? "");
78
139
  for (const [name, wanted] of Object.entries(CANONICAL_SCRIPTS)) {
140
+ if (
141
+ (name === "lint" || name === "format") &&
142
+ root?.pkg.scripts?.[name] === wanted
143
+ ) {
144
+ continue;
145
+ }
79
146
  const key =
80
147
  name === "type-check" &&
81
148
  scripts["typecheck"] !== undefined &&
@@ -84,6 +151,7 @@ export function findings(cwd) {
84
151
  : name;
85
152
  const current = scripts[key];
86
153
  if (current === wanted) continue;
154
+ if (name === "type-check" && (delegates(current) || runsTsc(current))) continue;
87
155
  // Only flag `check` as missing if the repo has scripts at all; a repo
88
156
  // truly without a check script still wants one on the nk model.
89
157
  out.push({
@@ -104,9 +172,11 @@ export function findings(cwd) {
104
172
  });
105
173
  }
106
174
 
107
- // 2. Superseded deps still declared.
175
+ // 2. Superseded deps still declared. A workspace member inherits nk-dev
176
+ // from the root that declares it (one toolchain, hoisted).
108
177
  const deps = { ...pkg.dependencies, ...pkg.devDependencies };
109
- if (deps["@ingram-tech/nk-dev"]) {
178
+ const rootDeps = { ...root?.pkg.dependencies, ...root?.pkg.devDependencies };
179
+ if (deps["@ingram-tech/nk-dev"] || rootDeps["@ingram-tech/nk-dev"]) {
110
180
  const superseded = SUPERSEDED_DEPS.filter((d) => d in deps);
111
181
  for (const dep of superseded) {
112
182
  out.push({
@@ -297,7 +367,9 @@ export function findings(cwd) {
297
367
  // 9. A `ci` script exists and runs the house gate. Its full contents are
298
368
  // the site's call (migrations, i18n, email catalogs, build…), so this only
299
369
  // warns and never writes — but the dep-upgrade flow and pre-push both
300
- // assume `bun run ci` is the one command that proves a change.
370
+ // assume `bun run ci` is the one command that proves a change. A
371
+ // workspace root that fans `ci` out to its members passes: each member's
372
+ // own `ci` is what this checks when doctor runs there.
301
373
  const ci = scripts["ci"];
302
374
  if (ci === undefined) {
303
375
  out.push({
@@ -306,9 +378,12 @@ export function findings(cwd) {
306
378
  message:
307
379
  'missing `ci` script — the one command that proves a change (e.g. "nk check && nk type-check && nk test")',
308
380
  });
309
- } else {
381
+ } else if (!delegates(ci)) {
310
382
  const missing = ["nk check", "nk type-check"].filter(
311
- (cmd) => !ci.includes(cmd) && !ci.includes(`bun run ${cmd.slice(3)}`),
383
+ (cmd) =>
384
+ !ci.includes(cmd) &&
385
+ !ci.includes(`bun run ${cmd.slice(3)}`) &&
386
+ !(cmd === "nk type-check" && runsTsc(ci)),
312
387
  );
313
388
  if (missing.length > 0) {
314
389
  out.push({
package/lib/og-image.js CHANGED
@@ -13,9 +13,51 @@ import { join, resolve } from "node:path";
13
13
 
14
14
  const PAGE_OR_LAYOUT = /^(layout|page)\.(tsx|jsx|ts|js)$/;
15
15
  const IMAGE_FILE = /^opengraph-image\d*\.(tsx|jsx|ts|js|png|jpe?g|gif)$/;
16
- const DECLARES_CARD = /\b(openGraph|twitter)\s*:/;
16
+ const CARD_KEY = /\b(openGraph|twitter)\s*:/g;
17
17
  const CARRIES_IMAGE = /\bimages\s*[:,}]|\bogImageMetadata\s*\(/;
18
18
 
19
+ /** Blank out string and template literals, keeping offsets, so braces inside
20
+ * copy (`"{name}"`) don't confuse the bracket walk below. */
21
+ const blankStrings = (src) =>
22
+ src.replace(/(["'`])(?:\\.|(?!\1)[\s\S])*\1/g, (m) => " ".repeat(m.length));
23
+
24
+ /**
25
+ * Whether the card key at `at` sits in a metadata object the file builds
26
+ * itself (`export const metadata = { ... }`, `return { ... }`, `=> ({ ... })`)
27
+ * rather than in an argument to a call (`createPageMetadata({ openGraph })`).
28
+ * A call can add the image (nk-seo's `createMetadata` merges `defaultImage`
29
+ * into the override), and a text check cannot see what it adds, so only a
30
+ * literal the file returns is provably missing one.
31
+ */
32
+ function inOwnLiteral(src, at) {
33
+ let depth = 0;
34
+ for (let i = at - 1; i >= 0; i--) {
35
+ const c = src[i];
36
+ if (c === ")" || c === "]" || c === "}") {
37
+ depth++;
38
+ continue;
39
+ }
40
+ if (c !== "(" && c !== "[" && c !== "{") continue;
41
+ if (depth > 0) {
42
+ depth--;
43
+ continue;
44
+ }
45
+ if (c === "[") continue; // an element of an array: keep walking out
46
+ const before = src.slice(0, i).trimEnd();
47
+ if (/(=>|\breturn|[^=!<>]=)$/.test(before)) return true;
48
+ if (c === "(") return false; // a call argument
49
+ if (/[:,{(]$/.test(before)) continue; // a nested object: keep walking out
50
+ return true; // a statement or function body: the literal is the file's own
51
+ }
52
+ return true;
53
+ }
54
+
55
+ /** Whether the file returns a card (openGraph/twitter) of its own building. */
56
+ const declaresOwnCard = (src) => {
57
+ const code = blankStrings(src);
58
+ return [...code.matchAll(CARD_KEY)].some((m) => inOwnLiteral(code, m.index));
59
+ };
60
+
19
61
  /** The site's app directory, or null when it has none. */
20
62
  function findAppDir(cwd) {
21
63
  const dir = resolve(cwd, "src/app");
@@ -47,7 +89,7 @@ function collect(cwd, dir, segments, nearest, out) {
47
89
  if (!PAGE_OR_LAYOUT.test(entry.name)) continue;
48
90
  if (here) continue; // the file convention wins in its own segment
49
91
  const src = readFileSync(full, "utf8");
50
- if (!DECLARES_CARD.test(src) || CARRIES_IMAGE.test(src)) continue;
92
+ if (!declaresOwnCard(src) || CARRIES_IMAGE.test(src)) continue;
51
93
  out.push({
52
94
  file: full.slice(cwd.length + 1),
53
95
  route: `/${segments.join("/")}`,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ingram-tech/nk-dev",
3
- "version": "0.20.0",
3
+ "version": "0.21.1",
4
4
  "description": "The nextkit dev toolchain in one package: the `nk` CLI plus shared oxlint/oxfmt, TypeScript, and Vitest config, the format-on-commit hook, and the AI agent guide. `nk init` scaffolds a site to use it.",
5
5
  "license": "MIT",
6
6
  "type": "module",