[Build] Improve treeWalker, renderer & wallpaper finder
This commit is contained in:
		| @@ -4,13 +4,16 @@ const fs = require( 'fs' ); | ||||
| const path = require( 'path' ); | ||||
| const os = require( 'os' ); | ||||
| const render = require( './render' ); | ||||
| const { treeWalker } = require('./util'); | ||||
|  | ||||
|  | ||||
| // Prompt user to select a wallpaper (if no path is passed as argument) | ||||
| const wallpapers = fs.readdirSync( path.join( os.homedir(), '/NextCloud/Wallpapers' ) ); | ||||
| const wallpapers = treeWalker( path.join( os.homedir(), '/NextCloud/Wallpapers' ), '*' ); | ||||
| // const wallpapers = fs.readdirSync( path.join( os.homedir(), '/NextCloud/Wallpapers' ) ); | ||||
| const wallpaperChoices = []; | ||||
| wallpapers.forEach(element => { | ||||
|     wallpaperChoices.push( { 'name': element.split( '.' )[ 0 ], 'value': element } ); | ||||
|     const name = element.split( '/' ); | ||||
|     wallpaperChoices.push( { 'name': name[ name.length - 1 ].split( '.' )[ 0 ], 'value': element } ); | ||||
| }); | ||||
|  | ||||
|  | ||||
| @@ -49,5 +52,5 @@ inquirer.default.prompt( [ | ||||
|     chooseLockpaper, | ||||
|     chooseTheme | ||||
| ] ).then( answers => { | ||||
|     render( path.join( os.homedir(), '/NextCloud/Wallpapers', answers.wallpaper ), path.join( os.homedir(), '/NextCloud/Wallpapers', answers.lockpaper ), answers.theme ); | ||||
|     render( answers.wallpaper, answers.lockpaper, answers.theme ); | ||||
| } ); | ||||
|   | ||||
| @@ -57,13 +57,13 @@ const build = ( wallpaper, lockpaper, theme ) => { | ||||
|  | ||||
|         // 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, '/../config/' ), '*' ); | ||||
|         const fileList = util.treeWalker( path.join( __dirname, '/../config/' ), '*', [ 'node_modules', '@girs', '.gitignore', '.git' ] ); | ||||
|         console.log( fileList ); | ||||
|         for (let index = 0; index < fileList; index++) { | ||||
|         for (let index = 0; index < fileList.length; index++) { | ||||
|             try { | ||||
|                 render( fileList[ index ], view ); | ||||
|             } catch ( e ) { | ||||
|                 console.error( '=> Render failed for ' + fileList[ index ] ); | ||||
|                 console.error( '=> Render failed for ' + fileList[ index ] + ' with error ' + e ); | ||||
|             } | ||||
|         } | ||||
|     } ).catch( e => { | ||||
| @@ -136,13 +136,16 @@ const yaziThemes = { | ||||
|  */ | ||||
| const render = ( templatePath, view ) => { | ||||
|     // Load template from disk (all can be found in <project-root>/config) | ||||
|     const template = '' + fs.readFileSync( path.join( __dirname, '/../config/', templatePath ) ); | ||||
|     const outPath = path.join( __dirname, 'dist', templatePath )  | ||||
|     // TODO: Make exclusion better plus copy other files maybe? | ||||
|     const template = '' + fs.readFileSync( templatePath ); | ||||
|     const outPath = path.join( templatePath.replace( 'config', 'dist' ) );  | ||||
|     console.log( '=> Rendering to ' + outPath ); | ||||
|     try { | ||||
|         fs.mkdir( path.dirname( outPath ) ); | ||||
|     } catch ( _ ) { | ||||
|  | ||||
|         fs.mkdirSync( path.dirname( outPath ), { | ||||
|             recursive: true, | ||||
|         } ); | ||||
|     } catch ( e ) { | ||||
|         console.error( e ); | ||||
|     } | ||||
|     fs.writeFileSync( outPath, mustache.render( template, view ) ); | ||||
| } | ||||
|   | ||||
| @@ -3,27 +3,27 @@ const fs = require( 'fs' ); | ||||
| const path = require( 'path' ); | ||||
|  | ||||
| /** | ||||
|  * Recursively find all HTML files in a directory | ||||
|  * Recursively find all files with extension 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 treeWalker = ( dir, extension, ignoreList ) => { | ||||
|     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 ) { | ||||
|             // Filter ignored directories | ||||
|             if ( ignoreList === undefined || !ignoreList.includes( ls[ file ] ) ) { | ||||
|                 const newFiles = treeWalker( path.join( dir, ls[ file ] ), extension, ignoreList ); | ||||
|                 for (let file = 0; file < newFiles.length; file++) { | ||||
|                     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 ] ) ); | ||||
|         } else if ( extension == '*' || ls[ file ].includes( extension ) ) { | ||||
|             if ( ignoreList === undefined || !ignoreList.includes( ls[ file ] ) ) { | ||||
|                 fileList.push( path.join( dir, ls[ file ] ) ); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user