Skip to content

ozum/hapi-plugins-dir

Repository files navigation

hapi-plugins-dir

Description

Traverse directory recursively and get array of Hapi plugins ready to feed Hapi's server.register(plugins, [options]) function.

Synopsis

Plugins in same directory with file

const getHapiPlugins = require("hapi-plugins-dir");
const plugins = getHapiPlugins();
await server.register(plugins); // Hapi server object...

Custom directory

const getHapiPlugins = require("hapi-plugins-dir");
const plugins = getHapiPlugins({ dir: "plugins" }); // Dir may be relative or absolute
await server.register(plugins); // Hapi server object...

With haute-couture: plugins/index.js

module.exports = require("hapi-plugins-dir")();

Details

  • Traverses recursively given directory. (Defaults to caller file's directory)
  • If a plugin isn't specified in plugins key, it will be required using filename.
  • If a directory name starts with @, it is treated as package scope. (i.e. plugins/@name/some-plugin.js -> @name/some-plugin)
  • Files can be ordered using number prefixes appended dot or dash (. or -). Numbers are stripped when requesting plugin. (i.e. plugins/001-hapi-auth-basic requires hapi-auth-basic).
  • Scope directory names can be prefixed with numbers too. (i.e. plugins/001-@scope/001-module.js requires @scope/module).
  • Result array must be registered using server.register(plugins, [options]). It is not automatically registered.

API

Functions

getHapiPlugins([options])Array.<Object>

Traverses given direcotry (absolute or relative to caller file) for hapi plugins options file and returns them ready to feed Hapi's server.register(plugins, [options])) function.

Typedefs

File : Object

File object.

FilterFunction : function

Filter function to exclude files.

getHapiPlugins([options]) ⇒ Array.<Object>

Traverses given direcotry (absolute or relative to caller file) for hapi plugins options file and returns them ready to feed Hapi's server.register(plugins, [options]) function.

Kind: global function
Returns: Array.<Object> - - Array of objects to feed Hapi's server.register(plugins, [options]) function.

Param Type Default Description
[options] Object Options
[options.dir] dir __dirname Directory to look plugins. Can be relative or absolute. Default is current file's directory.
[options.recursive] boolean true Traverse directory recursively.
[options.stripNumberPrefix] boolean true Strip number prefixes from beginnings. (Numbers appended by dash or dot (. or -))
[options.filter] FilterFunction | Array.<FilterFunction> Optional extra filter funtion or functions to exclude some files. (They are applied before builtin number filter)

Example

const plugins = getHapiPluginsFromDir();
// ./001-a.js        -> a
// ./002-@scope/b.js -> scope/b
// ./@other/c.js     -> other/c
await server.register(plugins)

File : Object

File object.

Kind: global typedef
Properties

Name Type Description
path string Absolute path of the file.
stats fs.stats fs.stats object of the file.

FilterFunction : function

Filter function to exclude files.

Kind: global typedef

Type Description
Array.<File> Array of klaw result items. Path of file and fs.stats object.

About

Traverse directory recursively and get array of Hapi plugins ready to feed Hapi's server.register(plugins, [options]) function.

Resources

License

Stars

Watchers

Forks

Packages

No packages published