dotfiles/build/helpers/render.js
2025-04-26 12:08:16 +02:00

69 lines
2.8 KiB
JavaScript

const mustache = require( 'mustache' );
const colorThief = require( '@janishutz/colorthief' );
const fs = require( 'fs' );
const path = require( 'path' );
const util = require( './util' );
const generateTheme = require( './generateTheme' );
const build = ( wallpaper, lockpaper, theme ) => {
console.log( '\n=> Extracting colours' );
// Extract colour palette from chosen wallpaper using Color-Thief
colorThief.getPalette( wallpaper ).then( palette => {
// Define view options (for rendering with mustache)
if ( theme === 'test' ) {
palette = [ [ 255, 0, 0 ], [ 0, 255, 0 ], [ 0, 0, 255 ] ];
}
const view = generateTheme.generateTheme( theme, wallpaper, lockpaper, palette );
try {
fs.mkdir( path.join( __dirname, '/dist' ) );
} catch ( e ) {
}
// recursively index files from config directory -> Maybe add a file to each
// directory to indicate whether or not to index files in it?
const fileList = util.treeWalker( path.join( __dirname, '/../../renderable/' ), '*', [ 'node_modules', '@girs', '.gitignore', '.git', 'flavours' ] );
for (let index = 0; index < fileList.length; index++) {
try {
render( fileList[ index ], view );
} catch ( e ) {
console.error( '=> Render failed for ' + fileList[ index ] + ' with error ' + e );
}
}
util.themePreProcessor( path.join( __dirname, '/../../gtk-theme/src/gtk-4.0/gtk.css' ), 'src', 'dist' );
util.themePreProcessor( path.join( __dirname, '/../../gtk-theme/src/gtk-3.0/gtk.css' ), 'src', 'dist' );
render( path.join( __dirname, '/../../gtk-theme/src/colours.css' ), view, 'src', 'dist' );
} ).catch( e => {
console.error( e );
console.error( '\n=> Failed to load image or retrieve colour palette from it' );
} );
}
/**
* @param {string} templatePath - absolute path to config directory
* @param {object} view - rendering config passed to mustache
* @param {string} originalDir - The original directory, defaults to renderable
* @param {string} newDir - the output directory override, defaults to config
*/
const render = ( templatePath, view, originalDir = 'renderable', newDir = 'config' ) => {
// Load template from disk (all can be found in <project-root>/renderable)
// TODO: Make exclusion better plus copy other files maybe?
const template = '' + fs.readFileSync( templatePath );
const outPath = path.join( templatePath.replace( originalDir, newDir ) );
console.log( '=> Rendering to ' + outPath );
try {
fs.mkdirSync( path.dirname( outPath ), {
recursive: true,
} );
} catch ( e ) {
console.error( e );
}
fs.writeFileSync( outPath, mustache.render( template, view ) );
}
module.exports = build;