-
Notifications
You must be signed in to change notification settings - Fork 347
/
Copy pathinspect.ts
233 lines (205 loc) · 6.58 KB
/
inspect.ts
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
/*
* inspect.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*/
import { existsSync } from "fs/mod.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 { kLocalDevelopment, quartoConfig } from "../core/quarto.ts";
import {
FileInclusion,
ProjectConfig,
ProjectFiles,
} from "../project/types.ts";
import { cssFileResourceReferences } from "../core/css.ts";
import { projectExcludeDirs } from "../project/project-shared.ts";
import { normalizePath, safeExistsSync } from "../core/path.ts";
import { kExtensionDir } from "../extension/constants.ts";
import { 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";
export interface FileInspection {
includeMap: FileInclusion[];
}
export interface InspectedConfig {
quarto: {
version: string;
};
engines: string[];
fileInformation: Record<string, FileInspection>;
}
export interface InspectedProjectConfig extends InspectedConfig {
dir: string;
config: ProjectConfig;
files: ProjectFiles;
}
export interface InspectedDocumentConfig extends InspectedConfig {
formats: Record<string, Format>;
resources: string[];
project?: InspectedProjectConfig;
}
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`);
}
// get quarto version
let version = quartoConfig.version();
if (version === kLocalDevelopment) {
version = "99.9.9";
}
const nbContext = notebookContext();
// get project context (if any)
const context = await projectContext(path, nbContext);
const inspectedProjectConfig = async () => {
if (context?.config) {
const fileInformation: Record<string, FileInspection> = {};
for (const file of context.files.input) {
const engine = await fileExecutionEngine(file, undefined, context);
await context.resolveFullMarkdownForFile(engine, file);
fileInformation[file] = {
includeMap: context.fileInformationCache.get(file)?.includeMap ?? [],
};
}
const config: InspectedProjectConfig = {
quarto: {
version,
},
dir: context.dir,
engines: context.engines,
config: context.config,
files: context.files,
fileInformation,
};
return config;
} else {
return undefined;
}
};
const stat = Deno.statSync(path);
if (stat.isDirectory) {
const config = await inspectedProjectConfig();
if (config) {
return config;
} else {
throw new Error(`${path} is not a quarto project.`);
}
} else {
const project = await projectContext(path, nbContext) ||
singleFileProjectContext(path, nbContext);
const engine = await fileExecutionEngine(path, undefined, project);
if (engine) {
// partition markdown
const partitioned = await engine.partitionedMarkdown(path);
// get formats
const context = (await projectContext(path, nbContext)) ||
singleFileProjectContext(path, nbContext);
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);
// data to write
const config: InspectedDocumentConfig = {
quarto: {
version,
},
engines: [engine.name],
formats,
resources,
fileInformation: {
[path]: {
includeMap: context.fileInformationCache.get(path)?.includeMap ??
[],
},
},
};
// if there is a project then add it
if (context?.config) {
config.project = await inspectedProjectConfig();
}
return config;
} else {
throw new Error(`${path} is not a valid Quarto input document`);
}
}
}
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));
}