| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  | import plugins = require('./smartfile.plugins'); | 
					
						
							|  |  |  | import SmartfileInterpreter = require('./smartfile.interpreter'); | 
					
						
							| 
									
										
										
										
											2017-03-04 21:10:46 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  | import { Smartfile } from './smartfile.classes.smartfile'; | 
					
						
							| 
									
										
										
										
											2017-03-04 21:10:46 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  | import * as memory from './smartfile.memory'; | 
					
						
							| 
									
										
										
										
											2016-06-24 03:36:51 +02:00
										 |  |  | /*=============================================================== | 
					
						
							|  |  |  | ============================ Checks ============================= | 
					
						
							|  |  |  | ===============================================================*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @param filePath | 
					
						
							|  |  |  |  * @returns {boolean} | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-06-27 10:42:35 +02:00
										 |  |  | export const fileExistsSync = (filePath): boolean => { | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |   let fileExistsBool: boolean = false; | 
					
						
							| 
									
										
										
										
											2017-03-04 21:10:46 +01:00
										 |  |  |   try { | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |     plugins.fsExtra.readFileSync(filePath); | 
					
						
							|  |  |  |     fileExistsBool = true; | 
					
						
							| 
									
										
										
										
											2017-03-04 21:10:46 +01:00
										 |  |  |   } catch (err) { | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |     fileExistsBool = false; | 
					
						
							| 
									
										
										
										
											2017-03-04 21:10:46 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |   return fileExistsBool; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2016-06-24 03:36:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @param filePath | 
					
						
							|  |  |  |  * @returns {any} | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-06-27 10:42:35 +02:00
										 |  |  | export let fileExists = async (filePath): Promise<boolean> => { | 
					
						
							| 
									
										
										
										
											2019-02-17 21:07:22 +01:00
										 |  |  |   const done = plugins.smartpromise.defer<boolean>(); | 
					
						
							| 
									
										
										
										
											2019-01-27 03:11:10 +01:00
										 |  |  |   plugins.fs.access(filePath, 4, err => { | 
					
						
							| 
									
										
										
										
											2019-02-17 21:07:22 +01:00
										 |  |  |     err ? done.resolve(false) : done.resolve(true); | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |   }); | 
					
						
							|  |  |  |   return done.promise; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2016-06-24 03:36:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Checks if given path points to an existing directory | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-06-27 10:42:35 +02:00
										 |  |  | export const isDirectory = (pathArg): boolean => { | 
					
						
							| 
									
										
										
										
											2017-05-07 20:50:59 +02:00
										 |  |  |   try { | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |     return plugins.fsExtra.statSync(pathArg).isDirectory(); | 
					
						
							| 
									
										
										
										
											2017-05-07 20:50:59 +02:00
										 |  |  |   } catch (err) { | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |     return false; | 
					
						
							| 
									
										
										
										
											2017-05-07 20:50:59 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2016-06-24 03:36:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Checks if a given path points to an existing file | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-06-27 10:42:35 +02:00
										 |  |  | export const isFile = (pathArg): boolean => { | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |   return plugins.fsExtra.statSync(pathArg).isFile(); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2016-06-24 03:36:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-23 17:42:08 +02:00
										 |  |  | /*=============================================================== | 
					
						
							|  |  |  | ============================ FS ACTIONS ========================= | 
					
						
							|  |  |  | ===============================================================*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 21:42:45 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * copies a file from A to B on the local disk | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-06-27 10:42:35 +02:00
										 |  |  | export const copy = async (fromArg: string, toArg: string): Promise<boolean> => { | 
					
						
							|  |  |  |   const done = plugins.smartpromise.defer<boolean>(); | 
					
						
							|  |  |  |   plugins.fsExtra.copy(fromArg, toArg, {}, (err) => { | 
					
						
							|  |  |  |     if (err) { | 
					
						
							|  |  |  |       throw new Error(`Could not copy from ${fromArg} to ${toArg}: ${err}`); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     done.resolve(true); | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |   }); | 
					
						
							|  |  |  |   return done.promise; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2016-09-24 21:42:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * copies a file SYNCHRONOUSLY from A to B on the local disk | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-06-27 10:42:35 +02:00
										 |  |  | export const copySync = (fromArg: string, toArg: string): boolean => { | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |   plugins.fsExtra.copySync(fromArg, toArg); | 
					
						
							|  |  |  |   return true; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2016-09-24 21:42:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-28 06:57:51 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * ensures that a directory is in place | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-09-20 17:56:49 +02:00
										 |  |  | export let ensureDir = (dirPathArg: string) => { | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |   let done = plugins.smartpromise.defer(); | 
					
						
							|  |  |  |   plugins.fsExtra.ensureDir(dirPathArg, done.resolve); | 
					
						
							|  |  |  |   return done.promise; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2016-06-28 06:57:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * ensures that a directory is in place | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-09-20 17:56:49 +02:00
										 |  |  | export let ensureDirSync = (dirPathArg: string) => { | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |   plugins.fsExtra.ensureDirSync(dirPathArg); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2016-06-28 06:57:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-01 02:45:53 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * ensure an empty directory | 
					
						
							|  |  |  |  * @executes ASYNC | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | export let ensureEmptyDir = (dirPathArg: string) => { | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |   let done = plugins.smartpromise.defer(); | 
					
						
							| 
									
										
										
										
											2017-03-04 21:10:46 +01:00
										 |  |  |   plugins.fsExtra.ensureDir(dirPathArg, () => { | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |     plugins.fsExtra.emptyDir(dirPathArg, done.resolve); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   return done.promise; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-01-01 02:45:53 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * ensure an empty directory | 
					
						
							|  |  |  |  * @executes SYNC | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | export let ensureEmptyDirSync = (dirPathArg: string) => { | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |   plugins.fsExtra.ensureDirSync(dirPathArg); | 
					
						
							|  |  |  |   plugins.fsExtra.emptyDirSync(dirPathArg); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-01-01 02:45:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-23 18:39:02 +02:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2016-09-24 21:42:45 +02:00
										 |  |  |  * ensures that a file is on disk | 
					
						
							|  |  |  |  * @param filePath the filePath to ensureDir | 
					
						
							|  |  |  |  * @param the fileContent to place into a new file in case it doesn't exist yet | 
					
						
							|  |  |  |  * @returns Promise<void> | 
					
						
							|  |  |  |  * @exec ASYNC | 
					
						
							| 
									
										
										
										
											2016-06-23 18:39:02 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-01-21 00:47:48 +01:00
										 |  |  | export let ensureFile = (filePathArg, initFileStringArg): Promise<void> => { | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |   let done = plugins.smartpromise.defer<void>(); | 
					
						
							|  |  |  |   ensureFileSync(filePathArg, initFileStringArg); | 
					
						
							|  |  |  |   done.resolve(); | 
					
						
							|  |  |  |   return done.promise; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2016-06-23 17:42:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-23 18:39:02 +02:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2016-09-24 21:42:45 +02:00
										 |  |  |  * ensures that a file is on disk | 
					
						
							|  |  |  |  * @param filePath the filePath to ensureDir | 
					
						
							|  |  |  |  * @param the fileContent to place into a new file in case it doesn't exist yet | 
					
						
							|  |  |  |  * @returns Promise<void> | 
					
						
							|  |  |  |  * @exec SYNC | 
					
						
							| 
									
										
										
										
											2016-06-23 18:39:02 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-09-24 21:42:45 +02:00
										 |  |  | export let ensureFileSync = (filePathArg: string, initFileStringArg: string): void => { | 
					
						
							| 
									
										
										
										
											2017-03-04 21:10:46 +01:00
										 |  |  |   if (fileExistsSync(filePathArg)) { | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |     return null; | 
					
						
							| 
									
										
										
										
											2017-03-04 21:10:46 +01:00
										 |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |     memory.toFsSync(initFileStringArg, filePathArg); | 
					
						
							| 
									
										
										
										
											2017-03-04 21:10:46 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2016-09-20 17:56:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-24 21:42:45 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * removes a file or folder from local disk | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  | export let remove = function(pathArg: string): Promise<void> { | 
					
						
							|  |  |  |   let done = plugins.smartpromise.defer<void>(); | 
					
						
							|  |  |  |   plugins.fsExtra.remove(pathArg, function() { | 
					
						
							|  |  |  |     done.resolve(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   return done.promise; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2016-06-23 17:42:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-23 18:39:02 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * removes a file SYNCHRONOUSLY from local disk | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  | export let removeSync = function(pathArg: string): boolean { | 
					
						
							|  |  |  |   plugins.fsExtra.removeSync(pathArg); | 
					
						
							|  |  |  |   return true; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2016-06-23 17:42:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-29 14:17:46 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * removes an array of filePaths from disk | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  | export let removeMany = function(filePathArrayArg: string[]) { | 
					
						
							|  |  |  |   let promiseArray: Promise<void>[] = []; | 
					
						
							| 
									
										
										
										
											2017-03-04 21:10:46 +01:00
										 |  |  |   for (let filePath of filePathArrayArg) { | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |     promiseArray.push(remove(filePath)); | 
					
						
							| 
									
										
										
										
											2017-03-04 21:10:46 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |   return Promise.all(promiseArray); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2016-09-29 14:17:46 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * like removeFilePathArray but SYNCHRONOUSLY | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  | export let removeManySync = function(filePathArrayArg: string[]): void { | 
					
						
							| 
									
										
										
										
											2017-03-04 21:10:46 +01:00
										 |  |  |   for (let filePath of filePathArrayArg) { | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |     removeSync(filePath); | 
					
						
							| 
									
										
										
										
											2017-03-04 21:10:46 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2016-09-29 14:17:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-23 17:42:08 +02:00
										 |  |  | /*=============================================================== | 
					
						
							|  |  |  | ============================ Write/Read ========================= | 
					
						
							|  |  |  | ===============================================================*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @param filePathArg | 
					
						
							|  |  |  |  * @param fileTypeArg | 
					
						
							|  |  |  |  * @returns {any} | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  | export let toObjectSync = function(filePathArg, fileTypeArg?) { | 
					
						
							| 
									
										
										
										
											2019-01-27 03:11:10 +01:00
										 |  |  |   const fileString = plugins.fsExtra.readFileSync(filePathArg, 'utf8'); | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |   let fileType; | 
					
						
							|  |  |  |   fileTypeArg ? (fileType = fileTypeArg) : (fileType = SmartfileInterpreter.filetype(filePathArg)); | 
					
						
							|  |  |  |   return SmartfileInterpreter.objectFile(fileString, fileType); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2016-06-23 17:42:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * reads a file content to a String | 
					
						
							|  |  |  |  * @param filePath | 
					
						
							|  |  |  |  * @returns {string|Buffer|any} | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-06-27 10:42:35 +02:00
										 |  |  | export const toStringSync = (filePath: string): string => { | 
					
						
							| 
									
										
										
										
											2019-01-27 03:11:10 +01:00
										 |  |  |   const fileString: string = plugins.fsExtra.readFileSync(filePath, 'utf8'); | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |   return fileString; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-03-04 21:10:46 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-26 21:17:58 +02:00
										 |  |  | export const fileTreeToObject = async (dirPathArg: string, miniMatchFilter: string) => { | 
					
						
							| 
									
										
										
										
											2017-04-30 15:37:34 +02:00
										 |  |  |   // handle absolute miniMatchFilter
 | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |   let dirPath: string; | 
					
						
							| 
									
										
										
										
											2017-04-30 15:37:34 +02:00
										 |  |  |   if (plugins.path.isAbsolute(miniMatchFilter)) { | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |     dirPath = '/'; | 
					
						
							| 
									
										
										
										
											2017-04-30 15:37:34 +02:00
										 |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |     dirPath = dirPathArg; | 
					
						
							| 
									
										
										
										
											2017-04-30 15:37:34 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-26 21:17:58 +02:00
										 |  |  |   const fileTree = await listFileTree(dirPath, miniMatchFilter); | 
					
						
							|  |  |  |   const smartfileArray: Smartfile[] = []; | 
					
						
							| 
									
										
										
										
											2017-03-04 21:10:46 +01:00
										 |  |  |   for (let filePath of fileTree) { | 
					
						
							| 
									
										
										
										
											2017-08-02 13:10:30 +02:00
										 |  |  |     let readPath = ((): string => { | 
					
						
							|  |  |  |       if (!plugins.path.isAbsolute(filePath)) { | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |         return plugins.path.join(dirPath, filePath); | 
					
						
							| 
									
										
										
										
											2017-08-02 13:10:30 +02:00
										 |  |  |       } else { | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |         return filePath; | 
					
						
							| 
									
										
										
										
											2017-08-02 13:10:30 +02:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |     })(); | 
					
						
							|  |  |  |     let fileContentString = toStringSync(readPath); | 
					
						
							| 
									
										
										
										
											2017-05-01 19:49:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-07 23:16:25 +02:00
										 |  |  |     // push a read file as Smartfile
 | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |     smartfileArray.push( | 
					
						
							|  |  |  |       new Smartfile({ | 
					
						
							| 
									
										
										
										
											2019-06-26 21:17:58 +02:00
										 |  |  |         contentBuffer: Buffer.from(fileContentString), | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |         base: dirPath, | 
					
						
							|  |  |  |         path: filePath | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2017-03-04 21:10:46 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |   return smartfileArray; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2016-06-23 17:42:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @param filePathArg | 
					
						
							|  |  |  |  * @param options | 
					
						
							|  |  |  |  * @returns {number} | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  | export let toVinylSync = function(filePathArg, options = {}) { | 
					
						
							|  |  |  |   return plugins.vinylFile.readSync(filePathArg, options); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2016-06-23 17:42:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-23 18:39:02 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * lists Folders in a directory on local disk | 
					
						
							| 
									
										
										
										
											2018-11-22 23:23:26 +01:00
										 |  |  |  * @returns Promise with an array that contains the folder names | 
					
						
							| 
									
										
										
										
											2016-06-23 18:39:02 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-01-27 03:11:10 +01:00
										 |  |  | export let listFolders = async (pathArg: string, regexFilter?: RegExp): Promise<string[]> => { | 
					
						
							|  |  |  |   return listFoldersSync(pathArg, regexFilter); | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2016-06-23 17:42:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-23 18:39:02 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * lists Folders SYNCHRONOUSLY in a directory on local disk | 
					
						
							| 
									
										
										
										
											2016-06-28 06:57:51 +02:00
										 |  |  |  * @returns an array with the folder names as strings | 
					
						
							| 
									
										
										
										
											2016-06-23 18:39:02 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-11-22 23:38:05 +01:00
										 |  |  | export let listFoldersSync = (pathArg: string, regexFilter?: RegExp): string[] => { | 
					
						
							| 
									
										
										
										
											2019-04-08 16:39:41 +02:00
										 |  |  |   let folderArray = plugins.fsExtra.readdirSync(pathArg).filter(file => { | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |     return plugins.fsExtra.statSync(plugins.path.join(pathArg, file)).isDirectory(); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2017-03-04 21:10:46 +01:00
										 |  |  |   if (regexFilter) { | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |     folderArray = folderArray.filter(fileItem => { | 
					
						
							|  |  |  |       return regexFilter.test(fileItem); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-03-04 21:10:46 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |   return folderArray; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2016-06-28 08:40:22 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * lists Files in a directory on local disk | 
					
						
							|  |  |  |  * @returns Promise | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-01-27 03:11:10 +01:00
										 |  |  | export let listFiles = async (pathArg: string, regexFilter?: RegExp): Promise<string[]> => { | 
					
						
							|  |  |  |   return listFilesSync(pathArg, regexFilter); | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2016-06-28 08:40:22 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * lists Files SYNCHRONOUSLY in a directory on local disk | 
					
						
							|  |  |  |  * @returns an array with the folder names as strings | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-01-27 03:11:10 +01:00
										 |  |  | export let listFilesSync = (pathArg: string, regexFilter?: RegExp): string[] => { | 
					
						
							|  |  |  |   let fileArray = plugins.fsExtra.readdirSync(pathArg).filter(file => { | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |     return plugins.fsExtra.statSync(plugins.path.join(pathArg, file)).isFile(); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2017-03-04 21:10:46 +01:00
										 |  |  |   if (regexFilter) { | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |     fileArray = fileArray.filter(fileItem => { | 
					
						
							|  |  |  |       return regexFilter.test(fileItem); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-03-04 21:10:46 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |   return fileArray; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2016-06-28 08:40:22 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * lists all items (folders AND files) in a directory on local disk | 
					
						
							| 
									
										
										
										
											2016-09-17 23:11:44 +02:00
										 |  |  |  * @returns Promise<string[]> | 
					
						
							| 
									
										
										
										
											2016-06-28 08:40:22 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-01-27 03:11:10 +01:00
										 |  |  | export let listAllItems = async (pathArg: string, regexFilter?: RegExp): Promise<string[]> => { | 
					
						
							|  |  |  |   return listAllItemsSync(pathArg, regexFilter); | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2016-06-28 08:40:22 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2016-09-17 23:11:44 +02:00
										 |  |  |  * lists all items (folders AND files) in a directory on local disk | 
					
						
							| 
									
										
										
										
											2016-06-28 08:40:22 +02:00
										 |  |  |  * @returns an array with the folder names as strings | 
					
						
							| 
									
										
										
										
											2016-09-17 23:11:44 +02:00
										 |  |  |  * @executes SYNC | 
					
						
							| 
									
										
										
										
											2016-06-28 08:40:22 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-01-27 03:11:10 +01:00
										 |  |  | export let listAllItemsSync = (pathArg: string, regexFilter?: RegExp): string[] => { | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |   let allItmesArray = plugins.fsExtra.readdirSync(pathArg).filter(function(file) { | 
					
						
							|  |  |  |     return plugins.fsExtra.statSync(plugins.path.join(pathArg, file)).isFile(); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2017-03-04 21:10:46 +01:00
										 |  |  |   if (regexFilter) { | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |     allItmesArray = allItmesArray.filter(fileItem => { | 
					
						
							|  |  |  |       return regexFilter.test(fileItem); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-03-04 21:10:46 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |   return allItmesArray; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2016-06-28 08:40:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-17 23:11:44 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * lists a file tree using a miniMatch filter | 
					
						
							| 
									
										
										
										
											2016-09-30 16:16:11 +02:00
										 |  |  |  * note: if the miniMatch Filter is an absolute path, the cwdArg will be omitted | 
					
						
							| 
									
										
										
										
											2016-09-17 23:11:44 +02:00
										 |  |  |  * @returns Promise<string[]> string array with the absolute paths of all matching files | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-06-26 21:17:58 +02:00
										 |  |  | export const listFileTree = async (dirPathArg: string, miniMatchFilter: string, absolutePathsBool: boolean = false): Promise<string[]> => { | 
					
						
							| 
									
										
										
										
											2019-01-27 03:11:10 +01:00
										 |  |  |   const done = plugins.smartpromise.defer<string[]>(); | 
					
						
							| 
									
										
										
										
											2017-03-04 21:10:46 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // handle absolute miniMatchFilter
 | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |   let dirPath: string; | 
					
						
							| 
									
										
										
										
											2017-03-04 21:10:46 +01:00
										 |  |  |   if (plugins.path.isAbsolute(miniMatchFilter)) { | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |     dirPath = '/'; | 
					
						
							| 
									
										
										
										
											2017-03-04 21:10:46 +01:00
										 |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |     dirPath = dirPathArg; | 
					
						
							| 
									
										
										
										
											2017-03-04 21:10:46 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-27 03:11:10 +01:00
										 |  |  |   const options = { | 
					
						
							| 
									
										
										
										
											2017-05-27 01:07:32 +02:00
										 |  |  |     cwd: dirPath, | 
					
						
							| 
									
										
										
										
											2017-05-27 02:54:22 +02:00
										 |  |  |     nodir: true, | 
					
						
							|  |  |  |     dot: true | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |   }; | 
					
						
							| 
									
										
										
										
											2017-03-04 21:10:46 +01:00
										 |  |  |   plugins.glob(miniMatchFilter, options, (err, files: string[]) => { | 
					
						
							|  |  |  |     if (err) { | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |       console.log(err); | 
					
						
							|  |  |  |       done.reject(err); | 
					
						
							| 
									
										
										
										
											2016-09-30 16:16:11 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  |     done.resolve(files); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2019-06-26 21:17:58 +02:00
										 |  |  |    | 
					
						
							|  |  |  |   let fileList = await done.promise; | 
					
						
							|  |  |  |   if (absolutePathsBool) { | 
					
						
							|  |  |  |     fileList = fileList.map(filePath => { | 
					
						
							|  |  |  |       return plugins.path.resolve(plugins.path.join(dirPath, filePath)); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return fileList; | 
					
						
							| 
									
										
										
										
											2018-07-03 08:55:09 +02:00
										 |  |  | }; |