-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.js
223 lines (169 loc) · 5.36 KB
/
agent.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
const Promise = require("bluebird");
const clone = Promise.promisify(require('git-clone'));
const rimraf = Promise.promisify(require('rimraf'));
const exec = Promise.promisify(require('child_process').exec);
const fs = require('fs');
const archiver = require('archiver');
const DecompressZip = require('decompress-zip');
const chalk = require('chalk');
const mkdirp = require('mkdirp');
const path = require('path');
const INTERNAL_COMMAND_SIGNATURE = '$tsero';
const COMMAND_UNZIP = 'unzip';
const COMMAND_DELDIR = 'deldir';
const COMMAND_ZIP = 'zip';
const COMMAND_GITCLON = 'gitclone';
let configstring = fs.readFileSync('tseroconf.json', {
encoding: 'utf-8'
});
let curretnConfig = JSON.parse(configstring);
function deleteDirectory(targetPath) {
console.log(chalk.yellow(`Deleting Directory ${targetPath} ...`));
return rimraf(targetPath)
.then(function () {
console.log(chalk.green('Done'));
return;
})
.catch(function (err) {
console.log(chalk.red(err));
return;
});
}
function cloneRepository(repoUri, cloneDest) {
console.log(chalk.yellow('Cloning Repository ... '));
return clone(repoUri, cloneDest)
.then(function () {
console.log(chalk.green('Done'));
return;
})
.catch(function (err) {
console.log(chalk.red(err));
return;
})
}
function run(cmd, cwd) {
return exec(cmd, {
cwd: cwd
})
.then(function (stdout, stderr) {
if (stderr) {
console.log('' + chalk.red(stderr));
}
if (stdout) {
console.log('' + chalk.green(stdout));
}
return;
})
.catch(function (err) {
if (err) {
console.log('' + chalk.red(err));
return;
}
})
};
function archiveFiles(workingPath, outartifact, ignoreFile) {
console.log(chalk.yellow('Archiving Artifact Files ... '));
return new Promise(function (resolve, reject) {
let ignoreArray = JSON.parse(fs.readFileSync(ignoreFile, {
encoding: 'utf-8'
}));
mkdirp.sync(path.dirname(outartifact));
var output = fs.createWriteStream(outartifact);
var archive = archiver('zip', {
zlib: {
level: 9
} // Sets the compression level.
});
// listen for all archive data to be written
output.on('close', function () {
console.log(chalk.green(archive.pointer() + ' total bytes'));
console.log(chalk.green('archiver has been finalized and the output file descriptor has closed.'));
resolve();
});
// good practice to catch warnings (ie stat failures and other non-blocking errors)
archive.on('warning', function (err) {
reject(err.code);
});
// good practice to catch this error explicitly
archive.on('error', function (err) {
reject(err);
});
// pipe archive data to the file
archive.pipe(output);
archive.glob(`**`, {
cwd: `${workingPath}`,
ignore: ignoreArray
}, {});
// finalize the archive (ie we are done appending files but streams have to finish yet)
archive.finalize();
}).catch(function (err) {
console.log(err);
});
}
function unzipArtifact(targetPath, unzipDest) {
console.log(chalk.yellow('Extracting Artifact Files From Archive ... '));
return new Promise(function (resolve, reject) {
var unzipper = new DecompressZip(targetPath)
unzipper.on('error', function (err) {
reject(err);
});
unzipper.on('extract', function (log) {
console.log(chalk.green('Finished extracting'));
resolve(log);
});
unzipper.on('progress', function (fileIndex, fileCount) {
console.log('Extracted file ' + (fileIndex + 1) + ' of ' + fileCount);
});
unzipper.extract({
path: unzipDest,
filter: function (file) {
return file.type !== "SymbolicLink";
}
});
}).catch(function (err) {
console.log(err);
});
}
function runCommandsFromConfig(commands) {
let p = Promise.resolve();
commands.forEach(function (item) {
item.cmds.forEach(function (command) {
p = p.then(function () {
if (isInternalCommand(command)) {
console.log(chalk.blue(command, item.cwd));
return internalCommandHandler(command)
}
console.log(chalk.yellow(command, item.cwd));
return run(command, item.cwd)
});
})
});
return p;
function isInternalCommand(command) {
let c = command.split(' ');
if (c[0].trim() === INTERNAL_COMMAND_SIGNATURE) return true;
}
}
function internalCommandHandler(command) {
return new Promise(function (resolve, reject) {
let c = command.split(' ');
if (c < 2) {
console.log(chalk.red('Invalid Internal Command ', command));
reject('Invalid Internal Command');
}
let internalCommand = c[1];
switch (internalCommand) {
case COMMAND_UNZIP:
return resolve(unzipArtifact(c[2], c[3]));
case COMMAND_ZIP:
return resolve(archiveFiles(c[2], c[3], c[4]));
case COMMAND_DELDIR:
return resolve(deleteDirectory(c[2]));
case COMMAND_GITCLON:
return resolve(cloneRepository(c[2], c[3]))
default:
return resolve();
}
})
}
runCommandsFromConfig(curretnConfig.commands);