-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
copyVersion.js
64 lines (56 loc) · 2.04 KB
/
copyVersion.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
const colors = require('colors');
const request = require('./request');
const getJob = require('./getJob');
const copyVersion = (opt, cb) => {
request(opt.apiPath + '/copy/' + opt.projectId + '/version/' + opt.fromVersion + '/' + opt.toVersion, {
method: 'post',
headers: {
'Authorization': opt.apiKey
}
}, (err, res, obj) => {
if (err || (obj && (obj.errorMessage || obj.message))) {
if (!cb) console.log(colors.red(`copy failed from ${opt.fromVersion} to ${opt.toVersion}...`));
if (err) {
if (!cb) { console.error(colors.red(err.message)); process.exit(1); }
if (cb) cb(err);
return;
}
if (obj && (obj.errorMessage || obj.message)) {
if (!cb) { console.error(colors.red((obj.errorMessage || obj.message))); process.exit(1); }
if (cb) cb(new Error((obj.errorMessage || obj.message)));
return;
}
}
if (res.status >= 300) {
if (!cb) { console.error(colors.red(res.statusText + ' (' + res.status + ')')); process.exit(1); }
if (cb) cb(new Error(res.statusText + ' (' + res.status + ')'));
return;
}
if (!obj || !obj.jobId) {
if (!cb) { console.error(colors.red('No jobId! Something went wrong!')); process.exit(1); }
if (cb) cb(new Error('No jobId! Something went wrong!'));
return;
}
(function waitForJob() {
getJob(opt, obj.jobId, (err, job) => {
if (err) {
if (!cb) { console.error(colors.red(err.message)); process.exit(1); }
if (cb) cb(err);
return;
}
if (job && !job.timeouted) {
setTimeout(waitForJob, 2000);
return;
}
if (job && job.timeouted) {
if (!cb) { console.error(colors.red('Job timeouted!')); process.exit(1); }
if (cb) cb(new Error('Job timeouted!'));
return;
}
if (!cb) console.log(colors.green(`copy from ${opt.fromVersion} to ${opt.toVersion} succesfully requested`));
if (cb) cb(null);
});
})();
});
};
module.exports = copyVersion;