@redocly/cli 1.18.0 → 1.19.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.
Files changed (91) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/lib/__mocks__/@redocly/openapi-core.d.ts +2 -2
  3. package/lib/__mocks__/@redocly/openapi-core.js +1 -0
  4. package/lib/__mocks__/fs.d.ts +0 -1
  5. package/lib/__mocks__/perf_hooks.d.ts +0 -1
  6. package/lib/__mocks__/redoc.d.ts +0 -1
  7. package/lib/__tests__/commands/build-docs.test.js +21 -23
  8. package/lib/__tests__/commands/bundle.test.js +21 -30
  9. package/lib/__tests__/commands/join.test.js +101 -70
  10. package/lib/__tests__/commands/lint.test.js +54 -54
  11. package/lib/__tests__/commands/push-region.test.js +24 -25
  12. package/lib/__tests__/commands/push.test.js +269 -170
  13. package/lib/__tests__/fetch-with-timeout.test.js +3 -12
  14. package/lib/__tests__/fixtures/config.d.ts +0 -1
  15. package/lib/__tests__/utils.test.js +32 -37
  16. package/lib/__tests__/wrapper.test.js +31 -20
  17. package/lib/cms/api/__tests__/api.client.test.js +29 -38
  18. package/lib/cms/api/api-client.d.ts +0 -2
  19. package/lib/cms/api/api-client.js +106 -127
  20. package/lib/cms/api/api-keys.js +1 -2
  21. package/lib/cms/api/domains.js +1 -2
  22. package/lib/cms/commands/__tests__/push-status.test.js +251 -162
  23. package/lib/cms/commands/__tests__/push.test.js +120 -102
  24. package/lib/cms/commands/__tests__/utils.test.js +12 -21
  25. package/lib/cms/commands/push-status.d.ts +3 -2
  26. package/lib/cms/commands/push-status.js +94 -106
  27. package/lib/cms/commands/push.d.ts +3 -2
  28. package/lib/cms/commands/push.js +66 -75
  29. package/lib/cms/commands/utils.js +20 -34
  30. package/lib/commands/build-docs/index.d.ts +2 -2
  31. package/lib/commands/build-docs/index.js +8 -17
  32. package/lib/commands/build-docs/utils.js +26 -38
  33. package/lib/commands/bundle.d.ts +2 -2
  34. package/lib/commands/bundle.js +70 -94
  35. package/lib/commands/join.d.ts +2 -2
  36. package/lib/commands/join.js +377 -390
  37. package/lib/commands/lint.d.ts +2 -2
  38. package/lib/commands/lint.js +70 -78
  39. package/lib/commands/login.d.ts +3 -2
  40. package/lib/commands/login.js +9 -21
  41. package/lib/commands/preview-docs/index.d.ts +2 -2
  42. package/lib/commands/preview-docs/index.js +92 -106
  43. package/lib/commands/preview-docs/preview-server/preview-server.js +64 -76
  44. package/lib/commands/preview-docs/preview-server/server.d.ts +0 -3
  45. package/lib/commands/preview-docs/preview-server/server.js +6 -6
  46. package/lib/commands/preview-project/index.d.ts +2 -1
  47. package/lib/commands/preview-project/index.js +5 -14
  48. package/lib/commands/push.d.ts +8 -11
  49. package/lib/commands/push.js +177 -195
  50. package/lib/commands/split/__tests__/index.test.js +31 -25
  51. package/lib/commands/split/index.d.ts +2 -1
  52. package/lib/commands/split/index.js +22 -35
  53. package/lib/commands/stats.d.ts +2 -2
  54. package/lib/commands/stats.js +34 -45
  55. package/lib/index.js +32 -46
  56. package/lib/types.d.ts +2 -2
  57. package/lib/utils/__mocks__/miscellaneous.d.ts +0 -1
  58. package/lib/utils/fetch-with-timeout.js +7 -12
  59. package/lib/utils/getCommandNameFromArgs.js +2 -4
  60. package/lib/utils/js-utils.js +6 -7
  61. package/lib/utils/miscellaneous.d.ts +9 -5
  62. package/lib/utils/miscellaneous.js +150 -160
  63. package/lib/utils/update-version-notifier.js +4 -13
  64. package/lib/wrapper.d.ts +9 -2
  65. package/lib/wrapper.js +27 -16
  66. package/package.json +5 -3
  67. package/src/__mocks__/@redocly/openapi-core.ts +1 -0
  68. package/src/__tests__/commands/build-docs.test.ts +5 -4
  69. package/src/__tests__/commands/join.test.ts +51 -51
  70. package/src/__tests__/commands/push-region.test.ts +10 -8
  71. package/src/__tests__/commands/push.test.ts +127 -102
  72. package/src/__tests__/utils.test.ts +1 -0
  73. package/src/__tests__/wrapper.test.ts +24 -2
  74. package/src/cms/commands/__tests__/push-status.test.ts +70 -56
  75. package/src/cms/commands/__tests__/push.test.ts +30 -24
  76. package/src/cms/commands/push-status.ts +8 -7
  77. package/src/cms/commands/push.ts +19 -13
  78. package/src/commands/build-docs/index.ts +10 -5
  79. package/src/commands/bundle.ts +14 -6
  80. package/src/commands/join.ts +10 -6
  81. package/src/commands/lint.ts +20 -9
  82. package/src/commands/login.ts +4 -2
  83. package/src/commands/preview-docs/index.ts +6 -1
  84. package/src/commands/preview-project/index.ts +5 -4
  85. package/src/commands/push.ts +14 -16
  86. package/src/commands/split/__tests__/index.test.ts +17 -6
  87. package/src/commands/split/index.ts +6 -4
  88. package/src/commands/stats.ts +4 -2
  89. package/src/utils/miscellaneous.ts +55 -26
  90. package/src/wrapper.ts +37 -11
  91. package/tsconfig.tsbuildinfo +1 -1
@@ -1,14 +1,6 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = startPreviewServer;
12
4
  const handlebars_1 = require("handlebars");
13
5
  const colorette = require("colorette");
14
6
  const get_port_please_1 = require("get-port-please");
@@ -47,79 +39,75 @@ function getPageHTML(htmlTemplate, redocOptions = {}, useRedocPro, wsPort, host)
47
39
  </script>`,
48
40
  });
49
41
  }
50
- function startPreviewServer(port, host, { getBundle, getOptions, useRedocPro, }) {
51
- return __awaiter(this, void 0, void 0, function* () {
52
- const defaultTemplate = path.join(__dirname, 'default.hbs');
53
- const handler = (request, response) => __awaiter(this, void 0, void 0, function* () {
54
- var _a;
55
- console.time(colorette.dim(`GET ${request.url}`));
56
- const { htmlTemplate } = getOptions() || {};
57
- if (((_a = request.url) === null || _a === void 0 ? void 0 : _a.endsWith('/')) || path.extname(request.url) === '') {
58
- (0, server_1.respondWithGzip)(getPageHTML(htmlTemplate || defaultTemplate, getOptions(), useRedocPro, wsPort, host), request, response, {
59
- 'Content-Type': 'text/html',
42
+ async function startPreviewServer(port, host, { getBundle, getOptions, useRedocPro, }) {
43
+ const defaultTemplate = path.join(__dirname, 'default.hbs');
44
+ const handler = async (request, response) => {
45
+ console.time(colorette.dim(`GET ${request.url}`));
46
+ const { htmlTemplate } = getOptions() || {};
47
+ if (request.url?.endsWith('/') || path.extname(request.url) === '') {
48
+ (0, server_1.respondWithGzip)(getPageHTML(htmlTemplate || defaultTemplate, getOptions(), useRedocPro, wsPort, host), request, response, {
49
+ 'Content-Type': 'text/html',
50
+ });
51
+ }
52
+ else if (request.url === '/openapi.json') {
53
+ const bundle = await getBundle();
54
+ if (bundle === undefined) {
55
+ (0, server_1.respondWithGzip)(JSON.stringify({
56
+ openapi: '3.0.0',
57
+ info: {
58
+ description: '<code> Failed to generate bundle: check out console output for more details </code>',
59
+ },
60
+ paths: {},
61
+ }), request, response, {
62
+ 'Content-Type': 'application/json',
60
63
  });
61
64
  }
62
- else if (request.url === '/openapi.json') {
63
- const bundle = yield getBundle();
64
- if (bundle === undefined) {
65
- (0, server_1.respondWithGzip)(JSON.stringify({
66
- openapi: '3.0.0',
67
- info: {
68
- description: '<code> Failed to generate bundle: check out console output for more details </code>',
69
- },
70
- paths: {},
71
- }), request, response, {
72
- 'Content-Type': 'application/json',
73
- });
74
- }
75
- else {
76
- (0, server_1.respondWithGzip)(JSON.stringify(bundle), request, response, {
77
- 'Content-Type': 'application/json',
78
- });
79
- }
80
- }
81
65
  else {
82
- let filePath =
83
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
84
- // @ts-ignore
85
- {
86
- '/hot.js': path.join(__dirname, 'hot.js'),
87
- '/oauth2-redirect.html': path.join(__dirname, 'oauth2-redirect.html'),
88
- '/simplewebsocket.min.js': require.resolve('simple-websocket/simplewebsocket.min.js'),
89
- }[request.url || ''];
90
- if (!filePath) {
91
- const basePath = htmlTemplate ? path.dirname(htmlTemplate) : process.cwd();
92
- filePath = path.resolve(basePath, `.${request.url}`);
93
- if (!(0, miscellaneous_1.isSubdir)(basePath, filePath)) {
94
- (0, server_1.respondWithGzip)('404 Not Found', request, response, { 'Content-Type': 'text/html' }, 404);
95
- console.timeEnd(colorette.dim(`GET ${request.url}`));
96
- return;
97
- }
66
+ (0, server_1.respondWithGzip)(JSON.stringify(bundle), request, response, {
67
+ 'Content-Type': 'application/json',
68
+ });
69
+ }
70
+ }
71
+ else {
72
+ let filePath =
73
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
74
+ // @ts-ignore
75
+ {
76
+ '/hot.js': path.join(__dirname, 'hot.js'),
77
+ '/oauth2-redirect.html': path.join(__dirname, 'oauth2-redirect.html'),
78
+ '/simplewebsocket.min.js': require.resolve('simple-websocket/simplewebsocket.min.js'),
79
+ }[request.url || ''];
80
+ if (!filePath) {
81
+ const basePath = htmlTemplate ? path.dirname(htmlTemplate) : process.cwd();
82
+ filePath = path.resolve(basePath, `.${request.url}`);
83
+ if (!(0, miscellaneous_1.isSubdir)(basePath, filePath)) {
84
+ (0, server_1.respondWithGzip)('404 Not Found', request, response, { 'Content-Type': 'text/html' }, 404);
85
+ console.timeEnd(colorette.dim(`GET ${request.url}`));
86
+ return;
98
87
  }
99
- const extname = String(path.extname(filePath)).toLowerCase();
100
- const contentType = server_1.mimeTypes[extname] || 'application/octet-stream';
101
- try {
102
- (0, server_1.respondWithGzip)(yield fs_1.promises.readFile(filePath), request, response, {
103
- 'Content-Type': contentType,
104
- });
88
+ }
89
+ const extname = String(path.extname(filePath)).toLowerCase();
90
+ const contentType = server_1.mimeTypes[extname] || 'application/octet-stream';
91
+ try {
92
+ (0, server_1.respondWithGzip)(await fs_1.promises.readFile(filePath), request, response, {
93
+ 'Content-Type': contentType,
94
+ });
95
+ }
96
+ catch (e) {
97
+ if (e.code === 'ENOENT') {
98
+ (0, server_1.respondWithGzip)('404 Not Found', request, response, { 'Content-Type': 'text/html' }, 404);
105
99
  }
106
- catch (e) {
107
- if (e.code === 'ENOENT') {
108
- (0, server_1.respondWithGzip)('404 Not Found', request, response, { 'Content-Type': 'text/html' }, 404);
109
- }
110
- else {
111
- (0, server_1.respondWithGzip)(`Something went wrong: ${e.code || e.message}...\n`, request, response, {}, 500);
112
- }
100
+ else {
101
+ (0, server_1.respondWithGzip)(`Something went wrong: ${e.code || e.message}...\n`, request, response, {}, 500);
113
102
  }
114
103
  }
115
- console.timeEnd(colorette.dim(`GET ${request.url}`));
116
- });
117
- const wsPort = yield (0, get_port_please_1.getPort)({ port: 32201, portRange: [32201, 32301], host });
118
- const server = (0, server_1.startHttpServer)(port, host, handler);
119
- server.on('listening', () => {
120
- process.stdout.write(`\n 🔎 Preview server running at ${colorette.blue(`http://${host}:${port}\n`)}`);
121
- });
122
- return (0, server_1.startWsServer)(wsPort, host);
104
+ }
105
+ console.timeEnd(colorette.dim(`GET ${request.url}`));
106
+ };
107
+ const wsPort = await (0, get_port_please_1.getPort)({ port: 32201, portRange: [32201, 32301], host });
108
+ const server = (0, server_1.startHttpServer)(port, host, handler);
109
+ server.on('listening', () => {
110
+ process.stdout.write(`\n 🔎 Preview server running at ${colorette.blue(`http://${host}:${port}\n`)}`);
123
111
  });
112
+ return (0, server_1.startWsServer)(wsPort, host);
124
113
  }
125
- exports.default = startPreviewServer;
@@ -1,6 +1,3 @@
1
- /// <reference types="node" />
2
- /// <reference types="node" />
3
- /// <reference types="node" />
4
1
  import * as http from 'http';
5
2
  import { ReadStream } from 'fs';
6
3
  export declare const mimeTypes: {
@@ -1,6 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.startWsServer = exports.startHttpServer = exports.respondWithGzip = exports.mimeTypes = void 0;
3
+ exports.mimeTypes = void 0;
4
+ exports.respondWithGzip = respondWithGzip;
5
+ exports.startHttpServer = startHttpServer;
6
+ exports.startWsServer = startWsServer;
4
7
  const http = require("http");
5
8
  const zlib = require("zlib");
6
9
  const SocketServer = require('simple-websocket/server.js');
@@ -26,11 +29,11 @@ function respondWithGzip(contents, request, response, headers = {}, code = 200)
26
29
  let compressedStream;
27
30
  const acceptEncoding = request.headers['accept-encoding'] || '';
28
31
  if (acceptEncoding.match(/\bdeflate\b/)) {
29
- response.writeHead(code, Object.assign(Object.assign({}, headers), { 'content-encoding': 'deflate' }));
32
+ response.writeHead(code, { ...headers, 'content-encoding': 'deflate' });
30
33
  compressedStream = zlib.createDeflate();
31
34
  }
32
35
  else if (acceptEncoding.match(/\bgzip\b/)) {
33
- response.writeHead(code, Object.assign(Object.assign({}, headers), { 'content-encoding': 'gzip' }));
36
+ response.writeHead(code, { ...headers, 'content-encoding': 'gzip' });
34
37
  compressedStream = zlib.createGzip();
35
38
  }
36
39
  else {
@@ -53,11 +56,9 @@ function respondWithGzip(contents, request, response, headers = {}, code = 200)
53
56
  contents.pipe(compressedStream).pipe(response);
54
57
  }
55
58
  }
56
- exports.respondWithGzip = respondWithGzip;
57
59
  function startHttpServer(port, host, handler) {
58
60
  return http.createServer(handler).listen(port, host);
59
61
  }
60
- exports.startHttpServer = startHttpServer;
61
62
  function startWsServer(port, host) {
62
63
  const socketServer = new SocketServer({ port, host, clientTracking: true });
63
64
  socketServer.on('connection', (socket) => {
@@ -82,4 +83,3 @@ function startWsServer(port, host) {
82
83
  };
83
84
  return socketServer;
84
85
  }
85
- exports.startWsServer = startWsServer;
@@ -1,2 +1,3 @@
1
1
  import type { PreviewProjectOptions } from './types';
2
- export declare const previewProject: (args: PreviewProjectOptions) => Promise<void>;
2
+ import type { CommandArgs } from '../../wrapper';
3
+ export declare const previewProject: ({ argv }: CommandArgs<PreviewProjectOptions>) => Promise<void>;
@@ -1,23 +1,14 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.previewProject = void 0;
13
4
  const path = require("path");
14
5
  const fs_1 = require("fs");
15
6
  const child_process_1 = require("child_process");
16
7
  const constants_1 = require("./constants");
17
- const previewProject = (args) => __awaiter(void 0, void 0, void 0, function* () {
18
- const { plan, port } = args;
19
- const projectDir = args['source-dir'];
20
- const product = args.product || tryGetProductFromPackageJson(projectDir);
8
+ const previewProject = async ({ argv }) => {
9
+ const { plan, port } = argv;
10
+ const projectDir = argv['source-dir'];
11
+ const product = argv.product || tryGetProductFromPackageJson(projectDir);
21
12
  if (!isValidProduct(product)) {
22
13
  process.stderr.write(`Invalid product ${product}`);
23
14
  throw new Error(`Project preview launch failed`);
@@ -30,7 +21,7 @@ const previewProject = (args) => __awaiter(void 0, void 0, void 0, function* ()
30
21
  stdio: 'inherit',
31
22
  cwd: projectDir,
32
23
  });
33
- });
24
+ };
34
25
  exports.previewProject = previewProject;
35
26
  const isValidProduct = (product) => {
36
27
  if (!product) {
@@ -1,5 +1,6 @@
1
1
  import { Config, Region } from '@redocly/openapi-core';
2
2
  import { handlePush as handleCMSPush } from '../cms/commands/push';
3
+ import type { CommandArgs } from '../wrapper';
3
4
  export declare const DESTINATION_REGEX: RegExp;
4
5
  export type PushOptions = {
5
6
  api?: string;
@@ -18,14 +19,10 @@ export type PushOptions = {
18
19
  export declare function commonPushHandler({ project, 'mount-path': mountPath, }: {
19
20
  project?: string;
20
21
  'mount-path'?: string;
21
- }): typeof handleCMSPush | (({ apis, branch, "batch-id": batchId, "job-id": jobId, ...rest }: Omit<PushOptions, "destination" | "branchName"> & {
22
- apis?: string[] | undefined;
23
- branch?: string | undefined;
24
- destination?: string | undefined;
25
- } & {
26
- 'batch-id'?: string | undefined;
27
- }, config: Config) => Promise<void>);
28
- export declare function handlePush(argv: PushOptions, config: Config): Promise<void>;
22
+ }): typeof handleCMSPush | (({ argv: { apis, branch, "batch-id": batchId, "job-id": jobId, ...rest }, config, version, }: CommandArgs<BarePushArgs & {
23
+ "batch-id"?: string;
24
+ }>) => Promise<void>);
25
+ export declare function handlePush({ argv, config }: CommandArgs<PushOptions>): Promise<void>;
29
26
  export declare function getDestinationProps(destination: string | undefined, organization: string | undefined): {
30
27
  organizationId: string | undefined;
31
28
  name: string | undefined;
@@ -36,9 +33,9 @@ type BarePushArgs = Omit<PushOptions, 'destination' | 'branchName'> & {
36
33
  branch?: string;
37
34
  destination?: string;
38
35
  };
39
- export declare const transformPush: (callback: typeof handlePush) => ({ apis, branch, "batch-id": batchId, "job-id": jobId, ...rest }: BarePushArgs & {
40
- 'batch-id'?: string;
41
- }, config: Config) => Promise<void>;
36
+ export declare const transformPush: (callback: typeof handlePush) => ({ argv: { apis, branch, "batch-id": batchId, "job-id": jobId, ...rest }, config, version, }: CommandArgs<BarePushArgs & {
37
+ "batch-id"?: string;
38
+ }>) => Promise<void>;
42
39
  export declare function getApiRoot({ name, version, config: { apis }, }: {
43
40
  name: string;
44
41
  version: string;