[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 navMenu = '' + fs.readFileSync( './src/nav.html' );
const footer = '' + fs.readFileSync( './src/footer.html' );
@@ -11,9 +12,11 @@ const footer = '' + fs.readFileSync( './src/footer.html' );
const treeWalker = ( dir, extension ) => {
const ls = fs.readdirSync( dir );
const fileList = [];
for ( let file in ls ) {
if ( !ls[ file ].includes( '.' ) ) {
const newFiles = treeWalker( dir + '/' + ls[ file ], extension );
for ( let file in newFiles ) {
fileList.push( newFiles[ file ] );
}
@@ -23,15 +26,30 @@ const treeWalker = ( dir, extension ) => {
}
return fileList;
}
};
const addNavAndFooterToFile = ( file ) => {
const addNavAndFooterToFile = file => {
const f = '' + fs.readFileSync( file );
const navIndex = f.indexOf( '<nav>' ) + 5;
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"
@@ -42,19 +60,23 @@ const addNavAndFooterToFile = ( file ) => {
const saveFile = ( filePath, data ) => {
const nPath = filePath.replace( 'src', 'dist' );
const dirSplit = nPath.split( '/' );
let currDir = nPath.slice( 0, nPath.indexOf( '/' ) + 1 );
for ( let dir in dirSplit ) {
if ( dirSplit[ dir ] !== '.' && !dirSplit[ dir ].includes( '.' ) ) {
currDir += dirSplit[ dir ] + '/';
try {
fs.readdirSync( currDir );
} catch ( e ) {
} catch {
fs.mkdirSync( currDir );
}
}
}
fs.writeFileSync( nPath, data );
}
};
const copyFiles = ( dir, extension ) => {
const files = treeWalker( dir, extension );
@@ -62,7 +84,7 @@ const copyFiles = ( dir, extension ) => {
for ( let file in files ) {
saveFile( files[ file ], '' + fs.readFileSync( files[ file ] ) );
}
}
};
const filesToParse = treeWalker( './src', '.html' );