@se-studio/project-build 1.2.0 → 1.3.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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @se-studio/project-build
2
2
 
3
+ ## 1.3.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 99cfb6c: `generate-marketing-routes --write` keeps generated person pages when a people path has no index page.
8
+
9
+ ## 1.3.0
10
+
11
+ ### Minor Changes
12
+
13
+ - 2323841: `generate-marketing-routes --write` rewrites `route-build-policy.json` `mustBeSsgOrStatic` from the route config and formats generated `(cms-routes)` pages with the app's Biome. Sites can call the bin directly instead of a local wrapper.
14
+
3
15
  ## 1.2.0
4
16
 
5
17
  ### Minor Changes
@@ -19,6 +19,10 @@
19
19
  * API. Otherwise it uses the delivery API. Shell values for DRAFT_ONLY and the
20
20
  * Contentful tokens override `.env.local`.
21
21
  *
22
+ * `--write` also rewrites `route-build-policy.json` `mustBeSsgOrStatic` from
23
+ * that config and formats the generated `(cms-routes)` pages with the app's
24
+ * Biome. `allowedDynamic` is left as it is.
25
+ *
22
26
  * Usage:
23
27
  * node scripts/generate-marketing-routes.mjs <app-dir> [--from-cms] [--write] [--force]
24
28
  * generate-marketing-routes <app-dir> [--from-cms] [--write] [--force]
@@ -10,6 +10,7 @@
10
10
  * @see docs/CUSTOMER_APP_SKELETON.md
11
11
  */
12
12
 
13
+ import { spawnSync } from 'node:child_process';
13
14
  import fs from 'node:fs';
14
15
  import path from 'node:path';
15
16
 
@@ -487,6 +488,9 @@ export function syncGeneratedRoutes(appRoot, options = {}) {
487
488
  }
488
489
  /** @type {string[]} */
489
490
  const removed = [];
491
+ if (options.write) {
492
+ removed.push(...removeStaleHandRoutes(appRoot));
493
+ }
490
494
  if (options.write && isSectionConfig(config)) {
491
495
  const live = new Set(files.keys());
492
496
  for (const file of listCmsPageFiles(appRoot)) {
@@ -503,6 +507,10 @@ export function syncGeneratedRoutes(appRoot, options = {}) {
503
507
  removeEmptyParents(path.dirname(abs), path.join(appRoot, 'src/app'));
504
508
  }
505
509
  }
510
+ if (options.write) {
511
+ syncRouteBuildPolicy(appRoot, config);
512
+ formatGeneratedPages(appRoot);
513
+ }
506
514
  return { wrote, skipped, removed, count: files.size, tagSkips: tagRouteSkips(config) };
507
515
  }
508
516
 
@@ -547,7 +555,7 @@ function generatedDrift(appRoot, config) {
547
555
  if (!current.startsWith(GENERATED_MARK)) {
548
556
  continue;
549
557
  }
550
- if (current !== expected) {
558
+ if (current !== formatGeneratedSource(appRoot, rel, expected)) {
551
559
  drift.push(`${rel} was edited after generation`);
552
560
  }
553
561
  }
@@ -596,6 +604,249 @@ function supportDrift(appRoot, config) {
596
604
  return drift;
597
605
  }
598
606
 
607
+ /**
608
+ * A people base with `index: false` has no `people/page.ts`. The person
609
+ * pages live under that folder, so the folder itself must stay.
610
+ *
611
+ * @param {string} dir
612
+ */
613
+ function hasGeneratedPage(dir) {
614
+ for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
615
+ const full = path.join(dir, entry.name);
616
+ if (entry.isDirectory()) {
617
+ if (hasGeneratedPage(full)) {
618
+ return true;
619
+ }
620
+ continue;
621
+ }
622
+ if (!entry.name.endsWith('.ts') && !entry.name.endsWith('.tsx')) {
623
+ continue;
624
+ }
625
+ if (fs.readFileSync(full, 'utf8').startsWith(GENERATED_MARK)) {
626
+ return true;
627
+ }
628
+ }
629
+ return false;
630
+ }
631
+
632
+ /**
633
+ * Legacy hand routes the CMS generator replaces. A folder is removed only
634
+ * when nothing in it was written by this generator. A generated person page
635
+ * with no index page is enough to keep `people/`.
636
+ *
637
+ * @param {string} appRoot
638
+ * @returns {string[]}
639
+ */
640
+ function removeStaleHandRoutes(appRoot) {
641
+ const cms = path.join(appRoot, 'src/app/(cms-routes)');
642
+ /** @type {string[]} */
643
+ const removed = [];
644
+ for (const rel of [
645
+ 'resources/[articleType]',
646
+ 'categories/[slug]',
647
+ 'articles',
648
+ 'people',
649
+ 'tags',
650
+ ]) {
651
+ const dir = path.join(cms, rel);
652
+ if (!fs.existsSync(dir)) {
653
+ continue;
654
+ }
655
+ if (hasGeneratedPage(dir)) {
656
+ continue;
657
+ }
658
+ fs.rmSync(dir, { recursive: true, force: true });
659
+ removed.push(rel);
660
+ }
661
+ const tsx = path.join(cms, 'page.tsx');
662
+ const ts = path.join(cms, 'page.ts');
663
+ const handHome =
664
+ fs.existsSync(tsx) &&
665
+ fs.existsSync(ts) &&
666
+ !fs.readFileSync(tsx, 'utf8').startsWith(GENERATED_MARK);
667
+ if (handHome) {
668
+ fs.unlinkSync(tsx);
669
+ removed.push('page.tsx');
670
+ }
671
+ return removed;
672
+ }
673
+
674
+ /**
675
+ * Bracket routes validate-build-routes requires, in the order sites already
676
+ * commit. Indexes stay off the list. Dotted text routes are appended because
677
+ * the policy check ignores them and the sites keep them here.
678
+ *
679
+ * @param {unknown} config
680
+ * @returns {string[]}
681
+ */
682
+ export function mustBeSsgOrStaticRoutes(config) {
683
+ const allowed = allowedRests(config);
684
+ /** @type {string[]} */
685
+ const routes = [];
686
+ /**
687
+ * @param {string} rest
688
+ */
689
+ const push = (rest) => {
690
+ if (!rest.includes('[') || !allowed.has(rest)) {
691
+ return;
692
+ }
693
+ const route = `/${rest}`;
694
+ if (!routes.includes(route)) {
695
+ routes.push(route);
696
+ }
697
+ };
698
+ push('[level1]');
699
+ push('[level1]/[...slugs]');
700
+ if (config && typeof config === 'object') {
701
+ const articles = typeof config.articlesSlug === 'string' ? config.articlesSlug : '';
702
+ const people = typeof config.peopleSlug === 'string' ? config.peopleSlug : '';
703
+ const tags = typeof config.tagsSlug === 'string' ? config.tagsSlug : '';
704
+ if (categoryPath(config)) {
705
+ for (const type of config.articleTypes ?? []) {
706
+ const route = articleTypeRoute(type, config);
707
+ push(`${route.slug}/[tag]`);
708
+ push(`${route.slug}/[tag]/[...slugs]`);
709
+ }
710
+ } else if (config.tagInPath === true && articles) {
711
+ push(`${articles}/[articleType]`);
712
+ push(`${articles}/[articleType]/[...slugs]`);
713
+ push(`${articles}/[articleType]/[tag]`);
714
+ push(`${articles}/[articleType]/[tag]/[...slugs]`);
715
+ } else if (Array.isArray(config.articleTypes)) {
716
+ for (const type of config.articleTypes) {
717
+ const route = articleTypeRoute(type, config);
718
+ push(`${underPrefix(articles, route.slug)}/[article]`);
719
+ }
720
+ }
721
+ if (people) {
722
+ push(`${people}/[person]`);
723
+ }
724
+ for (const type of config.personTypes ?? []) {
725
+ push(`${underPrefix(people, String(type))}/[person]`);
726
+ }
727
+ if (tags) {
728
+ push(`${tags}/[tag]`);
729
+ }
730
+ for (const type of config.tagTypes ?? []) {
731
+ push(`${underPrefix(tags, String(type))}/[tag]`);
732
+ }
733
+ push('page-test/[...slugs]');
734
+ if (Array.isArray(config.articleBases)) {
735
+ for (const base of config.articleBases) {
736
+ const segment = typeof base?.path === 'string' ? base.path : '';
737
+ push(`${segment}/[articleType]`);
738
+ push(`${segment}/[articleType]/[...slugs]`);
739
+ push(`${segment}/[articleType]/[tag]`);
740
+ push(`${segment}/[articleType]/[tag]/[...slugs]`);
741
+ push(`${segment}/[...slugs]`);
742
+ push(`${segment}/[tag]`);
743
+ push(`${segment}/[tag]/[...slugs]`);
744
+ }
745
+ }
746
+ if (Array.isArray(config.personBases)) {
747
+ for (const base of config.personBases) {
748
+ const segment = typeof base?.path === 'string' ? base.path : '';
749
+ push(`${segment}/[person]`);
750
+ }
751
+ }
752
+ if (config.tags === true) {
753
+ push('tags/[tag]');
754
+ }
755
+ }
756
+ for (const rest of allowed) {
757
+ push(rest);
758
+ }
759
+ routes.push('/llms.txt', '/markdown-index.txt', '/site-info.md');
760
+ return routes;
761
+ }
762
+
763
+ /**
764
+ * @param {string} appRoot
765
+ * @param {unknown} config
766
+ */
767
+ function syncRouteBuildPolicy(appRoot, config) {
768
+ const policyPath = path.join(appRoot, 'route-build-policy.json');
769
+ if (!fs.existsSync(policyPath)) {
770
+ return;
771
+ }
772
+ const policy = JSON.parse(fs.readFileSync(policyPath, 'utf8'));
773
+ policy.mustBeSsgOrStatic = mustBeSsgOrStaticRoutes(config);
774
+ fs.writeFileSync(policyPath, `${JSON.stringify(policy, null, 2)}\n`);
775
+ console.log(`Updated ${path.relative(appRoot, policyPath)}`);
776
+ }
777
+
778
+ /**
779
+ * App Biome, so a narrower line width does not get undone by the next generate.
780
+ * Looks next to the app, then up to the workspace root.
781
+ *
782
+ * @param {string} appRoot
783
+ * @returns {string | null}
784
+ */
785
+ function biomeBinary(appRoot) {
786
+ let dir = appRoot;
787
+ for (let i = 0; i < 4; i += 1) {
788
+ const candidate = path.join(dir, 'node_modules', '.bin', 'biome');
789
+ if (fs.existsSync(candidate)) {
790
+ return candidate;
791
+ }
792
+ const parent = path.dirname(dir);
793
+ if (parent === dir) {
794
+ break;
795
+ }
796
+ dir = parent;
797
+ }
798
+ return null;
799
+ }
800
+
801
+ /**
802
+ * @param {string} appRoot
803
+ * @param {string} rel
804
+ * @param {string} source
805
+ */
806
+ function formatGeneratedSource(appRoot, rel, source) {
807
+ const biome = biomeBinary(appRoot);
808
+ if (!biome) {
809
+ return source;
810
+ }
811
+ const result = spawnSync(biome, ['check', '--write', '--stdin-file-path', rel], {
812
+ cwd: appRoot,
813
+ input: source,
814
+ encoding: 'utf8',
815
+ });
816
+ if (result.status !== 0 || typeof result.stdout !== 'string' || result.stdout.length === 0) {
817
+ return source;
818
+ }
819
+ return result.stdout;
820
+ }
821
+
822
+ /**
823
+ * @param {string} appRoot
824
+ */
825
+ function formatGeneratedPages(appRoot) {
826
+ const biome = biomeBinary(appRoot);
827
+ if (!biome) {
828
+ return;
829
+ }
830
+ /** @type {string[]} */
831
+ const files = [];
832
+ for (const file of listCmsPageFiles(appRoot)) {
833
+ const abs = path.join(appRoot, file.rel);
834
+ if (fs.readFileSync(abs, 'utf8').startsWith(GENERATED_MARK)) {
835
+ files.push(abs);
836
+ }
837
+ }
838
+ if (files.length === 0) {
839
+ return;
840
+ }
841
+ const result = spawnSync(biome, ['check', '--write', ...files], {
842
+ cwd: appRoot,
843
+ stdio: 'inherit',
844
+ });
845
+ if (result.status !== 0) {
846
+ throw new Error('biome check --write failed on generated routes');
847
+ }
848
+ }
849
+
599
850
  /**
600
851
  * Bracket CMS routes in route-build-policy.json must be the generated tree.
601
852
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@se-studio/project-build",
3
- "version": "1.2.0",
3
+ "version": "1.3.1",
4
4
  "description": "Build tools and management scripts for SE Studio projects",
5
5
  "repository": {
6
6
  "type": "git",