-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.js
191 lines (190 loc) · 5.2 KB
/
main.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* exports toolkit
* @author genify([email protected])
*/
// klass exports map
var KLASS = {
// base klass
Event:'util/event',
Logger:'util/logger#Logger',
// resource meta
RES_Text:'meta/text',
RES_Html:'meta/html',
RES_Style:'meta/style',
RES_Script:'meta/script',
RES_Template:'meta/template',
RES_Resource:'meta/resource',
// resource explorer
EXP_Html:'explorer/html',
EXP_Style:'explorer/style',
EXP_Script:'explorer/script',
EXP_Template:'explorer/template',
EXP_Explorer:'explorer/explorer',
// resource adapter
ADP_Style:'adapter/style',
ADP_Script:'adapter/script',
// resource parser
PRS_Tag:'parser/tag#Parser',
PRS_Html:'parser/html',
PRS_Config:'parser/config',
PRS_Tokenizer:'parser/token',
// file parser
FLP_Parser:'script/parser',
FLP_NEJParser:'script/nej#Parser',
FLP_NEJPatcher:'script/nej/patcher',
// entry parser
Deployer:'deploy',
Exporter:'export'
};
// api exports map
var API = {
io:'util/io',
rg:'util/args',
fs:'util/file',
ps:'util/path',
ut:'util/util',
ks:'util/klass',
dp:'util/dependency',
lg:'util/logger#level,logger,log',
tag:'parser/tag#stringify',
nej:'script/nej/util'
};
// export klass or api
function global(map){
Object.keys(map).forEach(function(key){
var file = map[key],
arr = file.split('#'),
mdl = require('./lib/'+arr[0]+'.js');
// for util/logger#Logger
if (!!arr[1]){
// for util/logger#level,logger
var brr = arr[1].split(',');
if (brr.length>1){
var ret = {};
brr.forEach(function(name){
ret[name] = mdl[name];
});
mdl = ret;
}else{
mdl = mdl[brr[0]];
}
}
exports[key] = mdl;
});
};
// export constructor
// export api
global(KLASS);
global(API);
// bin api
var path = require('path'),
_fs = require('./lib/util/file.js'),
_path = require('./lib/util/path.js'),
_util = require('./lib/util/util.js'),
_log = require('./lib/util/logger.js'),
_logger = _log.logger;
/**
* init project deploy config
* @param {String} output - output path
* @return {Void}
*/
exports.init = function(output){
output = _path.absolute(
output+'/',process.cwd()+'/'
);
var content = require('swig').renderFile(
__dirname+'/template/release.conf',{
comment:'#',
DIR_WEBROOT:'../webapp/'
}
);
_fs.write(output+'release.conf',content);
_logger.info('output release.conf to %s',output);
};
/**
* deploy project by config file
* @param {String} file - config file path
* @param {Object} config - config options
* @param {Function} callback - deploy done callback
* @return {Void}
*/
exports.build = function(file,config,callback){
file = _path.absolute(
file,process.cwd()+'/'
);
var opt = _util.merge(config,{
file:file,
done:callback||function(){}
});
new (require('./lib/deploy.js'))(opt);
};
/**
* export script list
* @param {Array} list - script list
* @param {Object} config - config object
* @param {String} config.output - output file path
* @param {String} config.bags - output name bags
* @param {Function} callback - export callback
* @return {Void}
*/
exports.export = function(list,config,callback){
// check list
callback = callback||function(){};
if (!list||!list.length){
_logger.warn('no input script files');
callback();
return;
}
// absolute output
config = config||{};
var cwd = process.cwd()+'/';
config.output = _path.absolutePath(
config.output||'./output.js',cwd
);
// absolute name bags
if (!!config.bags){
config.bags = _path.absolutePath(
config.bags,cwd
);
}
// do export
new (require('./lib/export.js'))(
_util.merge(config,{
file:_path.absolutePath(
'./script.html',cwd
),
list:list,
done:callback||function(){}
})
);
};
/**
* build web cache information
* @param {String} file - config file path
* @param {Object} options - build config object
* @param {String} options.appid - web cache for app identify
* @param {String} options.token - web cache upload api token
* @param {Function} callback - after build callback
*/
exports.cache = function(file,options,callback){
_logger.setLevel(options.level);
var config = _path.absolute(
file,process.cwd()+'/'
);
var root = path.dirname(config)+'/';
_logger.info('run web cache build with config file %s',config);
try{
config = require(config);
_logger.debug('config information -> %j',config);
}catch(ex){
_logger.error('cant get web cache config for reason:\n%s',ex.stack);
process.abort();
}
config.root = root;
config.ondone = callback||function(){
// TODO
};
config.token = options.token||config.token||'';
config.appid = options.appid||config.appid||'';
require('./lib/cache.js').run(config);
};