This repository has been archived by the owner on Sep 28, 2018. It is now read-only.
/
utils.js
173 lines (163 loc) · 3.83 KB
/
utils.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* Copyright (c) Yohei Sasaki <yssk22@gmail.com>
* MIT Licensed
*/
var path = require('path'),
fs = require('fs');
var $ = require('jquery');
/**
* Merge the objects into the first argument.
*
* @api private
*/
exports.merge = function merge(){
return $.extend.apply($, arguments);
};
/**
* Clone the object. This is the deep copy implementation
*
* @param {Object} obj
* @api private
*/
exports.clone = function clone(obj){
if( obj === null || obj === undefined ){
return obj;
}else{
if( typeof(obj) == 'object' ){
if( Array.isArray(obj) ){
return $.extend.call($, true, [], obj);
}else{
return $.extend.call($, true, {}, obj);
}
}else{
// not a object.
return obj;
}
}
};
/**
* Returns abstract path
*
* @param {String} p path
* @return {String} abstract path
*/
exports.abspath = function abspath(p){
return p.indexOf('/') == 0 ? p : path.join(process.cwd(), p);
};
/**
* Bind the context with the given target object.
*
* @param {Object} target object bound with.
* @param {Object} context context object.
* @param {Function} callback the callback function which receives (err, bound) arguments.
*
*
*/
exports.bindContext = function bindContext(target, context, callback){
var type = typeof(target);
if( typeof(context) == 'function' ){
callback = context;
context = {};
}
if( target == null ){ // null or undefined
callback(null, target);
}else if( type == 'object' ){ // object or array
// Array or Object
target = exports.clone(target);
var wait = 0;
var cb = function(err){
wait -= 1;
if( wait == 0 ){
callback(err, target);
}
};
for(var i in target){
if( target.hasOwnProperty(i) ){
wait += 1;
(function(k){
process.nextTick(function(){
bindContext(target[k], context, function(err, result){
if( result === undefined ){
delete(target[k]);
}else{
target[k] = result;
}
cb(err);
});
});
})(i);
}
}
}else if( type == 'function' ){ // Function
target.call(context, callback);
}else{ // Number, String, or boolean
callback(null, target);
}
};
var log4js = undefined;
exports.logConfigure = function(fileSystem, standardOutput, configPaths){
log4js = require('log4js')(fileSystem, standardOutput, configPaths);
return log4js;
}
/**
* Returns the logger.
* This function is an wrapper for log4js to reduce dependencies.
*
* @param {String} name logger category name.
* @return {Object} logger instance
* @api public
*/
exports.getLogger = function getLogger(name){
if( log4js == undefined ){
log4js = require('log4js')();
}
return log4js.getLogger(name || "sunrise");
};
/**
* Load JSON file from the given filepath.
*
* @param {String} filepath path to json file.
* @param {Function} callback
* @api public
*/
exports.loadJSON = function(filepath, callback){
fs.readFile(path, function(err, data){
if( err ){
callback(e);
}else{
try{
callback(null, JSON.parse(data));
}catch(e){
callback(e);
}
}
});
};
/**
* Synchronized version of loadJSON
*/
exports.loadJSONSync = function(filepath){
var obj = JSON.parse(fs.readFileSync(filepath));
return obj;
};
/**
* Parallel executions for function list.
*/
exports.parallel = function(){
var args = Array.prototype.slice.call(arguments);
var callback = args.pop();
var remains = args.length;
if( callback == undefined || remains < 1 ){
throw new TypeError('one or more functions must be passed to `parallel`');
}
args.forEach(function(f){
process.nextTick(function(){
f(function(){
remains--;
if( remains == 0 ){
callback();
}
});
});
});
};