136 lines
3.4 KiB
TypeScript
136 lines
3.4 KiB
TypeScript
|
|
import * as fs from "fs";
|
|
import * as path from "path";
|
|
import { RJLog } from "../log/RJLog";
|
|
import { Files } from "../files/Files";
|
|
import { PathReference } from "../files/PathReference";
|
|
|
|
export class TemplatesInfo
|
|
{
|
|
templates:string[] = [];
|
|
}
|
|
|
|
export class TemplatesIndexBuilder
|
|
{
|
|
inputDirectories:string[];
|
|
outputDir:string;
|
|
|
|
constructor( source:string[], build:string )
|
|
{
|
|
this.inputDirectories = source;
|
|
this.outputDir = build;
|
|
}
|
|
|
|
filterFiles( fileName:string )
|
|
{
|
|
if ( fileName.startsWith( "__" ) )
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return fileName.endsWith( ".html" );
|
|
}
|
|
|
|
|
|
apply( compiler:any )
|
|
{
|
|
compiler.hooks.afterCompile.tapAsync( "TemplatesIndexBuilder",
|
|
async ( compilation:any, callback:any ) =>
|
|
{
|
|
for ( let inputDirectory of this.inputDirectories )
|
|
{
|
|
await Files.forAllIn( inputDirectory, null,
|
|
async ( p:PathReference ) =>
|
|
{
|
|
if ( await p.isDirectory() )
|
|
{
|
|
return Promise.resolve();
|
|
}
|
|
|
|
let fileName = p.fileName;
|
|
|
|
if ( ! this.filterFiles( fileName ) )
|
|
{
|
|
return Promise.resolve();
|
|
}
|
|
|
|
compilation.fileDependencies.add( p.absolutePath );
|
|
|
|
return Promise.resolve();
|
|
}
|
|
);
|
|
}
|
|
|
|
callback();
|
|
|
|
}
|
|
);
|
|
|
|
compiler.hooks.emit.tapAsync( "TemplatesIndexBuilder",
|
|
async ( compilation:any, callback:any ) =>
|
|
{
|
|
let templates = new TemplatesInfo();
|
|
|
|
for ( let inputDirectory of this.inputDirectories )
|
|
{
|
|
let templatesPathReference = new PathReference( path.resolve( inputDirectory ) );
|
|
|
|
// RJLog.log( templatesPathReference.absolutePath );
|
|
|
|
await Files.forAllIn( templatesPathReference.absolutePath, null,
|
|
async ( p:PathReference ) =>
|
|
{
|
|
if ( await p.isDirectory() )
|
|
{
|
|
return Promise.resolve();
|
|
}
|
|
|
|
let fileName = p.fileName;
|
|
|
|
if ( ! this.filterFiles( fileName ) )
|
|
{
|
|
return Promise.resolve();
|
|
}
|
|
|
|
let absoluteFilePath = p.absolutePath;
|
|
let inputRelativePath = templatesPathReference.createRelativeFromAbsolute( absoluteFilePath, true );
|
|
|
|
// RJLog.log( "Abs to Rel", absoluteFilePath, inputRelativePath.relativePath );
|
|
let outputFileName = path.join( this.outputDir, inputRelativePath.relativePath );
|
|
|
|
templates.templates.push( inputRelativePath.relativePath );
|
|
|
|
let content = fs.readFileSync( p.absolutePath, "utf-8" );
|
|
|
|
// RJLog.log( "Adding", p.absolutePath, outputFileName );
|
|
compilation.assets[ outputFileName ] =
|
|
{
|
|
source: () => content,
|
|
size: () => content.length,
|
|
};
|
|
|
|
|
|
return Promise.resolve();
|
|
}
|
|
);
|
|
|
|
}
|
|
|
|
let templatesPath = path.join( this.outputDir, "index.json" );
|
|
|
|
let templatesJSON = JSON.stringify( templates, null, " " );
|
|
|
|
compilation.assets[ templatesPath ] =
|
|
{
|
|
source: () => templatesJSON,
|
|
size: () => templatesJSON.length,
|
|
};
|
|
|
|
callback();
|
|
|
|
return Promise.resolve();
|
|
}
|
|
);
|
|
}
|
|
}
|