|
1
|
|
|
'use strict' |
|
2
|
|
|
|
|
3
|
|
|
const output = require('./output') |
|
4
|
|
|
const fs = require('fs') |
|
5
|
|
|
const io = require('socket.io-client') |
|
6
|
|
|
const tmp = require('tmp') |
|
7
|
|
|
const pkg = require('../package.json') |
|
8
|
|
|
const path = require('path') |
|
9
|
|
|
const chalk = require('chalk') |
|
10
|
|
|
const moment = require('moment') |
|
11
|
|
|
const request = require('./util').request |
|
12
|
|
|
const child = require('./childprocess') |
|
13
|
|
|
const mod = require('./module') |
|
14
|
|
|
const archiver = require('archiver') |
|
15
|
|
|
const filesize = require('file-size') |
|
16
|
|
|
|
|
17
|
|
|
module.exports = { |
|
18
|
|
|
uploadFile: function (containerToken, event, path, cb, spinner) { |
|
19
|
|
|
var r = request(containerToken + '/fs', { |
|
20
|
|
|
'action': event, |
|
21
|
|
|
'name': path |
|
22
|
|
|
}, { |
|
23
|
|
|
'Content-Type': 'multipart/form-data' |
|
24
|
|
|
}) |
|
25
|
|
|
|
|
26
|
|
|
if (event === 'add' || event === 'change' || event === 'zipBall') { |
|
27
|
|
|
r = r.attach('file', path) |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
var self = this |
|
31
|
|
|
return r.end(function (response) { |
|
32
|
|
|
if (response.body && response.body === 'Error: invalid container') { |
|
33
|
|
|
fs.unlink('.scholica', function () { |
|
34
|
|
|
(spinner || output.err)('Container ' + containerToken + ' does not exist anymore. We cleaned your local environment, so that you can start with a clean container.') |
|
35
|
|
|
process.exit() |
|
36
|
|
|
}) |
|
37
|
|
|
return |
|
38
|
|
|
} |
|
39
|
|
|
if (cb) { |
|
40
|
|
|
cb(response.body || '') |
|
41
|
|
|
return |
|
42
|
|
|
} |
|
43
|
|
|
if (response.body) { |
|
44
|
|
|
if (response.body.indexOf('Error:') === 0) { |
|
45
|
|
|
output.err(response.body) |
|
46
|
|
|
} else { |
|
47
|
|
|
self.logSyncMessage(response.body) |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
}) |
|
51
|
|
|
}, |
|
52
|
|
|
|
|
53
|
|
|
logMessage: function (data, ret) { |
|
54
|
|
|
var message = |
|
55
|
|
|
chalk.bold(chalk[data.color](data.component)) + ' ' + |
|
56
|
|
|
data.message |
|
57
|
|
|
|
|
58
|
|
|
if ('time' in data) { |
|
59
|
|
|
if (data.time === 'now') { |
|
60
|
|
|
data.time = (new Date()).getTime() / 1000 |
|
61
|
|
|
} |
|
62
|
|
|
message += ' ' + chalk.dim(moment(data.time * 1000).format('hh:mm:ss')) |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if (ret) { |
|
66
|
|
|
return message |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return output.log(message) |
|
70
|
|
|
}, |
|
71
|
|
|
|
|
72
|
|
|
logSyncMessage: function (message, ret) { |
|
73
|
|
|
return this.logMessage({ |
|
74
|
|
|
component: 'Sync', |
|
75
|
|
|
color: 'green', |
|
76
|
|
|
message: message, |
|
77
|
|
|
time: 'now' |
|
78
|
|
|
}, !!ret) |
|
79
|
|
|
}, |
|
80
|
|
|
|
|
81
|
|
|
createInitialZip: function (dir, containerToken, envr, cb) { |
|
82
|
|
|
if (fs.existsSync(path.join(dir, 'vendor'))) { |
|
83
|
|
|
output.warn('Ignoring directory \'vendor\'') |
|
84
|
|
|
} |
|
85
|
|
|
if (fs.existsSync(path.join(dir, 'node_modules'))) { |
|
86
|
|
|
output.warn('Ignoring directory \'node_modules\'') |
|
87
|
|
|
} |
|
88
|
|
|
if (fs.existsSync(path.join(dir, 'web/node_modules'))) { |
|
89
|
|
|
output.warn('Ignoring directory \'web/node_modules\'') |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
var zipName = tmp.tmpNameSync({}) |
|
93
|
|
|
var spinner = output.wait(this.logSyncMessage('Creating initial zipball', true)) |
|
94
|
|
|
var zipFile = fs.createWriteStream(zipName) |
|
95
|
|
|
var self = this |
|
96
|
|
|
|
|
97
|
|
|
zipFile.on('close', function () { |
|
98
|
|
|
// Upload file to api.sadev.io |
|
99
|
|
|
fs.stat(zipName, function (err, stat) { |
|
100
|
|
|
if (err) { |
|
101
|
|
|
spinner('Error creating zipball') |
|
102
|
|
|
process.exit() |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
spinner() |
|
106
|
|
|
|
|
107
|
|
|
var size = filesize(stat.size).human('si') |
|
108
|
|
|
var stopSpinner = output.wait(self.logSyncMessage('Uploading initial zipball (' + size + ')', true)) |
|
109
|
|
|
|
|
110
|
|
|
try { |
|
111
|
|
|
self.uploadFile(containerToken, 'zipBall', zipName, function (responseBody) { |
|
112
|
|
|
fs.unlink(zipName, function () {}) |
|
113
|
|
|
if (responseBody.indexOf('OK') !== 0) { |
|
114
|
|
|
stopSpinner(responseBody || 'Could not connect to server (empty response)') |
|
115
|
|
|
process.exit() |
|
116
|
|
|
} |
|
117
|
|
|
stopSpinner(true) |
|
118
|
|
|
cb() |
|
119
|
|
|
}, spinner) |
|
120
|
|
|
} catch (e) { |
|
121
|
|
|
stopSpinner(e.message) |
|
122
|
|
|
} |
|
123
|
|
|
}) |
|
124
|
|
|
}) |
|
125
|
|
|
|
|
126
|
|
|
var archive = archiver('zip', { |
|
127
|
|
|
store: true |
|
128
|
|
|
}) |
|
129
|
|
|
archive.on('error', function (err) { |
|
130
|
|
|
output.err(err) |
|
131
|
|
|
}) |
|
132
|
|
|
archive.pipe(zipFile) |
|
133
|
|
|
archive.glob(path.join(dir, '**'), { |
|
134
|
|
|
ignore: ['.git/**', '.scholica', '.scholica.beta', 'node_modules', 'web/node_modules', 'node_modules/**', 'web/node_modules/**', '.scholica.dev', 'vendor/**', '.gitignore', '.jshintrc'] |
|
135
|
|
|
}) |
|
136
|
|
|
archive.finalize() |
|
137
|
|
|
}, |
|
138
|
|
|
|
|
139
|
|
|
startChildProcess: function () { |
|
140
|
|
|
mod.getManifest(function (manifest) { |
|
141
|
|
|
if (!('scripts' in manifest) || typeof manifest.scripts !== 'object') { |
|
142
|
|
|
return |
|
143
|
|
|
} |
|
144
|
|
|
if (!('run' in manifest.scripts) || !('length' in manifest.scripts.run)) { |
|
145
|
|
|
return |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
for (var x in manifest.scripts.run) { |
|
149
|
|
|
var script = manifest.scripts.run[x] |
|
150
|
|
|
child.run(script) |
|
151
|
|
|
} |
|
152
|
|
|
}) |
|
153
|
|
|
}, |
|
154
|
|
|
|
|
155
|
|
|
startWebSocket: function (containerToken) { |
|
156
|
|
|
var socket = io('http://ws.sadev.io/') |
|
157
|
|
|
var self = this |
|
158
|
|
|
|
|
159
|
|
|
socket.on('connect', function () { |
|
160
|
|
|
socket.emit('room', 'SADEV:' + containerToken) |
|
161
|
|
|
socket.emit('room', 'BROADCAST') |
|
162
|
|
|
socket.emit('room', pkg.name + '/' + pkg.version) |
|
163
|
|
|
socket.emit('clientInfo', pkg) |
|
164
|
|
|
}) |
|
165
|
|
|
|
|
166
|
|
|
socket.on('log', function (data) { |
|
167
|
|
|
if (!('color' in data)) { |
|
168
|
|
|
if (data.component === 'Logger') { |
|
169
|
|
|
data.color = 'yellow' |
|
170
|
|
|
} else { |
|
171
|
|
|
data.color = 'blue' |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
self.logMessage(data) |
|
175
|
|
|
}) |
|
176
|
|
|
|
|
177
|
|
|
socket.on('disconnect', function () { |
|
178
|
|
|
socket.close() |
|
179
|
|
|
setTimeout(function () { |
|
180
|
|
|
self.startWebSocket(containerToken) |
|
181
|
|
|
}, 200) |
|
182
|
|
|
}) |
|
183
|
|
|
|
|
184
|
|
|
socket.open() |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
} |
|
188
|
|
|
|