185 lines
		
	
	
		
			7.5 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			185 lines
		
	
	
		
			7.5 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| const mustache = require( 'mustache' );
 | |
| const colorThief = require( '@janishutz/colorthief' );
 | |
| const fs = require( 'fs' );
 | |
| const path = require( 'path' );
 | |
| const util = require( './util' );
 | |
| const renderColourAsRGB = util.renderColourAsRGB;
 | |
| const renderColourAsRGBA = util.renderColourAsRGBA;
 | |
| const renderColourAsHex = util.renderColourAsHex;
 | |
| 
 | |
| 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)
 | |
|         const view = {
 | |
|             'wallpaper-path': wallpaper,
 | |
|             'lockpaper-path': lockpaper,
 | |
| 
 | |
|             // Colours
 | |
|             'colour-foreground-hex': renderColourAsHex( colours.foreground[ theme ] ),
 | |
|             'colour-foreground-rgb': renderColourAsRGB( colours.foreground[ theme ] ),
 | |
|             'colour-foreground-rgba': renderColourAsRGBA( colours.foreground[ theme ], 0.5 ),
 | |
|             'colour-accent-hex': renderColourAsHex( palette[ 0 ] ),
 | |
|             'colour-accent-rgb': renderColourAsRGB( palette[ 0 ] ),
 | |
|             'colour-accent-rgba': renderColourAsRGBA( palette[ 0 ], 0.3 ),
 | |
|             'colour-accent-2-hex': renderColourAsHex( palette[ 1 ] ),
 | |
|             'colour-accent-2-rgb': renderColourAsRGB( palette[ 1 ] ),
 | |
|             'colour-accent-3-hex': renderColourAsHex( palette[ 2 ] ),
 | |
|             'colour-accent-3-rgb': renderColourAsRGB( palette[ 2 ] ),
 | |
|             'colour-background-hex': renderColourAsHex( colours.background[ theme ] ),
 | |
|             'colour-background-rgb': renderColourAsRGB( colours.background[ theme ] ),
 | |
|             'colour-background-rgba': renderColourAsRGBA( colours.background[ theme ], 0.5 ),
 | |
|             'colour-background-alternative-hex': renderColourAsHex( colours[ 'background-alternative' ][ theme ] ),
 | |
|             'colour-background-alternative-hex': renderColourAsRGB( colours[ 'background-alternative' ][ theme ] ),
 | |
|             'colour-shadow-hex': renderColourAsHex( colours.shadow[ theme ] ),
 | |
|             'colour-shadow-rgb': renderColourAsRGB( colours.shadow[ theme ] ),
 | |
|             'colour-shadow-rgba': renderColourAsRGBA( colours.shadow[ theme ], 0.3 ),
 | |
|             'colour-inactive-hex': renderColourAsHex( colours.inactive[ theme ] ),
 | |
|             'colour-inactive-rgb': renderColourAsRGB( colours.inactive[ theme ] ),
 | |
|             'colour-inactive-background-hex': renderColourAsHex( colours[ 'inactive-background' ][ theme ] ),
 | |
|             'colour-inactive-background-rgb': renderColourAsRGB( colours[ 'inactive-background' ][ theme ] ),
 | |
| 
 | |
|             // Fonts
 | |
|             'font-primary': fonts.primary[ theme ],
 | |
|             'font-accent': fonts.accent[ theme ],
 | |
|             'font-mono': fonts.mono[ theme ],
 | |
| 
 | |
|             // yazi theme
 | |
|             'yazi-theme': yaziThemes[ theme ],
 | |
| 
 | |
|             // Path to this repo on disk
 | |
|             'path-to-dotfiles': __dirname.slice(0, __dirname.length - 5),
 | |
|         }
 | |
| 
 | |
|         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' );
 | |
|         util.themePreProcessor( path.join( __dirname, '/../gtk-theme/src/gtk-3.0/gtk.css' ), 'src' );
 | |
|         render( path.join( __dirname, '/../gtk-theme/temp/gtk-3.0/gtk.css' ), view, 'temp', 'dist' );
 | |
|         render( path.join( __dirname, '/../gtk-theme/temp/gtk-4.0/gtk.css' ), view, 'temp', 'dist' );
 | |
|         // TODO: Copy over to /usr/share/themes/Adaptive-Theme/
 | |
|     } ).catch( e => {
 | |
|         console.error( e );
 | |
|         console.error( '\n=> Failed to load image or retrieve colour palette from it' );
 | |
|     } );
 | |
| }
 | |
| 
 | |
| const colours = {
 | |
|     foreground: {
 | |
|         'nordic': [ 10, 220, 255 ],
 | |
|         'deep-dark': [ 230, 230, 230 ],
 | |
|         'material': [ 255, 255, 255 ], // TODO: Will be calculated by material theme generator
 | |
|         'light': [ 40, 40, 40 ],
 | |
|         'bright': [ 0, 0, 0 ]
 | |
|     },
 | |
|     background: {
 | |
|         'nordic': [ 10, 0, 50 ],
 | |
|         'deep-dark': [ 20, 20, 20 ],
 | |
|         'material': [ 30, 30, 30 ], // TODO: Will be calculated by material theme generator
 | |
|         'light': [ 230, 230, 230 ],
 | |
|         'bright': [ 255, 255, 255 ]
 | |
|     },
 | |
|     'background-alternative': {
 | |
|         'nordic': [ 15, 5, 60 ],
 | |
|         'deep-dark': [ 30, 30, 30 ],
 | |
|         'material': [ 40, 40, 40 ], // TODO: Will be calculated by material theme generator
 | |
|         'light': [ 210, 210, 210 ],
 | |
|         'bright': [ 230, 230, 230 ]
 | |
|     },
 | |
|     shadow: {
 | |
|         'nordic': [ 10, 0, 60 ],
 | |
|         'deep-dark': [ 20, 20, 20 ],
 | |
|         'material': [ 30, 30, 30 ], // TODO: Will be calculated by material theme generator
 | |
|         'light': [ 230, 230, 230 ],
 | |
|         'bright': [ 255, 255, 255 ]
 | |
|     },
 | |
|     inactive: {
 | |
|         'nordic': [ 200, 200, 200 ],
 | |
|         'deep-dark': [ 200, 200, 200 ],
 | |
|         'material': [ 200, 200, 200 ], // TODO: Will be calculated by material theme generator
 | |
|         'light': [ 65, 65, 65 ],
 | |
|         'bright': [ 60, 60, 60 ]
 | |
|     },
 | |
|     'inactive-background': {
 | |
|         'nordic': [ 0, 0, 0 ],
 | |
|         'deep-dark': [ 0, 0, 0 ],
 | |
|         'material': [ 255, 255, 255 ], // TODO: Will be calculated by material theme generator
 | |
|         'light': [ 200, 200, 200 ],
 | |
|         'bright': [ 60, 60, 60 ]
 | |
|     }
 | |
| }
 | |
| 
 | |
| const fonts = {
 | |
|     'primary': {
 | |
|         'nordic': 'Comfortaa',
 | |
|         'deep-dark': 'Comfortaa',
 | |
|         'material': 'Comfortaa',
 | |
|         'light': 'Adwaita Sans',
 | |
|         'bright': 'Adwaita Sans Extralight'
 | |
|     },
 | |
|     'accent': {
 | |
|         'nordic': 'Adwaita Sans',
 | |
|         'deep-dark': 'Adwaita Sans',
 | |
|         'material': 'Adwaita Sans',
 | |
|         'light': 'Cantarell',
 | |
|         'bright': 'Contarell Thin'
 | |
|     },
 | |
|     'mono': {
 | |
|         'nordic': 'Source Code Pro',
 | |
|         'deep-dark': 'Source Code Pro',
 | |
|         'material': 'Source Code Pro',
 | |
|         'light': 'Jetbrains Mono',
 | |
|         'bright': 'Jetbrains Mono',
 | |
|     }
 | |
| }
 | |
| 
 | |
| const yaziThemes = {
 | |
|     'nordic': 'tokyo-night',
 | |
|     'deep-dark': 'vscode-dark-modern',
 | |
|     'material': 'dracula',
 | |
|     'light': 'vscode-light-modern',
 | |
|     'bright': 'vscode-light-modern',
 | |
| }
 | |
| 
 | |
| 
 | |
| /**
 | |
|  * @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;
 |