forked from MobiusHorizons/dw-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.js
86 lines (74 loc) · 2.15 KB
/
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
function upload(config){
'use strict'
var path = require('path')
var dwServer = require('dw-webdav')
var activate = require('./activate')
var host = config.hostname
var username = config.username
var password = config.password
var zipfile = config.zipfile
var version = path.basename(zipfile)
var server = new dwServer(host, 'dw-utils', username, password)
var done = () => {
console.log('done')
}
var progress = (p) => {
if (p.done == true){
process.stdout.write('\r\x1b[2KUploading: ... ')
} else {
var date = new Date(null)
date.setSeconds(p.eta) // specify value for SECONDS here
var eta = '(' + date.toISOString().substr(14, 5) + ')'
process.stdout.write('\r\x1b[2KUploading: ... ' + p.percentage.toFixed(1) + '% ' + eta)
}
}
let tries = 0;
function authError(e){
if (e == 'EXIT') return Promise.reject('EXIT');
if (tries >= 3) {
console.log(`Could Not Connect after ${tries} attempts.`);
return Promise.reject('EXIT');
}
console.log('Invalid Username or Password')
tries ++;
return config.prompt(config)
.catch(() => Promise.reject('EXIT'))
.then((config) =>{
server = new dwServer(config.hostname, 'dw-utils', config.username, config.password);
return server.auth()
.then(() => {
return config.saveConfig(config).catch(() => {})
});
}).catch(authError);
}
server.auth()
.catch(authError)
.then(() => {
return server.upload(zipfile, version, progress)
})
.then(() => {
progress({done : true})
})
.then(done)
.then(() => {
process.stdout.write('Unzipping remote file: ... ')
return server.unzip(version)
})
.then(done)
.then(() => {
process.stdout.write('Deleting remote zip file: ... ')
return server.delete(version)
})
.then(done)
.then(() => {
if (config.activate){
process.stdout.write('Activating version: ... ')
return activate.activate(config,version)
.then(done)
}
})
.catch((error) => {
console.log('Error: ', error)
})
}
module.exports = upload