-
Notifications
You must be signed in to change notification settings - Fork 432
Expand file tree
/
Copy pathproject-context.ts
More file actions
1050 lines (965 loc) · 31.8 KB
/
project-context.ts
File metadata and controls
1050 lines (965 loc) · 31.8 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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* project-context.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*/
import {
dirname,
globToRegExp,
isAbsolute,
join,
relative,
resolve,
SEP,
} from "../deno_ral/path.ts";
import { existsSync, walk, walkSync } from "../deno_ral/fs.ts";
import * as ld from "../core/lodash.ts";
import { ProjectType } from "./types/types.ts";
import { Format, Metadata, PandocFlags } from "../config/types.ts";
import {
kProjectLibDir,
kProjectOutputDir,
kProjectPostRender,
kProjectPreRender,
kProjectRender,
kProjectType,
ProjectConfig,
ProjectContext,
} from "./types.ts";
import { isYamlPath, readYaml } from "../core/yaml.ts";
import { mergeConfigs } from "../core/config.ts";
import {
ensureTrailingSlash,
kSkipHidden,
normalizePath,
pathWithForwardSlashes,
safeExistsSync,
} from "../core/path.ts";
import { includedMetadata, mergeProjectMetadata } from "../config/metadata.ts";
import {
kHtmlMathMethod,
kLanguageDefaults,
kMetadataFile,
kMetadataFiles,
kMetadataFormat,
kQuartoVarsKey,
} from "../config/constants.ts";
import { projectType, projectTypes } from "./types/project-types.ts";
import { resolvePathGlobs } from "../core/path.ts";
import {
readLanguageTranslations,
resolveLanguageMetadata,
} from "../core/language.ts";
import {
engineIgnoreDirs,
executionEngineIntermediateFiles,
fileExecutionEngine,
fileExecutionEngineAndTarget,
projectIgnoreGlobs,
resolveEngines,
} from "../execute/engine.ts";
import { ExecutionEngineInstance, kMarkdownEngine } from "../execute/types.ts";
import { projectResourceFiles } from "./project-resources.ts";
import {
cleanupFileInformationCache,
FileInformationCacheMap,
ignoreFieldsForProjectType,
normalizeFormatYaml,
projectConfigFile,
projectFileMetadata,
projectResolveBrand,
projectResolveFullMarkdownForFile,
projectVarsFile,
} from "./project-shared.ts";
import { RenderOptions, RenderServices } from "../command/render/types.ts";
import { kWebsite } from "./types/website/website-constants.ts";
import { readAndValidateYamlFromFile } from "../core/schema/validated-yaml.ts";
import { getProjectConfigSchema } from "../core/lib/yaml-schema/project-config.ts";
import { kDefaultProjectFileContents } from "./types/project-default.ts";
import {
createExtensionContext,
filterExtensions,
} from "../extension/extension.ts";
import { initializeProfileConfig } from "./project-profile.ts";
import { dotenvSetVariables } from "../quarto-core/dotenv.ts";
import { ConcreteSchema } from "../core/lib/yaml-schema/types.ts";
import { ExtensionContext } from "../extension/types.ts";
import { asArray } from "../core/array.ts";
import { renderFormats } from "../command/render/render-contexts.ts";
import { computeProjectEnvironment } from "./project-environment.ts";
import { ProjectEnvironment } from "./project-environment-types.ts";
import { NotebookContext } from "../render/notebook/notebook-types.ts";
import { MappedString } from "../core/mapped-text.ts";
import { makeTimedFunctionAsync } from "../core/performance/function-times.ts";
import { createProjectCache } from "../core/cache/cache.ts";
import { createTempContext } from "../core/temp.ts";
import { onCleanup } from "../core/cleanup.ts";
import { Zod } from "../resources/types/zod/schema-types.ts";
import { ExternalEngine } from "../resources/types/schema-types.ts";
export const mergeExtensionMetadata = async (
context: ProjectContext,
pOptions: RenderOptions,
) => {
// this will mutate context.config.project to merge
// in any project metadata from extensions
if (context.config) {
const extensions = await pOptions.services.extension.extensions(
undefined,
context.config,
context.dir,
{ builtIn: false },
);
// Handle project metadata extensions
const projectMetadata = extensions.filter((extension) =>
extension.contributes.metadata?.project
).map((extension) => {
return Zod.ProjectConfig.parse(extension.contributes.metadata!.project);
});
context.config.project = mergeProjectMetadata(
context.config.project,
...projectMetadata,
);
}
};
export async function projectContext(
path: string,
notebookContext: NotebookContext,
renderOptions?: RenderOptions,
force = false,
): Promise<ProjectContext | undefined> {
const flags = renderOptions?.flags;
let dir = normalizePath(
Deno.statSync(path).isDirectory ? path : dirname(path),
);
const originalDir = dir;
// create an extension context if one doesn't exist
const extensionContext = renderOptions?.services.extension ||
createExtensionContext();
// first pass uses the config file resolve
const configSchema = await getProjectConfigSchema();
const configResolvers = [
quartoYamlProjectConfigResolver(configSchema),
await projectExtensionsConfigResolver(extensionContext, dir),
];
// Compute this on demand and only a single time per
// project context
let cachedEnv: ProjectEnvironment | undefined = undefined;
const environment = async (
project: ProjectContext,
) => {
if (cachedEnv) {
return Promise.resolve(cachedEnv);
} else {
cachedEnv = await computeProjectEnvironment(notebookContext, project);
return cachedEnv;
}
};
const returnResult = async (
context: ProjectContext,
) => {
if (renderOptions) {
await mergeExtensionMetadata(context, renderOptions);
}
onCleanup(context.cleanup);
return context;
};
while (true) {
// use the current resolver
const resolver = configResolvers[0];
const resolved = await resolver(dir);
if (resolved) {
let projectConfig = resolved.config;
const configFiles = resolved.files;
// migrate any legacy config
projectConfig = migrateProjectConfig(projectConfig);
// Look for project extension and load it
const projType = projectConfig.project[kProjectType];
if (projType && !(projectTypes().includes(projType))) {
projectConfig = await resolveProjectExtension(
extensionContext,
projType,
projectConfig,
dir,
);
// resolve includes
const configSchema = await getProjectConfigSchema();
const includedMeta = await includedMetadata(
dir,
projectConfig,
configSchema,
);
const metadata = includedMeta.metadata;
projectConfig = mergeProjectMetadata(projectConfig, metadata);
}
// Process engine extensions
if (extensionContext) {
projectConfig = await resolveEngineExtensions(
extensionContext,
projectConfig,
dir,
);
}
// collect then merge configuration profiles
const result = await initializeProfileConfig(
dir,
projectConfig,
configSchema,
);
projectConfig = result.config;
configFiles.push(...result.files);
// process dotenv files
const dotenvFiles = await dotenvSetVariables(dir);
configFiles.push(...dotenvFiles);
// read vars and merge into the project
const varsFile = projectVarsFile(dir);
if (varsFile) {
configFiles.push(varsFile);
const vars = readYaml(varsFile) as Metadata;
projectConfig[kQuartoVarsKey] = mergeConfigs(
projectConfig[kQuartoVarsKey] || {},
vars,
);
}
// resolve translations
const translationFiles = await resolveLanguageTranslations(
projectConfig,
dir,
);
configFiles.push(...translationFiles);
// inject format if specified in --to
if (flags?.to && flags?.to !== "all" && flags?.to !== "default") {
const projectFormats = normalizeFormatYaml(
projectConfig[kMetadataFormat],
);
const toFormat = projectFormats[flags?.to] || {};
delete projectFormats[flags?.to];
const formats = {
[flags?.to]: toFormat,
};
Object.keys(projectFormats).forEach((format) => {
formats[format] = projectFormats[format] as Record<never, never>;
});
projectConfig[kMetadataFormat] = formats;
}
if (projectConfig?.project) {
// provide output-dir from command line if specfified
if (flags?.outputDir) {
projectConfig.project[kProjectOutputDir] = flags.outputDir;
}
// convert pre and post render to array
if (typeof (projectConfig.project[kProjectPreRender]) === "string") {
projectConfig.project[kProjectPreRender] = [
projectConfig.project[kProjectPreRender] as unknown as string,
];
}
if (typeof (projectConfig.project[kProjectPostRender]) === "string") {
projectConfig.project[kProjectPostRender] = [
projectConfig.project[kProjectPostRender] as unknown as string,
];
}
// get project config and type-specific defaults for libDir and outputDir
const type = projectType(projectConfig.project?.[kProjectType]);
if (
projectConfig.project[kProjectLibDir] === undefined && type.libDir
) {
projectConfig.project[kProjectLibDir] = type.libDir;
}
if (!projectConfig.project[kProjectOutputDir] && type.outputDir) {
projectConfig.project[kProjectOutputDir] = type.outputDir;
}
// if the output-dir resolves to the project directory, that's equivalent to
// no output dir so make that conversion now (this allows code downstream to
// just check for no output dir rather than checking for ".", "./", etc.)
// Fixes issue #13892: output-dir: ./ would delete the entire project
const outputDir = projectConfig.project[kProjectOutputDir];
if (outputDir) {
const resolvedOutputDir = resolve(dir, outputDir);
const resolvedDir = resolve(dir);
if (resolvedOutputDir === resolvedDir) {
delete projectConfig.project[kProjectOutputDir];
}
}
// if the output-dir is absolute then make it project dir relative
const projOutputDir = projectConfig.project[kProjectOutputDir];
if (projOutputDir && isAbsolute(projOutputDir)) {
projectConfig.project[kProjectOutputDir] = relative(
dir,
projOutputDir,
);
}
const temp = createTempContext({
dir: join(dir, ".quarto"),
prefix: "quarto-session-temp",
});
const fileInformationCache = new FileInformationCacheMap();
const result: ProjectContext = {
clone: () => result,
resolveBrand: async (fileName?: string) =>
projectResolveBrand(result, fileName),
resolveFullMarkdownForFile: (
engine: ExecutionEngineInstance | undefined,
file: string,
markdown?: MappedString,
force?: boolean,
) => {
return projectResolveFullMarkdownForFile(
result,
engine,
file,
markdown,
force,
);
},
dir,
engines: [],
fileInformationCache,
files: {
input: [],
},
config: projectConfig,
// this is a relatively ugly hack to avoid a circular import chain
// that causes a deno bundler bug;
renderFormats,
environment: () => environment(result),
notebookContext,
fileExecutionEngineAndTarget: (
file: string,
) => {
return fileExecutionEngineAndTarget(
file,
flags,
result,
);
},
fileMetadata: async (file: string, force?: boolean) => {
return projectFileMetadata(result, file, force);
},
isSingleFile: false,
previewServer: renderOptions?.previewServer,
diskCache: await createProjectCache(join(dir, ".quarto")),
temp,
cleanup: () => {
cleanupFileInformationCache(result);
result.diskCache.close();
temp.cleanup();
},
};
// see if the project [kProjectType] wants to filter the project config
if (type.config) {
result.config = await type.config(
result,
projectConfig,
flags,
);
}
const { files, engines } = await projectInputFiles(
result,
projectConfig,
);
// if we are attemping to get the projectConext for a file and the
// file isn't in list of input files then return a single-file project
const fullPath = normalizePath(path);
if (Deno.statSync(fullPath).isFile && !files.includes(fullPath)) {
return undefined;
}
if (type.formatExtras) {
result.formatExtras = async (
source: string,
flags: PandocFlags,
format: Format,
services: RenderServices,
) => type.formatExtras!(result, source, flags, format, services);
}
result.engines = engines;
result.files = {
input: files,
resources: projectResourceFiles(dir, projectConfig),
config: configFiles,
configResources: projectConfigResources(dir, projectConfig, type),
};
return await returnResult(result);
} else {
const temp = createTempContext({
dir: join(dir, ".quarto"),
prefix: "quarto-session-temp",
});
const fileInformationCache = new FileInformationCacheMap();
const result: ProjectContext = {
clone: () => result,
resolveBrand: async (fileName?: string) =>
projectResolveBrand(result, fileName),
resolveFullMarkdownForFile: (
engine: ExecutionEngineInstance | undefined,
file: string,
markdown?: MappedString,
force?: boolean,
) => {
return projectResolveFullMarkdownForFile(
result,
engine,
file,
markdown,
force,
);
},
dir,
config: projectConfig,
engines: [],
fileInformationCache,
files: {
input: [],
},
renderFormats,
environment: () => environment(result),
fileExecutionEngineAndTarget: (
file: string,
) => {
return fileExecutionEngineAndTarget(
file,
flags,
result,
);
},
fileMetadata: async (file: string, force?: boolean) => {
return projectFileMetadata(result, file, force);
},
notebookContext,
isSingleFile: false,
previewServer: renderOptions?.previewServer,
diskCache: await createProjectCache(join(dir, ".quarto")),
temp,
cleanup: () => {
cleanupFileInformationCache(result);
result.diskCache.close();
temp.cleanup();
},
};
const { files, engines } = await projectInputFiles(
result,
projectConfig,
);
result.engines = engines;
result.files = {
input: files,
resources: projectResourceFiles(dir, projectConfig),
config: configFiles,
configResources: projectConfigResources(dir, projectConfig),
};
return await returnResult(result);
}
} else {
const nextDir = dirname(dir);
if (nextDir === dir) {
if (configResolvers.length > 1) {
// reset dir and proceed to next resolver
dir = originalDir;
configResolvers.shift();
} else if (force) {
const temp = createTempContext({
dir: join(originalDir, ".quarto"),
prefix: "quarto-session-temp",
});
const fileInformationCache = new FileInformationCacheMap();
const context: ProjectContext = {
clone: () => context,
resolveBrand: async (fileName?: string) =>
projectResolveBrand(context, fileName),
resolveFullMarkdownForFile: (
engine: ExecutionEngineInstance | undefined,
file: string,
markdown?: MappedString,
force?: boolean,
) => {
return projectResolveFullMarkdownForFile(
context,
engine,
file,
markdown,
force,
);
},
dir: originalDir,
engines: [],
config: {
project: {
[kProjectOutputDir]: flags?.outputDir,
},
},
fileInformationCache,
files: {
input: [],
},
renderFormats,
environment: () => environment(context),
notebookContext,
fileExecutionEngineAndTarget: (
file: string,
) => {
return fileExecutionEngineAndTarget(
file,
flags,
context,
);
},
fileMetadata: async (file: string, force?: boolean) => {
return projectFileMetadata(context, file, force);
},
isSingleFile: false,
previewServer: renderOptions?.previewServer,
diskCache: await createProjectCache(join(temp.baseDir, ".quarto")),
temp,
cleanup: () => {
cleanupFileInformationCache(context);
context.diskCache.close();
temp.cleanup();
},
};
if (Deno.statSync(path).isDirectory) {
const { files, engines } = await projectInputFiles(context);
context.engines = engines;
context.files.input = files;
} else {
const input = normalizePath(path);
const engine = await fileExecutionEngine(input, undefined, context);
context.engines = [engine?.name ?? kMarkdownEngine];
context.files.input = [input];
}
return await returnResult(context);
} else {
return undefined;
}
} else {
dir = nextDir;
}
}
}
}
type ResolvedProjectConfig = {
config: ProjectConfig;
files: string[];
};
function quartoYamlProjectConfigResolver(
configSchema: ConcreteSchema,
) {
return async (dir: string): Promise<ResolvedProjectConfig | undefined> => {
let configFile: string | undefined = undefined;
try {
configFile = projectConfigFile(dir);
} catch (e) {
if (e instanceof Deno.errors.PermissionDenied) {
// ignore permission denied errors
} else {
throw e;
}
}
if (configFile) {
// read config file
const files = [configFile];
const errMsg = `Project ${configFile} validation failed.`;
let config = (await readAndValidateYamlFromFile(
configFile,
configSchema,
errMsg,
kDefaultProjectFileContents,
)) as ProjectConfig;
config.project = config.project || {};
// resolve includes
const includedMeta = await includedMetadata(
dir,
config,
configSchema,
);
const metadata = includedMeta.metadata;
files.push(...includedMeta.files);
config = mergeProjectMetadata(config, metadata);
delete config[kMetadataFile];
delete config[kMetadataFiles];
return { config, files };
} else {
return undefined;
}
};
}
type ProjectTypeDetector = {
type: string;
detect: string[][];
};
async function projectExtensionsConfigResolver(
context: ExtensionContext,
dir: string,
) {
// load built-in project types and see if they have detectors
const projectTypeDetectors: ProjectTypeDetector[] =
(await context.extensions(dir)).reduce(
(projectTypeDetectors, extension) => {
if (extension.contributes.project) {
const project = extension.contributes.project as
| ProjectConfig
| undefined;
if (project?.project?.detect) {
const detect = asArray<string[]>(project?.project.detect);
projectTypeDetectors.push({
type: extension.id.name,
detect,
});
}
}
return projectTypeDetectors;
},
[] as ProjectTypeDetector[],
);
// function that will run the detectors on a directory
return (dir: string): Promise<ResolvedProjectConfig | undefined> => {
// look for the detector files
for (const detector of projectTypeDetectors) {
if (
detector.detect.some((files) =>
files.every((file) => safeExistsSync(join(dir, file)))
)
) {
return Promise.resolve({
config: {
project: {
type: detector.type,
},
},
files: [],
});
}
}
return Promise.resolve(undefined);
};
}
async function resolveProjectExtension(
context: ExtensionContext,
projectType: string,
projectConfig: ProjectConfig,
dir: string,
) {
// Find extensions
const extensions = await context.find(
projectType,
dir,
"project",
projectConfig,
dir,
);
// filter the extensions to resolve duplication
const filtered = filterExtensions(extensions, projectType, "project");
if (filtered.length > 0) {
const extension = filtered[0];
const projectExt = extension.contributes.project;
if (projectExt) {
// alias and clone (as we may mutate)
const projectExtConfig = ld.cloneDeep(projectExt) as ProjectConfig;
// remove the 'detect' field from the ext as that's just for bootstrapping
delete projectExtConfig.project.detect;
// user render config should fully override the extension config
if (projectConfig.project.render) {
delete projectExtConfig.project.render;
}
// Ensure that we replace the project type with a
// system supported project type (rather than the extension name)
const extProjType = () => {
const projectMeta = projectExt.project;
if (projectMeta && typeof projectMeta === "object") {
const extType = (projectMeta as Record<string, unknown>).type;
if (typeof extType === "string") {
return extType;
} else {
return "default";
}
} else {
return "default";
}
};
projectConfig.project[kProjectType] = extProjType();
// Merge config
projectConfig = mergeProjectMetadata(
projectExtConfig,
projectConfig,
);
}
}
return projectConfig;
}
export async function resolveEngineExtensions(
context: ExtensionContext,
projectConfig: ProjectConfig,
dir: string,
) {
// First, resolve any relative paths in existing project engines
if (projectConfig.engines) {
projectConfig.engines =
(projectConfig.engines as (string | ExternalEngine)[]).map(
(engine) => {
if (
typeof engine === "object" && engine.path &&
!isAbsolute(engine.path)
) {
// Convert relative path to absolute path based on project directory
return {
...engine,
path: join(dir, engine.path),
};
}
return engine;
},
);
}
// Find all extensions that contribute engines
const extensions = await context.extensions(
undefined,
projectConfig,
dir,
);
// Filter to only those with engines
const engineExtensions = extensions.filter((extension) =>
extension.contributes.engines !== undefined &&
extension.contributes.engines.length > 0
);
if (engineExtensions.length > 0) {
// Initialize engines array if needed
if (!projectConfig.engines) {
projectConfig.engines = [];
}
const existingEngines = projectConfig
.engines as (string | ExternalEngine)[];
// Extract and merge engines
const extensionEngines = engineExtensions
.map((extension) => extension.contributes.engines)
.flat();
projectConfig.engines = [...existingEngines, ...extensionEngines];
}
return projectConfig;
}
// migrate 'site' to 'website'
// TODO make this a deprecation warning
function migrateProjectConfig(projectConfig: ProjectConfig) {
const kSite = "site";
if (
projectConfig.project[kProjectType] !== kSite &&
projectConfig[kSite] === undefined
) {
return projectConfig;
}
projectConfig = ld.cloneDeep(projectConfig);
if (projectConfig.project[kProjectType] === kSite) {
projectConfig.project[kProjectType] = kWebsite;
}
if (projectConfig[kSite]) {
projectConfig[kWebsite] = ld.cloneDeep(projectConfig[kSite]);
delete projectConfig[kSite];
}
return projectConfig;
}
async function resolveLanguageTranslations(
projectConfig: ProjectConfig,
dir: string,
) {
const files: string[] = [];
// read any language file pointed to by the project
files.push(...(await resolveLanguageMetadata(projectConfig, dir)));
// read _language.yml and merge into the project
const translations = await readLanguageTranslations(
join(dir, "_language.yml"),
);
projectConfig[kLanguageDefaults] = mergeConfigs(
translations.language,
projectConfig[kLanguageDefaults],
);
files.push(...translations.files);
return files;
}
// read project context (if there is no project config file then still create
// a context (i.e. implicitly treat directory as a project)
export function projectContextForDirectory(
path: string,
notebookContext: NotebookContext,
renderOptions?: RenderOptions,
): Promise<ProjectContext> {
return projectContext(path, notebookContext, renderOptions, true) as Promise<
ProjectContext
>;
}
export function projectYamlFiles(dir: string): string[] {
const files: string[] = [];
// Be sure to ignore directory and paths that we shouldn't inspect
const projIgnoreGlobs = projectHiddenIgnoreGlob(dir);
// Walk through the directory discovering YAML files
for (
const walk of walkSync(dir, {
includeDirs: true,
// this was done b/c some directories e.g. renv/packrat and potentially python
// virtualenvs include symblinks to R or Python libraries that are in turn
// circular. much safer to not follow symlinks!
followSymlinks: false,
skip: [kSkipHidden].concat(
projIgnoreGlobs.map((ignore) => globToRegExp(join(dir, ignore) + SEP)),
),
})
) {
if (walk.isFile && isYamlPath(walk.path)) {
files.push(walk.path);
}
}
return files;
}
function projectHiddenIgnoreGlob(dir: string) {
return projectIgnoreGlobs(dir) // standard ignores for all projects
.concat(["**/_*", "**/_*/**"]) // underscore prefix
.concat(["**/.*", "**/.*/**"]) // hidden (dot prefix)
.concat(["**/README.?([Rrq])md"]) // README
.concat(["**/CLAUDE.md", "**/CLAUDE.local.md"]) // Anthropic claude code file
.concat(["**/AGENTS.md", "**/AGENTS.local.md"]) // https://agents.md/
.concat(["**/*.llms.md"]); // llms.txt companion markdown files
}
export const projectInputFiles = makeTimedFunctionAsync(
"projectInputFiles",
projectInputFilesInternal,
);
async function projectInputFilesInternal(
project: ProjectContext,
metadata?: ProjectConfig,
): Promise<{ files: string[]; engines: string[] }> {
// Resolve engines so engineIgnoreDirs() uses all engines (including external)
await resolveEngines(project);
const { dir } = project;
const outputDir = metadata?.project[kProjectOutputDir];
// Ignore project standard and hidden files
const projIgnoreGlobs = projectHiddenIgnoreGlob(dir);
// map to regex
const projectIgnores = projIgnoreGlobs.map((glob) =>
globToRegExp(glob, { extended: true, globstar: true })
);
type FileInclusion = {
file: string;
engineName: string;
engineIntermediates: string[];
};
const addFile = async (file: string): Promise<FileInclusion[]> => {
// ignore the file if it is in the output directory
if (
outputDir &&
ensureTrailingSlash(dirname(file)).startsWith(
ensureTrailingSlash(join(dir, outputDir)),
) &&
ensureTrailingSlash(join(dir, outputDir)).startsWith(
ensureTrailingSlash(dir),
)
) {
return [];
}
const engine = await fileExecutionEngine(file, undefined, project);
// ignore the file if there's no engine to handle it
if (!engine) {
return [];
}
const engineIntermediates = executionEngineIntermediateFiles(
engine,
file,
);
return [{
file,
engineName: engine.name,
engineIntermediates: engineIntermediates,
}];
};
const addDir = async (dir: string): Promise<FileInclusion[]> => {
const promises: Promise<FileInclusion[]>[] = [];
for await (
const walkEntry of walk(dir, {
includeDirs: false,
// this was done b/c some directories e.g. renv/packrat and potentially python
// virtualenvs include symblinks to R or Python libraries that are in turn
// circular. much safer to not follow symlinks!
followSymlinks: false,
skip: [kSkipHidden].concat(
engineIgnoreDirs().map((ignore) =>
globToRegExp(join(dir, ignore) + SEP)
),
),
})
) {
const pathRelative = pathWithForwardSlashes(
relative(dir, walkEntry.path),
);
if (projectIgnores.some((regex) => regex.test(pathRelative))) {
continue;
}
promises.push(addFile(walkEntry.path));
}
const inclusions = await Promise.all(promises);
return inclusions.flat();
};
const addEntry = async (entry: string) => {
if (Deno.statSync(entry).isDirectory) {
return addDir(entry);
} else {
return addFile(entry);
}
};
const renderFiles = metadata?.project[kProjectRender];
let inclusions: FileInclusion[];
if (renderFiles) {
const exclude = projIgnoreGlobs.concat(outputDir ? [outputDir] : []);
const resolved = resolvePathGlobs(dir, renderFiles, exclude, {
mode: "auto",
});
const toInclude = ld.difference(
resolved.include,
resolved.exclude,
) as string[];
inclusions = (await Promise.all(toInclude.map(addEntry))).flat();
} else {
inclusions = await addDir(dir);
}
const files = inclusions.map((inclusion) => inclusion.file);
const engines = ld.uniq(inclusions.map((inclusion) => inclusion.engineName));
const intermediateFiles = inclusions.map((inclusion) =>