|
5 | 5 | * |
6 | 6 | */ |
7 | 7 |
|
| 8 | +import { existsSync } from "fs/exists.ts"; |
| 9 | + |
8 | 10 | // ensures cell handlers are installed |
9 | 11 | import "../../core/handlers/handlers.ts"; |
10 | 12 |
|
@@ -56,7 +58,7 @@ import { |
56 | 58 | } from "./types.ts"; |
57 | 59 | import { error, info } from "log/mod.ts"; |
58 | 60 | import * as ld from "../../core/lodash.ts"; |
59 | | -import { basename, dirname, join, relative } from "path/mod.ts"; |
| 61 | +import { basename, dirname, isAbsolute, join, relative } from "path/mod.ts"; |
60 | 62 | import { Format } from "../../config/types.ts"; |
61 | 63 | import { figuresDir, inputFilesDir } from "../../core/render.ts"; |
62 | 64 | import { removeIfEmptyDir, removeIfExists } from "../../core/path.ts"; |
@@ -417,6 +419,13 @@ export async function renderFiles( |
417 | 419 | ); |
418 | 420 | resourceFiles.push(...ojsResourceFiles); |
419 | 421 |
|
| 422 | + // now that all execution is done and supportign files have been |
| 423 | + // contributed, normalize the supporting files so there is no overlap |
| 424 | + executeResult.supporting = normalizeSupporting( |
| 425 | + context.target.source, |
| 426 | + executeResult.supporting, |
| 427 | + ); |
| 428 | + |
420 | 429 | // keep md if requested |
421 | 430 | const keepMd = executionEngineKeepMd(context.target.input); |
422 | 431 | if (keepMd && context.format.execute[kKeepMd]) { |
@@ -486,3 +495,27 @@ class RenderInvalidYAMLError extends YAMLValidationError { |
486 | 495 | super("Render failed due to invalid YAML."); |
487 | 496 | } |
488 | 497 | } |
| 498 | + |
| 499 | +function normalizeSupporting(source: string, supporting: string[]): string[] { |
| 500 | + // first ensure all paths are absolute and normalized |
| 501 | + const dir = dirname(source); |
| 502 | + supporting = supporting.map((file) => |
| 503 | + isAbsolute(file) ? file : join(dir, file) |
| 504 | + ); |
| 505 | + |
| 506 | + // filter on existence |
| 507 | + supporting = supporting.filter(existsSync); |
| 508 | + |
| 509 | + // any file that is within another dir in the list is removed |
| 510 | + const parentDirs = supporting.filter((file) => |
| 511 | + Deno.statSync(file).isDirectory |
| 512 | + ); |
| 513 | + supporting = supporting.filter((file) => |
| 514 | + !parentDirs.some((parentDir) => |
| 515 | + file.startsWith(parentDir) && file !== parentDir |
| 516 | + ) |
| 517 | + ); |
| 518 | + |
| 519 | + // now de-dupe and make all paths relative |
| 520 | + return ld.uniq(supporting.map((file) => relative(dir, file))); |
| 521 | +} |
0 commit comments