inject-glob

0.0.4 • Public • Published

Build Status NPM version

Introduction

inject-glob is a simple wrapper around nject and glob. glob provides a mechanism to traverse a directory structure and 'find' files for injection to create a dependency injection tree.

Installation

%> npm install inject-glob --save

Usage

This is the basic usage. In this usage, where all options are defaulted, the names are derived from file names, no aggregation of any of the injectables is done, and the default globs are used.

path  = require('path')
di    = require('inject-glob')

di(null, (err, resolved) -> resolved.app.start())

For reference, here are the defaults, where the function injectName simply removes the extension of a file and uses the file name for the name of the injectable:

defaults =
  globs         : [ '**/*.js', '**/*.coffee', '**/*.json' ]
  cwd           : '.'
  interceptName : (f) -> injectName(f)
  aggregateOn   : ->

A possibly more complete and detailed version might look like the following. In this example everything in the app/model directory is aggregated on to resolved.model. The name of the default.json file is mapped to config and so provided on resolved.config and injected in functions which need those value with the normal mechanism module.exports = (config) ->.

path  = require('path')
di    = require('inject-glob')

opts =
  globs         : [ '**/*.js', '**/*.coffee', '**/*.json' ]
  cwd           : 'app/'
  aggregateOn   : (name) ->
    switch (path.dirname(name))
      when 'app/model' then { aggregateOn: 'model' }
      else undefined
  interceptName : (name, injectName) ->
    switch (name)
      when 'config/default.json' then 'config'
      else injectName(name)

di(null, (err, resolved) -> resolved.app.start())

License

See license file.

The use and distribution terms for this software are covered by the Eclipse Public License 1.0, which can be found in the file 'license' at the root of this distribution. By using this software in any fashion, you are agreeing to be bound by the terms of this license. You must not remove this notice, or any other, from this software.

Package Sidebar

Install

npm i inject-glob

Weekly Downloads

10

Version

0.0.4

License

Eclipse Public License (EPL)

Last publish

Collaborators

  • lcaballero