@decocms/blocks-cli 7.79.7 → 7.80.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@decocms/blocks-cli",
3
- "version": "7.79.7",
3
+ "version": "7.80.0",
4
4
  "type": "module",
5
5
  "description": "Deco codegen (generate-blocks, generate-schema, generate-invoke) and Fresh-to-TanStack migration tooling",
6
6
  "repository": {
@@ -33,7 +33,7 @@
33
33
  "lint:unused": "knip"
34
34
  },
35
35
  "dependencies": {
36
- "@decocms/blocks": "7.79.7",
36
+ "@decocms/blocks": "7.80.0",
37
37
  "ts-morph": "^27.0.0",
38
38
  "tsx": "^4.22.5"
39
39
  },
@@ -544,6 +544,44 @@ describe("generate-schema default output path (.deco/)", () => {
544
544
  expect(meta.manifest.blocks.sections).toHaveProperty("site/sections/Hero.tsx");
545
545
  }, 30_000);
546
546
 
547
+ it("emits deco.sectionPackages sections under their own namespace, with Props resolved across the package", () => {
548
+ fs.writeFileSync(
549
+ path.join(tmpDir, "package.json"),
550
+ JSON.stringify({ deco: { sectionPackages: { ui: "@zeedog/ui" } } }),
551
+ );
552
+ const ui = path.join(tmpDir, "vendor", "ui");
553
+ fs.mkdirSync(path.join(ui, "src", "sections"), { recursive: true });
554
+ fs.mkdirSync(path.join(ui, "src", "components"), { recursive: true });
555
+ fs.writeFileSync(path.join(ui, "package.json"), JSON.stringify({ name: "@zeedog/ui" }));
556
+ fs.writeFileSync(
557
+ path.join(ui, "src", "components", "Card.ts"),
558
+ "export interface Card { image: string; }\n",
559
+ );
560
+ fs.writeFileSync(
561
+ path.join(ui, "src", "sections", "Banner.tsx"),
562
+ [
563
+ 'import type { Card } from "../components/Card";',
564
+ "export interface Props { title: string; card: Card; }",
565
+ "export default function Banner(props: Props) { return null; }",
566
+ ].join("\n"),
567
+ );
568
+ fs.mkdirSync(path.join(tmpDir, "node_modules", "@zeedog"), { recursive: true });
569
+ fs.symlinkSync(ui, path.join(tmpDir, "node_modules", "@zeedog", "ui"), "dir");
570
+
571
+ const { code, stderr } = runGenerator(["--skip-apps"], { cwd: tmpDir });
572
+ expect(code, stderr).toBe(0);
573
+
574
+ const meta = JSON.parse(fs.readFileSync(path.join(tmpDir, ".deco", "meta.gen.json"), "utf-8"));
575
+ expect(meta.manifest.blocks.sections).toHaveProperty("site/sections/Hero.tsx");
576
+ expect(meta.manifest.blocks.sections["ui/sections/Banner.tsx"]).toMatchObject({
577
+ namespace: "ui",
578
+ });
579
+ const section =
580
+ meta.schema.definitions[Buffer.from("ui/sections/Banner.tsx").toString("base64")];
581
+ const props = meta.schema.definitions[section.allOf[0].$ref.replace("#/definitions/", "")];
582
+ expect(Object.keys(props.properties)).toEqual(["title", "card"]);
583
+ }, 30_000);
584
+
547
585
  it("does not warn about a legacy default path", () => {
548
586
  const { code, stderr } = runGenerator(["--skip-apps"], { cwd: tmpDir });
549
587
  expect(code).toBe(0);
@@ -47,7 +47,7 @@ import {
47
47
  type Type,
48
48
  } from "ts-morph";
49
49
  import { isExcludedCodegenFile } from "./lib/codegenExclusions";
50
- import { resolvePackageDirectory } from "./lib/installedPackages";
50
+ import { readSectionPackages, resolvePackageDirectory } from "./lib/installedPackages";
51
51
 
52
52
  // ---------------------------------------------------------------------------
53
53
  // CLI arg parsing
@@ -1819,7 +1819,8 @@ function generateMeta(): MetaResponse {
1819
1819
  // Second pass: scan sections
1820
1820
  // ---------------------------------------------------------------------------
1821
1821
 
1822
- if (!fs.existsSync(sectionsDir)) {
1822
+ const sectionPackages = readSectionPackages(root);
1823
+ if (!fs.existsSync(sectionsDir) && sectionPackages.length === 0) {
1823
1824
  console.error(`Sections directory not found: ${sectionsDir}`);
1824
1825
  process.exit(1);
1825
1826
  }
@@ -1830,16 +1831,28 @@ function generateMeta(): MetaResponse {
1830
1831
  // (prop-less) section rather than being silently skipped.
1831
1832
  const sectionExts =
1832
1833
  PLATFORM === "eitri" ? [".tsx", ".ts", ".jsx", ".js"] : DEFAULT_EXTS;
1833
- const sectionFiles = findTsxFiles(sectionsDir, sectionExts);
1834
+ // Site sections, then sections from deco.sectionPackages keyed
1835
+ // `<namespace>/sections/<path>` (same key generate-sections emits).
1836
+ const sectionFiles = [
1837
+ ...findTsxFiles(sectionsDir, sectionExts).map((filePath) => ({
1838
+ filePath,
1839
+ namespace: SITE_NAMESPACE,
1840
+ blockKey: `${SITE_NAMESPACE}/${path.relative(srcDir, filePath).replaceAll("\\", "/")}`,
1841
+ })),
1842
+ ...sectionPackages.flatMap(({ namespace, sectionsDir: dir }) =>
1843
+ findTsxFiles(dir, sectionExts).map((filePath) => ({
1844
+ filePath,
1845
+ namespace,
1846
+ blockKey: `${namespace}/sections/${path.relative(dir, filePath).replaceAll("\\", "/")}`,
1847
+ })),
1848
+ ),
1849
+ ];
1834
1850
  console.log(`Found ${sectionFiles.length} section files`);
1835
- for (const filePath of sectionFiles) {
1851
+ for (const { filePath } of sectionFiles) {
1836
1852
  getSourceFile(project, filePath, sourceFileCache);
1837
1853
  }
1838
1854
 
1839
- for (const filePath of sectionFiles) {
1840
- const relativePath = path.relative(srcDir, filePath).replaceAll("\\", "/");
1841
- const blockKey = `${SITE_NAMESPACE}/${relativePath}`;
1842
-
1855
+ for (const { filePath, blockKey, namespace } of sectionFiles) {
1843
1856
  try {
1844
1857
  const sourceFile = getSourceFile(project, filePath, sourceFileCache);
1845
1858
 
@@ -1906,7 +1919,7 @@ function generateMeta(): MetaResponse {
1906
1919
 
1907
1920
  sectionBlocks[blockKey] = {
1908
1921
  $ref: `#/definitions/${sectionDefKey}`,
1909
- namespace: SITE_NAMESPACE,
1922
+ namespace,
1910
1923
  };
1911
1924
  sectionRootAnyOf.push({
1912
1925
  $ref: `#/definitions/${sectionDefKey}`,
@@ -464,3 +464,62 @@ describe("generate-sections output hygiene (non-registry)", () => {
464
464
  expect(generated.endsWith("\n\n")).toBe(false);
465
465
  }, 30_000);
466
466
  });
467
+
468
+ describe("generate-sections deco.sectionPackages", () => {
469
+ let site: string;
470
+
471
+ beforeEach(() => {
472
+ site = fs.mkdtempSync(path.join(os.tmpdir(), "generate-sections-pkg-"));
473
+ fs.writeFileSync(
474
+ path.join(site, "package.json"),
475
+ JSON.stringify({ deco: { sectionPackages: { ui: "@zeedog/ui" } } }),
476
+ );
477
+ fs.mkdirSync(path.join(site, "src/sections"), { recursive: true });
478
+ fs.writeFileSync(
479
+ path.join(site, "src/sections/Hero.tsx"),
480
+ "export const eager = true;\nexport default function Hero() { return null; }\n",
481
+ );
482
+ });
483
+
484
+ afterEach(() => {
485
+ fs.rmSync(site, { recursive: true, force: true });
486
+ });
487
+
488
+ it("scans the package's src/sections as <namespace>/sections/... and emits packageSectionImports through node_modules", () => {
489
+ const ui = path.join(site, "vendor/ui");
490
+ fs.mkdirSync(path.join(ui, "src/sections/Nested"), { recursive: true });
491
+ fs.writeFileSync(path.join(ui, "package.json"), JSON.stringify({ name: "@zeedog/ui" }));
492
+ fs.writeFileSync(
493
+ path.join(ui, "src/sections/Banner.tsx"),
494
+ "export const sync = true;\nexport default function Banner() { return null; }\n",
495
+ );
496
+ fs.writeFileSync(
497
+ path.join(ui, "src/sections/Nested/Plain.tsx"),
498
+ "export default function Plain() { return null; }\n",
499
+ );
500
+ // `file:./vendor/ui` installs as a symlink in dev.
501
+ fs.mkdirSync(path.join(site, "node_modules/@zeedog"), { recursive: true });
502
+ fs.symlinkSync(ui, path.join(site, "node_modules/@zeedog/ui"), "dir");
503
+
504
+ const { code, stderr } = runGenerator([], { cwd: site });
505
+ expect(code, stderr).toBe(0);
506
+
507
+ const generated = fs.readFileSync(path.join(site, ".deco/sections.gen.ts"), "utf-8");
508
+ expect(generated).toContain(`"site/sections/Hero.tsx": { eager: true },`);
509
+ expect(generated).toContain(`"ui/sections/Banner.tsx": { sync: true },`);
510
+ expect(generated).toContain(
511
+ `import * as _sync0 from "../node_modules/@zeedog/ui/src/sections/Banner";`,
512
+ );
513
+ expect(generated).toContain(`"ui/sections/Banner.tsx": _sync0,`);
514
+ // Every package file is importable, convention exports or not.
515
+ expect(generated).toContain(
516
+ `"ui/sections/Nested/Plain.tsx": () => import("../node_modules/@zeedog/ui/src/sections/Nested/Plain"),`,
517
+ );
518
+ }, 30_000);
519
+
520
+ it("fails loudly when a declared package is not installed", () => {
521
+ const { code, stderr } = runGenerator([], { cwd: site });
522
+ expect(code).not.toBe(0);
523
+ expect(stderr).toContain(`"@zeedog/ui" is not installed`);
524
+ }, 30_000);
525
+ });
@@ -28,10 +28,15 @@
28
28
  * Built from every scanned section file, not just the ones
29
29
  * carrying convention exports. Off by default so existing
30
30
  * Vite sites regenerating sections.gen.ts in CI see zero diff.
31
+ *
32
+ * package.json `"deco": { "sectionPackages": { "<namespace>": "<package>" } }`
33
+ * also scans `<package>/src/sections/**` as `<namespace>/sections/...` and emits
34
+ * `packageSectionImports` (lazy imports keyed by CMS key).
31
35
  */
32
36
  import fs from "node:fs";
33
37
  import path from "node:path";
34
38
  import { isExcludedCodegenFile } from "./lib/codegenExclusions";
39
+ import { readSectionPackages } from "./lib/installedPackages";
35
40
 
36
41
  const args = process.argv.slice(2);
37
42
  function arg(name: string, fallback: string): string {
@@ -153,9 +158,9 @@ function walkDir(dir: string, base: string = dir): string[] {
153
158
  return results;
154
159
  }
155
160
 
156
- function fileToSectionKey(filePath: string, _sectionsDir: string): string {
161
+ function fileToSectionKey(filePath: string, _sectionsDir: string, namespace = "site"): string {
157
162
  const rel = path.relative(_sectionsDir, filePath).replace(/\\/g, "/");
158
- return `site/sections/${rel}`;
163
+ return `${namespace}/sections/${rel}`;
159
164
  }
160
165
 
161
166
  function relativeImportPath(from: string, to: string): string {
@@ -166,7 +171,9 @@ function relativeImportPath(from: string, to: string): string {
166
171
 
167
172
  // ---------------------------------------------------------------------------
168
173
 
169
- if (!fs.existsSync(sectionsDir)) {
174
+ const sectionPackages = readSectionPackages(process.cwd());
175
+
176
+ if (!fs.existsSync(sectionsDir) && sectionPackages.length === 0) {
170
177
  console.warn(`Sections directory not found: ${sectionsDir} — generating empty output.`);
171
178
  fs.mkdirSync(path.dirname(outFile), { recursive: true });
172
179
  fs.writeFileSync(outFile, [
@@ -178,13 +185,19 @@ if (!fs.existsSync(sectionsDir)) {
178
185
  }
179
186
 
180
187
  const sectionFiles = walkDir(sectionsDir);
188
+ // Sections from deco.sectionPackages, keyed `<namespace>/sections/<path>`.
189
+ const packageFiles = sectionPackages.flatMap(({ namespace, sectionsDir: dir }) =>
190
+ walkDir(dir).map((filePath) => ({ filePath, key: fileToSectionKey(filePath, dir, namespace) })),
191
+ );
181
192
  const entries: Array<{ key: string; meta: SectionMeta; filePath: string }> = [];
182
193
 
183
- for (const filePath of sectionFiles) {
194
+ for (const { filePath, key } of [
195
+ ...sectionFiles.map((filePath) => ({ filePath, key: fileToSectionKey(filePath, sectionsDir) })),
196
+ ...packageFiles,
197
+ ]) {
184
198
  const content = fs.readFileSync(filePath, "utf-8");
185
199
  const meta = extractMeta(content);
186
200
  if (!meta) continue;
187
- const key = fileToSectionKey(filePath, sectionsDir);
188
201
  entries.push({ key, meta, filePath });
189
202
  }
190
203
 
@@ -345,6 +358,19 @@ if (EMIT_REGISTRY) {
345
358
  lines.push("};");
346
359
  }
347
360
 
361
+ // Lazy imports for package sections (deco.sectionPackages), keyed by full CMS
362
+ // key. Always emitted when packages are configured: unlike site sections there
363
+ // is no `import.meta.glob` fallback, so sites spread this into the map they
364
+ // pass to createSiteSetup({ sections }) and applySectionConventions({ sectionGlob }).
365
+ if (sectionPackages.length > 0) {
366
+ lines.push("");
367
+ lines.push("export const packageSectionImports: Record<string, () => Promise<any>> = {");
368
+ for (const { filePath, key } of packageFiles) {
369
+ lines.push(` "${key}": () => import("${relativeImportPath(outFile, filePath)}"),`);
370
+ }
371
+ lines.push("};");
372
+ }
373
+
348
374
  fs.mkdirSync(path.dirname(outFile), { recursive: true });
349
375
  // Output hygiene: several sections above push a trailing "" separator
350
376
  // unconditionally (e.g. after the header comment, after the sync/fallback
@@ -127,7 +127,11 @@ import { createRequire } from "node:module";
127
127
  import path from "node:path";
128
128
  import { fileURLToPath } from "node:url";
129
129
  import { isExcludedCodegenFile } from "./lib/codegenExclusions";
130
- import { listScopedPackages, resolvePackageDirectory } from "./lib/installedPackages";
130
+ import {
131
+ listScopedPackages,
132
+ readSectionPackages,
133
+ resolvePackageDirectory,
134
+ } from "./lib/installedPackages";
131
135
  import { resolveInvokeSource } from "./lib/invokeSource";
132
136
 
133
137
  // Bump when the digests file format or digest recipe changes — recorded in
@@ -529,7 +533,10 @@ export function buildPlan(cwd: string, opts: CliOptions): GeneratorPlan[] {
529
533
  const actionsDirAbs = path.resolve(cwd, opts.actionsDir);
530
534
 
531
535
  const blocksDirExists = fs.existsSync(blocksDirAbs);
532
- const sectionsDirExists = fs.existsSync(sectionsDirAbs);
536
+ const sectionPackages = readSectionPackages(cwd);
537
+ // deco.sectionPackages lets a site with no src/sections still own sections.
538
+ const sectionsDirExists = fs.existsSync(sectionsDirAbs) || sectionPackages.length > 0;
539
+ const packageJson = statEntry(cwd, path.join(cwd, "package.json"));
533
540
  const isNext = isNextjsSite(cwd);
534
541
  const hasTanstackStart = hasPackage(cwd, "@tanstack/react-start");
535
542
 
@@ -596,7 +603,15 @@ export function buildPlan(cwd: string, opts: CliOptions): GeneratorPlan[] {
596
603
  stage: 1,
597
604
  ...enabledIf(sectionsDirExists, `${opts.sectionsDir} does not exist`),
598
605
  inputs: () =>
599
- sortEntries(walkTree(cwd, sectionsDirAbs, [".ts", ".tsx"], isExcludedCodegenFile)),
606
+ sortEntries(
607
+ [
608
+ ...walkTree(cwd, sectionsDirAbs, [".ts", ".tsx"], isExcludedCodegenFile),
609
+ ...sectionPackages.flatMap((p) =>
610
+ walkTree(cwd, p.sectionsDir, [".ts", ".tsx"], isExcludedCodegenFile),
611
+ ),
612
+ packageJson,
613
+ ].filter((e): e is InputEntry => e !== null),
614
+ ),
600
615
  outputs: [sectionsGen],
601
616
  },
602
617
  {
@@ -688,9 +703,13 @@ export function buildPlan(cwd: string, opts: CliOptions): GeneratorPlan[] {
688
703
  inputs: () => {
689
704
  const entries = [
690
705
  ...walkTree(cwd, path.join(cwd, "src"), [".ts", ".tsx"]),
691
- ...[statEntry(cwd, path.join(cwd, "tsconfig.json"))].filter(
706
+ ...[statEntry(cwd, path.join(cwd, "tsconfig.json")), packageJson].filter(
692
707
  (e): e is InputEntry => e !== null,
693
708
  ),
709
+ // Section packages: their whole src/, since section Props reach into it.
710
+ ...sectionPackages.flatMap((p) =>
711
+ walkTree(cwd, path.dirname(p.sectionsDir), [".ts", ".tsx"]),
712
+ ),
694
713
  ];
695
714
  if (!opts.skipApps) {
696
715
  for (const [name, dir] of listScopedPackages(cwd, "@decocms")) {
@@ -17,6 +17,41 @@ export function resolvePackageDirectory(cwd: string, name: string): string | nul
17
17
  return null;
18
18
  }
19
19
 
20
+ interface SectionPackage {
21
+ namespace: string;
22
+ sectionsDir: string;
23
+ }
24
+
25
+ /** Packages whose `src/sections/**` the site exposes as CMS sections, read from
26
+ * the site's package.json: `"deco": { "sectionPackages": { "ui": "@acme/ui" } }`.
27
+ * Section keys become `<namespace>/sections/<path>`. The node_modules path
28
+ * (not the realpath) is kept so generated imports stay stable whether the
29
+ * package is a symlink (dev) or a copy (CI). Throws when a declared package is
30
+ * missing, because skipping it would silently drop its sections from the admin. */
31
+ export function readSectionPackages(cwd: string): SectionPackage[] {
32
+ let pkg: { deco?: { sectionPackages?: Record<string, string> } };
33
+ try {
34
+ pkg = JSON.parse(fs.readFileSync(path.join(cwd, "package.json"), "utf8"));
35
+ } catch {
36
+ return [];
37
+ }
38
+ return Object.entries(pkg.deco?.sectionPackages ?? {}).map(([namespace, name]) => {
39
+ if (namespace === "site") {
40
+ throw new Error(
41
+ `deco.sectionPackages: namespace "site" is reserved for the site's own sections (${name})`,
42
+ );
43
+ }
44
+ const dir = resolvePackageDirectory(cwd, name);
45
+ if (!dir) {
46
+ throw new Error(
47
+ `deco.sectionPackages: "${name}" is not installed. If it is a git submodule, ` +
48
+ "run `git submodule update --init` and reinstall dependencies.",
49
+ );
50
+ }
51
+ return { namespace, sectionsDir: path.join(dir, "src", "sections") };
52
+ });
53
+ }
54
+
20
55
  /** Installed packages in a scope, keyed by full package name. The nearest
21
56
  * copy wins, matching resolvePackageDirectory even with mixed local/hoisted
22
57
  * installs; shadowed ancestor copies must not enter codegen fingerprints. */