[Build] Improve
This commit is contained in:
36
build.js
36
build.js
@@ -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,16 +26,31 @@ 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"
|
||||
* @param {string} filePath
|
||||
@@ -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' );
|
||||
|
||||
|
||||
Reference in New Issue
Block a user