importtype{PageSettings}from"../../types/site";import{File}from".";/** * Convert a file, wrapping that a file in a parent. * The parent file dispatches to a cloned child file for info. */constwrapFile=(sourceFile: File,// the ts filegetText: (source: File)=>string,{
extension,
addExtension =false,mimeType: mimeTypeArgument,}: {extension: string;// append the extension instead of replacing itaddExtension?: boolean;mimeType?: string;},getDependencies: (source: File,settings: PageSettings)=>File[]=()=>[])=>{constwrappingFile=sourceFile.clone();wrappingFile.fakeFileOf=sourceFile;wrappingFile.path=sourceFile.path[addExtension ? "addExtension" : "replaceExtension"](extension);wrappingFile.write=(config: PageSettings)=>{const{ sourceDir, targetDir }=config;// Write the source file// This has to be done first - the target file could be dependentsourceFile.write(config);// Write the wrapping filelettargetPath=wrappingFile.path.relativeTo(sourceDir,targetDir);if(sourceFile.isDirectory()&&extension==="html"){// Convert file with name `x/folder.html` to name `x/folder/index.html`targetPath=targetPath.replaceExtension().join("/index.html");}targetPath.writeString(wrappingFile.text(config));returnwrappingFile;};wrappingFile.dependencies=(settings: PageSettings)=>{returngetDependencies(sourceFile,settings);};wrappingFile.text=()=>getText(sourceFile);wrappingFile.isDirectory=()=>sourceFile.isDirectory();Object.defineProperty(wrappingFile,"mimeType",{get(){returnmimeTypeArgument??wrappingFile.path.mimeType;},});Object.defineProperty(wrappingFile,"require",{get(){returnrequire(sourceFile.path.toString());},});returnwrappingFile;// the js file};export{wrapFile};