@se-studio/project-build 1.2.0 → 1.3.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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @se-studio/project-build
2
2
 
3
+ ## 1.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 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.
8
+
3
9
  ## 1.2.0
4
10
 
5
11
  ### 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,227 @@ function supportDrift(appRoot, config) {
596
604
  return drift;
597
605
  }
598
606
 
607
+ /**
608
+ * Legacy hand routes the CMS generator replaces. A folder is removed only
609
+ * when its page was not written by this generator.
610
+ *
611
+ * @param {string} appRoot
612
+ * @returns {string[]}
613
+ */
614
+ function removeStaleHandRoutes(appRoot) {
615
+ const cms = path.join(appRoot, 'src/app/(cms-routes)');
616
+ /** @type {string[]} */
617
+ const removed = [];
618
+ for (const rel of [
619
+ 'resources/[articleType]',
620
+ 'categories/[slug]',
621
+ 'articles',
622
+ 'people',
623
+ 'tags',
624
+ ]) {
625
+ const dir = path.join(cms, rel);
626
+ if (!fs.existsSync(dir)) {
627
+ continue;
628
+ }
629
+ const page = ['page.ts', 'page.tsx']
630
+ .map((name) => path.join(dir, name))
631
+ .find((file) => fs.existsSync(file));
632
+ const text = page ? fs.readFileSync(page, 'utf8') : '';
633
+ if (text.startsWith(GENERATED_MARK)) {
634
+ continue;
635
+ }
636
+ fs.rmSync(dir, { recursive: true, force: true });
637
+ removed.push(rel);
638
+ }
639
+ const tsx = path.join(cms, 'page.tsx');
640
+ const ts = path.join(cms, 'page.ts');
641
+ const handHome =
642
+ fs.existsSync(tsx) &&
643
+ fs.existsSync(ts) &&
644
+ !fs.readFileSync(tsx, 'utf8').startsWith(GENERATED_MARK);
645
+ if (handHome) {
646
+ fs.unlinkSync(tsx);
647
+ removed.push('page.tsx');
648
+ }
649
+ return removed;
650
+ }
651
+
652
+ /**
653
+ * Bracket routes validate-build-routes requires, in the order sites already
654
+ * commit. Indexes stay off the list. Dotted text routes are appended because
655
+ * the policy check ignores them and the sites keep them here.
656
+ *
657
+ * @param {unknown} config
658
+ * @returns {string[]}
659
+ */
660
+ export function mustBeSsgOrStaticRoutes(config) {
661
+ const allowed = allowedRests(config);
662
+ /** @type {string[]} */
663
+ const routes = [];
664
+ /**
665
+ * @param {string} rest
666
+ */
667
+ const push = (rest) => {
668
+ if (!rest.includes('[') || !allowed.has(rest)) {
669
+ return;
670
+ }
671
+ const route = `/${rest}`;
672
+ if (!routes.includes(route)) {
673
+ routes.push(route);
674
+ }
675
+ };
676
+ push('[level1]');
677
+ push('[level1]/[...slugs]');
678
+ if (config && typeof config === 'object') {
679
+ const articles = typeof config.articlesSlug === 'string' ? config.articlesSlug : '';
680
+ const people = typeof config.peopleSlug === 'string' ? config.peopleSlug : '';
681
+ const tags = typeof config.tagsSlug === 'string' ? config.tagsSlug : '';
682
+ if (categoryPath(config)) {
683
+ for (const type of config.articleTypes ?? []) {
684
+ const route = articleTypeRoute(type, config);
685
+ push(`${route.slug}/[tag]`);
686
+ push(`${route.slug}/[tag]/[...slugs]`);
687
+ }
688
+ } else if (config.tagInPath === true && articles) {
689
+ push(`${articles}/[articleType]`);
690
+ push(`${articles}/[articleType]/[...slugs]`);
691
+ push(`${articles}/[articleType]/[tag]`);
692
+ push(`${articles}/[articleType]/[tag]/[...slugs]`);
693
+ } else if (Array.isArray(config.articleTypes)) {
694
+ for (const type of config.articleTypes) {
695
+ const route = articleTypeRoute(type, config);
696
+ push(`${underPrefix(articles, route.slug)}/[article]`);
697
+ }
698
+ }
699
+ if (people) {
700
+ push(`${people}/[person]`);
701
+ }
702
+ for (const type of config.personTypes ?? []) {
703
+ push(`${underPrefix(people, String(type))}/[person]`);
704
+ }
705
+ if (tags) {
706
+ push(`${tags}/[tag]`);
707
+ }
708
+ for (const type of config.tagTypes ?? []) {
709
+ push(`${underPrefix(tags, String(type))}/[tag]`);
710
+ }
711
+ push('page-test/[...slugs]');
712
+ if (Array.isArray(config.articleBases)) {
713
+ for (const base of config.articleBases) {
714
+ const segment = typeof base?.path === 'string' ? base.path : '';
715
+ push(`${segment}/[articleType]`);
716
+ push(`${segment}/[articleType]/[...slugs]`);
717
+ push(`${segment}/[articleType]/[tag]`);
718
+ push(`${segment}/[articleType]/[tag]/[...slugs]`);
719
+ push(`${segment}/[...slugs]`);
720
+ push(`${segment}/[tag]`);
721
+ push(`${segment}/[tag]/[...slugs]`);
722
+ }
723
+ }
724
+ if (Array.isArray(config.personBases)) {
725
+ for (const base of config.personBases) {
726
+ const segment = typeof base?.path === 'string' ? base.path : '';
727
+ push(`${segment}/[person]`);
728
+ }
729
+ }
730
+ if (config.tags === true) {
731
+ push('tags/[tag]');
732
+ }
733
+ }
734
+ for (const rest of allowed) {
735
+ push(rest);
736
+ }
737
+ routes.push('/llms.txt', '/markdown-index.txt', '/site-info.md');
738
+ return routes;
739
+ }
740
+
741
+ /**
742
+ * @param {string} appRoot
743
+ * @param {unknown} config
744
+ */
745
+ function syncRouteBuildPolicy(appRoot, config) {
746
+ const policyPath = path.join(appRoot, 'route-build-policy.json');
747
+ if (!fs.existsSync(policyPath)) {
748
+ return;
749
+ }
750
+ const policy = JSON.parse(fs.readFileSync(policyPath, 'utf8'));
751
+ policy.mustBeSsgOrStatic = mustBeSsgOrStaticRoutes(config);
752
+ fs.writeFileSync(policyPath, `${JSON.stringify(policy, null, 2)}\n`);
753
+ console.log(`Updated ${path.relative(appRoot, policyPath)}`);
754
+ }
755
+
756
+ /**
757
+ * App Biome, so a narrower line width does not get undone by the next generate.
758
+ * Looks next to the app, then up to the workspace root.
759
+ *
760
+ * @param {string} appRoot
761
+ * @returns {string | null}
762
+ */
763
+ function biomeBinary(appRoot) {
764
+ let dir = appRoot;
765
+ for (let i = 0; i < 4; i += 1) {
766
+ const candidate = path.join(dir, 'node_modules', '.bin', 'biome');
767
+ if (fs.existsSync(candidate)) {
768
+ return candidate;
769
+ }
770
+ const parent = path.dirname(dir);
771
+ if (parent === dir) {
772
+ break;
773
+ }
774
+ dir = parent;
775
+ }
776
+ return null;
777
+ }
778
+
779
+ /**
780
+ * @param {string} appRoot
781
+ * @param {string} rel
782
+ * @param {string} source
783
+ */
784
+ function formatGeneratedSource(appRoot, rel, source) {
785
+ const biome = biomeBinary(appRoot);
786
+ if (!biome) {
787
+ return source;
788
+ }
789
+ const result = spawnSync(biome, ['check', '--write', '--stdin-file-path', rel], {
790
+ cwd: appRoot,
791
+ input: source,
792
+ encoding: 'utf8',
793
+ });
794
+ if (result.status !== 0 || typeof result.stdout !== 'string' || result.stdout.length === 0) {
795
+ return source;
796
+ }
797
+ return result.stdout;
798
+ }
799
+
800
+ /**
801
+ * @param {string} appRoot
802
+ */
803
+ function formatGeneratedPages(appRoot) {
804
+ const biome = biomeBinary(appRoot);
805
+ if (!biome) {
806
+ return;
807
+ }
808
+ /** @type {string[]} */
809
+ const files = [];
810
+ for (const file of listCmsPageFiles(appRoot)) {
811
+ const abs = path.join(appRoot, file.rel);
812
+ if (fs.readFileSync(abs, 'utf8').startsWith(GENERATED_MARK)) {
813
+ files.push(abs);
814
+ }
815
+ }
816
+ if (files.length === 0) {
817
+ return;
818
+ }
819
+ const result = spawnSync(biome, ['check', '--write', ...files], {
820
+ cwd: appRoot,
821
+ stdio: 'inherit',
822
+ });
823
+ if (result.status !== 0) {
824
+ throw new Error('biome check --write failed on generated routes');
825
+ }
826
+ }
827
+
599
828
  /**
600
829
  * Bracket CMS routes in route-build-policy.json must be the generated tree.
601
830
  *
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.0",
4
4
  "description": "Build tools and management scripts for SE Studio projects",
5
5
  "repository": {
6
6
  "type": "git",