import { err , log } from "./log" ; import { join , basename , resolve } from "path" ; import { RMParser } from "./ricemood" ; import { getWallpaperFromConfigVariable } from "./wallpaper-grabber" ; import { execFileSync , execSync , spawnSync } from "child_process" ; import { resolvePath } from "../lib/fs-plus" ; interface configStructure { start_tag ? : string ; end_tag ? : string ; imagefile : string ; imagefile_script : string ; const : { [ key ] : string ; } ; quality : number ; [ key ] : any | { filename : string ; realfilepath : string ; } ; } const requiredItem = [ "filename" , "realfilepath" ] ; async function applyConfig ( c , cfgPath ) { const excludeObj = [ "const" ] ; let items = Object . keys ( c ) . filter ( k => c [ k ] instanceof Object && ! excludeObj . includes ( k ) ) ; if ( items . length < 1 ) err ( `There is no configured file yet. Please edit ${ cfgPath . file } first.` ) ; if ( c . imagefile_script ) { c . imagefile = execFileSync ( join ( cfgPath . folder , c . imagefile_script ) ) . toString ( ) ; c . imagefile = c . imagefile . trim ( ) ; } else if ( c . imagefile . startsWith ( "$" ) ) { const imageFromGrabber = getWallpaperFromConfigVariable ( c . imagefile ) ; if ( imageFromGrabber ) c . imagefile = resolvePath ( imageFromGrabber ) ; log ( "getting wallpaper..." ) ; } const { existsSync , readFileSync , writeFileSync } = await import ( "fs" ) ; const { getSwatch } = await import ( "./get-swatch" ) ; if ( ! ( c . imagefile && existsSync ( c . imagefile ) ) ) err ( `imagefile ${ c . imagefile } not found` ) ; displayImage ( c . imagefile ) ; log ( `getting color for ${ basename ( c . imagefile ) }` ) ; const swatch = await getSwatch ( c . imagefile , c . quality ) ; const delimiter = c . start_tag && c . end_tag ? [ c . start_tag , c . end_tag ] : undefined ; const parser = new RMParser ( swatch , delimiter ) ; if ( c . const ) { const appliedConst = applyConst ( c . const , parser ) ; log ( `Providing variable ${ appliedConst . join ( ", " ) }` ) ; } items . forEach ( v => { c [ v ] . filename = resolvePath ( join ( cfgPath . folder , c [ v ] . filename ) ) ; c [ v ] . realfilepath = resolvePath ( c [ v ] . realfilepath ) ; } ) ; items . forEach ( v => { if ( resolve ( c [ v ] . realfilepath , c [ v ] . filename ) == '' ) err ( `[${ v }] ${ c [ v ] . filepath } is overwriting itself. Aborting` ) ; } ) ; log ( `[CAUTION]
This app is really buggy, this step will overwrite

${ items . map ( v => c [ v ] . realfilepath ) . join ( "\n" ) }

Please backup those file first
Do you want to proceed? press Y to continue` ) ; process . stdin . on ( "data" , data => { process . stdin . destroy ( ) ; if ( data != "Y\n" ) err ( "Alright" ) ; for ( let item of items ) { const itemo = c [ item ] ; requiredItem . forEach ( v => ! itemo [ v ] ? err ( `item [${ item }] didn't have ${ v }` ) : null ) ; if ( existsSync ( itemo . filename ) ) { const parsedFile = parser . parseFile ( readFileSync ( itemo . filename ) . toString ( ) ) ; log ( `[${ item }] writing to ${ itemo . realfilepath }` ) ; writeFileSync ( itemo . realfilepath , parsedFile ) ; } } for ( let key of items ) { if ( 'onfinish' in c [ key ] ) { log ( `[${ key }] executing task ${ c [ key ] [ 'onfinish' ] }` ) ; const output = execSync ( c [ key ] [ 'onfinish' ] ) ; log ( output . toString ( ) ) ; } } log ( `Enjoy your new theme!` ) ; } ) ; } interface consts { [ key ] : string ; } function applyConst ( obj , parser ) { const swatches = [ ] ; for ( let key in obj ) { const parsed = parser . variableParser ( obj [ key ] , false ) . rgb ; if ( parsed && parsed . length > 1 ) { parser . addSwatch ( [ [ key ] , parsed ] ) ; swatches . push ( key ) ; } } return swatches ; } function displayImage ( path ) { if ( process . env . TERM && process . env . TERM == "xterm-kitty" ) execSync ( `kitty +kitten icat "${ path }"` ) ; } export { applyConfig } ;