[Build] Improve

This commit is contained in:
2026-03-01 17:05:40 +01:00
parent 4095a83a75
commit 2a67982d54

View File

@@ -1,4 +1,5 @@
const fs = require( 'fs' ); const fs = require( 'fs' );
const navMenu = '' + fs.readFileSync( './src/nav.html' ); const navMenu = '' + fs.readFileSync( './src/nav.html' );
const footer = '' + fs.readFileSync( './src/footer.html' ); const footer = '' + fs.readFileSync( './src/footer.html' );
@@ -11,9 +12,11 @@ const footer = '' + fs.readFileSync( './src/footer.html' );
const treeWalker = ( dir, extension ) => { const treeWalker = ( dir, extension ) => {
const ls = fs.readdirSync( dir ); const ls = fs.readdirSync( dir );
const fileList = []; const fileList = [];
for ( let file in ls ) { for ( let file in ls ) {
if ( !ls[ file ].includes( '.' ) ) { if ( !ls[ file ].includes( '.' ) ) {
const newFiles = treeWalker( dir + '/' + ls[ file ], extension ); const newFiles = treeWalker( dir + '/' + ls[ file ], extension );
for ( let file in newFiles ) { for ( let file in newFiles ) {
fileList.push( newFiles[ file ] ); fileList.push( newFiles[ file ] );
} }
@@ -23,15 +26,30 @@ const treeWalker = ( dir, extension ) => {
} }
return fileList; return fileList;
} };
const addNavAndFooterToFile = ( file ) => { const addNavAndFooterToFile = file => {
const f = '' + fs.readFileSync( file ); const f = '' + fs.readFileSync( file );
const navIndex = f.indexOf( '<nav>' ) + 5;
const footerIndex = f.indexOf( '<footer>' ) + 8; const footerIndex = f.indexOf( '<footer>' ) + 8;
return f.slice( 0, navIndex ) + navMenu + f.slice( navIndex, footerIndex ) + footer + f.substring( footerIndex );
} let navIndex = f.indexOf( '<nav>' ) + 5;
let out = '';
if ( navIndex >= 5 ) {
out += f.slice( 0, navIndex ) + navMenu;
} else {
navIndex = 0;
}
if ( footerIndex >= 8 ) {
out += f.slice( navIndex, footerIndex ) + footer + f.substring( footerIndex );
} else {
out += f.substring( navIndex );
}
return out;
};
/** /**
* Save a file to a path. All occurrences of "src" will be replaced by "dist" * Save a file to a path. All occurrences of "src" will be replaced by "dist"
@@ -42,19 +60,23 @@ const addNavAndFooterToFile = ( file ) => {
const saveFile = ( filePath, data ) => { const saveFile = ( filePath, data ) => {
const nPath = filePath.replace( 'src', 'dist' ); const nPath = filePath.replace( 'src', 'dist' );
const dirSplit = nPath.split( '/' ); const dirSplit = nPath.split( '/' );
let currDir = nPath.slice( 0, nPath.indexOf( '/' ) + 1 ); let currDir = nPath.slice( 0, nPath.indexOf( '/' ) + 1 );
for ( let dir in dirSplit ) { for ( let dir in dirSplit ) {
if ( dirSplit[ dir ] !== '.' && !dirSplit[ dir ].includes( '.' ) ) { if ( dirSplit[ dir ] !== '.' && !dirSplit[ dir ].includes( '.' ) ) {
currDir += dirSplit[ dir ] + '/'; currDir += dirSplit[ dir ] + '/';
try { try {
fs.readdirSync( currDir ); fs.readdirSync( currDir );
} catch ( e ) { } catch {
fs.mkdirSync( currDir ); fs.mkdirSync( currDir );
} }
} }
} }
fs.writeFileSync( nPath, data ); fs.writeFileSync( nPath, data );
} };
const copyFiles = ( dir, extension ) => { const copyFiles = ( dir, extension ) => {
const files = treeWalker( dir, extension ); const files = treeWalker( dir, extension );
@@ -62,7 +84,7 @@ const copyFiles = ( dir, extension ) => {
for ( let file in files ) { for ( let file in files ) {
saveFile( files[ file ], '' + fs.readFileSync( files[ file ] ) ); saveFile( files[ file ], '' + fs.readFileSync( files[ file ] ) );
} }
} };
const filesToParse = treeWalker( './src', '.html' ); const filesToParse = treeWalker( './src', '.html' );