charger-dossier
This module loads all JavaScript files in a folder and its subfolders and generates an object which represents the file tree where each folder is an object and each file a variable whose value is what the file returned or contains.
Quick start
You only have to require the module and then give it a folder name:
const chargerDossier = require("charger-dossier")
const myFolder = chargerDossier("./myfoldername")
console.log(myFolder instance of Object) // true
console.log(myFolder.mySubFolder.myScriptFile) // prints the `exports`ed value
console.log(myFolder.mySubFolder.myTextFile) // prints the file content (if activated in the options, see below)
Options
The module waits for a required parameter folderName
and an optional second parameter options
object.
Options are:
-
fileExtensionSeparator
(String): tells which character to consider as the file extension separator,"."
by default, -
namePattern
(RegExp): tells which folders and files include,/^[^_\.][^\\\/ \.,:;\?!\*'"<>\|]*$/u
by default, which will only keep folders and files not starting with"_"
or"."
and having not any character within “/\ .,:;?!*'"<>|
” in their name (extension apart), -
processers
({ extension: String | Function }
): tells how to process the file depending on its extension, all files whose file extension has no processer are ignored;{ "js": "code" }
by default. Accepted values are:-
"code"
:require
s the file, -
"text"
: reads the file content, -
function (filePath)
: executes the function with the file path as parameter,
-
Warning
The behavior of this module is undefined if it encounters both a folder and a file with the same name (extension apart). You should not do that as one will overwrite the other one.