112 lines
2.5 KiB
TypeScript
112 lines
2.5 KiB
TypeScript
![]() |
|
||
|
import * as fs from "fs";
|
||
|
import * as path from "path";
|
||
|
import { RJLog } from "../log/RJLog";
|
||
|
|
||
|
export class PagesInfo
|
||
|
{
|
||
|
pages:string[] = [];
|
||
|
}
|
||
|
|
||
|
export class PHPPagesBuilder
|
||
|
{
|
||
|
inputDir:string;
|
||
|
outputDir:string;
|
||
|
|
||
|
constructor( source:string, build:string )
|
||
|
{
|
||
|
this.inputDir = source;
|
||
|
this.outputDir = build;
|
||
|
}
|
||
|
|
||
|
filterFiles( fileName:string )
|
||
|
{
|
||
|
if ( fileName.startsWith( "__" ) )
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
return fileName.endsWith( ".html" );
|
||
|
}
|
||
|
|
||
|
modify( content:string )
|
||
|
{
|
||
|
return `<?php ?>\n${content}`;
|
||
|
}
|
||
|
|
||
|
apply( compiler:any )
|
||
|
{
|
||
|
compiler.hooks.afterCompile.tapAsync( "PHPPagesBuilder",
|
||
|
( compilation:any, callback:any ) =>
|
||
|
{
|
||
|
|
||
|
if ( fs.existsSync( this.inputDir ) )
|
||
|
{
|
||
|
fs.readdirSync( this.inputDir )
|
||
|
.filter( this.filterFiles )
|
||
|
.forEach(
|
||
|
( file ) =>
|
||
|
{
|
||
|
let fullPath = path.join(this.inputDir, file);
|
||
|
compilation.fileDependencies.add(fullPath); // Mark for watching
|
||
|
}
|
||
|
);
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
callback();
|
||
|
|
||
|
}
|
||
|
);
|
||
|
|
||
|
compiler.hooks.emit.tapAsync( "PHPPagesBuilder",
|
||
|
( compilation:any, callback:any ) =>
|
||
|
{
|
||
|
fs.readdir( this.inputDir,
|
||
|
( err, files ) =>
|
||
|
{
|
||
|
if ( err )
|
||
|
{
|
||
|
RJLog.log( "Error", this.inputDir );
|
||
|
return callback( err );
|
||
|
}
|
||
|
|
||
|
let pages = new PagesInfo();
|
||
|
files.filter( this.filterFiles ).forEach( ( file ) =>
|
||
|
{
|
||
|
let filePath = path.join( this.inputDir, file );
|
||
|
let content = fs.readFileSync( filePath, "utf-8" );
|
||
|
let modifiedContent = this.modify( content );
|
||
|
let phpFileName = file.replace( ".html", ".php" );
|
||
|
let outputFileName = path.join( this.outputDir, phpFileName );
|
||
|
|
||
|
pages.pages.push( phpFileName );
|
||
|
|
||
|
compilation.assets[ outputFileName ] =
|
||
|
{
|
||
|
source: () => modifiedContent,
|
||
|
size: () => modifiedContent.length,
|
||
|
};
|
||
|
}
|
||
|
);
|
||
|
|
||
|
let pagesPath = path.join( this.outputDir, "pages.json" );
|
||
|
|
||
|
let pagesJSON = JSON.stringify( pages, null, " " );
|
||
|
|
||
|
compilation.assets[ pagesPath ] =
|
||
|
{
|
||
|
source: () => pagesJSON,
|
||
|
size: () => pagesJSON.length,
|
||
|
};
|
||
|
|
||
|
callback();
|
||
|
|
||
|
}
|
||
|
);
|
||
|
}
|
||
|
);
|
||
|
}
|
||
|
}
|