This repository has been archived by the owner on Nov 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathresolver.js
49 lines (43 loc) · 1.52 KB
/
resolver.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
***** BEGIN LICENSE BLOCK *****
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*
* The Initial Developer of the Original Code is the Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2012
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Rob Miller ([email protected])
* Victor Ng ([email protected])
*
***** END LICENSE BLOCK *****
*/
"use strict";
/*
* Fetch a module attribute by name and return the actual object (usually
* a function) to which the name refers.
*
* @param name: String referring to the callable. Should consist of a module
* name (can be a bare name or a file path, anything that will
* work with `require`) and an exported module attribute name
* separated by a colon. For example, this function itself would
* be specified by './config:resolveName'.
*/
var resolveName = function(name) {
var pieces = name.split(':');
var module = require(pieces[0]);
var fn_path = pieces[1].split(".");
var fn = null;
fn = module[fn_path.shift()];
for (; fn_path.length > 0;) {
fn = fn[fn_path.shift()];
}
if (fn === undefined) {
var msg = "ERROR loading: ["+pieces[0]+":" + pieces[1] + "]. Make sure you've exported it properly.";
throw new Error(msg);
}
return fn;
};
exports.resolveName = resolveName;