This repository has been archived by the owner on Sep 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstrato-upload.js
334 lines (295 loc) · 8.74 KB
/
strato-upload.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
const program = require('commander');
const { prompt } = require('inquirer');
const rp = require('request-promise');
const yaml = require('js-yaml');
const co = require('co');
const fs = require('fs');
const path = require('path');
const urljoin = require('url-join');
const { validateConfig, dirExists } = require('./utils');
const { getBalance } = require('./balance');
const { APPLICATION, API_ENDPOINTS } = require('./properties');
const { zipFolder } = require('./strato-ziplib');
let data = {
username: null,
address: null,
hostname: null,
password: null
};
program.arguments('<dir>').action((dir) => {
currentDir = process.cwd();
dirValue = dir;
const dAppDir = path.join(currentDir, dirValue);
const zipTarget = path.join(APPLICATION.HOME_PATH, APPLICATION.CONFIG_FOLDER, APPLICATION.ZIP_FILE);
co(function* () {
yield validateConfig()
.catch((err) => {
console.error('Error: ' + err);
process.exit();
});
yield readYAML()
.catch((err) => {
console.error('Error: ' + err);
process.exit();
});
yield validateDApp(dAppDir)
.catch((err) => {
console.error('Error: ' + err);
process.exit();
});
yield getBalance()
.then((result) => {
if (result.balance <= 0) {
console.error('Sorry! You don\'t have enough tokens. Please send an email with your account address to [email protected] to request funds');
process.exit();
}
})
.catch((err) => {
console.error('Error: ' + err);
process.exit();
})
yield zipFolder(dAppDir, zipTarget)
.catch((err) => {
console.error('Error: ' + err);
deleteZip()
.then(() => {
process.exit();
})
.catch(() => {
process.exit();
});
});
yield getUserAddress()
.catch((err) => {
console.error('Error: ', err);
process.exit();
});
yield getPassword()
.then((password) => {
data.password = password.password;
})
.catch((err) => {
console.error('Error: ' + err);
process.exit();
});
yield uploadZip()
.then((response) => {
deleteZip();
console.log(response);
})
.catch((err) => {
console.error('Error: ' + err);
deleteZip();
process.exit();
});
});
});
program.parse(process.argv);
if (typeof dirValue === 'undefined') {
console.error('project path required with respect to User\'s home directory! Please refer to strato --help');
process.exit();
}
/**
* Validate dApp folder structure
* @returns {Promise}
*/
function validateDApp(dAppDir) {
return new Promise((resolve, reject) => {
dirExists(dAppDir)
.then((exists) => {
if (exists) {
let contents = fs.readdirSync(dAppDir);
let _tmpPath = path.join(dAppDir, 'index.html');
if (!(contents.indexOf('index.html') > -1 && fs.statSync(_tmpPath).isFile())) {
return reject('missing index.html file');
}
_tmpPath = path.join(dAppDir, 'metadata.json');
if (!(contents.indexOf('metadata.json') > -1 && fs.statSync(_tmpPath).isFile())) {
return reject('missing metadata.json file');
}
_tmpPath = path.join(dAppDir, 'contracts');
if (contents.indexOf('contracts') > -1 && fs.statSync(_tmpPath).isDirectory()) {
_tmpPath = path.join(dAppDir, 'contracts');
contents = fs.readdirSync(_tmpPath);
contents.forEach((file) => {
_tmpPath = path.join(dAppDir, 'contracts', file);
if (!(path.extname(file) === '.sol' && fs.statSync(_tmpPath).isFile())) {
return reject('contracts folder should only include .sol file(s)');
}
if (/^\..*/.test(file)) {
return reject('contracts folder contains hidden file(s)');
}
});
resolve();
} else {
return reject('missing contracts folder');
}
} else {
return reject('cannot find dApp at ' + dirValue + '\nNote: path must be with respects to User\'s home directory');
}
})
.catch((err) => {
console.error('Error: ' + err);
process.exit();
});
});
}
/**
* Read config.yaml file
* @returns {Promise}
*/
function readYAML() {
return new Promise((resolve, reject) => {
const YAMLFile = path.join(APPLICATION.HOME_PATH, APPLICATION.CONFIG_FOLDER, APPLICATION.CONFIG_FILE);
try {
const config = yaml.safeLoad(fs.readFileSync(YAMLFile, 'utf8'));
data.username = config.username;
data.hostname = config.hostname;
resolve();
} catch (err) {
reject(err);
}
});
}
/**
* Fetch user address
* @returns {Promise}
*/
function getUserAddress() {
return new Promise((resolve, reject) => {
// TODO: construct url using URL module
const url = urljoin(data.hostname, API_ENDPOINTS.BLOC_GET_USER_ADDRESS, data.username);
let options = {
method: 'GET',
url: url,
followRedirect: true
}
rp(options)
.then((response) => {
try {
let address = JSON.parse(response);
if (address.length > 0) {
data.address = address[0];
resolve();
} else {
reject('username not found. try running strato config to modify username');
}
} catch (err) {
reject('the hostname provided is not the STRATO node');
}
})
.catch((err) => {
if (err.error.code === 'ECONNREFUSED') {
reject('could not connect to the host. try running strato config to modify the hostname');
} else if (err.error.code === 'ENOTFOUND') {
reject('could not connect to the host');
} else {
if (err.error.code) {
reject('status code: ' + err.error.code);
} else {
reject('status code: ' + err.statusCode);
}
}
});
});
}
/**
* Upload zip on host
* @returns {Promise}
*/
function uploadZip() {
return new Promise((resolve, reject) => {
const url = urljoin(data.hostname, API_ENDPOINTS.APEX_UPLOAD_ZIP);
const zipFile = path.join(APPLICATION.HOME_PATH, APPLICATION.CONFIG_FOLDER, APPLICATION.ZIP_FILE);
let options = {
method: 'POST',
url: url,
formData: {
username: data.username,
password: data.password,
address: data.address,
file: {
value: fs.createReadStream(zipFile),
options: {
filename: APPLICATION.ZIP_FILE,
contentType: 'application/zip'
}
}
},
followRedirect: true
}
console.log('uploading...');
rp(options)
.then((response) => {
try {
let url = JSON.parse(response).url;
resolve('application successfully deployed at ' + url);
} catch (err) {
reject('the hostname provided is not the STRATO node');
}
})
.catch((err) => {
if (err.error.code === 'ECONNREFUSED') {
reject('could not connect to the host. try running strato config to modify the hostname');
} else if (err.error.code === 'ENOTFOUND') {
reject('could not connect to the host');
} else {
if (err.error.code) {
reject('status code: ' + err.error.code);
} else {
try {
let text = JSON.parse(err.error).error.message;
reject(text);
} catch (error) {
reject('status code: ' + err.statusCode);
}
}
}
})
.finally(() => {
const target = path.join(APPLICATION.HOME_PATH, APPLICATION.CONFIG_FOLDER, APPLICATION.ZIP_FILE);
if (fs.existsSync(target)) {
fs.unlinkSync(target);
}
});
});
}
/**
* Detele zip file created in User's home directory /strato
*/
function deleteZip() {
return new Promise((resolve, reject) => {
const target = path.join(APPLICATION.HOME_PATH, APPLICATION.CONFIG_FOLDER, APPLICATION.ZIP_FILE);
if (fs.existsSync(target)) {
fs.unlink(target, (err) => {
if (err) {
return reject('Error: unable to delete temporary zip folder');
}
resolve();
});
}
});
}
/**
* Get password
* @returns {Promise}
*/
function getPassword() {
return new Promise((resolve, reject) => {
prompt({
type: 'password',
name: 'password',
message: 'password: ',
validate: value => {
if (!value) return 'password required';
return true;
}
})
.then((password) => {
resolve(password);
})
.catch((err) => {
reject(err);
});
});
}