-
Notifications
You must be signed in to change notification settings - Fork 434
Expand file tree
/
Copy pathinspect.ts
More file actions
266 lines (245 loc) · 7.83 KB
/
inspect.ts
File metadata and controls
266 lines (245 loc) · 7.83 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
/*
* inspect.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*/
import { existsSync } from "../deno_ral/fs.ts";
import { dirname, join, relative } from "../deno_ral/path.ts";
import * as ld from "../core/lodash.ts";
import { kCss, kResources } from "../config/constants.ts";
import { Format } from "../config/types.ts";
import { projectContext } from "../project/project-context.ts";
import { fileExecutionEngine } from "../execute/engine.ts";
import { renderFormats } from "../command/render/render-contexts.ts";
import {
resolveFileResources,
resourcesFromMetadata,
} from "../command/render/resources.ts";
import { quartoConfig } from "../core/quarto.ts";
import { cssFileResourceReferences } from "../core/css.ts";
import {
projectExcludeDirs,
projectFileMetadata,
projectResolveCodeCellsForFile,
withProjectCleanup,
} from "../project/project-shared.ts";
import { normalizePath, safeExistsSync } from "../core/path.ts";
import { kExtensionDir } from "../extension/constants.ts";
import {
createExtensionContext,
extensionFilesFromDirs,
} from "../extension/extension.ts";
import { withRenderServices } from "../command/render/render-services.ts";
import { notebookContext } from "../render/notebook/notebook-context.ts";
import { RenderServices } from "../command/render/types.ts";
import { singleFileProjectContext } from "../project/types/single-file/single-file.ts";
import {
InspectedConfig,
InspectedDocumentConfig,
InspectedFile,
InspectedProjectConfig,
} from "./inspect-types.ts";
import { validateDocumentFromSource } from "../core/schema/validate-document.ts";
import { error } from "../deno_ral/log.ts";
import { ProjectContext } from "../project/types.ts";
export function isProjectConfig(
config: InspectedConfig,
): config is InspectedProjectConfig {
return (config as InspectedProjectConfig).files !== undefined;
}
export function isDocumentConfig(
config: InspectedConfig,
): config is InspectedDocumentConfig {
return (config as InspectedDocumentConfig).formats !== undefined;
}
export async function inspectConfig(path?: string): Promise<InspectedConfig> {
path = path || Deno.cwd();
if (!existsSync(path)) {
throw new Error(`${path} not found`);
}
const stat = Deno.statSync(path);
if (stat.isDirectory) {
const nbContext = notebookContext();
const ctx = await projectContext(path, nbContext);
if (!ctx) {
throw new Error(`${path} is not a Quarto project.`);
}
const config = await withProjectCleanup(ctx, inspectProjectConfig);
if (!config) {
throw new Error(`${path} is not a Quarto project.`);
}
return config;
} else {
return await inspectDocumentConfig(path);
}
}
const inspectProjectConfig = async (context: ProjectContext) => {
if (!context.config) {
return undefined;
}
const fileInformation: Record<string, InspectedFile> = {};
for (const file of context.files.input) {
await populateFileInformation(context, fileInformation, file);
}
const extensions = await populateExtensionInformation(context);
const config: InspectedProjectConfig = {
quarto: {
version: quartoConfig.version(),
},
dir: context.dir,
engines: context.engines,
config: context.config,
files: context.files,
fileInformation,
extensions: extensions,
};
return config;
};
const populateExtensionInformation = async (
context: ProjectContext,
) => {
const extensionContext = createExtensionContext();
return await extensionContext.extensions(
context.dir,
context.config,
context.dir,
{ builtIn: false },
);
};
const populateFileInformation = async (
context: ProjectContext,
fileInformation: Record<string, InspectedFile>,
file: string,
) => {
const engine = await fileExecutionEngine(file, undefined, context);
const src = await context.resolveFullMarkdownForFile(engine, file);
if (engine) {
const errors = await validateDocumentFromSource(
src,
engine.name,
error,
);
if (errors.length) {
throw new Error(`${file} is not a valid Quarto input document`);
}
}
await projectResolveCodeCellsForFile(context, engine, file);
await projectFileMetadata(context, file);
fileInformation[file] = {
includeMap: context.fileInformationCache.get(file)?.includeMap ??
[],
codeCells: context.fileInformationCache.get(file)?.codeCells ?? [],
metadata: context.fileInformationCache.get(file)?.metadata ?? {},
};
};
const inspectDocumentConfig = async (path: string) => {
const nbContext = notebookContext();
const project = await projectContext(path, nbContext) ||
(await singleFileProjectContext(path, nbContext));
return withProjectCleanup(project, async (project) => {
const engine = await fileExecutionEngine(path, undefined, project);
if (!engine) {
throw new Error(`${path} is not a valid Quarto input document`);
}
// partition markdown
const partitioned = await engine.partitionedMarkdown(path);
const context = project;
const src = await context.resolveFullMarkdownForFile(engine, path);
if (engine) {
const errors = await validateDocumentFromSource(
src,
engine.name,
error,
);
if (errors.length) {
throw new Error(`${path} is not a valid Quarto input document`);
}
}
const formats = await withRenderServices(
nbContext,
(services: RenderServices) =>
renderFormats(path!, services, "all", context),
);
// accumulate resources from formats then resolve them
const resourceConfig: string[] = Object.values(formats).reduce(
(resources: string[], format: Format) => {
resources = ld.uniq(resources.concat(
resourcesFromMetadata(format.metadata[kResources]),
));
// include css specified in metadata
if (format.pandoc[kCss]) {
return ld.uniq(resources.concat(
resourcesFromMetadata(format.pandoc[kCss]),
));
} else {
return resources;
}
},
[],
);
const fileDir = normalizePath(dirname(path));
const excludeDirs = context ? projectExcludeDirs(context) : [];
const resources = await resolveResources(
context ? context.dir : fileDir,
fileDir,
excludeDirs,
partitioned.markdown,
resourceConfig,
);
// if there is an _extensions dir then add it
const extensions = join(fileDir, kExtensionDir);
if (safeExistsSync(extensions)) {
resources.push(
...extensionFilesFromDirs([extensions]).map((file) =>
relative(fileDir, file)
),
);
}
await context.resolveFullMarkdownForFile(engine, path);
await projectResolveCodeCellsForFile(context, engine, path);
await projectFileMetadata(context, path);
const fileInformation = context.fileInformationCache.get(path);
// data to write
const config: InspectedDocumentConfig = {
quarto: {
version: quartoConfig.version(),
},
engines: [engine.name],
formats,
resources,
fileInformation: {
[path]: {
includeMap: fileInformation?.includeMap ?? [],
codeCells: fileInformation?.codeCells ?? [],
metadata: fileInformation?.metadata ?? {},
},
},
};
// if there is a project then add it
if (context?.config) {
config.project = await inspectProjectConfig(context);
}
return config;
});
};
async function resolveResources(
rootDir: string,
fileDir: string,
excludeDirs: string[],
markdown: string,
globs: string[],
): Promise<string[]> {
const resolved = await resolveFileResources(
rootDir,
fileDir,
excludeDirs,
markdown,
globs,
);
const resources = ld.difference(
resolved.include,
resolved.exclude,
) as string[];
const allResources = resources.concat(cssFileResourceReferences(resources));
return allResources.map((file) => relative(rootDir, file));
}