Hapi filesystem utilities
Filesystem tools for building hapi
apps.
npm i -s wranch
- bindFunctions
- deleteRecursive
- dirContents
- listModules
- mapArray
- mapFiles
- nameMethods
- recurseFiles
- requireFiles
- testMirror
- testMirrorPath
- tryRequirePlugins
Examples
All examples are based on the following trees:
./lib
lib
├── bindFunctions.js
├── deleteRecursive.js
├── dirContents.js
├── listModules.js
├── mapArray.js
├── mapFiles.js
├── nameMethods.js
├── noop.js
├── recurseFiles.js
├── requireFiles.js
├── testMirror.js
├── testMirrorPath.js
└── tryRequirePlugins.js
./test/fixture
test/fixture
├── extensions
│ ├── js.js
│ └── json.json
├── flat
│ └── index.js
├── fldr
│ ├── fldrer
│ │ └── fldrerest
│ │ └── goal.html
│ └── index.js
├── ignore.php
├── ignore.py
├── ignore.rb
├── index.js
├── methods
│ ├── dontwork.js
│ ├── index.js
│ ├── notFunction.js
│ └── work.js
├── noindex
│ └── test.js
├── tast.js
├── test.js
└── tist.js
API
### bindFunctions(obj, bindTo)
Iterates through an object and binds functions. Returns a new object.
Parameters
obj: object
, Object to iterate over
bindTo: object
, Object to bind to
Returns: object
, A new object with bound functions
Example:
const obj1 = { console } { console } const bindTo = a: true b: false ;
deleteRecursive(dirname)
Deletes files recursively
Parameters
dirname: string
, Path to start deleting from
Example:
// Make a temporary folderconst tmpfolder = __dirname + '/tmp'; try Fs;catch e {} // Make folders and files inside of tmpfolder'test' 'tast' 'tist'; // Delete the entire folder recursively;
dirContents(dirname, options)
Reads contents from all files in dirname
and maps as key: value pairs
Parameters
dirname: string
, Path to require plugin from
options: object
, Options to pass
Returns: object
, An object of { [filename]: fileContents }
Example:
const contents = ;// {// 'extensions/js': 'module.exports = \'js\';\n',// 'extensions/json': '["json"]',// 'flat/index':// 'module.exports = [{ name: \'flat\' }, 1, 2, 3, \'a\', \'b\', \'c\'];\n',// 'fldr/fldrer/fldrerest/goal': '<h1>success!</h1>',// 'fldr/index': 'module.exports = { name: \'fldr\' };\n',// ignore: '',// index: '',// 'methods/dontwork': 'module.exports = () => \'not work!\';\n\n',// 'methods/index': 'module.exports = false;\n',// 'methods/notFunction': 'module.exports = \'not a function\';',// 'methods/work': 'module.exports = () => \'works!\';\n\n',// 'noindex/test': 'module.exports = true;\n',// tast: 'module.exports = { name: \'tast\' };\n',// test: 'module.exports = { name: \'test\' };\n',// tist: 'module.exports = { name: \'tist\' };\n'// } // And so you can do things like this:Object; // You can also pass the following options:const contents = ;
listModules(dirname)
Lists all files in a path except index.js
Parameters
dirname: string
, Path to list files
Returns: array
, An array of paths except index.js
Example:
// [// 'extensions',// 'flat',// 'fldr',// 'ignore.php',// 'ignore.py',// 'ignore.rb',// 'methods',// 'noindex',// 'tast.js',// 'test.js',// 'tist.js'// ]
mapArray(dirname)
Requires all modules in dirname
and flattens them
Parameters
dirname: string
, Path to require plugin from
Returns: array
, An array of required modules
Example:
; // [// [Function: bindFunctions],// [Function],// [Function: dirContents],// [Function],// [Function],// [Function],// [Function],// [Function],// [Function: crawl],// [Function],// [Function],// [Function],// [Function]// ]
mapFiles(dirname)
Uses listModules
and requires all js modules in give dir
and place them into a JS map
Parameters
dirname: string
, Path to require plugin from
Returns: map
, a js map of files using their name minus extension as the key, and the module export as the value
Example:
// Map {// 'bindFunctions' => [Function: bindFunctions],// 'deleteRecursive' => [Function],// 'dirContents' => [Function: dirContents],// 'listModules' => [Function],// 'mapArray' => [Function],// 'mapFiles' => [Function],// 'nameMethods' => [Function],// 'noop' => [Function],// 'recurseFiles' => [Function: crawl],// 'requireFiles' => [Function],// 'testMirror' => [Function],// 'testMirrorPath' => [Function],// 'tryRequirePlugins' => [Function]// }
nameMethods(dirname)
Uses mapFiles
to imports file and converts them to an array of objects with key as name
and export as method
. Will only convert where export is a function.
Parameters
dirname: string
, Directory to get items from
Returns: array
, An array of [{ name, method }]
Example:
const methods = ;// [// { name: 'bindFunctions', method: [Function: bindFunctions] },// { name: 'deleteRecursive', method: [Function] },// { name: 'dirContents', method: [Function: dirContents] },// { name: 'listModules', method: [Function] },// { name: 'mapArray', method: [Function] },// { name: 'mapFiles', method: [Function] },// { name: 'nameMethods', method: [Function] },// { name: 'noop', method: [Function] },// { name: 'recurseFiles', method: [Function: crawl] },// { name: 'requireFiles', method: [Function] },// { name: 'testMirror', method: [Function] },// { name: 'testMirrorPath', method: [Function] },// { name: 'tryRequirePlugins', method: [Function] }// ] server;
recurseFiles(path, fileFn, dirFn)
Performs file operations recursively
Parameters
path: string
, required - Path to start crawling
fileFn: function
, required - Function to execute on a file
dirFn: function
, optional - Callback passes original path variable
Example:
// Example 1 fs; ; // Example 2 (deleteRecursively) const rm = Fs;const rmRf = Fs; const deleteRecursively = ;
requireFiles(dirname)
Requires all js modules in dirname
and maps as Hapi methods array
Parameters
dirname: string
, Path to require plugin from
Returns: object
, An object with in the format of { key: module }
Example:
const listModules = ;
testMirror(pathname, replace)
Requires a module from root folder mirrored in /test. Allows for renaming and moving of module / test files without breaking test as long as hierarchy stays the same.
Parameters
pathname: string
, Path to require plugin from
replace: string
, Path to replace, in case your path is not '/test'. Optional
Returns: module
, The mirrored module
Example:
// test/lib/utils/myModule.js will require lib/utils/myModule.jsconst ModuleToTest = ; // test/server/lib/utils/myModule.js will require lib/utils/myModule.jsconst ModuleToTest = ;
testMirrorPath(pathname, replace)
Creates an absolute path from given pathname
that replaces /test
by default.
Parameters
pathname: string
, Path to require plugin from
replace: string
, Path to replace, in case your path is not '/test'. Optional
Returns: string
, An absolute path to your mirrored module
Example:
// test/lib/utils/myModule.jsconst ModuleToTest = ;// /home/me/myproject/lib/utils/myModule.js // test/server/lib/utils/myModule.jsconst ModuleToTest = ;// /home/me/myproject/lib/utils/myModule.js
tryRequirePlugins(plugins)
Attempts to require a Hapi plugin
Parameters
plugins: array
, Array of Hapi plugin objects without require
ing the module
Returns: array
, An array of Hapi plugins with module required
Returns: , An array of hapi plugins
Example:
const plugins = ;