forked from VKCOM/vk-miniapps-deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.js
157 lines (129 loc) · 4.43 KB
/
deploy.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
const fetch = require('node-fetch').default;
const stringify = require('querystring').stringify;
const chalk = require('chalk');
const assert = require('./assert');
const confirm = require('./confirm');
const URL_NAMES = {
DESKTOP_DEV: 'vk_app_desktop_dev_url',
MOBILE_DEV: 'vk_app_dev_url',
MOBILE_WEB_DEV: 'vk_mini_app_mvk_dev_url',
WEB_LEGACY: 'iframe_url',
WEB: 'iframe_secure_url',
MOBILE: 'm_iframe_secure_url',
MOBILE_WEB: 'vk_mini_app_mvk_url'
};
const PLATFORMS = {
WEB: 'vk.com',
MOBILE: 'iOS & Android',
MOBILE_WEB: 'm.vk.com'
};
const URL_NAMES_MAP = {
[URL_NAMES.DESKTOP_DEV]: PLATFORMS.WEB,
[URL_NAMES.MOBILE_DEV]: PLATFORMS.MOBILE,
[URL_NAMES.MOBILE_WEB_DEV]: PLATFORMS.MOBILE_WEB,
[URL_NAMES.WEB_LEGACY]: PLATFORMS.WEB,
[URL_NAMES.WEB]: PLATFORMS.WEB,
[URL_NAMES.MOBILE]: PLATFORMS.MOBILE,
[URL_NAMES.MOBILE_WEB]: PLATFORMS.MOBILE_WEB
};
const PAD_LENGTH = 16;
const ENVIRONMENT_DEV = 1;
const ENVIRONMENT_PROD = 2;
const CODE_SUCCESS = 200;
const CODE_DEPLOY = 201;
const CODE_SKIP = 202;
const CODE_PUSH_SENT_VIA_PUSH = 203;
const CODE_PUSH_APPROVED = 204;
const CODE_CONFIRM_SENT_VIA_MESSAGE = 205;
const CI_URLS = process.env.CI_URLS === 'true' || process.env.CI_URLS === '1';
const pull = async (config, version, server, state) => {
if (state.dev && state.prod) {
return;
}
const pullURL = server.base_url + '?act=a_check&key=' + server.key + '&ts=' + server.ts + '&id=' + server.app_id + '&wait=5';
const payload = await (await fetch(pullURL)).json();
if (payload.events) {
for (const pulled of payload.events) {
const event = pulled.data;
if (event.type === 'error') {
const message = event.message || '';
throw new Error('Deploy failed, error code: #' + event.code + ' ' + message);
}
if (event.type === 'success') {
switch (event.code) {
case CODE_SUCCESS:
console.info(chalk.green('Deploy success...'));
continue;
case CODE_PUSH_SENT_VIA_PUSH:
console.info(chalk.green('Please, confirm deploy on your phone.'));
continue;
case CODE_PUSH_APPROVED:
console.info(chalk.green('Deploy confirmed successfully.'));
continue;
case CODE_CONFIRM_SENT_VIA_MESSAGE:
await confirm(config, version);
continue;
case CODE_SKIP:
switch (event.message.environment) {
case ENVIRONMENT_DEV:
state.dev = true;
continue;
case ENVIRONMENT_PROD:
state.prod = true;
continue;
default:
continue;
}
case CODE_DEPLOY: {
const urls = event.message && event.message.urls;
if (urls) {
const isProduction = event.message.is_production;
if (isProduction && !state.prod) {
state.prod = true;
console.info(chalk.green('URLs changed for production:'));
}
if (!isProduction && !state.dev) {
state.dev = true;
console.info(chalk.green('URLs changed for dev:'));
}
for (const name in urls) {
if (name === URL_NAMES.WEB_LEGACY) {
continue;
}
const url = urls[name];
let prefix = '';
if (!CI_URLS) {
prefix = name in URL_NAMES_MAP ? (URL_NAMES_MAP[name] + ':').padEnd(PAD_LENGTH, ' ') : prefix;
} else {
// Backward compatible
prefix = name + ':\t';
}
console.log(prefix + url);
}
}
}
}
}
}
}
server.ts = payload.ts;
return pull(config, version, server, state);
};
module.exports = async (config, version) => {
const params = {
app_id: config.app_id,
version: version,
v: config.api_version,
access_token: config.access_token
};
const payload = await (await fetch(config.api_host + 'apps.subscribeToHostingQueue?' + stringify(params))).json();
assert(payload);
const server = payload.response;
if (!server || !server.base_url || !server.key || !server.ts || !server.app_id) {
throw new Error('Unfortunately, the server is temporarily unavailable. Please try again later.');
}
return pull(config, version, server, {
dev: false,
prod: false
});
};