@matra/site-generation 0.1.0-site.0 → 0.1.0-site.2

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.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/src/site.mjs +76 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@matra/site-generation",
3
- "version": "0.1.0-site.0",
3
+ "version": "0.1.0-site.2",
4
4
  "description": "Declarative static site generation for Matra",
5
5
  "type": "module",
6
6
  "exports": {
@@ -11,7 +11,7 @@
11
11
  ],
12
12
  "dependencies": {
13
13
  "@matra/core": "^0.3.1-site.0",
14
- "@matra/html": "^0.3.0",
14
+ "@matra/html": "^0.3.1-site.0",
15
15
  "js-yaml": "^5.2.2",
16
16
  "markdown-it": "^14.3.0"
17
17
  },
package/src/site.mjs CHANGED
@@ -8,6 +8,7 @@ import {
8
8
  readFile,
9
9
  rename,
10
10
  rm,
11
+ rmdir,
11
12
  writeFile,
12
13
  } from "node:fs/promises"
13
14
  import { basename, dirname, extname, isAbsolute, join, relative, resolve, sep } from "node:path"
@@ -19,6 +20,7 @@ import { markdownToNodes } from "./markdown.mjs"
19
20
 
20
21
  const CONFIG_NAMES = ["matra.site.yaml", "matra.site.json", "matra.site.matra"]
21
22
  const SITE_KEYS = new Set(["route", "layout", "data"])
23
+ const OUTPUT_MANIFEST = ".matra-site-manifest.json"
22
24
 
23
25
  export async function renderLiterateDocument(markdown, options = {}) {
24
26
  const bundle = await buildLiterate(markdown, { document: options.document })
@@ -59,7 +61,7 @@ export async function buildSite(directory) {
59
61
  settings.routesByRoute = new Set(pages.map(page => page.route))
60
62
  settings.assetFiles = new Set(assets.map(asset => resolve(asset.source)))
61
63
  settings.assetOutputs = new Set(assets.map(asset => `/${asset.output.split(sep).join("/")}`))
62
- await validateOutput(root, settings.outputDirectory, outputPlan)
64
+ await validateOutput(root, settings.outputDirectory, { files: [] })
63
65
  settings.documents = documents.map(document => ({
64
66
  id: document.id,
65
67
  path: document.id,
@@ -574,6 +576,9 @@ function planOutput(pages, assets, settings) {
574
576
  const seen = new Map()
575
577
  for (const output of outputs) {
576
578
  const normalized = output.split(sep).join("/").toLocaleLowerCase("en-US")
579
+ if (normalized === OUTPUT_MANIFEST || normalized.startsWith(`${OUTPUT_MANIFEST}/`)) {
580
+ throw new Error(`Reserved site output path: ${output}`)
581
+ }
577
582
  if (seen.has(normalized))
578
583
  throw new Error(`Output collision: ${output} and ${seen.get(normalized)}`)
579
584
  seen.set(normalized, output)
@@ -634,7 +639,7 @@ async function validateOutputFile(output, relativePath) {
634
639
  async function publish(root, outputDirectory, rendered) {
635
640
  const parent = dirname(outputDirectory)
636
641
  await ensureDirectoryPath(root, parent)
637
- await validateOutput(root, outputDirectory, { files: rendered.map(file => file.path) })
642
+ await validateOutput(root, outputDirectory, { files: [] })
638
643
  const stage = await mkdtemp(join(parent, `.${basename(outputDirectory)}-stage-`))
639
644
  const backup = join(parent, `.${basename(outputDirectory)}-backup-${randomUUID()}`)
640
645
  try {
@@ -650,11 +655,22 @@ async function publish(root, outputDirectory, rendered) {
650
655
  await validateTree(outputDirectory)
651
656
  await cp(outputDirectory, stage, { recursive: true, force: true })
652
657
  }
658
+ const previousFiles = await readOutputManifest(stage)
659
+ const files = rendered.map(file => file.path.split(sep).join("/")).sort()
660
+ const currentFiles = new Set(files)
661
+ for (const file of previousFiles) {
662
+ if (!currentFiles.has(file)) await removeGeneratedFile(stage, file)
663
+ }
664
+ await validateOutput(root, stage, { files: rendered.map(file => file.path) })
653
665
  for (const file of rendered) {
654
666
  const stagedPath = join(stage, file.path)
655
667
  await mkdir(dirname(stagedPath), { recursive: true })
656
668
  await writeFile(stagedPath, file.content)
657
669
  }
670
+ await writeFile(
671
+ join(stage, OUTPUT_MANIFEST),
672
+ `${JSON.stringify({ version: 1, files }, null, 2)}\n`,
673
+ )
658
674
  let hadOutput = false
659
675
  try {
660
676
  await rename(outputDirectory, backup)
@@ -674,6 +690,64 @@ async function publish(root, outputDirectory, rendered) {
674
690
  }
675
691
  }
676
692
 
693
+ async function readOutputManifest(output) {
694
+ const path = join(output, OUTPUT_MANIFEST)
695
+ let source
696
+ try {
697
+ source = await readFile(path, "utf8")
698
+ } catch (error) {
699
+ if (error.code === "ENOENT") return []
700
+ throw error
701
+ }
702
+ let manifest
703
+ try {
704
+ manifest = JSON.parse(source)
705
+ } catch (error) {
706
+ throw new SyntaxError(`Invalid site output manifest: ${path}`, { cause: error })
707
+ }
708
+ if (
709
+ !isRecord(manifest) ||
710
+ manifest.version !== 1 ||
711
+ Object.keys(manifest).some(key => !["version", "files"].includes(key)) ||
712
+ !Array.isArray(manifest.files)
713
+ ) {
714
+ throw new TypeError(`Invalid site output manifest: ${path}`)
715
+ }
716
+ const seen = new Set()
717
+ for (const file of manifest.files) {
718
+ if (
719
+ typeof file !== "string" ||
720
+ file.includes("\\") ||
721
+ file.split("/").some(part => !isSafeSegment(part)) ||
722
+ file.toLocaleLowerCase("en-US") === OUTPUT_MANIFEST ||
723
+ file.toLocaleLowerCase("en-US").startsWith(`${OUTPUT_MANIFEST}/`)
724
+ ) {
725
+ throw new TypeError(`Invalid generated file path in site output manifest: ${path}`)
726
+ }
727
+ const key = file.toLocaleLowerCase("en-US")
728
+ if (seen.has(key))
729
+ throw new TypeError(`Duplicate generated file in site output manifest: ${path}`)
730
+ seen.add(key)
731
+ await validateOutputFile(output, file.split("/").join(sep))
732
+ }
733
+ return manifest.files
734
+ }
735
+
736
+ async function removeGeneratedFile(output, file) {
737
+ const path = join(output, ...file.split("/"))
738
+ await rm(path, { force: true })
739
+ let directory = dirname(path)
740
+ while (directory !== output) {
741
+ try {
742
+ await rmdir(directory)
743
+ } catch (error) {
744
+ if (error.code === "ENOTEMPTY" || error.code === "EEXIST") break
745
+ if (error.code !== "ENOENT") throw error
746
+ }
747
+ directory = dirname(directory)
748
+ }
749
+ }
750
+
677
751
  async function ensureDirectoryPath(root, path) {
678
752
  if (path !== root && !path.startsWith(`${root}${sep}`)) {
679
753
  throw new Error(`Site output parent escapes project root: ${path}`)