Skip to content

Commit 3c1b767

Browse files
committed
inspect - provide information about output files from input files
1 parent e426fbc commit 3c1b767

File tree

6 files changed

+43
-16
lines changed

6 files changed

+43
-16
lines changed

src/inspect/inspect-types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Copyright (C) 2020-2022 Posit Software, PBC
55
*/
66

7-
import { Format } from "../config/types.ts";
7+
import { Format, FormatIdentifier } from "../config/types.ts";
88
import { Extension } from "../extension/types.ts";
99
import {
1010
FileInclusion,
@@ -25,6 +25,7 @@ export interface InspectedFile {
2525
includeMap: FileInclusion[];
2626
codeCells: InspectedMdCell[];
2727
metadata: Record<string, unknown>;
28+
outputFiles: Record<string, FormatIdentifier>;
2829
}
2930

3031
export interface InspectedConfig {

src/inspect/inspect.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import {
4949
import { validateDocumentFromSource } from "../core/schema/validate-document.ts";
5050
import { error } from "../deno_ral/log.ts";
5151
import { ProjectContext } from "../project/types.ts";
52+
import { resolveOutputFileNames } from "../project/project-index.ts";
5253

5354
export function isProjectConfig(
5455
config: InspectedConfig,
@@ -90,6 +91,7 @@ const inspectProjectConfig = async (context: ProjectContext) => {
9091
return undefined;
9192
}
9293
const fileInformation: Record<string, InspectedFile> = {};
94+
await resolveOutputFileNames(context);
9395
for (const file of context.files.input) {
9496
await populateFileInformation(context, fileInformation, file);
9597
}
@@ -144,6 +146,7 @@ const populateFileInformation = async (
144146
[],
145147
codeCells: context.fileInformationCache.get(file)?.codeCells ?? [],
146148
metadata: context.fileInformationCache.get(file)?.metadata ?? {},
149+
outputFiles: context.fileInformationCache.get(file)?.outputFiles ?? {},
147150
};
148151
};
149152

@@ -213,9 +216,11 @@ const inspectDocumentConfig = async (path: string) => {
213216
);
214217
}
215218

219+
path = normalizePath(path);
216220
await context.resolveFullMarkdownForFile(engine, path);
217221
await projectResolveCodeCellsForFile(context, engine, path);
218222
await projectFileMetadata(context, path);
223+
await resolveOutputFileNames(context);
219224
const fileInformation = context.fileInformationCache.get(path);
220225

221226
// data to write
@@ -231,6 +236,7 @@ const inspectDocumentConfig = async (path: string) => {
231236
includeMap: fileInformation?.includeMap ?? [],
232237
codeCells: fileInformation?.codeCells ?? [],
233238
metadata: fileInformation?.metadata ?? {},
239+
outputFiles: fileInformation?.outputFiles ?? {},
234240
},
235241
},
236242
};

src/project/project-context.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ export async function projectContext(
274274
});
275275
const fileInformationCache = new Map();
276276
const result: ProjectContext = {
277+
clone: () => result,
277278
resolveBrand: async (fileName?: string) =>
278279
projectResolveBrand(result, fileName),
279280
resolveFullMarkdownForFile: (
@@ -370,6 +371,7 @@ export async function projectContext(
370371
});
371372
const fileInformationCache = new Map();
372373
const result: ProjectContext = {
374+
clone: () => result,
373375
resolveBrand: async (fileName?: string) =>
374376
projectResolveBrand(result, fileName),
375377
resolveFullMarkdownForFile: (
@@ -445,6 +447,7 @@ export async function projectContext(
445447
});
446448
const fileInformationCache = new Map();
447449
const context: ProjectContext = {
450+
clone: () => context,
448451
resolveBrand: async (fileName?: string) =>
449452
projectResolveBrand(context, fileName),
450453
resolveFullMarkdownForFile: (

src/project/project-index.ts

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ import {
2727
import { kTitle } from "../config/constants.ts";
2828
import { fileExecutionEngine } from "../execute/engine.ts";
2929

30-
import { projectConfigFile, projectOutputDir } from "./project-shared.ts";
30+
import {
31+
ensureFileInformationCache,
32+
projectConfigFile,
33+
projectOutputDir,
34+
} from "./project-shared.ts";
3135
import { projectScratchPath } from "./project-scratch.ts";
3236
import { parsePandocTitle } from "../core/pandoc/pandoc-partition.ts";
3337
import { readYamlFromString } from "../core/yaml.ts";
@@ -297,20 +301,13 @@ export async function resolveInputTarget(
297301
}
298302
}
299303

300-
export async function inputFileForOutputFile(
304+
// populates the outputNameIndex field in the project context
305+
export async function resolveOutputFileNames(
301306
project: ProjectContext,
302-
output: string,
303-
): Promise<{ file: string; format: Format } | undefined> {
304-
// compute output dir
307+
): Promise<void> {
308+
if (project.outputNameIndex) return;
305309
const outputDir = projectOutputDir(project);
306310

307-
// full path to output (it's relative to output dir)
308-
output = isAbsolute(output) ? output : join(outputDir, output);
309-
310-
if (project.outputNameIndex !== undefined) {
311-
return project.outputNameIndex.get(output);
312-
}
313-
314311
project.outputNameIndex = new Map();
315312
for (const file of project.files.input) {
316313
const inputRelative = relative(project.dir, file);
@@ -319,6 +316,8 @@ export async function inputFileForOutputFile(
319316
relative(project.dir, file),
320317
);
321318
if (index) {
319+
const cache = ensureFileInformationCache(project, file);
320+
cache.outputFiles = cache.outputFiles || {};
322321
Object.keys(index.formats).forEach((key) => {
323322
const format = index.formats[key];
324323
const outputFile = formatOutputFile(format);
@@ -329,11 +328,25 @@ export async function inputFileForOutputFile(
329328
outputFile,
330329
);
331330
project.outputNameIndex!.set(formatOutputPath, { file, format });
331+
cache.outputFiles![formatOutputPath] = format.identifier;
332332
}
333333
});
334334
}
335335
}
336-
return project.outputNameIndex.get(output);
336+
}
337+
338+
export async function inputFileForOutputFile(
339+
project: ProjectContext,
340+
output: string,
341+
): Promise<{ file: string; format: Format } | undefined> {
342+
// compute output dir
343+
const outputDir = projectOutputDir(project);
344+
345+
// full path to output (it's relative to output dir)
346+
output = isAbsolute(output) ? output : join(outputDir, output);
347+
348+
await resolveOutputFileNames(project);
349+
return project.outputNameIndex?.get(output);
337350
}
338351

339352
export async function inputTargetIndexForOutputFile(

src/project/types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
import { RenderServices } from "../command/render/types.ts";
8-
import { Metadata, PandocFlags } from "../config/types.ts";
8+
import { FormatIdentifier, Metadata, PandocFlags } from "../config/types.ts";
99
import { Format, FormatExtras } from "../config/types.ts";
1010
import { Brand, LightDarkBrand } from "../core/brand/brand.ts";
1111
import { MappedString } from "../core/mapped-text.ts";
@@ -22,6 +22,7 @@ import {
2222
import { ProjectEnvironment } from "./project-environment-types.ts";
2323
import { ProjectCache } from "../core/cache/cache-types.ts";
2424
import { TempContext } from "../core/temp-types.ts";
25+
import { Cloneable } from "../core/safe-clone-deep.ts";
2526

2627
export {
2728
type NavigationItem as NavItem,
@@ -56,9 +57,11 @@ export type FileInformation = {
5657
target?: ExecutionTarget;
5758
metadata?: Metadata;
5859
brand?: LightDarkBrand;
60+
outputFiles?: Record<string, FormatIdentifier>;
5961
};
6062

6163
export interface ProjectContext {
64+
clone: () => ProjectContext;
6265
dir: string;
6366
engines: string[];
6467
files: ProjectFiles;

src/project/types/single-file/single-file.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,12 @@ export async function singleFileProjectContext(
4040
const projectCacheBaseDir = temp.createDir();
4141

4242
const result: ProjectContext = {
43+
clone: () => result,
4344
resolveBrand: (fileName?: string) => projectResolveBrand(result, fileName),
4445
dir: normalizePath(dirname(source)),
4546
engines: [],
4647
files: {
47-
input: [],
48+
input: [normalizePath(source)],
4849
},
4950
notebookContext,
5051
environment: () => environmentMemoizer(result),

0 commit comments

Comments
 (0)