-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
286 lines (252 loc) · 8.5 KB
/
index.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/**
* fis.baidu.com
*/
var pth = require('path');
var prompt = require('prompt');
var info = deployInfo() || {};
function getServerInfo() {
var conf = pth.join(fis.project.getTempPath('server'), 'conf.json');
if (fis.util.isFile(conf)) {
return fis.util.readJSON(conf);
}
return {};
}
var serverRoot = (function(){
var key = 'FIS_SERVER_DOCUMENT_ROOT';
var serverInfo = getServerInfo();
if(process.env && process.env[key]){
var path = process.env[key];
if(fis.util.exists(path) && !fis.util.isDir(path)){
fis.log.error('invalid environment variable [' + key + '] of document root [' + path + ']');
}
return path;
} else if (serverInfo['root'] && fis.util.is(serverInfo['root'], 'String')) {
return serverInfo['root'];
} else {
return fis.project.getTempPath('www');
}
})();
var cwd = fis.processCWD || process.cwd();
function normalizePath(to, root){
if(to[0] === '.'){
to = fis.util(cwd + '/' + to);
} else if(/^output\b/.test(to)){
to = fis.util(root + '/' + to);
} else if(to === 'preview'){
to = serverRoot;
} else {
to = fis.util(to);
}
return to;
}
function deliver(output, release, content, file, callback){
if(!release){
fis.log.error('unable to get release path of file['
+ file.realpath
+ ']: Maybe this file is neither in current project or releasable');
}
if(fis.util.exists(output) && !fis.util.isDir(output)){
fis.log.error('unable to deliver file['
+ file.realpath + '] to dir['
+ output + ']: invalid output dir.');
}
var target;
target = fis.util(output, release);
fis.util.write(target, content);
fis.log.debug(
'release ' +
file.subpath.replace(/^\//, '') +
' >> '.yellow.bold +
target
);
callback();
}
var quenue = null;
function requireEmail(settings, cb) {
if (quenue) {
return quenue.push(cb);
}
if (!settings.authApi) {
return cb('config.authApi is Required');
}
if (info.token) {
console.log('Token expired!');
}
quenue = [cb];
prompt.get({
properties: {
email: {
pattern: /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i,
message: 'The specified value must be a valid email address.',
description: 'Enter your email',
required: true,
default: info.email
}
}
}, function(error, ret) {
if (error) {
return cb(error);
}
info.email = ret.email;
deployInfo(info);
fetch(settings.authApi, {
email: ret.email
}, function(error, ret) {
if (error) {
return cb(error);
}
console.log('We\'re already sent the code to your email.')
requireToken(settings, callback);
});
})
function callback(error, ret) {
var arr = quenue;
quenue = null;
arr.forEach(function(fn) {
fn(error, ret);
});
}
}
function requireToken(settings, cb) {
if (!settings.validateApi) {
return cb('config.authApi is Required');
}
prompt.get({
properties: {
code: {
description: 'Enter your code',
required: true,
hide: true
}
}}, function(error, ret) {
if (error) {
return cb(error);
}
info.code = ret.code;
deployInfo(info);
fetch(settings.validateApi, {
email: info.email,
code: info.code
}, function(error, ret) {
if (error) {
return cb(error);
}
info.token = ret.data.token;
deployInfo(info);
cb(null, info);
});
})
}
function fetch(url, data, callback) {
var endl = '\r\n';
var collect = [];
var opt = {};
fis.util.map(data, function(key, value) {
collect.push(key + '=' + encodeURIComponent(value))
});
var content = collect.join('&');
opt.method = opt.method || 'POST';
opt.headers = {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
};
opt = fis.util.parseUrl(url, opt);
var http = opt.protocol === 'https:' ? require('https') : require('http');
var req = http.request(opt, function(res) {
var status = res.statusCode;
var body = '';
res
.on('data', function(chunk) {
body += chunk;
})
.on('end', function() {
if (status >= 200 && status < 300 || status === 304) {
var json = null;
try {json = JSON.parse(body);} catch(e) {};
if (!json || json.errno) {
callback(json || 'The response is not valid json string.')
} else {
callback(null, json);
}
} else {
callback(status);
}
})
.on('error', function(err) {
callback(err.message || err);
});
});
req.write(content);
req.end();
}
function upload(receiver, to, release, content, file, callback, settings){
var subpath = file.subpath;
fis.util.upload(
//url, request options, post data, file
receiver, null, { to : to + release, email: info.email, token: info.token }, content, subpath,
function(err, res){
var json = null;
try {
json = res ? JSON.parse(res) : null;
} catch (e) {}
if (!err && json && json.errno) {
if (json.errno === 100302 || json.errno === 100305) {
requireEmail(settings, function(error, info) {
if (error) {
return fis.log.error('upload file [' + subpath + '] to [' + to +
'] by receiver [' + receiver + '] error [' + error.errmsg + ']');
}
upload(receiver, to, release, content, file, callback, settings);
});
} else {
fis.log.error('Server Error: ' + json.errmsg);
}
} else if(err || !json && res.trim() != '0'){
fis.log.error('upload file [' + subpath + '] to [' + to +
'] by receiver [' + receiver + '] error [' + (err || res) + ']');
} else {
var time = '[' + fis.log.now(true) + ']';
process.stdout.write(
' - '.green.bold +
time.grey + ' ' +
subpath.replace(/^\//, '') +
' >> '.yellow.bold +
to + release +
'\n'
);
callback();
}
}
);
}
function getTmpFile() {
return fis.project.getTempPath('deploy.json');
}
function deployInfo(options) {
var conf = getTmpFile();
if (arguments.length) {
// setter
return options && fis.util.write(conf, JSON.stringify(options, null, 2));
} else {
var ret = null;
try {
// getter
ret = fis.util.isFile(conf) ? require(conf) : null;
} catch (e) {
}
return ret;
}
};
module.exports = function (dest, file, content, settings, callback) {
var root = fis.project.getProjectPath();
var to = normalizePath(dest.to, root);
if (settings.host) {
settings.receiver = settings.host + '/v1/upload';
settings.authApi = settings.host + '/v1/authorize';
settings.validateApi = settings.host + '/v1/validate';
}
if(settings && settings.receiver) {
upload(settings.receiver, to, dest.release, content, file, callback, settings);
} else {
deliver(to, dest.release, content, file, callback);
}
};