use-module

angularjs-style DI for nodemodules

Added by: Sam Deering

GitHub

Repository

stoeffel/use-module

Stats

Stars: 1

Watchers: 1

Forks: 0

Module Description

not yet ready for production

use-module

angularjs-style DI for nodemodules

from

var _ = require('underscore');
var fs = require('fs');
var path = require('path');
var winston = require('winston');

to

use(function(underscore, fs, path, winston) {
});

Installation

npm install use-module

API

init

Use this function to set the root of your module.

use.init(String folder)

use

Use this function to inject modules into your module.

use(function callback, Object mappings)

override

Use override in your tests to mock a module

use.override(String moduleName, Object module)

Usage

Make sure you checkout the examples.

  1. Setup
// setup your base folder in the root of your module
require('use-module'); creates a global 'use'
use.init(__dirname); // this also clears all overriden modules.
  // inject modules
  use(function(fs, path){
    fs.readFile(path.join('foo', 'bar', 'test.md'), function (err, data) {
      if (err) throw err;
      console.log(data);
    });
  });

  // map module name
  use(function(_, _s, fs, request, winston, cliColor){ 
    // camelcase is converted to dashed
  }, {
    _: 'underscore',
    _s: 'underscore.string'
  });

  // inject local modules
  use(function($util, $$){ 
    // == require('./util.js');
    // == require('../');
  });

  // mock a dependency
  use.override('fs', {
    readFile: function(filename, options, callback) {
      callback(null, 'hello');
    }
  }).override('path', {
    /*...*/
  });

Module stats last updated: 2015-05-28 10:23:14

Disclaimer: Some data on this page may have been gathered from the authors GitHub respository. If you see any mistakes or outdated information please let us know. Thanks!