-
Notifications
You must be signed in to change notification settings - Fork 434
Expand file tree
/
Copy pathproject-index.ts
More file actions
393 lines (351 loc) · 11.2 KB
/
project-index.ts
File metadata and controls
393 lines (351 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
* project-index.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*/
import { dirname, isAbsolute, join, relative } from "../deno_ral/path.ts";
import * as ld from "../core/lodash.ts";
import {
InputTarget,
InputTargetIndex,
kProjectType,
ProjectContext,
} from "./types.ts";
import { Metadata } from "../config/types.ts";
import { Format } from "../config/types.ts";
import {
dirAndStem,
normalizePath,
pathWithForwardSlashes,
removeIfExists,
safeExistsSync,
} from "../core/path.ts";
import { kTitle } from "../config/constants.ts";
import { fileExecutionEngine } from "../execute/engine.ts";
import {
ensureFileInformationCache,
projectConfigFile,
projectOutputDir,
} from "./project-shared.ts";
import { projectScratchPath } from "./project-scratch.ts";
import { parsePandocTitle } from "../core/pandoc/pandoc-partition.ts";
import { readYamlFromString } from "../core/yaml.ts";
import { formatKeys } from "../config/metadata.ts";
import {
formatsPreferHtml,
websiteFormatPreferHtml,
} from "./types/website/website-config.ts";
import { kDefaultProjectFileContents } from "./types/project-default.ts";
import { formatOutputFile } from "../core/render.ts";
import { projectType } from "./types/project-types.ts";
import { withRenderServices } from "../command/render/render-services.ts";
import { RenderServices } from "../command/render/types.ts";
import { kDraft } from "../format/html/format-html-shared.ts";
export async function inputTargetIndex(
project: ProjectContext,
input: string,
): Promise<InputTargetIndex | undefined> {
// calculate input file
const inputFile = join(project.dir, input);
// return undefined if the file doesn't exist
if (!safeExistsSync(inputFile) || Deno.statSync(inputFile).isDirectory) {
return Promise.resolve(undefined);
}
// filter it out if its not in the list of input files
if (!project.files.input.includes(normalizePath(inputFile))) {
return Promise.resolve(undefined);
}
// see if we have an up to date index file (but not for notebooks
// as they could have ipynb-filters that vary based on config)
const { index: targetIndex } = readInputTargetIndex(
project.dir,
input,
);
// There is already a targetIndex entry, just use that
if (targetIndex) {
return targetIndex;
}
// Create an index entry for the input
const index = await readBaseInputIndex(inputFile, project);
if (index) {
const indexFile = inputTargetIndexFile(project.dir, input);
Deno.writeTextFileSync(indexFile, JSON.stringify(index));
}
return index;
}
export async function readBaseInputIndex(
inputFile: string,
project: ProjectContext,
) {
// check if this can be handled by one of our engines
const engine = await fileExecutionEngine(inputFile, undefined, project);
if (engine === undefined) {
return Promise.resolve(undefined);
}
// otherwise read the metadata and index it
const formats = await withRenderServices(
project.notebookContext,
(services: RenderServices) =>
project.renderFormats(inputFile, services, "all", project),
);
const firstFormat = Object.values(formats)[0];
const markdown = await engine.partitionedMarkdown(inputFile, firstFormat);
const index: InputTargetIndex = {
title: (firstFormat?.metadata?.[kTitle] || markdown.yaml?.[kTitle] ||
markdown.headingText) as
| string
| undefined,
markdown,
formats,
draft: (firstFormat?.metadata?.[kDraft] || markdown.yaml?.[kDraft]) as
| boolean
| undefined,
};
// if we got a title, make sure it doesn't carry attributes
if (index.title) {
// locally guard against a badly-formed title.
// See https://github.com/quarto-dev/quarto-cli/issues/8594 for why
// we can't do a proper fix at this time
if (typeof index.title !== "string") {
throw new Error(
`${
relative(project.dir, inputFile)
}: Title must be a string, but is instead of type ${typeof index
.title}`,
);
}
const parsedTitle = parsePandocTitle(index.title);
index.title = parsedTitle.heading;
} else {
// if there is no title then try to extract it from a header
index.title = index.markdown.headingText;
}
if (project.config) {
index.projectFormats = formatKeys(project.config);
}
return index;
}
// reads an existing input target index file
export function readInputTargetIndex(
projectDir: string,
input: string,
): {
index?: InputTargetIndex;
missingReason?: "stale" | "formats";
} {
// check if we have an index that's still current vis-a-vis the
// last modified date of the source file
const index = readInputTargetIndexIfStillCurrent(projectDir, input);
if (!index) {
return {
missingReason: "stale",
};
}
// if its still current vis-a-vis the input file, we then need to
// check if the format list has changed (which is also an invalidation)
// normalize html to first if its included in the formats
if (Object.keys(index.formats).includes("html")) {
// note that the cast it okay here b/c we know that index.formats
// includes only full format objects
index.formats = websiteFormatPreferHtml(index.formats) as Record<
string,
Format
>;
}
// when we write the index to disk we write it with the formats
// so we need to check if the formats have changed
const formats = (index.projectFormats as string[] | undefined) ??
Object.keys(index.formats);
const projConfigFile = projectConfigFile(projectDir);
if (!projConfigFile) {
return { index };
}
let contents = Deno.readTextFileSync(projConfigFile);
if (contents.trim().length === 0) {
contents = kDefaultProjectFileContents;
}
const config = readYamlFromString(contents) as Metadata;
const projFormats = formatKeys(config);
if (ld.isEqual(formats, projFormats)) {
return {
index,
};
} else {
return {
missingReason: "formats",
};
}
}
export function inputTargetIsEmpty(index: InputTargetIndex) {
// if we have markdown we are not empty
if (index.markdown.markdown.trim().length > 0) {
return false;
}
// if we have a key other than title we are not empty
if (
index.markdown.yaml &&
Object.keys(index.markdown.yaml).find((key) => key !== kTitle)
) {
return false;
}
// otherwise we are empty
return true;
}
const inputTargetIndexCache = new Map<string, InputTargetIndex>();
export const inputTargetIndexCacheMetrics = {
hits: 0,
misses: 0,
invalidations: 0,
};
function readInputTargetIndexIfStillCurrent(projectDir: string, input: string) {
const inputFile = join(projectDir, input);
const indexFile = inputTargetIndexFile(projectDir, input);
try {
const inputMod = Deno.statSync(inputFile).mtime;
const indexMod = Deno.statSync(indexFile).mtime;
if (
inputMod && indexMod
) {
if (inputMod > indexMod) {
inputTargetIndexCacheMetrics.invalidations++;
inputTargetIndexCache.delete(indexFile);
return undefined;
}
if (inputTargetIndexCache.has(indexFile)) {
inputTargetIndexCacheMetrics.hits++;
return inputTargetIndexCache.get(indexFile);
} else {
inputTargetIndexCacheMetrics.misses++;
try {
const result = JSON.parse(
Deno.readTextFileSync(indexFile),
) as InputTargetIndex;
inputTargetIndexCache.set(indexFile, result);
return result;
} catch {
return undefined;
}
}
}
} catch (e) {
if (e instanceof Deno.errors.NotFound) {
return undefined;
} else {
throw e;
}
}
}
export async function resolveInputTarget(
project: ProjectContext,
href: string,
absolute = true,
): Promise<InputTarget | undefined> {
const index = await inputTargetIndex(project, href);
if (index) {
const formats = formatsPreferHtml(index.formats) as Record<string, Format>;
const format = Object.values(formats)[0];
// lookup the project type
const projType = projectType(project.config?.project?.[kProjectType]);
const projOutputFile = projType.outputFile
? projType.outputFile(href, format, project)
: undefined;
const [hrefDir, hrefStem] = dirAndStem(href);
const outputFile = projOutputFile || formatOutputFile(format) ||
`${hrefStem}.html`;
const outputHref = pathWithForwardSlashes(
(absolute ? "/" : "") + join(hrefDir, outputFile),
);
const inputTarget = {
input: href,
title: index.title,
outputHref,
draft: index.draft === true,
};
if (projType.filterInputTarget) {
return projType.filterInputTarget(inputTarget, project);
} else {
return inputTarget;
}
} else {
return undefined;
}
}
// populates the outputNameIndex field in the project context
export async function resolveOutputFileNames(
project: ProjectContext,
): Promise<void> {
if (project.outputNameIndex) return;
const outputDir = projectOutputDir(project);
project.outputNameIndex = new Map();
for (const file of project.files.input) {
const inputRelative = relative(project.dir, file);
const index = await inputTargetIndex(
project,
relative(project.dir, file),
);
if (index) {
const cache = ensureFileInformationCache(project, file);
cache.outputFiles = cache.outputFiles || {};
Object.keys(index.formats).forEach((key) => {
const format = index.formats[key];
const outputFile = formatOutputFile(format);
if (outputFile) {
const formatOutputPath = join(
outputDir!,
dirname(inputRelative),
outputFile,
);
project.outputNameIndex!.set(formatOutputPath, { file, format });
cache.outputFiles![formatOutputPath] = format.identifier;
}
});
}
}
}
export async function inputFileForOutputFile(
project: ProjectContext,
output: string,
): Promise<{ file: string; format: Format } | undefined> {
// compute output dir
const outputDir = projectOutputDir(project);
// full path to output (it's relative to output dir)
output = isAbsolute(output) ? output : join(outputDir, output);
await resolveOutputFileNames(project);
return project.outputNameIndex?.get(output);
}
export async function inputTargetIndexForOutputFile(
project: ProjectContext,
outputRelative: string,
) {
const input = await inputFileForOutputFile(project, outputRelative);
if (!input) {
return undefined;
}
return await inputTargetIndex(
project,
relative(project.dir, input.file),
);
}
export async function resolveInputTargetForOutputFile(
project: ProjectContext,
outputRelative: string,
) {
const input = await inputFileForOutputFile(project, outputRelative);
if (!input) {
return undefined;
}
return await resolveInputTarget(
project,
pathWithForwardSlashes(relative(project.dir, input.file)),
);
}
export function clearProjectIndex(projectDir: string) {
const indexPath = projectScratchPath(projectDir, "idx");
removeIfExists(indexPath);
}
function inputTargetIndexFile(projectDir: string, input: string): string {
return indexPath(projectDir, `${input}.json`);
}
function indexPath(projectDir: string, path: string): string {
return projectScratchPath(projectDir, join("idx", path));
}