@thegetty/quire-cli 1.0.0-rc.37 → 1.0.0-rc.38

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 (85) hide show
  1. package/package.json +3 -2
  2. package/src/commands/build.js +13 -6
  3. package/src/commands/clean.js +10 -2
  4. package/src/commands/clean.spec.js +10 -0
  5. package/src/commands/clean.test.js +122 -0
  6. package/src/commands/config.js +3 -1
  7. package/src/commands/config.test.js +42 -1
  8. package/src/commands/doctor.js +251 -0
  9. package/src/commands/doctor.spec.js +115 -0
  10. package/src/commands/doctor.test.js +1409 -0
  11. package/src/commands/epub.js +9 -2
  12. package/src/commands/info.js +79 -71
  13. package/src/commands/info.spec.js +8 -0
  14. package/src/commands/info.test.js +173 -76
  15. package/src/commands/pdf.js +10 -3
  16. package/src/commands/validate.js +39 -7
  17. package/src/commands/validate.spec.js +8 -0
  18. package/src/commands/validate.test.js +144 -0
  19. package/src/lib/README.md +77 -22
  20. package/src/lib/conf/README.md +6 -0
  21. package/src/lib/conf/build-status.js +103 -0
  22. package/src/lib/conf/build-status.test.js +247 -0
  23. package/src/lib/conf/defaults.js +14 -0
  24. package/src/lib/conf/format.js +1 -1
  25. package/src/lib/conf/schema.js +46 -1
  26. package/src/lib/constants.js +28 -0
  27. package/src/lib/doctor/README.md +667 -0
  28. package/src/lib/doctor/checks/environment/cli-version.js +62 -0
  29. package/src/lib/doctor/checks/environment/cli-version.test.js +132 -0
  30. package/src/lib/doctor/checks/environment/git-available.js +77 -0
  31. package/src/lib/doctor/checks/environment/git-available.test.js +52 -0
  32. package/src/lib/doctor/checks/environment/index.js +13 -0
  33. package/src/lib/doctor/checks/environment/node-version.js +66 -0
  34. package/src/lib/doctor/checks/environment/node-version.test.js +17 -0
  35. package/src/lib/doctor/checks/environment/npm-available.js +66 -0
  36. package/src/lib/doctor/checks/environment/npm-available.test.js +52 -0
  37. package/src/lib/doctor/checks/environment/os-info.js +48 -0
  38. package/src/lib/doctor/checks/environment/os-info.test.js +81 -0
  39. package/src/lib/doctor/checks/environment/runtime-info.js +51 -0
  40. package/src/lib/doctor/checks/environment/runtime-info.test.js +132 -0
  41. package/src/lib/doctor/checks/outputs/epub-output.js +119 -0
  42. package/src/lib/doctor/checks/outputs/epub-output.test.js +277 -0
  43. package/src/lib/doctor/checks/outputs/index.js +10 -0
  44. package/src/lib/doctor/checks/outputs/pdf-output.js +144 -0
  45. package/src/lib/doctor/checks/outputs/pdf-output.test.js +377 -0
  46. package/src/lib/doctor/checks/outputs/stale-build.js +122 -0
  47. package/src/lib/doctor/checks/outputs/stale-build.test.js +282 -0
  48. package/src/lib/doctor/checks/project/data-files.js +56 -0
  49. package/src/lib/doctor/checks/project/data-files.test.js +125 -0
  50. package/src/lib/doctor/checks/project/dependencies.js +53 -0
  51. package/src/lib/doctor/checks/project/dependencies.test.js +71 -0
  52. package/src/lib/doctor/checks/project/index.js +11 -0
  53. package/src/lib/doctor/checks/project/quire-11ty.js +98 -0
  54. package/src/lib/doctor/checks/project/quire-11ty.test.js +170 -0
  55. package/src/lib/doctor/checks/project/quire-project.js +38 -0
  56. package/src/lib/doctor/checks/project/quire-project.test.js +47 -0
  57. package/src/lib/doctor/checks/tools/index.js +10 -0
  58. package/src/lib/doctor/checks/tools/pandoc-available.js +82 -0
  59. package/src/lib/doctor/checks/tools/pandoc-available.test.js +73 -0
  60. package/src/lib/doctor/checks/tools/prince-available.js +81 -0
  61. package/src/lib/doctor/checks/tools/prince-available.test.js +73 -0
  62. package/src/lib/doctor/constants.js +39 -0
  63. package/src/lib/doctor/formatDuration.js +108 -0
  64. package/src/lib/doctor/formatDuration.test.js +76 -0
  65. package/src/lib/doctor/formatters/human.js +257 -0
  66. package/src/lib/doctor/formatters/human.test.js +463 -0
  67. package/src/lib/doctor/formatters/index.js +8 -0
  68. package/src/lib/doctor/formatters/json.js +78 -0
  69. package/src/lib/doctor/formatters/json.test.js +174 -0
  70. package/src/lib/doctor/formatters/shared.js +129 -0
  71. package/src/lib/doctor/formatters/shared.test.js +194 -0
  72. package/src/lib/doctor/index.js +271 -0
  73. package/src/lib/doctor/index.test.js +797 -0
  74. package/src/lib/platform.js +95 -0
  75. package/src/lib/project/build.js +44 -25
  76. package/src/lib/project/build.test.js +30 -8
  77. package/src/lib/project/detect.js +16 -2
  78. package/src/lib/project/index.js +18 -2
  79. package/src/lib/project/output-paths.js +87 -0
  80. package/src/lib/project/output-paths.test.js +66 -0
  81. package/src/lib/project/paths.js +48 -0
  82. package/src/main.js +24 -2
  83. package/src/packageConfig.js +17 -0
  84. package/src/validators/validate-data-files.js +154 -0
  85. package/src/validators/validate-data-files.test.js +217 -0
@@ -0,0 +1,144 @@
1
+ /**
2
+ * PDF output check
3
+ *
4
+ * Checks for PDF output files using the same path resolution logic as the
5
+ * PDF generation command. Supports both default engine-named paths and
6
+ * config-aware custom output paths.
7
+ *
8
+ * @module lib/doctor/checks/outputs/pdf-output
9
+ */
10
+ import fs from 'node:fs'
11
+ import path from 'node:path'
12
+ import createDebug from '#debug'
13
+ import { getPdfOutputPaths } from '#lib/project/output-paths.js'
14
+ import { loadProjectConfig } from '#lib/project/config.js'
15
+ import config from '#lib/conf/config.js'
16
+ import { getStatus } from '#lib/conf/build-status.js'
17
+ import { DOCS_BASE_URL, resolveStaleThreshold } from '#lib/doctor/constants.js'
18
+ import { formatDuration } from '#lib/doctor/formatDuration.js'
19
+
20
+ const debug = createDebug('lib:doctor:pdf-output')
21
+
22
+ /**
23
+ * Load pdf config from the project's config.yaml
24
+ *
25
+ * Returns undefined if config cannot be loaded (not in a project, missing
26
+ * config file, no pdf section). The caller falls back to engine defaults.
27
+ *
28
+ * @returns {Promise<Object|undefined>}
29
+ */
30
+ async function loadPdfConfig() {
31
+ try {
32
+ const projectConfig = await loadProjectConfig()
33
+ return projectConfig.pdf
34
+ } catch {
35
+ debug('Could not load project config, using engine defaults only')
36
+ return undefined
37
+ }
38
+ }
39
+
40
+ /**
41
+ * Check if PDF output exists and is up to date with _site
42
+ *
43
+ * PDF is generated from _site, so if _site is newer than the PDF,
44
+ * the PDF is considered stale.
45
+ *
46
+ * Loads the project config to resolve config-aware PDF output paths,
47
+ * matching the same resolution logic as `lib/pdf/index.js`.
48
+ *
49
+ * @returns {Promise<import('../../index.js').CheckResult>}
50
+ */
51
+ export async function checkPdfOutput() {
52
+ const pdfConfig = await loadPdfConfig()
53
+
54
+ // Get all possible PDF paths (config-aware + engine defaults)
55
+ const candidatePaths = getPdfOutputPaths({ pdfConfig })
56
+ debug('candidate PDF paths: %o', candidatePaths)
57
+
58
+ // Find which PDF files actually exist
59
+ const existingPdfs = candidatePaths.filter((p) => fs.existsSync(p))
60
+
61
+ if (existingPdfs.length === 0) {
62
+ debug('No PDF files found, checking stored pdf status')
63
+
64
+ const status = getStatus(process.cwd(), 'pdf')
65
+ if (status?.status === 'failed') {
66
+ debug('Last pdf generation failed (stored status)')
67
+ return {
68
+ ok: false,
69
+ message: 'Last PDF generation failed',
70
+ remediation: `The last "quire pdf" run failed. Check the output for errors and try again.
71
+ • Run "quire pdf --debug" for detailed error output`,
72
+ docsUrl: `${DOCS_BASE_URL}/quire-commands/#pdf`,
73
+ }
74
+ }
75
+
76
+ return {
77
+ ok: true,
78
+ level: 'na',
79
+ message: 'No PDF output found',
80
+ remediation: `Run "quire pdf" to generate a PDF.
81
+ • If you already ran "quire pdf" and it failed, check the output for errors`,
82
+ docsUrl: `${DOCS_BASE_URL}/quire-commands/#pdf`,
83
+ }
84
+ }
85
+
86
+ // Use basenames for display
87
+ const existingNames = existingPdfs.map((p) => path.basename(p))
88
+ debug('found PDF files: %o', existingNames)
89
+
90
+ // Check if _site exists
91
+ if (!fs.existsSync('_site')) {
92
+ debug('_site directory not found')
93
+ return {
94
+ ok: true,
95
+ message: `${existingNames.join(', ')} exists (no _site to compare)`,
96
+ }
97
+ }
98
+
99
+ // Get _site last modified time
100
+ const { mtimeMs: siteLastModified } = fs.statSync('_site')
101
+ debug('_site lastModified: %d', siteLastModified)
102
+
103
+ // Check each PDF for staleness beyond the configured threshold
104
+ const thresholdMs = resolveStaleThreshold(config.get('staleThreshold'))
105
+ const staleFiles = []
106
+ let oldestPdfLastModified = Infinity
107
+
108
+ for (const pdfFile of existingPdfs) {
109
+ const { mtimeMs: pdfLastModified } = fs.statSync(pdfFile)
110
+ debug('%s lastModified: %d', path.basename(pdfFile), pdfLastModified)
111
+
112
+ const staleDelta = siteLastModified - pdfLastModified
113
+ if (staleDelta > thresholdMs) {
114
+ staleFiles.push(pdfFile)
115
+ if (pdfLastModified < oldestPdfLastModified) {
116
+ oldestPdfLastModified = pdfLastModified
117
+ }
118
+ }
119
+ }
120
+
121
+ if (staleFiles.length > 0) {
122
+ const staleDuration = formatDuration(siteLastModified - oldestPdfLastModified)
123
+ const staleNames = staleFiles.map((p) => path.basename(p))
124
+ const fileList = staleNames.join(', ')
125
+ return {
126
+ ok: false,
127
+ level: 'warn',
128
+ message: `${fileList} is ${staleDuration} older than _site`,
129
+ remediation: `Your PDF may not reflect recent build changes.
130
+ • Run "quire pdf" to regenerate the PDF
131
+ • Or delete the old PDF files and run "quire pdf"`,
132
+ docsUrl: `${DOCS_BASE_URL}/quire-commands/#pdf`,
133
+ }
134
+ }
135
+
136
+ // All PDFs are up to date
137
+ const fileList = existingNames.join(', ')
138
+ return {
139
+ ok: true,
140
+ message: `${fileList} up to date`,
141
+ }
142
+ }
143
+
144
+ export default checkPdfOutput
@@ -0,0 +1,377 @@
1
+ import test from 'ava'
2
+ import path from 'node:path'
3
+ import sinon from 'sinon'
4
+ import esmock from 'esmock'
5
+
6
+ const TWO_MINUTES = 120000
7
+ const TEN_MINUTES = 600000
8
+ const ONE_HOUR = 3600000
9
+ const TWO_HOURS = 7200000
10
+
11
+ const configMock = { default: { get: () => 'HOURLY' } }
12
+ const buildStatusMock = { getStatus: () => undefined }
13
+
14
+ test.beforeEach((t) => {
15
+ t.context.sandbox = sinon.createSandbox()
16
+ })
17
+
18
+ test.afterEach.always((t) => {
19
+ t.context.sandbox.restore()
20
+ })
21
+
22
+ test('checkPdfOutput returns N/A when no PDF files exist', async (t) => {
23
+ const { sandbox } = t.context
24
+
25
+ const { checkPdfOutput } = await esmock('./pdf-output.js', {
26
+ 'node:fs': {
27
+ existsSync: sandbox.stub().returns(false),
28
+ },
29
+ '#lib/project/output-paths.js': {
30
+ getPdfOutputPaths: () => ['pagedjs.pdf', 'prince.pdf'],
31
+ },
32
+ '#lib/project/config.js': {
33
+ loadProjectConfig: async () => ({}),
34
+ },
35
+ '#lib/conf/config.js': configMock,
36
+ '#lib/conf/build-status.js': buildStatusMock,
37
+ })
38
+
39
+ const result = await checkPdfOutput()
40
+
41
+ t.true(result.ok)
42
+ t.is(result.level, 'na', 'should return N/A level when no PDF output')
43
+ t.regex(result.message, /No PDF output found/)
44
+ })
45
+
46
+ test('checkPdfOutput includes remediation when no output found', async (t) => {
47
+ const { sandbox } = t.context
48
+
49
+ const { checkPdfOutput } = await esmock('./pdf-output.js', {
50
+ 'node:fs': {
51
+ existsSync: sandbox.stub().returns(false),
52
+ },
53
+ '#lib/project/output-paths.js': {
54
+ getPdfOutputPaths: () => ['pagedjs.pdf', 'prince.pdf'],
55
+ },
56
+ '#lib/project/config.js': {
57
+ loadProjectConfig: async () => ({}),
58
+ },
59
+ '#lib/conf/config.js': configMock,
60
+ '#lib/conf/build-status.js': buildStatusMock,
61
+ })
62
+
63
+ const result = await checkPdfOutput()
64
+
65
+ t.truthy(result.remediation)
66
+ t.regex(result.remediation, /quire pdf/)
67
+ t.regex(result.remediation, /failed/)
68
+ t.truthy(result.docsUrl)
69
+ })
70
+
71
+ test('checkPdfOutput returns failure when last PDF generation failed and no output exists', async (t) => {
72
+ const { sandbox } = t.context
73
+
74
+ const { checkPdfOutput } = await esmock('./pdf-output.js', {
75
+ 'node:fs': {
76
+ existsSync: sandbox.stub().returns(false),
77
+ },
78
+ '#lib/project/output-paths.js': {
79
+ getPdfOutputPaths: () => ['pagedjs.pdf', 'prince.pdf'],
80
+ },
81
+ '#lib/project/config.js': {
82
+ loadProjectConfig: async () => ({}),
83
+ },
84
+ '#lib/conf/config.js': configMock,
85
+ '#lib/conf/build-status.js': {
86
+ getStatus: () => ({ status: 'failed', timestamp: Date.now() }),
87
+ },
88
+ })
89
+
90
+ const result = await checkPdfOutput()
91
+
92
+ t.false(result.ok)
93
+ t.is(result.level, undefined)
94
+ t.regex(result.message, /Last PDF generation failed/)
95
+ t.truthy(result.remediation)
96
+ t.regex(result.remediation, /quire pdf --debug/)
97
+ t.truthy(result.docsUrl)
98
+ })
99
+
100
+ test('checkPdfOutput returns ok when PDF exists but no _site', async (t) => {
101
+ const { sandbox } = t.context
102
+
103
+ const existsSync = sandbox.stub()
104
+ existsSync.withArgs('pagedjs.pdf').returns(true)
105
+ existsSync.withArgs('prince.pdf').returns(false)
106
+ existsSync.withArgs('_site').returns(false)
107
+
108
+ const { checkPdfOutput } = await esmock('./pdf-output.js', {
109
+ 'node:fs': {
110
+ existsSync,
111
+ },
112
+ '#lib/project/output-paths.js': {
113
+ getPdfOutputPaths: () => ['pagedjs.pdf', 'prince.pdf'],
114
+ },
115
+ '#lib/project/config.js': {
116
+ loadProjectConfig: async () => ({}),
117
+ },
118
+ '#lib/conf/config.js': configMock,
119
+ })
120
+
121
+ const result = await checkPdfOutput()
122
+
123
+ t.true(result.ok)
124
+ t.regex(result.message, /pagedjs\.pdf exists/)
125
+ t.regex(result.message, /no _site to compare/)
126
+ })
127
+
128
+ test('checkPdfOutput returns ok when PDF is up to date', async (t) => {
129
+ const { sandbox } = t.context
130
+
131
+ const now = Date.now()
132
+ const pdfTime = now // PDF is current
133
+ const siteTime = now - TEN_MINUTES // _site is older
134
+
135
+ const existsSync = sandbox.stub()
136
+ existsSync.withArgs('pagedjs.pdf').returns(true)
137
+ existsSync.withArgs('prince.pdf').returns(false)
138
+ existsSync.withArgs('_site').returns(true)
139
+
140
+ const statSync = sandbox.stub()
141
+ statSync.withArgs('_site').returns({ mtimeMs: siteTime })
142
+ statSync.withArgs('pagedjs.pdf').returns({ mtimeMs: pdfTime })
143
+
144
+ const { checkPdfOutput } = await esmock('./pdf-output.js', {
145
+ 'node:fs': {
146
+ existsSync,
147
+ statSync,
148
+ },
149
+ '#lib/project/output-paths.js': {
150
+ getPdfOutputPaths: () => ['pagedjs.pdf', 'prince.pdf'],
151
+ },
152
+ '#lib/project/config.js': {
153
+ loadProjectConfig: async () => ({}),
154
+ },
155
+ '#lib/conf/config.js': configMock,
156
+ })
157
+
158
+ const result = await checkPdfOutput()
159
+
160
+ t.true(result.ok)
161
+ t.regex(result.message, /pagedjs\.pdf up to date/)
162
+ })
163
+
164
+ test('checkPdfOutput returns warning when PDF is stale beyond threshold', async (t) => {
165
+ const { sandbox } = t.context
166
+
167
+ const pdfTime = Date.now() - TWO_HOURS // 2 hours ago
168
+ const siteTime = Date.now() // now
169
+
170
+ const existsSync = sandbox.stub()
171
+ existsSync.withArgs('pagedjs.pdf').returns(true)
172
+ existsSync.withArgs('prince.pdf').returns(false)
173
+ existsSync.withArgs('_site').returns(true)
174
+
175
+ const statSync = sandbox.stub()
176
+ statSync.withArgs('_site').returns({ mtimeMs: siteTime })
177
+ statSync.withArgs('pagedjs.pdf').returns({ mtimeMs: pdfTime })
178
+
179
+ const { checkPdfOutput } = await esmock('./pdf-output.js', {
180
+ 'node:fs': {
181
+ existsSync,
182
+ statSync,
183
+ },
184
+ '#lib/project/output-paths.js': {
185
+ getPdfOutputPaths: () => ['pagedjs.pdf', 'prince.pdf'],
186
+ },
187
+ '#lib/project/config.js': {
188
+ loadProjectConfig: async () => ({}),
189
+ },
190
+ '#lib/conf/config.js': configMock,
191
+ })
192
+
193
+ const result = await checkPdfOutput()
194
+
195
+ t.false(result.ok)
196
+ t.is(result.level, 'warn')
197
+ t.regex(result.message, /pagedjs\.pdf/)
198
+ t.regex(result.message, /older than _site/)
199
+ t.truthy(result.remediation)
200
+ t.truthy(result.docsUrl)
201
+ })
202
+
203
+ test('checkPdfOutput returns ok when PDF is stale but within threshold', async (t) => {
204
+ const { sandbox } = t.context
205
+
206
+ const pdfTime = Date.now() - TWO_MINUTES // 2 minutes ago
207
+ const siteTime = Date.now()
208
+
209
+ const existsSync = sandbox.stub()
210
+ existsSync.withArgs('pagedjs.pdf').returns(true)
211
+ existsSync.withArgs('prince.pdf').returns(false)
212
+ existsSync.withArgs('_site').returns(true)
213
+
214
+ const statSync = sandbox.stub()
215
+ statSync.withArgs('_site').returns({ mtimeMs: siteTime })
216
+ statSync.withArgs('pagedjs.pdf').returns({ mtimeMs: pdfTime })
217
+
218
+ const { checkPdfOutput } = await esmock('./pdf-output.js', {
219
+ 'node:fs': {
220
+ existsSync,
221
+ statSync,
222
+ },
223
+ '#lib/project/output-paths.js': {
224
+ getPdfOutputPaths: () => ['pagedjs.pdf', 'prince.pdf'],
225
+ },
226
+ '#lib/project/config.js': {
227
+ loadProjectConfig: async () => ({}),
228
+ },
229
+ '#lib/conf/config.js': configMock,
230
+ })
231
+
232
+ const result = await checkPdfOutput()
233
+
234
+ t.true(result.ok)
235
+ t.regex(result.message, /pagedjs\.pdf up to date/)
236
+ })
237
+
238
+ test('checkPdfOutput checks multiple PDF files', async (t) => {
239
+ const { sandbox } = t.context
240
+
241
+ const now = Date.now()
242
+ const pdfTime = now
243
+ const siteTime = now - TEN_MINUTES
244
+
245
+ const existsSync = sandbox.stub()
246
+ existsSync.withArgs('pagedjs.pdf').returns(true)
247
+ existsSync.withArgs('prince.pdf').returns(true)
248
+ existsSync.withArgs('_site').returns(true)
249
+
250
+ const statSync = sandbox.stub()
251
+ statSync.withArgs('_site').returns({ mtimeMs: siteTime })
252
+ statSync.withArgs('pagedjs.pdf').returns({ mtimeMs: pdfTime })
253
+ statSync.withArgs('prince.pdf').returns({ mtimeMs: pdfTime })
254
+
255
+ const { checkPdfOutput } = await esmock('./pdf-output.js', {
256
+ 'node:fs': {
257
+ existsSync,
258
+ statSync,
259
+ },
260
+ '#lib/project/output-paths.js': {
261
+ getPdfOutputPaths: () => ['pagedjs.pdf', 'prince.pdf'],
262
+ },
263
+ '#lib/project/config.js': {
264
+ loadProjectConfig: async () => ({}),
265
+ },
266
+ '#lib/conf/config.js': configMock,
267
+ })
268
+
269
+ const result = await checkPdfOutput()
270
+
271
+ t.true(result.ok)
272
+ t.regex(result.message, /pagedjs\.pdf/)
273
+ t.regex(result.message, /prince\.pdf/)
274
+ })
275
+
276
+ test('checkPdfOutput includes remediation with pdf command', async (t) => {
277
+ const { sandbox } = t.context
278
+
279
+ const pdfTime = Date.now() - TWO_HOURS
280
+ const siteTime = Date.now()
281
+
282
+ const existsSync = sandbox.stub()
283
+ existsSync.withArgs('pagedjs.pdf').returns(true)
284
+ existsSync.withArgs('prince.pdf').returns(false)
285
+ existsSync.withArgs('_site').returns(true)
286
+
287
+ const statSync = sandbox.stub()
288
+ statSync.withArgs('_site').returns({ mtimeMs: siteTime })
289
+ statSync.withArgs('pagedjs.pdf').returns({ mtimeMs: pdfTime })
290
+
291
+ const { checkPdfOutput } = await esmock('./pdf-output.js', {
292
+ 'node:fs': {
293
+ existsSync,
294
+ statSync,
295
+ },
296
+ '#lib/project/output-paths.js': {
297
+ getPdfOutputPaths: () => ['pagedjs.pdf', 'prince.pdf'],
298
+ },
299
+ '#lib/project/config.js': {
300
+ loadProjectConfig: async () => ({}),
301
+ },
302
+ '#lib/conf/config.js': configMock,
303
+ })
304
+
305
+ const result = await checkPdfOutput()
306
+
307
+ t.false(result.ok)
308
+ t.regex(result.remediation, /quire pdf/)
309
+ })
310
+
311
+ test('checkPdfOutput loads project config to resolve config-aware paths', async (t) => {
312
+ const { sandbox } = t.context
313
+
314
+ const pdfConfig = { outputDir: 'pdf', filename: 'my-publication' }
315
+ const configPath = path.join('_site', 'pdf', 'my-publication.pdf')
316
+
317
+ const existsSync = sandbox.stub().returns(false)
318
+ existsSync.withArgs(configPath).returns(true)
319
+ existsSync.withArgs('_site').returns(true)
320
+
321
+ const now = Date.now()
322
+ const statSync = sandbox.stub()
323
+ statSync.withArgs('_site').returns({ mtimeMs: now - TEN_MINUTES })
324
+ statSync.withArgs(configPath).returns({ mtimeMs: now })
325
+
326
+ const { checkPdfOutput } = await esmock('./pdf-output.js', {
327
+ 'node:fs': {
328
+ existsSync,
329
+ statSync,
330
+ },
331
+ '#lib/project/output-paths.js': {
332
+ getPdfOutputPaths: (opts) => {
333
+ const paths = []
334
+ if (opts.pdfConfig) {
335
+ paths.push(configPath)
336
+ }
337
+ paths.push('pagedjs.pdf', 'prince.pdf')
338
+ return paths
339
+ },
340
+ },
341
+ '#lib/project/config.js': {
342
+ loadProjectConfig: async () => ({ pdf: pdfConfig }),
343
+ },
344
+ '#lib/conf/config.js': configMock,
345
+ })
346
+
347
+ const result = await checkPdfOutput()
348
+
349
+ t.true(result.ok)
350
+ t.regex(result.message, /my-publication\.pdf up to date/)
351
+ })
352
+
353
+ test('checkPdfOutput falls back to engine defaults when config loading fails', async (t) => {
354
+ const { sandbox } = t.context
355
+
356
+ const { checkPdfOutput } = await esmock('./pdf-output.js', {
357
+ 'node:fs': {
358
+ existsSync: sandbox.stub().returns(false),
359
+ },
360
+ '#lib/project/output-paths.js': {
361
+ getPdfOutputPaths: (opts) => {
362
+ t.is(opts.pdfConfig, undefined, 'pdfConfig should be undefined when config fails')
363
+ return ['pagedjs.pdf', 'prince.pdf']
364
+ },
365
+ },
366
+ '#lib/project/config.js': {
367
+ loadProjectConfig: async () => { throw new Error('No config.yaml') },
368
+ },
369
+ '#lib/conf/config.js': configMock,
370
+ '#lib/conf/build-status.js': buildStatusMock,
371
+ })
372
+
373
+ const result = await checkPdfOutput()
374
+
375
+ t.true(result.ok)
376
+ t.is(result.level, 'na')
377
+ })
@@ -0,0 +1,122 @@
1
+ /**
2
+ * Stale build check
3
+ *
4
+ * @module lib/doctor/checks/outputs/stale-build
5
+ */
6
+ import fs from 'node:fs'
7
+ import path from 'node:path'
8
+ import { SOURCE_DIRECTORIES } from '#lib/project/index.js'
9
+ import config from '#lib/conf/config.js'
10
+ import { getStatus } from '#lib/conf/build-status.js'
11
+ import createDebug from '#debug'
12
+ import { DOCS_BASE_URL, resolveStaleThreshold } from '#lib/doctor/constants.js'
13
+ import { formatDuration } from '#lib/doctor/formatDuration.js'
14
+
15
+ const debug = createDebug('lib:doctor:stale-build')
16
+
17
+ /**
18
+ * Get the latest modification time from files in a directory (recursive)
19
+ * @param {string} dir - Directory to scan
20
+ * @returns {number} - Latest modified timestamp in milliseconds, or 0 if directory doesn't exist
21
+ */
22
+ function getLatestModified(dir) {
23
+ if (!fs.existsSync(dir)) {
24
+ return 0
25
+ }
26
+
27
+ let newest = 0
28
+
29
+ const scan = (currentDir) => {
30
+ const entries = fs.readdirSync(currentDir, { withFileTypes: true })
31
+ for (const entry of entries) {
32
+ const fullPath = path.join(currentDir, entry.name)
33
+ try {
34
+ const { mtimeMs: lastModified } = fs.statSync(fullPath)
35
+ if (lastModified > newest) {
36
+ newest = lastModified
37
+ }
38
+ if (entry.isDirectory()) {
39
+ scan(fullPath)
40
+ }
41
+ } catch {
42
+ // Skip files we can't stat (permissions, etc.)
43
+ }
44
+ }
45
+ }
46
+
47
+ scan(dir)
48
+ return newest
49
+ }
50
+
51
+ /**
52
+ * Check if build output is stale (source files newer than _site)
53
+ * @returns {import('../../index.js').CheckResult}
54
+ */
55
+ export function checkStaleBuild() {
56
+ const siteDir = '_site'
57
+
58
+ // Skip if not in a project or no build output exists
59
+ if (!fs.existsSync(siteDir)) {
60
+ debug('No _site directory found, checking stored build status')
61
+
62
+ const status = getStatus(process.cwd(), 'build')
63
+ if (status?.status === 'failed') {
64
+ debug('Last build failed (stored status)')
65
+ return {
66
+ ok: false,
67
+ message: 'Last build failed',
68
+ remediation: `The last "quire build" run failed. Check the output for errors and try again.
69
+ • Run "quire build --debug" for detailed error output`,
70
+ docsUrl: `${DOCS_BASE_URL}/quire-commands/#preview-and-build`,
71
+ }
72
+ }
73
+
74
+ return {
75
+ ok: true,
76
+ level: 'na',
77
+ message: 'No build output found',
78
+ remediation: `Run "quire build" to generate the site.
79
+ • If you already ran "quire build" and it failed, check the output for errors`,
80
+ docsUrl: `${DOCS_BASE_URL}/quire-commands/#preview-and-build`,
81
+ }
82
+ }
83
+
84
+ // Get _site last modified time
85
+ const { mtimeMs: siteLastModified } = fs.statSync(siteDir)
86
+ debug('_site lastModified: %d', siteLastModified)
87
+
88
+ // Find newest source file across all source directories
89
+ let newestSourceLastModified = 0
90
+ for (const dir of SOURCE_DIRECTORIES) {
91
+ const lastModified = getLatestModified(dir)
92
+ if (lastModified > newestSourceLastModified) {
93
+ newestSourceLastModified = lastModified
94
+ }
95
+ }
96
+ debug('Newest source lastModified: %d', newestSourceLastModified)
97
+
98
+ // If source is newer than build by more than the stale threshold, warn
99
+ const thresholdMs = resolveStaleThreshold(config.get('staleThreshold'))
100
+ const staleDelta = newestSourceLastModified - siteLastModified
101
+ debug('stale delta: %dms, threshold: %dms (%s)', staleDelta, thresholdMs, config.get('staleThreshold'))
102
+
103
+ if (staleDelta > thresholdMs) {
104
+ const staleDuration = formatDuration(staleDelta)
105
+ return {
106
+ ok: false,
107
+ level: 'warn',
108
+ message: `Build output is ${staleDuration} older than source files`,
109
+ remediation: `Your build output may not reflect recent changes.
110
+ • Run "quire build" to regenerate the site
111
+ • Or run "quire preview" which auto-rebuilds on changes`,
112
+ docsUrl: `${DOCS_BASE_URL}/quire-commands/#preview-and-build`,
113
+ }
114
+ }
115
+
116
+ return {
117
+ ok: true,
118
+ message: 'Build output is up to date',
119
+ }
120
+ }
121
+
122
+ export default checkStaleBuild