swaf/src/frontend/CopyAssetPreCompiler.ts

32 lines
1.2 KiB
TypeScript

import {promises as fs} from "fs";
import path from "path";
import {logger} from "../Logger.js";
import AssetPreCompiler from "./AssetPreCompiler.js";
export default class CopyAssetPreCompiler extends AssetPreCompiler {
protected readonly actualTargetDir: string;
public constructor(
targetDir: string,
assetType: string,
extension: string,
additionalFallbackAssetPaths: string[] = [],
outputToPublic: boolean = true,
overrideTargetAssetType?: string,
) {
super(targetDir, assetType, extension, outputToPublic, ...additionalFallbackAssetPaths);
this.actualTargetDir = overrideTargetAssetType ?
path.join(targetDir, overrideTargetAssetType) :
this.targetDir;
}
public async preCompile(canonicalName: string): Promise<void> {
const inputFile = await this.resolveFileFromCanonicalNameOrFail(canonicalName);
const outputFile = path.join(this.actualTargetDir, canonicalName);
logger.info('Copying', inputFile, '->', outputFile);
await fs.mkdir(path.dirname(outputFile), {recursive: true});
await fs.copyFile(inputFile, outputFile);
}
}