forked from gasteve/node-soop
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsoop.js
More file actions
92 lines (81 loc) · 2.83 KB
/
Copy pathsoop.js
File metadata and controls
92 lines (81 loc) · 2.83 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
var path = require('path');
var callsite = require('callsite');
var realModulePaths = module.paths;
var realModuleFilename = module.filename;
// Decorate the given constructor with some useful
// object oriented constructs (mainly a convenient inherit()
// method and the ability to do a super send)
module.exports = function(constructor) {
// inherit from the given constructor
constructor.inherit = function(parent) {
if (arguments.length > 1) {
// this allows chaining multiple classes in the call
parent.inherit(Array.prototype.slice.call(arguments, 1));
}
this.super_ = parent;
this.prototype.__proto__ = parent.prototype;
this.__proto__ = parent;
};
// invoke the given method of the parent
constructor.super = function(receiver, method, args) {
if (!this.super_) return;
if (typeof method == 'string') {
// invoke the named method
return this.super_.prototype[method].apply(receiver, args);
} else {
// invoke the constructor of the parent
return this.super_.apply(receiver, method);
}
};
// a standarized way to access a cached default instance
constructor.default = function() {
if (!this._default) this._default = new this();
return this._default;
};
// set the parent if one is specified
if (constructor.parent) {
constructor.inherit(constructor.parent);
}
return constructor;
};
// load the given module using the given imports
// @fname the module name (relative paths are relative to the caller's
// location in the file system
// @imports namespace for binding values in the loaded module
var load = function(fname, imports) {
if((fname.slice(0,2) == './') || (fname.slice(0,3) == '../')) {
var callerFilename = callsite()[1].getFileName();
fname = path.resolve(path.dirname(callerFilename), fname);
}
// fake out module path resolution here
module.paths = module.parent.paths;
module.filename = module.parent.filename;
fname = require.resolve(fname);
module.paths = realModulePaths;
module.filename = realModuleFilename;
var cachedModule = require.cache[fname];
if (cachedModule) delete require.cache[fname];
global._imports = imports;
var answer = require(fname);
delete require.cache[fname];
if (cachedModule) require.cache[fname] = cachedModule;
return answer;
};
var load_browser = function(fname, imports) {
global._imports = imports;
var answer;
try {
answer = require('!' + fname);
} catch (e) {
console.log('SOOP:' + e.message + '\nNote that SOOP requires a custom browserify configuration. please check soop\'s readme');
throw e;
}
return answer;
};
module.exports.load = process.versions ? load : load_browser;
// access the imports passed from a call to load()
module.exports.imports = function() {
var answer = global._imports || {};
global._imports = {};
return answer;
};