@vkontakte/vk-miniapps-deploy 0.1.8-rc.0 → 0.1.9-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.eslintrc.js ADDED
@@ -0,0 +1,14 @@
1
+ module.exports = {
2
+ extends: ['plugin:@vkontakte/eslint-plugin/default', 'prettier'],
3
+ parserOptions: {
4
+ requireConfigFile: false,
5
+ },
6
+ rules: {
7
+ 'import/no-duplicates': 'off',
8
+ 'no-undef': 'off',
9
+ 'no-shadow': 'off',
10
+ 'no-var': 'off',
11
+ 'camelcase': 'off',
12
+ 'valid-jsdoc': 'off',
13
+ },
14
+ };
@@ -1,2 +1,2 @@
1
1
  * @VKCOM/vk-miniapp
2
- .github @VKCOM/vk-sec
2
+ .github/ @VKCOM/vk-sec
@@ -13,7 +13,7 @@ runs:
13
13
  registry-url: 'https://registry.npmjs.org'
14
14
 
15
15
  - name: Install dependencies
16
- run: yarn install --frozen-lockfile --ignore-scripts
16
+ run: yarn install --ignore-scripts
17
17
  shell: bash
18
18
 
19
19
  - name: Set Git credentials
@@ -0,0 +1,9 @@
1
+ name: 'Validation'
2
+ description: It runs linter and prettier checks
3
+
4
+ runs:
5
+ using: composite
6
+ steps:
7
+ - name: Run linters
8
+ run: yarn lint:ci
9
+ shell: bash
@@ -33,6 +33,9 @@ jobs:
33
33
  with:
34
34
  version: ${{ github.event.inputs.new_version }}
35
35
 
36
+ - name: Validation
37
+ uses: ./.github/actions/validation
38
+
36
39
  - name: Complete publish
37
40
  uses: ./.github/actions/complete-publish
38
41
  with:
@@ -47,6 +47,9 @@ jobs:
47
47
  version: ${{ github.event.inputs.custom_version || github.event.inputs.new_version }}
48
48
  npm_tag: ${{ github.event.inputs.npm_tag }}
49
49
 
50
+ - name: Validation
51
+ uses: ./.github/actions/validation
52
+
50
53
  - name: Complete publish
51
54
  uses: ./.github/actions/complete-publish
52
55
  with:
@@ -0,0 +1,19 @@
1
+ name: 'Pull Request'
2
+
3
+ on:
4
+ pull_request:
5
+
6
+ jobs:
7
+ lint:
8
+ runs-on: ubuntu-latest
9
+ steps:
10
+ - name: Checkout
11
+ uses: actions/checkout@v4
12
+ with:
13
+ token: ${{ secrets.DEVTOOLS_GITHUB_TOKEN }}
14
+
15
+ - name: Setting up the repository environment
16
+ uses: ./.github/actions/setup
17
+
18
+ - name: Validation
19
+ uses: ./.github/actions/validation
@@ -0,0 +1,30 @@
1
+ # OS
2
+ .DS_Store
3
+
4
+ # IDE
5
+ .idea/
6
+ .vscode/
7
+
8
+ # Dependencices
9
+ node_modules/
10
+
11
+ # Caches
12
+ .cache
13
+ tmp/
14
+
15
+ # Logs
16
+ test-results.json
17
+ lint-results.json
18
+ yarn-error.log
19
+
20
+ # Caches
21
+ .cache
22
+
23
+ # Coverage directories used by tools like istanbul
24
+ coverage
25
+ .nyc_output
26
+
27
+ # GitHub
28
+ .github/*
29
+
30
+ /bin
package/.prettierrc.js ADDED
@@ -0,0 +1 @@
1
+ module.exports = require('@vkontakte/prettier-config').createConfig();
package/index.js CHANGED
@@ -11,8 +11,8 @@ const vault = new Configstore(packageJson.name, {});
11
11
 
12
12
  var configJSON = require('require-module')('./vk-hosting-config.json');
13
13
  var cfg = configJSON || {};
14
- prompt.message = "vk-mini-apps-deploy".grey;
15
- prompt.delimiter = "=>".grey;
14
+ prompt.message = 'vk-mini-apps-deploy'.grey;
15
+ prompt.delimiter = '=>'.grey;
16
16
 
17
17
  const DEBUG_MODE = !!cfg.debug;
18
18
 
@@ -40,14 +40,14 @@ const ERROR_TEST_GROUP_UPDATE = 109;
40
40
  const TYPE_SUCCESS = 'success';
41
41
 
42
42
  const URL_NAMES = {
43
- DESKTOP_DEV : 'vk_app_desktop_dev_url',
43
+ DESKTOP_DEV: 'vk_app_desktop_dev_url',
44
44
  MOBILE_DEV: 'vk_app_dev_url',
45
45
  MOBILE_WEB_DEV: 'vk_mini_app_mvk_dev_url',
46
46
  WEB_LEGACY: 'iframe_url',
47
47
  WEB: 'iframe_secure_url',
48
48
  MOBILE: 'm_iframe_secure_url',
49
49
  MOBILE_WEB: 'vk_mini_app_mvk_url',
50
- }
50
+ };
51
51
 
52
52
  const PLATFORMS = {
53
53
  WEB: 'vk.com',
@@ -66,11 +66,11 @@ const URL_NAMES_MAP = {
66
66
  };
67
67
 
68
68
  function getTraceId() {
69
- return crypto.randomBytes(4).toString("hex");
69
+ return crypto.randomBytes(4).toString('hex');
70
70
  }
71
71
 
72
72
  /**
73
- * @param {nodeFetch.RequestInfo} url
73
+ * @param {nodeFetch.RequestInfo} url
74
74
  * @param {nodeFetch.RequestInit} options
75
75
  * @returns {Promise<nodeFetch.Response>}
76
76
  */
@@ -79,7 +79,7 @@ async function fetch(url, options) {
79
79
  return nodeFetch(url, options);
80
80
  }
81
81
 
82
- const traceId = chalk.hex(`#${(Math.random() * 0xFFFFFF << 0).toString(16)}`)(getTraceId());
82
+ const traceId = chalk.hex(`#${((Math.random() * 0xffffff) << 0).toString(16)}`)(getTraceId());
83
83
  const logLine = (line, type) => console.log(`[${traceId}][${type}]`, chalk.cyan(line));
84
84
  const logError = (error) => console.error(`[${traceId}][ERROR]`, chalk.red(error));
85
85
 
@@ -93,15 +93,20 @@ async function fetch(url, options) {
93
93
  logLine(`${response.status} ${response.statusText}`, 'RESP');
94
94
  logLine(body, 'RESP');
95
95
 
96
- return response
96
+ return response;
97
97
  } catch (e) {
98
98
  logError(e);
99
99
  throw e;
100
100
  }
101
101
  }
102
102
 
103
- async function auth() {
104
- const get_auth_code = await fetch(OAUTH_HOST + 'get_auth_code?scope=offline&client_id=' + DEPLOY_APP_ID);
103
+ /**
104
+ * @param {number} app_id
105
+ * @returns {Promise<{access_token: string, expires_in: string}>}
106
+ */
107
+ async function auth(app_id) {
108
+ const get_auth_code_url = `${OAUTH_HOST}get_auth_code?scope=offline&client_id=${DEPLOY_APP_ID}&mini_app_id=${app_id}`;
109
+ const get_auth_code = await fetch(get_auth_code_url);
105
110
  const get_auth_code_res = await get_auth_code.json();
106
111
 
107
112
  if (get_auth_code_res.error !== void 0) {
@@ -114,9 +119,9 @@ async function auth() {
114
119
  }
115
120
 
116
121
  if (get_auth_code_res.auth_code) {
117
- const {auth_code, device_id} = get_auth_code_res;
122
+ const { auth_code, device_id } = get_auth_code_res;
118
123
 
119
- const url = OAUTH_HOST + 'code_auth?stage=check&code=' + auth_code;
124
+ const code_auth_url = `${OAUTH_HOST}code_auth?stage=check&code=${auth_code}&revoke=1`;
120
125
 
121
126
  let handled = false;
122
127
  do {
@@ -124,14 +129,15 @@ async function auth() {
124
129
  type: 'confirm',
125
130
  name: 'result',
126
131
  initial: true,
127
- message: chalk.yellow('Please open this url in browser', url)
132
+ message: chalk.yellow('Please open this url in browser', code_auth_url),
128
133
  });
129
134
 
130
135
  if (!prompt_question.result) {
131
- return Promise.reject("empty response " + prompt_question.result);
136
+ return Promise.reject('empty response ' + prompt_question.result);
132
137
  }
133
138
 
134
- const code_auth_token = await fetch(OAUTH_HOST + 'code_auth_token?device_id=' + device_id + '&client_id=' + DEPLOY_APP_ID);
139
+ const code_auth_token_url = `${OAUTH_HOST}code_auth_token?device_id=${device_id}&client_id=${DEPLOY_APP_ID}&mini_app_id=${app_id}`;
140
+ const code_auth_token = await fetch(code_auth_token_url);
135
141
  const code_auth_token_json = await code_auth_token.json();
136
142
 
137
143
  if (code_auth_token.status !== CODE_SUCCESS) {
@@ -139,13 +145,14 @@ async function auth() {
139
145
  continue;
140
146
  }
141
147
 
142
- const {access_token} = code_auth_token_json;
148
+ const { access_token, expires_in } = code_auth_token_json;
143
149
  if (access_token || access_token === null) {
144
150
  handled = true;
145
151
  }
146
-
147
- return Promise.resolve(access_token);
148
-
152
+ return Promise.resolve({
153
+ access_token,
154
+ expires_in,
155
+ });
149
156
  } while (handled === false);
150
157
  }
151
158
  }
@@ -160,7 +167,11 @@ async function api(method, params) {
160
167
  return false;
161
168
  }
162
169
 
163
- const queryParams = Object.keys(params).map((k) => { return k + "=" + encodeURIComponent(params[k]) }).join('&');
170
+ const queryParams = Object.keys(params)
171
+ .map((k) => {
172
+ return k + '=' + encodeURIComponent(params[k]);
173
+ })
174
+ .join('&');
164
175
  try {
165
176
  const query = await fetch(API_HOST + method + '?' + queryParams);
166
177
  const res = await query.json();
@@ -178,12 +189,12 @@ async function api(method, params) {
178
189
 
179
190
  async function upload(uploadUrl, bundleFile) {
180
191
  const formData = new FormData();
181
- formData.append('file', fs.createReadStream(bundleFile), {contentType: 'application/zip'});
192
+ formData.append('file', fs.createReadStream(bundleFile), { contentType: 'application/zip' });
182
193
  try {
183
194
  const upload = await fetch(uploadUrl, {
184
195
  method: 'POST',
185
196
  headers: formData.getHeaders(),
186
- body: formData
197
+ body: formData,
187
198
  });
188
199
  return await upload.json();
189
200
  } catch (e) {
@@ -235,14 +246,14 @@ async function handleQueue(user_id, base_url, key, ts, version, handled, cfg) {
235
246
  const result = await prompt({
236
247
  type: 'text',
237
248
  name: 'code',
238
- message: chalk.yellow('Please, enter code from Administration: ')
249
+ message: chalk.yellow('Please, enter code from Administration: '),
239
250
  });
240
251
 
241
252
  if (result.code) {
242
253
  const r = await api('apps.confirmDeploy', {
243
254
  app_id: cfg.app_id,
244
255
  version: version,
245
- code: result.code
256
+ code: result.code,
246
257
  });
247
258
  if (r.error) {
248
259
  console.error('Invalid confirm code');
@@ -314,7 +325,7 @@ async function handleQueue(user_id, base_url, key, ts, version, handled, cfg) {
314
325
  if (event.code === CODE_UPDATE_TEST_GROUP_URL) {
315
326
  if (event.message && event.message.url) {
316
327
  console.info(chalk.green('URL changed for test group "' + cfg.test_group_name + '":'));
317
- console.info(event.message.url)
328
+ console.info(event.message.url);
318
329
  }
319
330
  }
320
331
  }
@@ -325,7 +336,7 @@ async function handleQueue(user_id, base_url, key, ts, version, handled, cfg) {
325
336
  }
326
337
 
327
338
  async function getQueue(version, cfg) {
328
- const r = await api('apps.subscribeToHostingQueue', {app_id: cfg.app_id, version: version});
339
+ const r = await api('apps.subscribeToHostingQueue', { app_id: cfg.app_id, version: version });
329
340
  if (!r.base_url || !r.key || !r.ts || !r.app_id) {
330
341
  throw new Error(JSON.stringify(r));
331
342
  }
@@ -334,7 +345,6 @@ async function getQueue(version, cfg) {
334
345
  }
335
346
 
336
347
  async function run(cfg) {
337
-
338
348
  if (!configJSON) {
339
349
  throw new Error('For deploy you need to create config file "vk-hosting-config.json"');
340
350
  }
@@ -344,51 +354,59 @@ async function run(cfg) {
344
354
  const defaultEnvironment = APPLICATION_ENV_DEV | APPLICATION_ENV_PRODUCTION;
345
355
  const environmentMapping = {
346
356
  dev: APPLICATION_ENV_DEV,
347
- production: APPLICATION_ENV_PRODUCTION
357
+ production: APPLICATION_ENV_PRODUCTION,
348
358
  };
349
359
 
350
360
  const environment = process.env.MINI_APPS_ENVIRONMENT
351
- ? (environmentMapping[process.env.MINI_APPS_ENVIRONMENT] || defaultEnvironment)
361
+ ? environmentMapping[process.env.MINI_APPS_ENVIRONMENT] || defaultEnvironment
352
362
  : defaultEnvironment;
353
363
 
364
+ if (process.env.MINI_APPS_APP_ID) {
365
+ const appId = parseInt(process.env.MINI_APPS_APP_ID, 10);
366
+ if (isNaN(appId)) {
367
+ throw new Error('env MINI_APPS_APP_ID is not valid number');
368
+ }
369
+ cfg.app_id = appId;
370
+ }
371
+
372
+ if (!cfg.app_id) {
373
+ throw new Error('Please provide "app_id" to vk-hosting-config.json or env MINI_APPS_APP_ID');
374
+ }
375
+
354
376
  if (process.env.MINI_APPS_ACCESS_TOKEN) {
355
377
  cfg.access_token = process.env.MINI_APPS_ACCESS_TOKEN;
356
378
  }
357
379
 
358
380
  if (!cfg.access_token && vault.get('access_token')) {
359
381
  cfg.access_token = vault.get('access_token');
382
+ cfg.expires_in = vault.get('expires_in') || 0;
360
383
  }
361
384
 
362
- if (!cfg.access_token) {
385
+ if (!cfg.access_token || (cfg.expires_in > 0 && cfg.expires_in * 1000 < Date.now())) {
363
386
  console.log('Try to retrieve access token...');
364
- const access_token = await auth();
387
+ const { access_token, expires_in } = await auth(cfg.app_id);
365
388
  cfg.access_token = access_token;
366
389
  vault.set('access_token', access_token);
390
+ vault.set('expires_in', expires_in);
367
391
  console.log(chalk.cyan('Token is saved in config store!'));
368
- console.log(chalk.cyan('\nFor your CI, you can use \n > $ env MINI_APPS_ACCESS_TOKEN=' + access_token + ' yarn deploy'));
369
- }
370
-
371
- if (process.env.MINI_APPS_APP_ID) {
372
- const appId = parseInt(process.env.MINI_APPS_APP_ID, 10);
373
- if (isNaN(appId)) {
374
- throw new Error('env MINI_APPS_APP_ID is not valid number');
375
- }
376
- cfg.app_id = appId;
377
- }
378
-
379
- if (!cfg.app_id) {
380
- throw new Error('Please provide "app_id" to vk-hosting-config.json or env MINI_APPS_APP_ID');
392
+ console.log(
393
+ chalk.cyan(
394
+ '\nFor your CI, you can use \n > $ env MINI_APPS_ACCESS_TOKEN=' +
395
+ access_token +
396
+ ' yarn deploy',
397
+ ),
398
+ );
381
399
  }
382
400
 
383
401
  const params = {
384
402
  app_id: cfg.app_id,
385
403
  environment: environment,
386
- update_prod: + cfg.update_prod,
387
- update_dev: + cfg.update_dev
404
+ update_prod: +cfg.update_prod,
405
+ update_dev: +cfg.update_dev,
388
406
  };
389
407
 
390
408
  if ('test_group_name' in cfg) {
391
- params.test_group_name = cfg.test_group_name
409
+ params.test_group_name = cfg.test_group_name;
392
410
  }
393
411
 
394
412
  const endpointPlatformKeys = Object.keys(cfg.endpoints);
@@ -415,10 +433,9 @@ async function run(cfg) {
415
433
 
416
434
  if (!cfg.bundleFile) {
417
435
  if (await fs.pathExists(bundleFile)) {
418
- fs.removeSync(bundleFile)
436
+ fs.removeSync(bundleFile);
419
437
  }
420
438
 
421
-
422
439
  await zip('./' + staticPath, bundleFile);
423
440
  }
424
441
 
@@ -432,11 +449,10 @@ async function run(cfg) {
432
449
  console.log('Uploaded version ' + r.version + '!');
433
450
  return getQueue(r.version, cfg);
434
451
  } else {
435
- console.error('Upload error:', r)
452
+ console.error('Upload error:', r);
436
453
  process.exit(1);
437
454
  }
438
455
  });
439
-
440
456
  } catch (e) {
441
457
  console.error(chalk.red(e));
442
458
  process.exit(1);
@@ -444,5 +460,5 @@ async function run(cfg) {
444
460
  }
445
461
 
446
462
  module.exports = {
447
- run: run
463
+ run: run,
448
464
  };
package/package.json CHANGED
@@ -1,11 +1,15 @@
1
1
  {
2
2
  "name": "@vkontakte/vk-miniapps-deploy",
3
- "version": "0.1.8-rc.0",
3
+ "version": "0.1.9-rc.0",
4
4
  "description": "Deploy to VK Mini Apps hosting with one simple command",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
7
  "test": "echo \"Error: no test specified\" && exit 1",
8
- "deploy": "./bin/vk-miniapps-deploy"
8
+ "deploy": "./bin/vk-miniapps-deploy",
9
+ "lint:eslint": "eslint ./index.js",
10
+ "lint:prettier": "prettier --check ./index.js",
11
+ "lint:fix": "eslint ./index.js --fix",
12
+ "lint:ci": "yarn lint:eslint && yarn lint:prettier"
9
13
  },
10
14
  "license": "MIT",
11
15
  "engines": {
@@ -42,5 +46,12 @@
42
46
  "prompts": "^2.1.0",
43
47
  "require-module": "^0.1.0",
44
48
  "zip-a-folder": "0.0.12"
49
+ },
50
+ "devDependencies": {
51
+ "@vkontakte/eslint-plugin": "^2.0.0",
52
+ "@vkontakte/prettier-config": "^0.1.0",
53
+ "eslint": "^8.0.0",
54
+ "eslint-config-prettier": "^9.1.0",
55
+ "prettier": "^2.7.1"
45
56
  }
46
57
  }