dotfiles/build/util.js
Janis Hutz c2f38bc39a [Build] Improve rendering, add fs parsing
FS parsing needs a lot of improvements (namely ignoring gitignore
files). Probably gonna migrate to a library for this
2025-03-25 20:06:50 +01:00

46 lines
1.5 KiB
JavaScript

const convert = require( 'color-convert' );
const fs = require( 'fs' );
const path = require( 'path' );
/**
* Recursively find all HTML files in a directory
* @param {string} dir The directory to search. Either absolute or relative path
* @param {string} extension The file extension to look for
* @returns {string[]} returns a list of html files with their full path
*/
const treeWalker = ( dir, extension ) => {
const ls = fs.readdirSync( dir );
const fileList = [];
for ( let file in ls ) {
if ( fs.statSync( path.join( dir, ls[ file ] ) ).isDirectory() ) {
const newFiles = treeWalker( path.join( dir, ls[ file ] ), extension );
// TODO: Ignore files in .gitignore, as well as .gitignore
if ( !newFiles.includes( '.configbuildignore' ) ) {
for ( let file in newFiles ) {
fileList.push( newFiles[ file ] );
}
}
} else if ( extension == '*' ) {
fileList.push( path.join( dir, ls[ file ] ) );
} else if ( ls[ file ].includes( extension ) ) {
fileList.push( path.join( dir, ls[ file ] ) );
}
}
return fileList;
}
const renderColourAsHex = ( colour ) => {
return '#' + convert.default.rgb.hex( colour[ 0 ], colour[ 1 ], colour[ 2 ] );
}
const renderColourAsRGB = ( colour ) => {
return `rgb( ${ colour[ 0 ] }, ${ colour[ 1 ] }, ${ colour[ 2 ] } )`
}
module.exports = {
treeWalker,
renderColourAsHex,
renderColourAsRGB
}