-
Notifications
You must be signed in to change notification settings - Fork 31
/
fis-kernel.js
132 lines (110 loc) · 2.96 KB
/
fis-kernel.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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* fis
* http://fis.baidu.com/
*/
'use strict';
var last = Date.now();
//oo
Function.prototype.derive = function(constructor, proto){
if(typeof constructor === 'object'){
proto = constructor;
constructor = proto.constructor || function(){};
delete proto.constructor;
}
var parent = this;
var fn = function(){
parent.apply(this, arguments);
constructor.apply(this, arguments);
};
var tmp = function(){};
tmp.prototype = parent.prototype;
var fp = new tmp(),
cp = constructor.prototype,
key;
for(key in cp){
if(cp.hasOwnProperty(key)){
fp[key] = cp[key];
}
}
proto = proto || {};
for(key in proto){
if(proto.hasOwnProperty(key)){
fp[key] = proto[key];
}
}
fp.constructor = constructor.prototype.constructor;
fn.prototype = fp;
return fn;
};
//factory
Function.prototype.factory = function(){
var clazz = this;
function F(args){
clazz.apply(this, args);
}
F.prototype = clazz.prototype;
return function(){
return new F(arguments);
};
};
var fis = module.exports = {};
//register global variable
Object.defineProperty(global, 'fis', {
enumerable : true,
writable : false,
value : fis
});
fis.emitter = new (require('events').EventEmitter);
//time for debug
fis.time = function(title){
console.log(title + ' : ' + (Date.now() - last) + 'ms');
last = Date.now();
};
//log
fis.log = require('./lib/log.js');
//require
fis.require = function(){
var path;
var name = Array.prototype.slice.call(arguments, 0).join('-');
if(fis.require._cache.hasOwnProperty(name)) return fis.require._cache[name];
var names = [];
for(var i = 0, len = fis.require.prefixes.length; i < len; i++){
try {
var pluginName = fis.require.prefixes[i] + '-' + name;
names.push(pluginName);
path = require.resolve(pluginName);
try {
return fis.require._cache[name] = require(pluginName);
} catch (e){
fis.log.error('load plugin [' + pluginName + '] error : ' + e.message);
}
} catch (e){
if (e.code !== 'MODULE_NOT_FOUND') {
throw e;
}
}
}
fis.log.error('unable to load plugin [' + names.join('] or [') + ']');
};
fis.require._cache = {};
fis.require.prefixes = ['fis'];
//system config
fis.config = require('./lib/config.js');
//utils
fis.util = require('./lib/util.js');
//resource location
fis.uri = require('./lib/uri.js');
//project
fis.project = require('./lib/project.js');
//file
fis.file = require('./lib/file.js');
//cache
fis.cache = require('./lib/cache.js');
//compile kernel
fis.compile = require('./lib/compile.js');
//release api
fis.release = require('./lib/release.js');
//package info
fis.info = fis.util.readJSON(__dirname + '/package.json');
//kernel version
fis.version = fis.info.version;