forked from Platform-OS/pos-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
marketplace-kit-push.js
executable file
·80 lines (67 loc) · 2.51 KB
/
marketplace-kit-push.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
#!/usr/bin/env node
const program = require('commander'),
fs = require('fs'),
{ performance } = require('perf_hooks'),
ora = require('ora'),
validate = require('./lib/validators'),
Gateway = require('./lib/proxy'),
ServerError = require('./lib/ServerError'),
logger = require('./lib/logger'),
version = require('./package.json').version;
const checkParams = params => {
validate.existence({ argumentValue: params.token, argumentName: 'token', fail: program.help.bind(program) });
validate.existence({ argumentValue: params.url, argumentName: 'url', fail: program.help.bind(program) });
if (params.url.slice(-1) != '/') {
params.url = params.url + '/';
}
};
program
.version(version)
.option('--email <email>', 'developer email', process.env.MARKETPLACE_EMAIL)
.option('--token <token>', 'authentication token', process.env.MARKETPLACE_TOKEN)
.option('--url <url>', 'marketplace url', process.env.MARKETPLACE_URL);
program.parse(process.argv);
checkParams(program);
const formatMMSS = s => (s - (s %= 60)) / 60 + (9 < s ? ':' : ':0') + s;
const duration = (t0, t1) => {
const duration = Math.round((t1 - t0) / 1000);
return formatMMSS(duration);
};
const t0 = performance.now();
const spinner = ora({ text: `Deploying to: ${program.url}`, stream: process.stdout, spinner: 'bouncingBar' }).start();
const gateway = new Gateway(program);
const formData = {
'marketplace_builder[force_mode]': process.env.FORCE || 'false',
'marketplace_builder[partial_deploy]': process.env.PARTIAL_DEPLOY || 'false',
'marketplace_builder[zip_file]': fs.createReadStream('./tmp/marketplace-release.zip')
};
const getDeploymentStatus = id => {
return new Promise((resolve, reject) => {
(getStatus = () => {
gateway.getStatus(id).then(response => {
if (response.status === 'ready_for_import') {
setTimeout(getStatus, 1500);
} else if (response.status === 'error') {
const t1 = performance.now();
ServerError.deploy(JSON.parse(response.error));
spinner.fail(`Deploy failed after ${duration(t0, t1)}`);
reject();
} else {
resolve(response);
}
});
})();
});
};
gateway
.push(formData)
.then(response => {
getDeploymentStatus(response.id).then(() => {
const t1 = performance.now();
spinner.stopAndPersist().succeed(`Deploy succeeded after ${duration(t0, t1)}`);
}).catch(() => process.exit(1));
})
.catch(() => {
spinner.stopAndPersist().fail('Deploy failed');
process.exit(1);
});