-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathclean.js
126 lines (114 loc) · 3.45 KB
/
clean.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
function clean(config){
'use strict'
var utils = require('./utils.js')
var fs = require('fs')
var path = require('path');
var branch = require('git-branch');
var dwServer = require('dw-webdav')
var activate = require('./activate');
var host = config.hostname
var version = config.version
var username = config.username
var cartridges = config.cartridges
var uploadPath = config.uploadPath || cartridges
var password = config.password
var cartridgeRelativePath = version;
if (cartridges != uploadPath) {
cartridgeRelativePath = path.join(version, path.relative(cartridges, uploadPath));
}
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(`Could not connect to '${config.hostname}' as '${config.username}'`);
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(() => {
try {
let branchName = branch.sync();
console.log('[ Project Clean on branch \'' + branchName + '\': ' + (new Date()).toLocaleString() + ' ]');
} catch (e){
console.log('[ Project Clean: ' + (new Date()).toLocaleString() + ' ]');
}
})
.then(() => {
process.stdout.write('Zipping local files: ... ')
return utils.zip(uploadPath, cartridgeRelativePath, version + '.zip')
})
.then(done)
.then(() => {
return server.upload(version + '.zip', progress)
})
.then(() => {
progress({done : true})
})
.then(done)
.then(() => {
process.stdout.write('Deleting old files: ... ')
return server.delete(cartridgeRelativePath)
})
.then(done)
.then(() => {
process.stdout.write('Unzipping remote file: ... ')
return server.unzip(version + '.zip')
})
.then(done)
.then(() => {
process.stdout.write('Deleting temporary files: ... ')
var wait = []
wait.push(server.delete(version + '.zip'))
wait.push(new Promise((resolve, reject) => {
fs.unlink(version + '.zip', function(error){
if (error){
reject(error)
} else {
resolve()
}
})
}))
return Promise.all(wait)
})
.then(done)
.then(() => {
if (config.activate){
process.stdout.write('Activating version: ... ')
return activate.activate(config,version)
.then(done)
}
return true;
})
.catch((error) => {
if (error == 'EXIT') return;
console.log('Error: ', error)
})
}
module.exports = clean