@uniweb/build 0.78.0 → 0.79.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.
@@ -1,79 +0,0 @@
1
- // Surgical, comment-preserving upsert of a TOP-LEVEL scalar key in a YAML file.
2
- //
3
- // `site.yml` and `collections.yml` are hand-authored — they carry comments and an
4
- // author-chosen key order. Round-tripping them through js-yaml (load → dump) would
5
- // discard every comment and re-flow the file. The sync back-fill only ever needs to
6
- // write ONE machine-owned scalar (the entity `$uuid`) into such a file, so we edit
7
- // the single line in place and leave the rest byte-for-byte.
8
- //
9
- // Scope, stated: top-level (column-0) scalar keys only — which is all `$uuid` ever
10
- // is. Not a general YAML writer; do not reach for it to set nested or list values.
11
-
12
- import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'node:fs'
13
- import { dirname } from 'node:path'
14
-
15
- // A top-level `key:` line (column 0, no leading space), capturing any inline value.
16
- function topLevelKeyLine(key) {
17
- // Escape regex metachars in the key (`$uuid` contains `$`).
18
- const esc = key.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
19
- return new RegExp(`^${esc}:[^\\n]*$`, 'm')
20
- }
21
-
22
- /**
23
- * Set `key: value` at the top level of the YAML file at `filePath`, preserving all
24
- * other lines (comments included). If the key already exists at column 0, its line
25
- * is replaced; otherwise a new line is prepended (machine-owned identity reads
26
- * cleanest at the top). The file (and its directory) is created if absent.
27
- *
28
- * @param {string} filePath
29
- * @param {string} key - a top-level scalar key (e.g. `$uuid`)
30
- * @param {string} value - the scalar value, written verbatim (uuids need no quoting)
31
- * @returns {boolean} true if the file changed
32
- */
33
- export function upsertYamlScalar(filePath, key, value) {
34
- const line = `${key}: ${value}`
35
- let text = ''
36
- if (existsSync(filePath)) text = readFileSync(filePath, 'utf8')
37
-
38
- const re = topLevelKeyLine(key)
39
- let next
40
- if (re.test(text)) {
41
- next = text.replace(re, line)
42
- } else if (text.length === 0) {
43
- next = line + '\n'
44
- } else {
45
- // Prepend, keeping the rest intact (and a trailing newline if the file lacked one).
46
- const body = text.endsWith('\n') ? text : text + '\n'
47
- next = line + '\n' + body
48
- }
49
- if (next === text) return false
50
- mkdirSync(dirname(filePath), { recursive: true })
51
- writeFileSync(filePath, next)
52
- return true
53
- }
54
-
55
- /**
56
- * Remove a TOP-LEVEL scalar key from the YAML file at `filePath`, preserving every
57
- * other line (comments included) — the inverse of `upsertYamlScalar`, with the same
58
- * scope: one `key: value` line at column 0. A key followed by indented lines, or
59
- * opening a block scalar (`|` / `>`), is not a one-line scalar and is left alone
60
- * rather than half-removed.
61
- *
62
- * @param {string} filePath
63
- * @param {string} key - a top-level scalar key (e.g. `preview`)
64
- * @returns {boolean} true if the file changed
65
- */
66
- export function removeYamlScalar(filePath, key) {
67
- if (!existsSync(filePath)) return false
68
- const lines = readFileSync(filePath, 'utf8').split('\n')
69
- const esc = key.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
70
- const re = new RegExp(`^${esc}:(.*)$`)
71
- const at = lines.findIndex((l) => re.test(l))
72
- if (at === -1) return false
73
- const inline = lines[at].replace(re, '$1').trim()
74
- const continues = at + 1 < lines.length && /^[ \t]+\S/.test(lines[at + 1])
75
- if (continues || /^[|>]/.test(inline)) return false
76
- lines.splice(at, 1)
77
- writeFileSync(filePath, lines.join('\n'))
78
- return true
79
- }