Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
require-folder-tree
Advanced tools
Utility to require multiple files in a folder tree with flexible options
API is stable and tests cover all options. No known issues.
To require all the .js
and .json
files in a folder and any sub-folders:
var requireFolderTree = require('require-folder-tree');
var modules = requireFolderTree('/path/to/folder');
If the file structure is:
/
a.js
b.js
c/
d.js
e.js
The returned value is:
{
a: ...,
b: ...,
c: {
d: ...,
e: ...
}
}
Options can be passed to alter the behaviour.
When true
, recurses through subfolders and sub-subfolders requiring an entire tree. Set to false
to ignore subfolders.
Defaults to true
.
// Require only files in base folder
requireFolderTree('/path/to/folder', { recurse: false });
// returns { a: ..., b: ... }
A regular expression for what files to include. First matching group defines the key used in returned object.
Defaults to /^(.+)\.js(on)?$/
(i.e. include all .js
and .json
files)
// Include all .js files except those starting with `_`
requireFolderTree('/path/to/folder', { filterFiles: /^([^_].*)\.js$/ });
A regular expression for what folders to iterate into. First matching group defines the key used in returned object.
Defaults to /^([^\.].*)$/
(i.e. process all folders except those beginning with .
)
// Process all folders except those starting with `.` or `_`
requireFolderTree('/path/to/folder', { filterFolders: /^([^\._].*)$/ });
Function for transforming object keys for files.
// Transform file names to lower case
requireFolderTree('/path/to/folder', {
fileNameTransform: function(str) { return str.toLowerCase(); }
});
Like fileNameTransform
, but for folder names.
By default, inherits value of options.fileNameTransform
. If you want to define a fileNameTransform
function but leave folder names unchanged, set options.folderNameTransform = null
.
When set, saves the file name (after fileNameTransform
and flatten
have been applied) as an attribute of each require
-d file.
If true
, uses value 'name'
. Defaults to undefined
.
requireFolderTree('/path/to/folder', { fileNameAttribute: true, recurse: false });
// returns { a: { name: 'a', ... }, b: { name: 'b', ... } }
Like fileNameAttribute
, but for folder names.
By default, inherits value of options.fileNameAttribute
. If you want to save file names only, set options.folderNameAttribute = null
.
When set, saves the parent folder as an attribute of each require
-d file.
If true
, uses value 'parent'
. Defaults to undefined
.
requireFolderTree('/path/to/folder', { fileParentAttribute: true, recurse: false });
// returns { a: { parent: <reference to root object>, ... }, b: { parent: <reference to root object>, ... } }
Like fileParentAttribute
, but for folders.
By default, inherits value of options.fileParentAttribute
. If you want to save file parents only, set options.folderParentAttribute = null
.
Flattens the folder structure when true
.
Defaults to false
.
requireFolderTree('/path/to/folder', { flatten: true });
// returns { a: ..., b: ..., d: ..., e: ... }
Controls how the object keys are created for files nested in folders when flattening.
true
concatenates the folder name and file name.
Defaults to false
requireFolderTree('/path/to/folder', { flatten: true, flattenPrefix: true });
// returns { a: ..., b: ..., cd: ..., ce: ... }
When true
, camel-cases the keys when concatenating the folder name and file name.
Defaults to false
requireFolderTree('/path/to/folder', { flatten: true, flattenCamel: true });
// returns { a: ..., b: ..., cD: ..., cE: ... }
Sets separator between folder name and file name when concatenating.
Defaults to undefined
.
requireFolderTree('/path/to/folder', { flatten: true, flattenSeparator: '_' });
// returns { a: ..., b: ..., c_d: ..., c_e: ... }
Sets a custom function for combining folder name and file name when concatenating.
Defaults to undefined
.
requireFolderTree('/path/to/folder', {
flatten: true,
flattenCustom: function(a, b) { return a + 'x' + b; }
});
// returns { a: ..., b: ..., cxd: ..., cxe: ... }
If set, looks for the named file in each folder, and if found uses it as a base for the returned object.
Defaults to undefined
.
// if index.js contains code `module.exports = { x: 1 }`:
requireFolderTree('/path/to/folder');
// returns { a: ..., b: ..., c: { d: ..., e: ... }, index: { x: 1 } }
requireFolderTree('/path/to/folder', { indexFile: 'index.js' });
// returns { x: 1, a: ..., b: ..., c: { d: ..., e: ... } }
If set, puts all retrieved sub-folders inside the key specified.
Defaults to undefined
.
requireFolderTree('/path/to/folder', { foldersKey: 'folders' });
// returns { a: ..., b: ..., folders: { c: { d: ..., e: ... } } }
If set, puts all retrieved files inside the key specified.
Defaults to undefined
.
requireFolderTree('/path/to/folder', { filesKey: 'files' });
// returns { files: { a: ..., b: ... }, c: { files: { d: ..., e: ... } } }
This module will never alter the contents of the require cache.
i.e. if you require()
any of the files again, they will not have been altered by e.g. using fileNameAttribute
option.
Use npm test
to run the tests. Use npm run cover
to check coverage.
See changelog.md
If you discover a bug, please raise an issue on Github. https://github.com/overlookmotel/require-folder-tree/issues
Pull requests are very welcome. Please:
1.4.7
flattenCamel
optionlodash
dependencyFAQs
Utility to require multiple files in a folder tree with flexible options
The npm package require-folder-tree receives a total of 1,567 weekly downloads. As such, require-folder-tree popularity was classified as popular.
We found that require-folder-tree demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.