Skip to content

Commit 463cd0b

Browse files
committed
Adds Utilities.getRealPath to look for the real path to load based on the file where it was called from (for db.load and db.use)
1 parent 9c931df commit 463cd0b

2 files changed

Lines changed: 35 additions & 25 deletions

File tree

‎lib/ORM.js‎

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ var Validators = require("./Validators");
1111
var Settings = require("./Settings");
1212
var Singleton = require("./Singleton");
1313
var ErrorCodes = require("./ErrorCodes");
14+
var Utilities = require("./Utilities");
1415

1516
exports.validators = Validators;
1617
exports.singleton = Singleton;
1718
exports.settings = new Settings.Container(Settings.defaults());
18-
exports.Settings = Settings;
19+
1920
exports.Property = require("./Property");
21+
exports.Settings = Settings;
2022
exports.ErrorCodes = ErrorCodes;
2123

2224
exports.Text = Query.Text;
@@ -161,7 +163,11 @@ util.inherits(ORM, events.EventEmitter);
161163

162164
ORM.prototype.use = function (plugin_const, opts) {
163165
if (typeof plugin_const == "string") {
164-
plugin_const = require(plugin_const);
166+
try {
167+
plugin_const = require(Utilities.getRealPath(plugin_const));
168+
} catch (e) {
169+
throw e;
170+
}
165171
}
166172

167173
var plugin = new plugin_const(this, opts || {});
@@ -219,27 +225,8 @@ ORM.prototype.close = function (cb) {
219225
return this;
220226
};
221227
ORM.prototype.load = function (file, cb) {
222-
var cwd = process.cwd();
223-
var err = new Error();
224-
var tmp = err.stack.split(/\r?\n/)[2], m;
225-
226-
if ((m = tmp.match(/^\s*at\s+(.+):\d+:\d+$/)) !== null) {
227-
cwd = path.dirname(m[1]);
228-
} else if ((m = tmp.match(/^\s*at\s+module\.exports\s+\((.+?)\)/)) !== null) {
229-
cwd = path.dirname(m[1]);
230-
} else if ((m = tmp.match(/^\s*at\s+.+\s+\((.+):\d+:\d+\)$/)) !== null) {
231-
cwd = path.dirname(m[1]);
232-
}
233-
234-
if (file[0] != path.sep) {
235-
file = cwd + "/" + file;
236-
}
237-
if (file.substr(-1) == path.sep) {
238-
file += "index";
239-
}
240-
241228
try {
242-
return require(file)(this, cb);
229+
return require(Utilities.getRealPath(file))(this, cb);
243230
} catch (ex) {
244231
return cb(ex);
245232
}

‎lib/Utilities.js‎

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
* 9. [ 'property1', '-property2' ] (ORDER BY property1 ASC, property2 DESC)
1616
* ...
1717
*/
18-
1918
exports.standardizeOrder = function (order) {
2019
if (typeof order == "string") {
2120
if (order[0] == "-") {
@@ -46,9 +45,9 @@ exports.standardizeOrder = function (order) {
4645
};
4746

4847
/**
49-
* Gets all the values within an object or array, optionally using a keys array to get only specific values
48+
* Gets all the values within an object or array, optionally
49+
* using a keys array to get only specific values
5050
*/
51-
5251
exports.values = function (obj, keys) {
5352
var i, k, vals = [];
5453

@@ -154,3 +153,27 @@ exports.formatField = function (model, name, required, reversed) {
154153

155154
return fields;
156155
};
156+
157+
exports.getRealPath = function (path_str, stack_index) {
158+
var path = require("path"); // for now, load here (only when needed)
159+
var cwd = process.cwd();
160+
var err = new Error();
161+
var tmp = err.stack.split(/\r?\n/)[typeof stack_index != "undefined" ? stack_index : 3], m;
162+
163+
if ((m = tmp.match(/^\s*at\s+(.+):\d+:\d+$/)) !== null) {
164+
cwd = path.dirname(m[1]);
165+
} else if ((m = tmp.match(/^\s*at\s+module\.exports\s+\((.+?)\)/)) !== null) {
166+
cwd = path.dirname(m[1]);
167+
} else if ((m = tmp.match(/^\s*at\s+.+\s+\((.+):\d+:\d+\)$/)) !== null) {
168+
cwd = path.dirname(m[1]);
169+
}
170+
171+
if (path_str[0] != path.sep) {
172+
path_str = cwd + "/" + path_str;
173+
}
174+
if (path_str.substr(-1) == path.sep) {
175+
path_str += "index";
176+
}
177+
178+
return path_str;
179+
};

0 commit comments

Comments
 (0)