Path: blob/main/src/command/render/pandoc-dependencies-resources.ts
3583 views
/*1* pandoc-dependencies-resources.ts2*3* Copyright (C) 2020-2022 Posit Software, PBC4*/56import {7kFormatResources,8kResources,9kSupporting,10} from "../../config/constants.ts";11import { copyTo } from "../../core/copy.ts";12import { lines } from "../../core/text.ts";1314import { basename, isAbsolute, join, relative } from "../../deno_ral/path.ts";15import {16appendDependencies,17FormatResourceDependency,18} from "./pandoc-dependencies.ts";19import { existsSync } from "../../deno_ral/fs.ts";2021export interface Resource {22file: string;23}2425// Populates the dependency file with format resources26// from typescript.27export async function writeFormatResources(28inputDir: string,29dependenciesFile: string,30formatResources: string | string[] | undefined,31) {32if (formatResources) {33const files = Array.isArray(formatResources)34? formatResources35: [formatResources];3637const dependencies: FormatResourceDependency[] = files.map((file) => {38const absPath = join(inputDir, file);39if (!existsSync(absPath)) {40throw new Error(41`The referenced format resource '${file}' does not exist.`,42);43}44return {45type: kFormatResources,46content: { file: absPath },47};48});49await appendDependencies(dependenciesFile, dependencies);50}51}5253// Processes the dependency file to copy54export async function processFormatResources(55inputDir: string,56dependenciesFile: string,57) {58// Read the dependency file59const resources: string[] = [];60const supporting: string[] = [];61const dependencyJsonStream = await Deno.readTextFile(dependenciesFile);62for (const jsonBlob of lines(dependencyJsonStream)) {63if (jsonBlob) {64// Read the dependency and process format resources65const dependency = JSON.parse(jsonBlob);66if (dependency.type === kFormatResources) {67// Copy the file to the input directory68const formatResource = dependency.content as Resource;69const targetPath = join(inputDir, basename(formatResource.file));70copyTo(71formatResource.file,72targetPath,73{74overwrite: true,75preserveTimestamps: true,76},77);78} else if (dependency.type === kResources) {79const resource = dependency.content as Resource;80resources.push(81isAbsolute(resource.file)82? relative(inputDir, resource.file)83: resource.file,84);85} else if (dependency.type === kSupporting) {86const supportingResource = dependency.content as Resource;87supporting.push(supportingResource.file);88}89}90}91return { supporting, resources };92}939495