Skip to content

Commit

Permalink
relative paths to config files in node.js (Carrooi#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
sakren committed Jan 7, 2014
1 parent ee41b08 commit b790ee3
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 12 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,13 @@ argument will be passed into this object property.

```
var DIConfigurator = require('dependency-injection/DIConfigurator');
var configurator = new DIConfigurator('/path/to/your/configuration/file.json');
var configurator = new DIConfigurator('./path/to/your/configuration/file.json');
var di = configurator.create();
di.basePath = __dirname;
```

**Relative paths to config files are supported only on node (not in browser)!!!**

This will create new instance of DI class which holding all your services.

You have to also set the basePath property. DI will prepend this basePath to all services' paths from your configuration.
Expand All @@ -74,6 +75,14 @@ di.create('application');
di.getFactory('application');
```

## Base path to services

Default base path in node is directory of file from which you are initializing DI. You have to set this manually in browser.

```
di.basePath = __dirname;
```

## Auto exposing into global

DI can be automatically exposed into window object (when on browser) or into global object (in node). Default name for
Expand Down Expand Up @@ -444,6 +453,9 @@ $ npm test

## Changelog

* 2.2.0
+ Relative paths to config files

* 2.1.1
+ Hints has exactly the same syntax as arguments configuration
+ Inject method's second argument is args, not scope (BC break!)
Expand Down
27 changes: 24 additions & 3 deletions lib/DIConfigurator.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions lib/Helpers.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
},
"main": "./lib/DI",
"dependencies": {
"easy-configuration": "1.6.6"
"easy-configuration": "1.6.6",
"callsite": "1.0.0"
},
"devDependencies": {
"chai": "1.8.1",
Expand Down
22 changes: 21 additions & 1 deletion src/DIConfigurator.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
DI = require './DI'
Configuration = require 'easy-configuration'

isWindow = typeof window != 'undefined'

if !isWindow
callsite = require 'callsite'
path = require 'path'

class DIConfigurator


Expand All @@ -11,6 +17,8 @@ class DIConfigurator

path: null

basePath: null

defaultSetup:
windowExpose: null # deprecated
expose: false
Expand All @@ -24,7 +32,16 @@ class DIConfigurator
setup: {}


constructor: (@path) ->
constructor: (_path) ->
if _path[0] == '.' && isWindow
throw new Error 'Relative paths to config files are not supported in browser.'

if _path[0] == '.'
stack = callsite()
@basePath = path.dirname(stack[1].getFileName())
_path = path.join(@basePath, _path)

@path = _path


create: ->
Expand All @@ -47,6 +64,9 @@ class DIConfigurator
configuration = @config.load()
di = new DI

if @basePath != null
di.basePath = @basePath

di.config = @config
di.parameters = @config.parameters

Expand Down
4 changes: 4 additions & 0 deletions src/Helpers.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class Helpers
return result


@dirname: (path) ->



@log: (message) ->
if console?.log?
console.log(message)
Expand Down
114 changes: 109 additions & 5 deletions test/browser/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,8 @@
return result;
};

Helpers.dirname = function(path) {};

Helpers.log = function(message) {
if ((typeof console !== "undefined" && console !== null ? console.log : void 0) != null) {
return console.log(message);
Expand Down Expand Up @@ -1184,6 +1186,30 @@
}).call(this);


}, 'callsite/index.js': function(exports, module) {

/** node globals **/
var require = function(name) {return window.require(name, 'callsite/index.js');};
require.resolve = function(name, parent) {if (parent === null) {parent = 'callsite/index.js';} return window.require.resolve(name, parent);};
require.define = function(bundle) {window.require.define(bundle);};
require.cache = window.require.cache;
var __filename = 'callsite/index.js';
var __dirname = 'callsite';
var process = {cwd: function() {return '/';}, argv: ['node', 'callsite/index.js'], env: {}};

/** code **/

module.exports = function(){
var orig = Error.prepareStackTrace;
Error.prepareStackTrace = function(_, stack){ return stack; };
var err = new Error;
Error.captureStackTrace(err, arguments.callee);
var stack = err.stack;
Error.prepareStackTrace = orig;
return stack;
};


}, '/test/browser/tests/DI.coffee': function(exports, module) {

/** node globals **/
Expand Down Expand Up @@ -1406,6 +1432,13 @@
di = configurator.create();
return di.basePath = dir;
});
describe('#constructor()', function() {
return it('should throw an error for relative paths', function() {
return expect(function() {
return new DIConfigurator('../data/config.json');
}).to["throw"](Error, 'Relative paths to config files are not supported in browser.');
});
});
describe('#parameters', function() {
return it('should contain all parameters', function() {
return expect(di.parameters).to.be.eql({
Expand Down Expand Up @@ -1620,19 +1653,28 @@
/** code **/
// Generated by CoffeeScript 1.6.3
(function() {
var Configuration, DI, DIConfigurator;
var Configuration, DI, DIConfigurator, callsite, isWindow, path;

DI = require('./DI');

Configuration = require('easy-configuration');

isWindow = typeof window !== 'undefined';

if (!isWindow) {
callsite = require('callsite');
path = require('path');
}

DIConfigurator = (function() {
DIConfigurator.EXPOSE_NAME = 'di';

DIConfigurator.prototype.config = null;

DIConfigurator.prototype.path = null;

DIConfigurator.prototype.basePath = null;

DIConfigurator.prototype.defaultSetup = {
windowExpose: null,
expose: false
Expand All @@ -1647,8 +1689,17 @@
setup: {}
};

function DIConfigurator(path) {
this.path = path;
function DIConfigurator(_path) {
var stack;
if (_path[0] === '.' && isWindow) {
throw new Error('Relative paths to config files are not supported in browser.');
}
if (_path[0] === '.') {
stack = callsite();
this.basePath = path.dirname(stack[1].getFileName());
_path = path.join(this.basePath, _path);
}
this.path = _path;
}

DIConfigurator.prototype.create = function() {
Expand All @@ -1671,6 +1722,9 @@
};
configuration = this.config.load();
di = new DI;
if (this.basePath !== null) {
di.basePath = this.basePath;
}
di.config = this.config;
di.parameters = this.config.parameters;
if (configuration.setup.windowExpose !== null) {
Expand Down Expand Up @@ -1918,7 +1972,8 @@
},
"main": "./lib/DI",
"dependencies": {
"easy-configuration": "1.6.6"
"easy-configuration": "1.6.6",
"callsite": "1.0.0"
},
"devDependencies": {
"chai": "1.8.1",
Expand Down Expand Up @@ -2002,11 +2057,60 @@
}).call(this);


}, 'callsite/package.json': function(exports, module) {

/** node globals **/
var require = function(name) {return window.require(name, 'callsite/package.json');};
require.resolve = function(name, parent) {if (parent === null) {parent = 'callsite/package.json';} return window.require.resolve(name, parent);};
require.define = function(bundle) {window.require.define(bundle);};
require.cache = window.require.cache;
var __filename = 'callsite/package.json';
var __dirname = 'callsite';
var process = {cwd: function() {return '/';}, argv: ['node', 'callsite/package.json'], env: {}};

/** code **/
module.exports = (function() {
return {
"name": "callsite",
"version": "1.0.0",
"description": "access to v8's CallSites",
"keywords": [
"stack",
"trace",
"line"
],
"author": {
"name": "TJ Holowaychuk",
"email": "tj@vision-media.ca"
},
"dependencies": {},
"devDependencies": {
"mocha": "*",
"should": "*"
},
"main": "index",
"engines": {
"node": "*"
},
"readme": "# callstack\n\n Access to v8's \"raw\" `CallSite`s.\n\n## Installation\n\n $ npm install callsite\n\n## Example\n\n```js\nvar stack = require('callsite');\n\nfoo();\n\nfunction foo() {\n bar();\n}\n\nfunction bar() {\n baz();\n}\n\nfunction baz() {\n console.log();\n stack().forEach(function(site){\n console.log(' \\033[36m%s\\033[90m in %s:%d\\033[0m'\n , site.getFunctionName() || 'anonymous'\n , site.getFileName()\n , site.getLineNumber());\n });\n console.log();\n}\n```\n\n## Why?\n\n Because you can do weird, stupid, clever, wacky things such as:\n\n - [better-assert](https://github.com/visionmedia/better-assert)\n\n## License\n\n MIT\n",
"readmeFilename": "Readme.md",
"_id": "callsite@1.0.0",
"dist": {
"shasum": "5bd0a21871110cc4720abf4d8498bab17a74c902"
},
"_from": "callsite@1.0.0",
"_resolved": "https://registry.npmjs.org/callsite/-/callsite-1.0.0.tgz"
}

}).call(this);


}, 'easy-configuration': function(exports, module) { module.exports = window.require('easy-configuration/lib/EasyConfiguration.js'); }
, 'recursive-merge': function(exports, module) { module.exports = window.require('recursive-merge/lib/Merge.js'); }
, 'callsite': function(exports, module) { module.exports = window.require('callsite/index.js'); }

});
require.__setStats({"/lib/Service.js":{"atime":1388653091000,"mtime":1388653079000,"ctime":1388653079000},"/lib/Helpers.js":{"atime":1388655646000,"mtime":1388655643000,"ctime":1388655643000},"/lib/DI.js":{"atime":1388656553000,"mtime":1388656444000,"ctime":1388656444000},"easy-configuration/lib/EasyConfiguration.js":{"atime":1388654516000,"mtime":1385411214000,"ctime":1385450928000},"recursive-merge/lib/Merge.js":{"atime":1388654516000,"mtime":1385409966000,"ctime":1385450932000},"easy-configuration/lib/Extension.js":{"atime":1388654516000,"mtime":1385411214000,"ctime":1385450928000},"easy-configuration/lib/Helpers.js":{"atime":1388654516000,"mtime":1385411214000,"ctime":1385450928000},"/test/browser/tests/DI.coffee":{"atime":1388656553000,"mtime":1388656504000,"ctime":1388656504000},"/test/browser/tests/DIConfigurator.coffee":{"atime":1388653053000,"mtime":1388653053000,"ctime":1388653053000},"/test/browser/tests/Helpers.coffee":{"atime":1388655458000,"mtime":1388655455000,"ctime":1388655455000},"/lib/DIConfigurator.js":{"atime":1388653112000,"mtime":1388653079000,"ctime":1388653079000},"/test/data/Application.coffee":{"atime":1388654516000,"mtime":1386925844000,"ctime":1386925844000},"/test/data/AutowirePath.coffee":{"atime":1388654516000,"mtime":1386934815000,"ctime":1386934815000},"/test/data/Http.coffee":{"atime":1388654516000,"mtime":1384940373000,"ctime":1384940373000},"/test/data/config.json":{"atime":1388653053000,"mtime":1388653053000,"ctime":1388653053000},"/package.json":{"atime":1388655896000,"mtime":1388655784000,"ctime":1388655784000},"easy-configuration/package.json":{"atime":1388654516000,"mtime":1385450929000,"ctime":1385450929000}});
require.__setStats({"/lib/Service.js":{"atime":1389081257000,"mtime":1389081238000,"ctime":1389081238000},"/lib/Helpers.js":{"atime":1389081885000,"mtime":1389081854000,"ctime":1389081854000},"/lib/DI.js":{"atime":1389081257000,"mtime":1389081238000,"ctime":1389081238000},"easy-configuration/lib/EasyConfiguration.js":{"atime":1389081274000,"mtime":1385411214000,"ctime":1385450928000},"recursive-merge/lib/Merge.js":{"atime":1389081274000,"mtime":1385409966000,"ctime":1385450932000},"easy-configuration/lib/Extension.js":{"atime":1389081274000,"mtime":1385411214000,"ctime":1385450928000},"easy-configuration/lib/Helpers.js":{"atime":1389081274000,"mtime":1385411214000,"ctime":1385450928000},"callsite/index.js":{"atime":1389081083000,"mtime":1359062982000,"ctime":1389081065000},"/test/browser/tests/DI.coffee":{"atime":1389081274000,"mtime":1388656504000,"ctime":1388656504000},"/test/browser/tests/DIConfigurator.coffee":{"atime":1389082345000,"mtime":1389082317000,"ctime":1389082317000},"/test/browser/tests/Helpers.coffee":{"atime":1389081274000,"mtime":1388655455000,"ctime":1388655455000},"/lib/DIConfigurator.js":{"atime":1389083042000,"mtime":1389083040000,"ctime":1389083040000},"/test/data/Application.coffee":{"atime":1389081274000,"mtime":1386925844000,"ctime":1386925844000},"/test/data/AutowirePath.coffee":{"atime":1389081274000,"mtime":1386934815000,"ctime":1386934815000},"/test/data/Http.coffee":{"atime":1389081274000,"mtime":1384940373000,"ctime":1384940373000},"/test/data/config.json":{"atime":1389081274000,"mtime":1388653053000,"ctime":1388653053000},"/package.json":{"atime":1389081064000,"mtime":1389081044000,"ctime":1389081044000},"easy-configuration/package.json":{"atime":1389081064000,"mtime":1385450929000,"ctime":1385450929000},"callsite/package.json":{"atime":1389081083000,"mtime":1389081065000,"ctime":1389081065000}});
require.version = '5.5.1';

/** run section **/
Expand Down
5 changes: 5 additions & 0 deletions test/browser/tests/DIConfigurator.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ describe 'DIConfiguration', ->
di.basePath = dir
)

describe '#constructor()', ->

it 'should throw an error for relative paths', ->
expect( -> new DIConfigurator('../data/config.json')).to.throw(Error, 'Relative paths to config files are not supported in browser.')

describe '#parameters', ->

it 'should contain all parameters', ->
Expand Down
7 changes: 7 additions & 0 deletions test/node/DIConfigurator.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ describe 'DIConfiguration', ->
di.basePath = dir
)

describe '#constructor()', ->

it 'should resolve relative path to absolute path', ->
configurator = new DIConfigurator('../data/config.json')
expect(configurator.path).to.be.equal(dir + '/config.json')
expect(configurator.create().parameters.language).to.be.equal('en')

describe '#parameters', ->

it 'should contain all parameters', ->
Expand Down
Loading

0 comments on commit b790ee3

Please sign in to comment.