-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfsr.js
197 lines (169 loc) · 6.3 KB
/
fsr.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
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const prompts = require('prompts');
const util = require('./util');
function upload(receiver, to, data, release, content, file, callback) {
let subpath = file.subpath;
data.to = path.join(file.to, subpath);
util.upload({
receiver,
data,
content,
subpath
},
function (err, res) {
let json = null;
res = res && res.trim();
try {
json = res ? JSON.parse(res) : null;
} catch (e) {}
if (!err && json && json.errno) {
callback(json);
} else if (err || !json && res != '0') {
callback('upload file [' + subpath + '] to [' + data.to + '] by receiver [' + receiver + '] error [' + (err || res) + ']');
} else {
let time = '[' + util.now(true) + ']';
process.stdout.write(
chalk.green.bold('\n - ') +
chalk.grey(time) + ' ' +
subpath.replace(/^\//, '') +
chalk.yellow.bold(' >> ') +
data.to
);
callback();
}
}
);
}
function requireEmail(authApi, validateApi, info, cb) {
prompts({
type: 'text',
name: 'email',
message: 'Enter your email',
validate: value => /^((([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.test(value)
}).then(response => {
info.email = response.email;
deployInfo(info);
util.fetch(authApi, {
email: response.email
}, function (error, ret) {
if (error) {
return cb(error);
}
console.log('We\'ve already sent the code to your email.')
requireToken(validateApi, info, cb);
});
}).catch(error => {
cb(error);
});
}
function requireToken(validateApi, info, cb) {
prompts({
type: 'password',
name: 'code',
message: 'Enter your code'
}).then(response => {
info.code = response.code;
deployInfo(info);
util.fetch(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);
});
}).catch(error => {
cb(error);
});
}
function getTmpFile() {
const dir = require('os').homedir() + '/.deploy-tmp';
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
return dir + '/deploy.json';
}
function deployInfo(options) {
let conf = getTmpFile();
if (arguments.length) {
// setter
return options && fs.writeFileSync(conf, JSON.stringify(options, null, 2));
} else {
let ret = null;
try {
// getter
ret = fs.existsSync(conf) ? require(conf) : null;
} catch (e) {
}
return ret;
}
};
module.exports = function (modified, options, callback) {
if (!options.to) {
// throw new Error('options.to is required!');
}
let info = deployInfo() || {};
let to = options.to;
let receiver = options.receiver;
let authApi = options.authApi;
let validateApi = options.validateApi;
let data = options.data || {};
if (options.host) {
receiver = options.receiver = options.host + '/v1/upload';
authApi = options.authApi = options.host + '/v1/authorize';
validateApi = options.validateApi = options.host + '/v1/validate';
}
if (!options.receiver) {
throw new Error('options.receiver is required!');
}
let steps = [];
modified.forEach(file => {
let reTryCount = options.retry;
steps.push(function (next) {
if (options.aborted) {
return;
}
/*eslint-disable */
let $$upload = arguments.callee;
/* eslint-enable */
data.email = info.email;
data.token = info.token;
upload(receiver, to, data, file.releasePath, file.content, file, error => {
if (!error) {
next();
return;
}
if (error.errno === 100302 || error.errno === 100305) {
// 检测到后端限制了上传,要求用户输入信息再继续。
if (!authApi || !validateApi) {
throw new Error('options.authApi and options.validateApi is required!');
}
if (info.email) {
console.error('\nToken is invalid: ', error.errmsg, '\n');
}
requireEmail(authApi, validateApi, info, function (error) {
if (error) {
throw new Error('Auth failed! ' + error.errmsg);
} else {
$$upload(next);
}
});
} else if (options.retry && !--reTryCount) {
throw new Error(error.errmsg || error);
} else {
$$upload(next);
}
});
});
});
steps.reduceRight((next, current) => {
return () => {
current(next);
};
}, callback)();
};