uniweb 1.26.8 → 1.26.9

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.
@@ -44,7 +44,7 @@
44
44
 
45
45
  import { execFileSync } from 'node:child_process'
46
46
  import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs'
47
- import { basename, dirname, isAbsolute, join, posix, relative, resolve, sep } from 'node:path'
47
+ import { basename, dirname, join, posix, relative, resolve, sep } from 'node:path'
48
48
  import yaml from 'js-yaml'
49
49
 
50
50
  import {
@@ -80,6 +80,7 @@ import {
80
80
  siteTreeIn,
81
81
  unmergedPaths
82
82
  } from '../utils/sync-point.js'
83
+ import { submoduleMountRoots, writableMounts, refusedMountLines } from '../utils/site-roots.js'
83
84
  import { aheadBehind, remoteMoved, upstreamOf } from '../utils/git.js'
84
85
  import { mountedRepositories, askRemote, standing, mountName, short } from '../utils/mounted-repositories.js'
85
86
  import { getLatestVersion, isBehind } from '../utils/update-check.js'
@@ -304,21 +305,12 @@ function otherUncommitted(siteDir, repo, mountedTops = []) {
304
305
  .map((abs) => relative(siteDir, abs) || '.')
305
306
  }
306
307
 
307
- /**
308
- * The folders this site mounts from elsewhere in its repository, relative to the site: a push
309
- * sends their pages as they are on disk, so they wait to be pushed like the site's own.
310
- *
311
- * ⛔ Until 2026-10-31 `status` read the site's own folder alone: it said "nothing waiting" over
312
- * four commits of mounted docs, and the push that followed sent them. A mount in a repository of
313
- * its own is a submodule's pin here, and reads as changed when the pin moves; one outside this
314
- * repository has no commit to compare with, and is left out.
315
- */
316
- async function mountedRoots(siteDir, repo) {
317
- const { mountEntriesOf } = await import('@uniweb/build/site')
318
- return mountEntriesOf(readSiteYml(siteDir)?.paths)
319
- .map(([, dir]) => dir)
320
- .filter((dir) => !isAbsolute(dir) && !posix.normalize(`${repo.prefix}${dir}`).startsWith('..'))
321
- }
308
+ // ⭐ What waits to be pushed is read under the roots a push sends from — a mounted folder the site's
309
+ // repository holds among them (`sentRoots`) — and the submodules that hold a mounted folder, whose
310
+ // pin reads as changed when it moves (`submoduleMountRoots`, `utils/site-roots.js`). A mount outside
311
+ // this repository has no commit to compare with, and is left out. ⛔ Until 2026-10-31 `status` read
312
+ // the site's own folder alone: it said "nothing waiting" over four commits of mounted docs, and the
313
+ // push that followed sent them.
322
314
 
323
315
  /**
324
316
  * Git: conflicts, a merge waiting, what is not committed, and the git remote.
@@ -408,7 +400,7 @@ async function gitChecks(siteDir, { offline, origin, mounts = [] }) {
408
400
  let localFiles = null
409
401
  const point = readSyncPoint(siteDir, origin)
410
402
  if (repo.head && point && isAncestor(repo, point) && siteTreeIn(repo, point)) {
411
- localFiles = filesDifferFrom(repo, point, [...sentRoots(siteDir), ...(await mountedRoots(siteDir, repo))])
403
+ localFiles = filesDifferFrom(repo, point, [...sentRoots(siteDir), ...submoduleMountRoots(siteDir)])
412
404
  }
413
405
 
414
406
  const upstream = upstreamOf(siteDir)
@@ -912,7 +904,7 @@ function changeChecks(siteDir, changes, unpushedFiles, proposals, { elsewhere =
912
904
  * `app.changes` from the pull's own answer (`probePull`): the files it would change, each
913
905
  * as the pull would write it.
914
906
  */
915
- function probedChanges({ files: list, recordsNotPlaced, noBase, movedFrom, itemsBehind = 0 }, unpushedFiles, proposals) {
907
+ function probedChanges({ files: list, recordsNotPlaced, noBase, movedFrom, itemsBehind = 0, mountedMoved = [], held = [] }, unpushedFiles, proposals) {
916
908
  if (noBase && movedFrom) {
917
909
  if (!proposals.some((p) => p.step === 'pull')) {
918
910
  proposals.push({ step: 'pull', command: 'npx uniweb pull', why: 'record where the site is now' })
@@ -922,15 +914,22 @@ function probedChanges({ files: list, recordsNotPlaced, noBase, movedFrom, items
922
914
  if (noBase) return noBaseChanges(list, recordsNotPlaced)
923
915
  // ⛔ Until 2026-10-31 this said nothing changed, and the push that followed was refused: the
924
916
  // backend held newer versions of a mounted folder's pages, which a pull does not write.
925
- if (!list.length && itemsBehind) {
917
+ if (!list.length && (itemsBehind || mountedMoved.length || held.length)) {
926
918
  proposals.push({ step: 'pull', command: 'npx uniweb pull', why: 'record what moved on the backend' })
919
+ // ⭐ Named, page by page, where the pull can say which (`mountedMoved`, `held`): a mounted page the
920
+ // app changed, which the next push puts back, and a page whose file cannot say what the app made of it.
921
+ const named = [
922
+ mountedMoved.length && `${count(mountedMoved.length, 'mounted page')} changed in the app, which the next push puts back: ${mountedMoved.map((r) => `/${r}`).join(', ')}`,
923
+ held.length && `${count(held.length, 'page')} whose files cannot say what the app made of them, kept on the site as the app has them: ${held.map((h) => `/${h.route}`).join(', ')}`,
924
+ ].filter(Boolean)
927
925
  return [
928
926
  {
929
927
  id: 'app.changes',
930
928
  state: 'attention',
931
- summary:
932
- 'The backend moved with no file here to show it: a push from another copy, or pages from a mounted folder. A pull changes no file and records that, and a push before it may be refused.',
933
- detail: { source: 'pull', files: [], itemsBehind }
929
+ summary: named.length
930
+ ? `The backend moved with no file here to show it — ${named.join('; ')}. A pull changes no file and records that.`
931
+ : 'The backend moved with no file here to show it: a push from another copy, or pages from a mounted folder. A pull changes no file and records that, and a push before it may be refused.',
932
+ detail: { source: 'pull', files: [], itemsBehind, mountedMoved, held }
934
933
  }
935
934
  ]
936
935
  }
@@ -1282,32 +1281,45 @@ async function projectChecks(siteDir, siteYml) {
1282
1281
  }
1283
1282
  })
1284
1283
 
1285
- // ⭐ Pages from outside the site's folder (`paths:` mounts, by the build's own rule): a pull
1286
- // never writes them (`uwx/git-authored.js`), and one in its own repository is edited there.
1284
+ // ⭐ Pages from outside the site's folder (`paths:` mounts, by the build's own rule): a pull writes
1285
+ // one the site marks `writable: true` where this copy can merge it (`writableMounts`), and no other
1286
+ // (`uwx/page-files.js`); one in its own repository is edited there.
1287
1287
  const { mountEntriesOf } = await import('@uniweb/build/site')
1288
1288
  const siteRepo = repoOf(siteDir)?.top ?? null
1289
+ const { writable, refused } = writableMounts(siteDir)
1289
1290
  const mounts = mountEntriesOf(siteYml?.paths).map(([segment, dir]) => {
1290
1291
  const abs = resolve(siteDir, String(dir))
1291
1292
  const exists = existsSync(abs)
1292
1293
  const top = exists ? repoOf(abs)?.top ?? null : null
1293
1294
  const repository = !exists ? null : !top ? 'none' : top === siteRepo ? 'same' : 'other'
1294
- return { segment, dir: String(dir), exists, repository }
1295
+ return { segment, dir: String(dir), exists, repository, writable: writable.includes(segment) }
1295
1296
  })
1296
1297
  const mountLine = (m) =>
1297
1298
  !m.exists
1298
1299
  ? `pages/${m.segment} names ${m.dir}, which does not exist.`
1299
1300
  : m.repository === 'other'
1300
- ? `pages/${m.segment} comes from ${m.dir}, its own git repository — edits there are commits in it.`
1301
- : `pages/${m.segment} comes from ${m.dir}, outside the site's folder.`
1301
+ ? m.writable
1302
+ ? `pages/${m.segment} comes from ${m.dir}, its own git repository — a pull writes the app's edits there as commits, which you push there before this repository.`
1303
+ : `pages/${m.segment} comes from ${m.dir}, its own git repository — edits there are commits in it.`
1304
+ : m.writable
1305
+ ? `pages/${m.segment} comes from ${m.dir}, outside the site's folder — a pull writes the app's edits into it.`
1306
+ : `pages/${m.segment} comes from ${m.dir}, outside the site's folder.`
1307
+ // The pages an app edit to does not come back to: each mount but a writable one, and one a pull refuses.
1308
+ const readOnly = mounts.filter((m) => !m.writable && !refused.some((r) => r.segment === m.segment))
1302
1309
  checks.push(
1303
1310
  mounts.length
1304
1311
  ? {
1305
1312
  id: 'project.mounts',
1306
- state: mounts.some((m) => !m.exists) ? 'attention' : 'ok',
1313
+ state: mounts.some((m) => !m.exists) || refused.length ? 'attention' : 'ok',
1307
1314
  summary: mounts.length === 1 ? mountLine(mounts[0]) : `${count(mounts.length, 'page folder')} come from outside the site's folder.`,
1308
1315
  lines: [
1309
1316
  ...(mounts.length === 1 ? [] : mounts.map(mountLine)),
1310
- 'An edit made in the app to those pages does not come back on a pull, and the next push replaces it with the files.'
1317
+ ...refused.flatMap(refusedMountLines),
1318
+ ...(readOnly.length
1319
+ ? [
1320
+ `An edit made in the app to ${readOnly.length === mounts.length ? 'those pages' : `the pages of ${readOnly.map((m) => `pages/${m.segment}`).join(', ')}`} does not come back on a pull, and the next push replaces it with the files.`
1321
+ ]
1322
+ : [])
1311
1323
  ],
1312
1324
  detail: { mounts }
1313
1325
  }
@@ -1375,7 +1387,7 @@ function ciWorkflow(repo, host) {
1375
1387
  function siteCommitsSince(repo, sha, siteDir) {
1376
1388
  try {
1377
1389
  if (!isAncestor(repo, sha)) return null
1378
- const roots = sentRoots(siteDir).map((r) => `${repo.prefix}${r}`)
1390
+ const roots = sentRoots(siteDir).map((r) => posix.normalize(`${repo.prefix}${r}`))
1379
1391
  const out = execFileSync('git', ['rev-list', '--count', `${sha}..HEAD`, '--', ...roots], {
1380
1392
  cwd: repo.top,
1381
1393
  encoding: 'utf8',
@@ -1,15 +1,15 @@
1
1
  {
2
2
  "schemaVersion": 1,
3
- "generatedAt": "2026-11-04T16:56:34.431Z",
3
+ "generatedAt": "2026-11-04T20:40:44.545Z",
4
4
  "packages": {
5
5
  "@uniweb/api": {
6
6
  "version": "1.6.9"
7
7
  },
8
8
  "@uniweb/build": {
9
- "version": "1.15.5"
9
+ "version": "1.15.6"
10
10
  },
11
11
  "@uniweb/content-reader": {
12
- "version": "1.5.8"
12
+ "version": "1.5.9"
13
13
  },
14
14
  "@uniweb/content-writer": {
15
15
  "version": "1.3.1"
@@ -42,7 +42,7 @@
42
42
  "version": "1.0.1"
43
43
  },
44
44
  "@uniweb/schemas": {
45
- "version": "1.6.7"
45
+ "version": "1.6.8"
46
46
  },
47
47
  "@uniweb/scholar": {
48
48
  "version": "1.1.0"
package/src/utils/git.js CHANGED
@@ -22,68 +22,11 @@
22
22
  */
23
23
 
24
24
  import { execFile, execFileSync } from 'node:child_process'
25
- import { readFileSync } from 'node:fs'
26
- import { join } from 'node:path'
27
- import yaml from 'js-yaml'
28
25
  import { repoOf } from './sync-point.js'
29
26
 
30
- /**
31
- * The files and directories `uniweb pull` writes into.
32
- *
33
- * ONE definition, shared by the guard that stops a pull outside git and the merge a
34
- * pull makes in a repository, whose sync points hold exactly these (and `public/`, where
35
- * the media land) — a second copy would drift, and a stale list here means a guard that
36
- * misses or a merge that drops a file. `paths:` can relocate the content roots, so the
37
- * local site.yml is consulted rather than assuming the defaults.
38
- */
39
- export function siteContentRoots(siteDir) {
40
- // ⚠️ `queries.yml` IS AT THE SITE ROOT, and that is why it must be named here.
41
- // Its predecessor lived at `collections/collections.yml`, so the `collections`
42
- // root added below already covered it and the bare `'collections.yml'` entry
43
- // that used to sit in this list resolved to a path no site ever had. A
44
- // root-level file has no directory entry standing in for it.
45
- //
46
- // The records folder's `folder.yml` is the reverse case: it lives in the records
47
- // directory, which is a root below, so it needs no entry. (It was `records.yml` at
48
- // the site root until 2026-09-21, and was listed here then.)
49
- const roots = new Set([
50
- 'site.yml',
51
- 'theme.yml',
52
- 'head.html',
53
- 'queries.yml',
54
- 'locales'
55
- ])
56
- let paths = {}
57
- try {
58
- paths =
59
- yaml.load(readFileSync(join(siteDir, 'site.yml'), 'utf8'))?.paths || {}
60
- } catch {
61
- /* no or unreadable site.yml — the defaults are right */
62
- }
63
- roots.add(paths.pages || 'pages')
64
- roots.add(paths.layout || 'layout')
65
- roots.add(paths.records || 'records')
66
- return [...roots]
67
- }
68
-
69
- /**
70
- * What each lane of a push carries, as roots: the folder lane the records — their files,
71
- * and their translations — and the content lane everything else a pull writes, media
72
- * (`public/`) included. ⚠️ `locales` stays whole on the content side, record translations
73
- * with it: a root too many there costs a less precise base, never a wrong one.
74
- */
75
- export function siteLaneRoots(siteDir) {
76
- let records = 'records'
77
- try {
78
- records = yaml.load(readFileSync(join(siteDir, 'site.yml'), 'utf8'))?.paths?.records || 'records'
79
- } catch {
80
- /* no or unreadable site.yml — the default is right */
81
- }
82
- return {
83
- content: [...siteContentRoots(siteDir), 'public'].filter((r) => r !== records),
84
- records: [records, 'locales/records']
85
- }
86
- }
27
+ // The roots a pull writes into and a push sends from are the build's to say, mounts included: they
28
+ // moved to `utils/site-roots.js` on 2026-11-04, which may import `@uniweb/build` — this module may not,
29
+ // since the CLI's entry loads it.
87
30
 
88
31
  function git(args, cwd) {
89
32
  return execFileSync('git', args, {
@@ -5,7 +5,7 @@
5
5
  * build or a push reads that folder as it is on disk. When the folder is its own git repository
6
6
  * (a submodule of the site's, or a clone beside it), nothing else keeps it current. A `git pull`
7
7
  * of the site moves a submodule's recorded commit and leaves its checkout where it was. A
8
- * `uniweb pull` never writes into a mount (`uwx/git-authored.js`), so after a teammate pushes
8
+ * `uniweb pull` never writes into a mount (`uwx/page-files.js`), so after a teammate pushes
9
9
  * newer docs, a pull here takes nothing and the next push puts the older ones back. So `status`
10
10
  * says where each one stands, `refresh` takes its latest, and a push or publish stops before
11
11
  * sending one that is not current (`backend/mounts-before-sending.js`).
@@ -23,7 +23,7 @@
23
23
  */
24
24
 
25
25
  import { execFile, execFileSync } from 'node:child_process'
26
- import { existsSync, readFileSync } from 'node:fs'
26
+ import { existsSync, readFileSync, realpathSync } from 'node:fs'
27
27
  import { join, relative, resolve, sep } from 'node:path'
28
28
  import yaml from 'js-yaml'
29
29
  import { repoOf } from './sync-point.js'
@@ -120,6 +120,37 @@ function submoduleHolding(siteTop, abs) {
120
120
  return null
121
121
  }
122
122
 
123
+ /**
124
+ * Which repository holds a mounted folder, as `mountedRepositories` tells them apart:
125
+ * - `site`: the site's own repository holds its files — they are the site's roots (`utils/site-roots.js`);
126
+ * - `submodule`: a submodule of the site's repository holds it, `submodule` naming which;
127
+ * - `own`: a repository the site's does not record — a clone beside the site;
128
+ * - `none`: no repository holds it, or the site is in none.
129
+ *
130
+ * @param {string} siteDir
131
+ * @param {string} abs - the mounted folder
132
+ * @returns {{ kind: 'site'|'submodule'|'own'|'none', submodule?: { path: string, recorded: string } }}
133
+ */
134
+ export function mountHolding(siteDir, abs) {
135
+ const site = repoOf(siteDir)
136
+ if (!site) return { kind: 'none' }
137
+ const sub = site.head ? submoduleHolding(site.top, realOf(abs)) : null
138
+ if (sub) return { kind: 'submodule', submodule: sub }
139
+ const own = existsSync(abs) ? repoOf(abs) : null
140
+ if (!own) return { kind: 'none' }
141
+ return { kind: own.top === site.top ? 'site' : 'own' }
142
+ }
143
+
144
+ // git names a repository's top by its real path, and a mount may be reached through a link (a
145
+ // temporary folder on macOS is): compared as written, it would read as outside the repository.
146
+ function realOf(abs) {
147
+ try {
148
+ return realpathSync(abs)
149
+ } catch {
150
+ return abs // not there: compared as written
151
+ }
152
+ }
153
+
123
154
  /** The submodule's name in `.gitmodules`, and the branch it is told to follow there. */
124
155
  function gitmodulesEntry(siteTop, path) {
125
156
  const out = text(siteTop, ['config', '-f', '.gitmodules', '--get-regexp', '^submodule\\..*\\.path$'])
@@ -171,13 +202,14 @@ export async function mountedRepositories(siteDir) {
171
202
  const out = []
172
203
  for (const [segment, dir] of entries) {
173
204
  const abs = resolve(siteDir, String(dir))
174
- const sub = site?.head ? submoduleHolding(site.top, abs) : null
205
+ // ⭐ The one classification `mountHolding` gives — a submodule's mount among them.
206
+ const sub = site?.head ? submoduleHolding(site.top, realOf(abs)) : null
175
207
  const own = existsSync(abs) ? repoOf(abs) : null
176
208
  // A submodule never checked out is an empty folder of the site's own repository.
177
209
  const top = own && own.top !== site?.top ? own.top : null
178
210
  // In the site's own repository, or in none: the site's own git says all there is.
179
211
  if (!sub && !top) continue
180
- const prefix = slash(relative(top ?? join(site.top, sub.path), abs))
212
+ const prefix = slash(relative(top ?? join(site.top, sub.path), realOf(abs)))
181
213
  const head = top ? text(top, ['rev-parse', 'HEAD']) : null
182
214
  let edits = []
183
215
  if (top) {
@@ -294,14 +326,20 @@ function siteName(siteDir) {
294
326
  * uncommitted changes in it, never backwards, and never from a commit the branch does not
295
327
  * contain, which is a pin put somewhere else on purpose.
296
328
  *
329
+ * ⭐ `merge`: a mount a pull writes into (`writable: true`) takes its latest as the site takes its own,
330
+ * by merging, since the pull's commits there are its own work: a checkout that has them and a branch
331
+ * that moved on have each moved, and the merge of the two is its latest. Never a rebase, which would
332
+ * replay a pull's commit onto another one while the site's sync point still names it.
333
+ *
297
334
  * @param {string} siteDir
298
335
  * @param {object} m - from `mountedRepositories`
336
+ * @param {{ merge?: boolean }} [opts]
299
337
  * @returns {Promise<{ from: string|null, to: string|null, branch: string|null, commits: number|null,
300
- * committed: string|null, left: string|null, kind: 'edits'|'no-remote'|'no-answer'|'ahead'|'off-branch'|null,
338
+ * committed: string|null, left: string|null, kind: 'edits'|'no-remote'|'no-answer'|'ahead'|'off-branch'|'conflict'|null,
301
339
  * error: string|null }>} `left` says why it stayed where it was, and `kind` names that reason;
302
340
  * `error`, what failed
303
341
  */
304
- export async function takeLatest(siteDir, m) {
342
+ export async function takeLatest(siteDir, m, { merge = false } = {}) {
305
343
  const r = { from: m.head, to: m.head, branch: null, commits: null, committed: null, left: null, kind: null, error: null }
306
344
  if (m.edits.length) return { ...r, left: `it has ${plural(m.edits.length, 'uncommitted change')}`, kind: 'edits' }
307
345
  let cur = m
@@ -338,18 +376,26 @@ export async function takeLatest(siteDir, m) {
338
376
  }
339
377
  contained = ancestor(cur.top, cur.head, answer.tip)
340
378
  }
341
- if (contained !== true) return { ...r, left: `${short(cur.head)} is not on ${answer.branch ?? 'its branch'}`, kind: 'off-branch' }
379
+ if (contained !== true && !merge) return { ...r, left: `${short(cur.head)} is not on ${answer.branch ?? 'its branch'}`, kind: 'off-branch' }
342
380
  try {
343
- if (cur.follows.upstream) run(cur.top, ['merge', '--ff-only', '--quiet', answer.tip])
381
+ if (contained !== true) {
382
+ // Each has moved: the checkout's commits — a pull's among them — merged with the branch's.
383
+ try {
384
+ run(cur.top, ['merge', '--no-edit', '--no-verify', '--quiet', '-m', `Merge ${answer.branch ?? 'the latest'} of ${label}`, answer.tip])
385
+ } catch {
386
+ return { ...r, left: `its merge with ${answer.branch ?? 'its branch'} of ${label} has conflicts: settle them there and commit, then record it here`, kind: 'conflict' }
387
+ }
388
+ } else if (cur.follows.upstream) run(cur.top, ['merge', '--ff-only', '--quiet', answer.tip])
344
389
  else run(cur.top, ['checkout', '--quiet', '--detach', answer.tip])
345
390
  } catch (err) {
346
391
  return { ...r, error: gitError(err, 'git could not move it') }
347
392
  }
348
- r.to = answer.tip
393
+ const tip = contained === true ? answer.tip : text(cur.top, ['rev-parse', 'HEAD'])
394
+ r.to = tip
349
395
  const n = Number(text(cur.top, ['rev-list', '--count', `${cur.head}..${answer.tip}`]))
350
396
  r.commits = Number.isInteger(n) ? n : null
351
397
  if (cur.submodule) {
352
- const message = `${siteName(siteDir)}: pages/${cur.segment} from ${cur.submodule.path} at ${short(answer.tip)}, the latest of ${label}`
398
+ const message = `${siteName(siteDir)}: pages/${cur.segment} from ${cur.submodule.path} at ${short(tip)}, ${contained === true ? 'the latest' : 'merged with the latest'} of ${label}`
353
399
  try {
354
400
  run(cur.siteTop, ['commit', '-q', '-m', message, '--', cur.submodule.path])
355
401
  r.committed = text(cur.siteTop, ['rev-parse', 'HEAD'])
@@ -0,0 +1,157 @@
1
+ /**
2
+ * The places a push sends a site's content from and a pull merges it into, each relative to the site.
3
+ *
4
+ * ⭐ ONE ANSWER, the build's (`siteContentRoots`, `@uniweb/build/uwx`): the site's config, its
5
+ * translations, its pages, layout and records where `paths:` puts them, its media, and each mounted
6
+ * page folder. A mount the site's own repository holds is one of the site's roots like any other —
7
+ * committed with it, sent from it, merged into it. A mount that is a repository of its own is held to
8
+ * its commits by `backend/mounts-before-sending.js`, and one in no repository has none to be held to
9
+ * (`mountHolding`, `utils/mounted-repositories.js`).
10
+ *
11
+ * ⛔ Until 2026-11-04 the CLI kept a list of its own (`siteContentRoots` in `utils/git.js`) that named
12
+ * no mount: a push sent a same-repository mount's uncommitted edits and named HEAD as what it sent,
13
+ * while `uniweb status` counted that folder among what waits to be pushed.
14
+ *
15
+ * Project-local: it imports `@uniweb/build`, which a global `uniweb` may lack, so nothing the CLI's
16
+ * entry loads statically imports this.
17
+ */
18
+ import { readFileSync, realpathSync } from 'node:fs'
19
+ import { isAbsolute, join, posix, relative, resolve, sep } from 'node:path'
20
+ import yaml from 'js-yaml'
21
+ import { siteContentRoots } from '@uniweb/build/uwx'
22
+ import { mountDeclarationsOf } from '@uniweb/build/site'
23
+ import { mountHolding } from './mounted-repositories.js'
24
+ import { repoOf } from './sync-point.js'
25
+
26
+ function readSiteYml(siteDir) {
27
+ try {
28
+ return yaml.load(readFileSync(join(siteDir, 'site.yml'), 'utf8')) || {}
29
+ } catch {
30
+ return {} // no or unreadable site.yml — the defaults are right
31
+ }
32
+ }
33
+
34
+ const rootsOf = (siteDir) => siteContentRoots(siteDir, readSiteYml(siteDir))
35
+
36
+ /**
37
+ * The site's roots: its own, and each mount its repository holds. `media: false` leaves out
38
+ * `public/`, for what a pull writes over outside git.
39
+ *
40
+ * @param {string} siteDir
41
+ * @param {{ media?: boolean }} [opts]
42
+ * @returns {string[]}
43
+ */
44
+ export function siteRoots(siteDir, { media = true } = {}) {
45
+ return rootsOf(siteDir)
46
+ .filter((r) => (r.kind === 'media' ? media : r.kind !== 'mount' || mountHolding(siteDir, r.abs).kind === 'site'))
47
+ .map((r) => r.path)
48
+ }
49
+
50
+ /** What a push sends from the files: the site's roots, its media among them. */
51
+ export const sentRoots = (siteDir) => siteRoots(siteDir)
52
+
53
+ /**
54
+ * What each lane of a push carries, as roots: the folder lane the records — their files, and their
55
+ * translations — and the content lane everything else a pull writes, media (`public/`) and the
56
+ * mounts the site's repository holds included. ⚠️ `locales` stays whole on the content side, record
57
+ * translations with it: a root too many there costs a less precise base, never a wrong one.
58
+ */
59
+ export function laneRoots(siteDir) {
60
+ const records = rootsOf(siteDir).find((r) => r.kind === 'records').path
61
+ return {
62
+ content: siteRoots(siteDir).filter((r) => r !== records),
63
+ records: [records, 'locales/records'],
64
+ }
65
+ }
66
+
67
+ /**
68
+ * The submodules that hold a mounted folder, each by its path relative to the site: the commit the
69
+ * site's repository records for one is the site's to commit, and `uniweb status` reads a pin that
70
+ * moved as a change waiting. ⭐ The submodule's own path, not the mounted folder's — a folder inside
71
+ * one is no path of the site's repository (`unipress/docs` is in the `unipress` submodule).
72
+ */
73
+ export function submoduleMountRoots(siteDir) {
74
+ const repo = repoOf(siteDir)
75
+ if (!repo) return []
76
+ const out = new Set()
77
+ for (const r of rootsOf(siteDir)) {
78
+ if (r.kind !== 'mount') continue
79
+ const held = mountHolding(siteDir, r.abs)
80
+ if (held.kind !== 'submodule') continue
81
+ out.add(posix.relative(repo.prefix.replace(/\/$/, '') || '.', held.submodule.path) || '.')
82
+ }
83
+ return [...out]
84
+ }
85
+
86
+ /**
87
+ * ⭐ THE MOUNTS A PULL MAY WRITE INTO: each one the site declares
88
+ * `writable: true` that this copy can merge —
89
+ * - a folder the site's own repository holds, whose files are among the site's roots;
90
+ * - a folder in a submodule of the site's repository (`submodules`), merged in the submodule's own
91
+ * history: git's own pairing of the two, since every commit of the site names the submodule's;
92
+ * - for a site in no repository, a folder inside the site's own, which a pull writes as it writes the
93
+ * site's files.
94
+ * Every other one is refused, with why:
95
+ * - `own`: a repository of its own that the site's does not record — a clone beside the site;
96
+ * - `none`: no repository holds it, or one ignores it, or it is outside the folder of a site in none;
97
+ * - `unchecked`: a submodule that is not checked out.
98
+ * The first two have no history that pairs them with the site's, so no copy of the site — a
99
+ * teammate's, CI's — could find what it last merged from.
100
+ *
101
+ * @param {string} siteDir
102
+ * @returns {{ writable: string[], refused: Array<{ segment: string, path: string, why: 'own'|'none'|'unchecked' }>,
103
+ * submodules: Array<{ segment: string, at: string, root: string, repo: object }> }} `submodules`: each
104
+ * writable mount in one, with the submodule's path from the site repository's top, the mounted
105
+ * folder's path in it, and its `repoOf`
106
+ */
107
+ export function writableMounts(siteDir) {
108
+ const repo = repoOf(siteDir)
109
+ const writable = []
110
+ const refused = []
111
+ const submodules = []
112
+ for (const { segment, path, writable: declared } of mountDeclarationsOf(readSiteYml(siteDir).paths)) {
113
+ if (!declared || typeof path !== 'string' || !path) continue
114
+ const abs = resolve(siteDir, path)
115
+ let why = null
116
+ if (repo) {
117
+ const held = mountHolding(siteDir, abs)
118
+ if (held.kind === 'submodule') {
119
+ const top = join(repo.top, held.submodule.path)
120
+ const sub = repoOf(top)
121
+ if (!sub?.head) why = 'unchecked'
122
+ else submodules.push({ segment, at: held.submodule.path, root: relative(realOf(top), realOf(abs)).split(sep).join('/') || '.', repo: sub })
123
+ } else if (held.kind !== 'site') why = held.kind
124
+ } else {
125
+ const rel = relative(siteDir, abs)
126
+ if (!rel || isAbsolute(rel) || rel === '..' || rel.startsWith(`..${posix.sep}`) || rel.startsWith('..\\')) why = 'none'
127
+ }
128
+ if (why) refused.push({ segment, path, why })
129
+ else writable.push(segment)
130
+ }
131
+ return { writable, refused, submodules }
132
+ }
133
+
134
+ const realOf = (p) => {
135
+ try {
136
+ return realpathSync(p)
137
+ } catch {
138
+ return p
139
+ }
140
+ }
141
+
142
+ /** What to tell a person about a mount a pull may not write, and what settles it. */
143
+ export function refusedMountLines({ segment, path, why }) {
144
+ if (why === 'unchecked') {
145
+ return [
146
+ `pages/${segment} is mounted from ${path} with \`writable: true\`, but its submodule is not checked out, so a pull has nothing to merge into.`,
147
+ ' Check it out (`git submodule update --init`), or run `npx uniweb refresh`, then pull.'
148
+ ]
149
+ }
150
+ const what = why === 'own' ? `a repository of its own, which the site's repository does not record` : `a folder no history of the site's holds`
151
+ return [
152
+ `pages/${segment} is mounted from ${path} with \`writable: true\`, but it is ${what}, so a pull cannot merge into it.`,
153
+ why === 'own'
154
+ ? " Make it a submodule of the site's repository, or remove `writable: true` to keep it read-only."
155
+ : " Make it part of the site's repository (or a folder of the site, outside one), or remove `writable: true` to keep it read-only."
156
+ ]
157
+ }