@redocly/cli 1.34.4 → 2.0.0-next.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 (321) hide show
  1. package/README.md +34 -3
  2. package/bin/cli.js +1 -1
  3. package/lib/auth/device-flow.d.ts +1 -0
  4. package/lib/auth/device-flow.d.ts.map +1 -0
  5. package/lib/auth/device-flow.js +15 -17
  6. package/lib/auth/device-flow.js.map +1 -0
  7. package/lib/auth/oauth-client.d.ts +1 -0
  8. package/lib/auth/oauth-client.d.ts.map +1 -0
  9. package/lib/auth/oauth-client.js +21 -23
  10. package/lib/auth/oauth-client.js.map +1 -0
  11. package/lib/commands/auth.d.ts +3 -5
  12. package/lib/commands/auth.d.ts.map +1 -0
  13. package/lib/commands/auth.js +19 -44
  14. package/lib/commands/auth.js.map +1 -0
  15. package/lib/commands/build-docs/index.d.ts +4 -3
  16. package/lib/commands/build-docs/index.d.ts.map +1 -0
  17. package/lib/commands/build-docs/index.js +23 -24
  18. package/lib/commands/build-docs/index.js.map +1 -0
  19. package/lib/commands/build-docs/types.d.ts +2 -1
  20. package/lib/commands/build-docs/types.d.ts.map +1 -0
  21. package/lib/commands/build-docs/types.js +2 -2
  22. package/lib/commands/build-docs/types.js.map +1 -0
  23. package/lib/commands/build-docs/utils.d.ts +3 -2
  24. package/lib/commands/build-docs/utils.d.ts.map +1 -0
  25. package/lib/commands/build-docs/utils.js +35 -37
  26. package/lib/commands/build-docs/utils.js.map +1 -0
  27. package/lib/commands/bundle.d.ts +6 -3
  28. package/lib/commands/bundle.d.ts.map +1 -0
  29. package/lib/commands/bundle.js +34 -38
  30. package/lib/commands/bundle.js.map +1 -0
  31. package/lib/commands/eject.d.ts +3 -2
  32. package/lib/commands/eject.d.ts.map +1 -0
  33. package/lib/commands/eject.js +11 -13
  34. package/lib/commands/eject.js.map +1 -0
  35. package/lib/commands/join.d.ts +4 -3
  36. package/lib/commands/join.d.ts.map +1 -0
  37. package/lib/commands/join.js +79 -81
  38. package/lib/commands/join.js.map +1 -0
  39. package/lib/commands/lint.d.ts +8 -6
  40. package/lib/commands/lint.d.ts.map +1 -0
  41. package/lib/commands/lint.js +53 -62
  42. package/lib/commands/lint.js.map +1 -0
  43. package/lib/commands/preview-project/constants.d.ts +2 -1
  44. package/lib/commands/preview-project/constants.d.ts.map +1 -0
  45. package/lib/commands/preview-project/constants.js +4 -6
  46. package/lib/commands/preview-project/constants.js.map +1 -0
  47. package/lib/commands/preview-project/index.d.ts +3 -2
  48. package/lib/commands/preview-project/index.d.ts.map +1 -0
  49. package/lib/commands/preview-project/index.js +21 -23
  50. package/lib/commands/preview-project/index.js.map +1 -0
  51. package/lib/commands/preview-project/types.d.ts +3 -2
  52. package/lib/commands/preview-project/types.d.ts.map +1 -0
  53. package/lib/commands/preview-project/types.js +2 -2
  54. package/lib/commands/preview-project/types.js.map +1 -0
  55. package/lib/commands/split/index.d.ts +4 -3
  56. package/lib/commands/split/index.d.ts.map +1 -0
  57. package/lib/commands/split/index.js +53 -57
  58. package/lib/commands/split/index.js.map +1 -0
  59. package/lib/commands/split/types.d.ts +2 -2
  60. package/lib/commands/split/types.d.ts.map +1 -0
  61. package/lib/commands/split/types.js +11 -13
  62. package/lib/commands/split/types.js.map +1 -0
  63. package/lib/commands/stats.d.ts +3 -2
  64. package/lib/commands/stats.d.ts.map +1 -0
  65. package/lib/commands/stats.js +21 -24
  66. package/lib/commands/stats.js.map +1 -0
  67. package/lib/commands/translations.d.ts +3 -2
  68. package/lib/commands/translations.d.ts.map +1 -0
  69. package/lib/commands/translations.js +11 -13
  70. package/lib/commands/translations.js.map +1 -0
  71. package/lib/index.d.ts +2 -1
  72. package/lib/index.d.ts.map +1 -0
  73. package/lib/index.js +109 -239
  74. package/lib/index.js.map +1 -0
  75. package/lib/otel.d.ts +2 -1
  76. package/lib/otel.d.ts.map +1 -0
  77. package/lib/otel.js +22 -23
  78. package/lib/otel.js.map +1 -0
  79. package/lib/reunite/api/api-client.d.ts +4 -3
  80. package/lib/reunite/api/api-client.d.ts.map +1 -0
  81. package/lib/reunite/api/api-client.js +14 -20
  82. package/lib/reunite/api/api-client.js.map +1 -0
  83. package/lib/reunite/api/api-keys.d.ts +2 -1
  84. package/lib/reunite/api/api-keys.d.ts.map +1 -0
  85. package/lib/reunite/api/api-keys.js +4 -20
  86. package/lib/reunite/api/api-keys.js.map +1 -0
  87. package/lib/reunite/api/domains.d.ts +5 -2
  88. package/lib/reunite/api/domains.d.ts.map +1 -0
  89. package/lib/reunite/api/domains.js +6 -15
  90. package/lib/reunite/api/domains.js.map +1 -0
  91. package/lib/reunite/api/index.d.ts +4 -3
  92. package/lib/reunite/api/index.d.ts.map +1 -0
  93. package/lib/reunite/api/index.js +4 -19
  94. package/lib/reunite/api/index.js.map +1 -0
  95. package/lib/reunite/api/types.d.ts +1 -0
  96. package/lib/reunite/api/types.d.ts.map +1 -0
  97. package/lib/reunite/api/types.js +2 -2
  98. package/lib/reunite/api/types.js.map +1 -0
  99. package/lib/reunite/commands/push-status.d.ts +6 -5
  100. package/lib/reunite/commands/push-status.d.ts.map +1 -0
  101. package/lib/reunite/commands/push-status.js +31 -37
  102. package/lib/reunite/commands/push-status.js.map +1 -0
  103. package/lib/reunite/commands/push.d.ts +6 -6
  104. package/lib/reunite/commands/push.d.ts.map +1 -0
  105. package/lib/reunite/commands/push.js +31 -40
  106. package/lib/reunite/commands/push.js.map +1 -0
  107. package/lib/reunite/commands/utils.d.ts +4 -3
  108. package/lib/reunite/commands/utils.d.ts.map +1 -0
  109. package/lib/reunite/commands/utils.js +9 -12
  110. package/lib/reunite/commands/utils.js.map +1 -0
  111. package/lib/reunite/utils.d.ts +1 -0
  112. package/lib/reunite/utils.d.ts.map +1 -0
  113. package/lib/reunite/utils.js +2 -5
  114. package/lib/reunite/utils.js.map +1 -0
  115. package/lib/types.d.ts +15 -27
  116. package/lib/types.d.ts.map +1 -0
  117. package/lib/types.js +2 -5
  118. package/lib/types.js.map +1 -0
  119. package/lib/utils/assert-node-version.d.ts +1 -0
  120. package/lib/utils/assert-node-version.d.ts.map +1 -0
  121. package/lib/utils/assert-node-version.js +8 -10
  122. package/lib/utils/assert-node-version.js.map +1 -0
  123. package/lib/utils/error.d.ts +4 -0
  124. package/lib/utils/error.d.ts.map +1 -0
  125. package/lib/utils/error.js +8 -0
  126. package/lib/utils/error.js.map +1 -0
  127. package/lib/utils/fetch-with-timeout.d.ts +1 -0
  128. package/lib/utils/fetch-with-timeout.d.ts.map +1 -0
  129. package/lib/utils/fetch-with-timeout.js +6 -8
  130. package/lib/utils/fetch-with-timeout.js.map +1 -0
  131. package/lib/utils/{getCommandNameFromArgs.d.ts → get-command-name-from-args.d.ts} +1 -0
  132. package/lib/utils/get-command-name-from-args.d.ts.map +1 -0
  133. package/lib/utils/get-command-name-from-args.js +4 -0
  134. package/lib/utils/get-command-name-from-args.js.map +1 -0
  135. package/lib/utils/js-utils.d.ts +1 -0
  136. package/lib/utils/js-utils.d.ts.map +1 -0
  137. package/lib/utils/js-utils.js +8 -12
  138. package/lib/utils/js-utils.js.map +1 -0
  139. package/lib/utils/miscellaneous.d.ts +9 -48
  140. package/lib/utils/miscellaneous.d.ts.map +1 -0
  141. package/lib/utils/miscellaneous.js +131 -269
  142. package/lib/utils/miscellaneous.js.map +1 -0
  143. package/lib/utils/package.d.ts +2 -0
  144. package/lib/utils/package.d.ts.map +1 -0
  145. package/lib/utils/package.js +4 -0
  146. package/lib/utils/package.js.map +1 -0
  147. package/lib/utils/platform.d.ts +1 -0
  148. package/lib/utils/platform.d.ts.map +1 -0
  149. package/lib/utils/platform.js +5 -9
  150. package/lib/utils/platform.js.map +1 -0
  151. package/lib/utils/proxy-agent.d.ts +3 -0
  152. package/lib/utils/proxy-agent.d.ts.map +1 -0
  153. package/lib/utils/proxy-agent.js +6 -0
  154. package/lib/utils/proxy-agent.js.map +1 -0
  155. package/lib/utils/spinner.d.ts +1 -0
  156. package/lib/utils/spinner.d.ts.map +1 -0
  157. package/lib/utils/spinner.js +8 -10
  158. package/lib/utils/spinner.js.map +1 -0
  159. package/lib/utils/telemetry.d.ts +40 -0
  160. package/lib/utils/telemetry.d.ts.map +1 -0
  161. package/lib/utils/telemetry.js +138 -0
  162. package/lib/utils/telemetry.js.map +1 -0
  163. package/lib/utils/update-version-notifier.d.ts +1 -1
  164. package/lib/utils/update-version-notifier.d.ts.map +1 -0
  165. package/lib/utils/update-version-notifier.js +35 -38
  166. package/lib/utils/update-version-notifier.js.map +1 -0
  167. package/lib/utils/validate-positive-number.d.ts +2 -0
  168. package/lib/utils/validate-positive-number.d.ts.map +1 -0
  169. package/lib/utils/validate-positive-number.js +13 -0
  170. package/lib/utils/validate-positive-number.js.map +1 -0
  171. package/lib/wrapper.d.ts +3 -3
  172. package/lib/wrapper.d.ts.map +1 -0
  173. package/lib/wrapper.js +39 -26
  174. package/lib/wrapper.js.map +1 -0
  175. package/package.json +16 -17
  176. package/CHANGELOG.md +0 -777
  177. package/lib/__mocks__/@redocly/openapi-core.d.ts +0 -99
  178. package/lib/__mocks__/@redocly/openapi-core.js +0 -84
  179. package/lib/__mocks__/documents.d.ts +0 -150
  180. package/lib/__mocks__/documents.js +0 -123
  181. package/lib/__mocks__/fs.d.ts +0 -8
  182. package/lib/__mocks__/fs.js +0 -9
  183. package/lib/__mocks__/perf_hooks.d.ts +0 -3
  184. package/lib/__mocks__/perf_hooks.js +0 -6
  185. package/lib/__mocks__/redoc.d.ts +0 -6
  186. package/lib/__mocks__/redoc.js +0 -5
  187. package/lib/__tests__/commands/build-docs.test.d.ts +0 -1
  188. package/lib/__tests__/commands/build-docs.test.js +0 -54
  189. package/lib/__tests__/commands/bundle.test.d.ts +0 -1
  190. package/lib/__tests__/commands/bundle.test.js +0 -235
  191. package/lib/__tests__/commands/join.test.d.ts +0 -1
  192. package/lib/__tests__/commands/join.test.js +0 -274
  193. package/lib/__tests__/commands/lint.test.d.ts +0 -1
  194. package/lib/__tests__/commands/lint.test.js +0 -149
  195. package/lib/__tests__/commands/push-region.test.d.ts +0 -1
  196. package/lib/__tests__/commands/push-region.test.js +0 -90
  197. package/lib/__tests__/commands/push.test.d.ts +0 -1
  198. package/lib/__tests__/commands/push.test.js +0 -496
  199. package/lib/__tests__/fetch-with-timeout.test.d.ts +0 -1
  200. package/lib/__tests__/fetch-with-timeout.test.js +0 -64
  201. package/lib/__tests__/fixtures/config.d.ts +0 -21
  202. package/lib/__tests__/fixtures/config.js +0 -24
  203. package/lib/__tests__/spinner.test.d.ts +0 -1
  204. package/lib/__tests__/spinner.test.js +0 -43
  205. package/lib/__tests__/utils.test.d.ts +0 -1
  206. package/lib/__tests__/utils.test.js +0 -651
  207. package/lib/__tests__/wrapper.test.d.ts +0 -1
  208. package/lib/__tests__/wrapper.test.js +0 -76
  209. package/lib/auth/__tests__/device-flow.test.d.ts +0 -1
  210. package/lib/auth/__tests__/device-flow.test.js +0 -62
  211. package/lib/auth/__tests__/oauth-client.test.d.ts +0 -1
  212. package/lib/auth/__tests__/oauth-client.test.js +0 -93
  213. package/lib/commands/preview-docs/index.d.ts +0 -12
  214. package/lib/commands/preview-docs/index.js +0 -127
  215. package/lib/commands/preview-docs/preview-server/default.hbs +0 -24
  216. package/lib/commands/preview-docs/preview-server/hot.js +0 -59
  217. package/lib/commands/preview-docs/preview-server/oauth2-redirect.html +0 -21
  218. package/lib/commands/preview-docs/preview-server/preview-server.d.ts +0 -5
  219. package/lib/commands/preview-docs/preview-server/preview-server.js +0 -113
  220. package/lib/commands/preview-docs/preview-server/server.d.ts +0 -22
  221. package/lib/commands/preview-docs/preview-server/server.js +0 -85
  222. package/lib/commands/push.d.ts +0 -44
  223. package/lib/commands/push.js +0 -301
  224. package/lib/commands/split/__tests__/index.test.d.ts +0 -1
  225. package/lib/commands/split/__tests__/index.test.js +0 -91
  226. package/lib/reunite/api/__tests__/api-keys.test.d.ts +0 -1
  227. package/lib/reunite/api/__tests__/api-keys.test.js +0 -26
  228. package/lib/reunite/api/__tests__/api.client.test.d.ts +0 -1
  229. package/lib/reunite/api/__tests__/api.client.test.js +0 -337
  230. package/lib/reunite/api/__tests__/domains.test.d.ts +0 -1
  231. package/lib/reunite/api/__tests__/domains.test.js +0 -32
  232. package/lib/reunite/commands/__tests__/push-status.test.d.ts +0 -1
  233. package/lib/reunite/commands/__tests__/push-status.test.js +0 -563
  234. package/lib/reunite/commands/__tests__/push.test.d.ts +0 -1
  235. package/lib/reunite/commands/__tests__/push.test.js +0 -315
  236. package/lib/reunite/commands/__tests__/utils.test.d.ts +0 -1
  237. package/lib/reunite/commands/__tests__/utils.test.js +0 -51
  238. package/lib/utils/__mocks__/miscellaneous.d.ts +0 -43
  239. package/lib/utils/__mocks__/miscellaneous.js +0 -24
  240. package/lib/utils/getCommandNameFromArgs.js +0 -6
  241. package/src/__mocks__/@redocly/openapi-core.ts +0 -88
  242. package/src/__mocks__/documents.ts +0 -124
  243. package/src/__mocks__/fs.ts +0 -6
  244. package/src/__mocks__/perf_hooks.ts +0 -3
  245. package/src/__mocks__/redoc.ts +0 -2
  246. package/src/__tests__/commands/build-docs.test.ts +0 -59
  247. package/src/__tests__/commands/bundle.test.ts +0 -285
  248. package/src/__tests__/commands/join.test.ts +0 -334
  249. package/src/__tests__/commands/lint.test.ts +0 -190
  250. package/src/__tests__/commands/push-region.test.ts +0 -104
  251. package/src/__tests__/commands/push.test.ts +0 -553
  252. package/src/__tests__/fetch-with-timeout.test.ts +0 -81
  253. package/src/__tests__/fixtures/config.ts +0 -21
  254. package/src/__tests__/fixtures/openapi.json +0 -0
  255. package/src/__tests__/fixtures/openapi.yaml +0 -0
  256. package/src/__tests__/fixtures/redocly.yaml +0 -0
  257. package/src/__tests__/spinner.test.ts +0 -51
  258. package/src/__tests__/utils.test.ts +0 -781
  259. package/src/__tests__/wrapper.test.ts +0 -91
  260. package/src/auth/__tests__/device-flow.test.ts +0 -73
  261. package/src/auth/__tests__/oauth-client.test.ts +0 -117
  262. package/src/auth/device-flow.ts +0 -175
  263. package/src/auth/oauth-client.ts +0 -111
  264. package/src/commands/auth.ts +0 -66
  265. package/src/commands/build-docs/index.ts +0 -55
  266. package/src/commands/build-docs/template.hbs +0 -23
  267. package/src/commands/build-docs/types.ts +0 -25
  268. package/src/commands/build-docs/utils.ts +0 -110
  269. package/src/commands/bundle.ts +0 -154
  270. package/src/commands/eject.ts +0 -42
  271. package/src/commands/join.ts +0 -794
  272. package/src/commands/lint.ts +0 -169
  273. package/src/commands/preview-docs/index.ts +0 -183
  274. package/src/commands/preview-docs/preview-server/default.hbs +0 -24
  275. package/src/commands/preview-docs/preview-server/hot.js +0 -59
  276. package/src/commands/preview-docs/preview-server/oauth2-redirect.html +0 -21
  277. package/src/commands/preview-docs/preview-server/preview-server.ts +0 -159
  278. package/src/commands/preview-docs/preview-server/server.ts +0 -92
  279. package/src/commands/preview-project/constants.ts +0 -23
  280. package/src/commands/preview-project/index.ts +0 -71
  281. package/src/commands/preview-project/types.ts +0 -12
  282. package/src/commands/push.ts +0 -470
  283. package/src/commands/split/__tests__/fixtures/samples.json +0 -61
  284. package/src/commands/split/__tests__/fixtures/spec.json +0 -70
  285. package/src/commands/split/__tests__/fixtures/webhooks.json +0 -85
  286. package/src/commands/split/__tests__/index.test.ts +0 -156
  287. package/src/commands/split/index.ts +0 -397
  288. package/src/commands/split/types.ts +0 -60
  289. package/src/commands/stats.ts +0 -140
  290. package/src/commands/translations.ts +0 -32
  291. package/src/custom.d.ts +0 -1
  292. package/src/index.ts +0 -970
  293. package/src/otel.ts +0 -59
  294. package/src/reunite/api/__tests__/api-keys.test.ts +0 -37
  295. package/src/reunite/api/__tests__/api.client.test.ts +0 -452
  296. package/src/reunite/api/__tests__/domains.test.ts +0 -41
  297. package/src/reunite/api/api-client.ts +0 -381
  298. package/src/reunite/api/api-keys.ts +0 -26
  299. package/src/reunite/api/domains.ts +0 -23
  300. package/src/reunite/api/index.ts +0 -3
  301. package/src/reunite/api/types.ts +0 -108
  302. package/src/reunite/commands/__tests__/push-status.test.ts +0 -653
  303. package/src/reunite/commands/__tests__/push.test.ts +0 -385
  304. package/src/reunite/commands/__tests__/utils.test.ts +0 -62
  305. package/src/reunite/commands/push-status.ts +0 -325
  306. package/src/reunite/commands/push.ts +0 -235
  307. package/src/reunite/commands/utils.ts +0 -66
  308. package/src/reunite/utils.ts +0 -1
  309. package/src/types.ts +0 -64
  310. package/src/utils/__mocks__/miscellaneous.ts +0 -24
  311. package/src/utils/assert-node-version.ts +0 -19
  312. package/src/utils/fetch-with-timeout.ts +0 -30
  313. package/src/utils/getCommandNameFromArgs.ts +0 -5
  314. package/src/utils/js-utils.ts +0 -24
  315. package/src/utils/miscellaneous.ts +0 -729
  316. package/src/utils/platform.ts +0 -31
  317. package/src/utils/spinner.ts +0 -50
  318. package/src/utils/update-version-notifier.ts +0 -115
  319. package/src/wrapper.ts +0 -77
  320. package/tsconfig.json +0 -9
  321. package/tsconfig.tsbuildinfo +0 -1
@@ -1,397 +0,0 @@
1
- import { red, blue, yellow, green } from 'colorette';
2
- import * as fs from 'fs';
3
- import { parseYaml, slash, isRef, isTruthy } from '@redocly/openapi-core';
4
- import { dequal } from '@redocly/openapi-core/lib/utils';
5
- import * as path from 'path';
6
- import { performance } from 'perf_hooks';
7
- import {
8
- printExecutionTime,
9
- pathToFilename,
10
- readYaml,
11
- exitWithError,
12
- escapeLanguageName,
13
- langToExt,
14
- writeToFileByExtension,
15
- getAndValidateFileExtension,
16
- } from '../../utils/miscellaneous';
17
- import { isObject, isEmptyObject } from '../../utils/js-utils';
18
- import {
19
- OPENAPI3_COMPONENT,
20
- COMPONENTS,
21
- OPENAPI3_METHOD_NAMES,
22
- OPENAPI3_COMPONENT_NAMES,
23
- } from './types';
24
-
25
- import type { Oas3Definition, Oas3_1Definition, Oas2Definition } from '@redocly/openapi-core';
26
- import type {
27
- Oas3Schema,
28
- Oas3_1Schema,
29
- Oas3Components,
30
- Oas3_1Components,
31
- Oas3ComponentName,
32
- Oas3PathItem,
33
- OasRef,
34
- Referenced,
35
- } from '@redocly/openapi-core/lib/typings/openapi';
36
- import type { ComponentsFiles, Definition, RefObject } from './types';
37
- import type { CommandArgs } from '../../wrapper';
38
- import type { VerifyConfigOptions } from '../../types';
39
-
40
- export type SplitOptions = {
41
- api: string;
42
- outDir: string;
43
- separator: string;
44
- } & VerifyConfigOptions;
45
-
46
- export async function handleSplit({ argv, collectSpecData }: CommandArgs<SplitOptions>) {
47
- const startedAt = performance.now();
48
- const { api, outDir, separator } = argv;
49
- validateDefinitionFileName(api);
50
- const ext = getAndValidateFileExtension(api);
51
- const openapi = readYaml(api) as Oas3Definition | Oas3_1Definition;
52
- collectSpecData?.(openapi);
53
- splitDefinition(openapi, outDir, separator, ext);
54
- process.stderr.write(
55
- `🪓 Document: ${blue(api)} ${green('is successfully split')}
56
- and all related files are saved to the directory: ${blue(outDir)} \n`
57
- );
58
- printExecutionTime('split', startedAt, api);
59
- }
60
-
61
- function splitDefinition(
62
- openapi: Oas3Definition | Oas3_1Definition,
63
- openapiDir: string,
64
- pathSeparator: string,
65
- ext: string
66
- ) {
67
- fs.mkdirSync(openapiDir, { recursive: true });
68
-
69
- const componentsFiles: ComponentsFiles = {};
70
- iterateComponents(openapi, openapiDir, componentsFiles, ext);
71
- iteratePathItems(
72
- openapi.paths,
73
- openapiDir,
74
- path.join(openapiDir, 'paths'),
75
- componentsFiles,
76
- pathSeparator,
77
- undefined,
78
- ext
79
- );
80
- const webhooks =
81
- (openapi as Oas3_1Definition).webhooks || (openapi as Oas3Definition)['x-webhooks'];
82
- // use webhook_ prefix for code samples to prevent potential name-clashes with paths samples
83
- iteratePathItems(
84
- webhooks,
85
- openapiDir,
86
- path.join(openapiDir, 'webhooks'),
87
- componentsFiles,
88
- pathSeparator,
89
- 'webhook_',
90
- ext
91
- );
92
-
93
- replace$Refs(openapi, openapiDir, componentsFiles);
94
- writeToFileByExtension(openapi, path.join(openapiDir, `openapi.${ext}`));
95
- }
96
-
97
- export function startsWithComponents(node: string) {
98
- return node.startsWith(`#/${COMPONENTS}/`);
99
- }
100
-
101
- function isSupportedExtension(filename: string) {
102
- return filename.endsWith('.yaml') || filename.endsWith('.yml') || filename.endsWith('.json');
103
- }
104
-
105
- function loadFile(fileName: string) {
106
- try {
107
- return parseYaml(fs.readFileSync(fileName, 'utf8')) as Definition;
108
- } catch (e) {
109
- return exitWithError(e.message);
110
- }
111
- }
112
-
113
- function validateDefinitionFileName(fileName: string) {
114
- if (!fs.existsSync(fileName)) exitWithError(`File ${blue(fileName)} does not exist.`);
115
- const file = loadFile(fileName);
116
- if ((file as Oas2Definition).swagger)
117
- exitWithError('OpenAPI 2 is not supported by this command.');
118
- if (!(file as Oas3Definition | Oas3_1Definition).openapi)
119
- exitWithError(
120
- 'File does not conform to the OpenAPI Specification. OpenAPI version is not specified.'
121
- );
122
- return true;
123
- }
124
-
125
- function traverseDirectoryDeep(directory: string, callback: any, componentsFiles: object) {
126
- if (!fs.existsSync(directory) || !fs.statSync(directory).isDirectory()) return;
127
- const files = fs.readdirSync(directory);
128
- for (const f of files) {
129
- const filename = path.join(directory, f);
130
- if (fs.statSync(filename).isDirectory()) {
131
- traverseDirectoryDeep(filename, callback, componentsFiles);
132
- } else {
133
- callback(filename, directory, componentsFiles);
134
- }
135
- }
136
- }
137
-
138
- function traverseDirectoryDeepCallback(
139
- filename: string,
140
- directory: string,
141
- componentsFiles: object
142
- ) {
143
- if (!isSupportedExtension(filename)) return;
144
- const pathData = readYaml(filename);
145
- replace$Refs(pathData, directory, componentsFiles);
146
- writeToFileByExtension(pathData, filename);
147
- }
148
-
149
- export function crawl(object: unknown, visitor: (node: Record<string, unknown>) => void) {
150
- if (!isObject(object)) return;
151
-
152
- visitor(object);
153
- for (const key of Object.keys(object)) {
154
- crawl(object[key], visitor);
155
- }
156
- }
157
-
158
- function replace$Refs(obj: unknown, relativeFrom: string, componentFiles = {} as ComponentsFiles) {
159
- crawl(obj, (node: Record<string, unknown>) => {
160
- if (node.$ref && typeof node.$ref === 'string' && startsWithComponents(node.$ref)) {
161
- replace(node as RefObject, '$ref');
162
- } else if (isObject(node.discriminator) && isObject(node.discriminator.mapping)) {
163
- const { mapping } = node.discriminator;
164
- for (const name of Object.keys(mapping)) {
165
- const mappingPointer = mapping[name];
166
- if (typeof mappingPointer === 'string' && startsWithComponents(mappingPointer)) {
167
- replace(node.discriminator.mapping as RefObject, name);
168
- }
169
- }
170
- }
171
- });
172
-
173
- function replace(node: RefObject, key: string) {
174
- const splittedNode = node[key].split('/');
175
- const name = splittedNode.pop();
176
- const groupName = splittedNode[2];
177
- const filesGroupName = componentFiles[groupName];
178
- if (!filesGroupName || !filesGroupName[name!]) return;
179
- let filename = slash(path.relative(relativeFrom, filesGroupName[name!].filename));
180
- if (!filename.startsWith('.')) {
181
- filename = './' + filename;
182
- }
183
- node[key] = filename;
184
- }
185
- }
186
-
187
- function implicitlyReferenceDiscriminator(
188
- obj: any,
189
- defName: string,
190
- filename: string,
191
- schemaFiles: any
192
- ) {
193
- if (!obj.discriminator) return;
194
- const defPtr = `#/${COMPONENTS}/${OPENAPI3_COMPONENT.Schemas}/${defName}`;
195
- const implicitMapping: Record<string, string> = {};
196
- for (const [name, { inherits, filename: parentFilename }] of Object.entries(schemaFiles) as any) {
197
- if (inherits.indexOf(defPtr) > -1) {
198
- const res = slash(path.relative(path.dirname(filename), parentFilename));
199
- implicitMapping[name] = res.startsWith('.') ? res : './' + res;
200
- }
201
- }
202
-
203
- if (isEmptyObject(implicitMapping)) return;
204
- const discriminatorPropSchema = obj.properties[obj.discriminator.propertyName];
205
- const discriminatorEnum = discriminatorPropSchema && discriminatorPropSchema.enum;
206
- const mapping = (obj.discriminator.mapping = obj.discriminator.mapping || {});
207
- for (const name of Object.keys(implicitMapping)) {
208
- if (discriminatorEnum && !discriminatorEnum.includes(name)) {
209
- continue;
210
- }
211
- if (mapping[name] && mapping[name] !== implicitMapping[name]) {
212
- process.stderr.write(
213
- yellow(
214
- `warning: explicit mapping overlaps with local mapping entry ${red(name)} at ${blue(
215
- filename
216
- )}. Please check it.`
217
- )
218
- );
219
- }
220
- mapping[name] = implicitMapping[name];
221
- }
222
- }
223
-
224
- function isNotSecurityComponentType(componentType: string) {
225
- return componentType !== OPENAPI3_COMPONENT.SecuritySchemes;
226
- }
227
-
228
- function findComponentTypes(components: any) {
229
- return OPENAPI3_COMPONENT_NAMES.filter(
230
- (item) => isNotSecurityComponentType(item) && Object.keys(components).includes(item)
231
- );
232
- }
233
-
234
- function doesFileDiffer(filename: string, componentData: any) {
235
- return fs.existsSync(filename) && !dequal(readYaml(filename), componentData);
236
- }
237
-
238
- function removeEmptyComponents(
239
- openapi: Oas3Definition | Oas3_1Definition,
240
- componentType: Oas3ComponentName<Oas3Schema | Oas3_1Schema>
241
- ) {
242
- if (openapi.components && isEmptyObject(openapi.components[componentType])) {
243
- delete openapi.components[componentType];
244
- }
245
- if (isEmptyObject(openapi.components)) {
246
- delete openapi.components;
247
- }
248
- }
249
-
250
- function createComponentDir(componentDirPath: string, componentType: string) {
251
- if (isNotSecurityComponentType(componentType)) {
252
- fs.mkdirSync(componentDirPath, { recursive: true });
253
- }
254
- }
255
-
256
- function extractFileNameFromPath(filename: string) {
257
- return path.basename(filename, path.extname(filename));
258
- }
259
-
260
- function getFileNamePath(componentDirPath: string, componentName: string, ext: string) {
261
- return path.join(componentDirPath, componentName) + `.${ext}`;
262
- }
263
-
264
- function gatherComponentsFiles(
265
- components: Oas3Components | Oas3_1Components,
266
- componentsFiles: ComponentsFiles,
267
- componentType: Oas3ComponentName<Oas3Schema | Oas3_1Schema>,
268
- componentName: string,
269
- filename: string
270
- ) {
271
- let inherits: string[] = [];
272
- if (componentType === OPENAPI3_COMPONENT.Schemas) {
273
- inherits = (
274
- (components?.[componentType]?.[componentName] as Oas3Schema | Oas3_1Schema)?.allOf || []
275
- )
276
- .map(({ $ref }) => $ref)
277
- .filter(isTruthy);
278
- }
279
- componentsFiles[componentType] = componentsFiles[componentType] || {};
280
- componentsFiles[componentType][componentName] = { inherits, filename };
281
- }
282
-
283
- function iteratePathItems(
284
- pathItems: Record<string, Referenced<Oas3PathItem>> | undefined,
285
- openapiDir: string,
286
- outDir: string,
287
- componentsFiles: object,
288
- pathSeparator: string,
289
- codeSamplesPathPrefix: string = '',
290
- ext: string
291
- ) {
292
- if (!pathItems) return;
293
- fs.mkdirSync(outDir, { recursive: true });
294
-
295
- for (const pathName of Object.keys(pathItems)) {
296
- const pathFile = `${path.join(outDir, pathToFilename(pathName, pathSeparator))}.${ext}`;
297
- const pathData = pathItems[pathName];
298
-
299
- if (isRef(pathData)) continue;
300
-
301
- for (const method of OPENAPI3_METHOD_NAMES) {
302
- const methodData = pathData[method];
303
- const methodDataXCode = methodData?.['x-code-samples'] || methodData?.['x-codeSamples'];
304
- if (!methodDataXCode || !Array.isArray(methodDataXCode)) {
305
- continue;
306
- }
307
- for (const sample of methodDataXCode) {
308
- if (sample.source && (sample.source as unknown as OasRef).$ref) continue;
309
- const sampleFileName = path.join(
310
- openapiDir,
311
- 'code_samples',
312
- escapeLanguageName(sample.lang),
313
- codeSamplesPathPrefix + pathToFilename(pathName, pathSeparator),
314
- method + langToExt(sample.lang)
315
- );
316
-
317
- fs.mkdirSync(path.dirname(sampleFileName), { recursive: true });
318
- fs.writeFileSync(sampleFileName, sample.source);
319
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
320
- // @ts-ignore
321
- sample.source = {
322
- $ref: slash(path.relative(outDir, sampleFileName)),
323
- };
324
- }
325
- }
326
- writeToFileByExtension(pathData, pathFile);
327
- pathItems[pathName] = {
328
- $ref: slash(path.relative(openapiDir, pathFile)),
329
- };
330
-
331
- traverseDirectoryDeep(outDir, traverseDirectoryDeepCallback, componentsFiles);
332
- }
333
- }
334
-
335
- function iterateComponents(
336
- openapi: Oas3Definition | Oas3_1Definition,
337
- openapiDir: string,
338
- componentsFiles: ComponentsFiles,
339
- ext: string
340
- ) {
341
- const { components } = openapi;
342
- if (components) {
343
- const componentsDir = path.join(openapiDir, COMPONENTS);
344
- fs.mkdirSync(componentsDir, { recursive: true });
345
- const componentTypes = findComponentTypes(components);
346
- componentTypes.forEach(iterateAndGatherComponentsFiles);
347
- componentTypes.forEach(iterateComponentTypes);
348
-
349
- // eslint-disable-next-line no-inner-declarations
350
- function iterateAndGatherComponentsFiles(
351
- componentType: Oas3ComponentName<Oas3Schema | Oas3_1Schema>
352
- ) {
353
- const componentDirPath = path.join(componentsDir, componentType);
354
- for (const componentName of Object.keys(components?.[componentType] || {})) {
355
- const filename = getFileNamePath(componentDirPath, componentName, ext);
356
- gatherComponentsFiles(components!, componentsFiles, componentType, componentName, filename);
357
- }
358
- }
359
-
360
- // eslint-disable-next-line no-inner-declarations
361
- function iterateComponentTypes(componentType: Oas3ComponentName<Oas3Schema | Oas3_1Schema>) {
362
- const componentDirPath = path.join(componentsDir, componentType);
363
- createComponentDir(componentDirPath, componentType);
364
- for (const componentName of Object.keys(components?.[componentType] || {})) {
365
- const filename = getFileNamePath(componentDirPath, componentName, ext);
366
- const componentData = components?.[componentType]?.[componentName];
367
- replace$Refs(componentData, path.dirname(filename), componentsFiles);
368
- implicitlyReferenceDiscriminator(
369
- componentData,
370
- extractFileNameFromPath(filename),
371
- filename,
372
- componentsFiles.schemas || {}
373
- );
374
-
375
- if (doesFileDiffer(filename, componentData)) {
376
- process.stderr.write(
377
- yellow(
378
- `warning: conflict for ${componentName} - file already exists with different content: ${blue(
379
- filename
380
- )} ... Skip.\n`
381
- )
382
- );
383
- } else {
384
- writeToFileByExtension(componentData, filename);
385
- }
386
-
387
- if (isNotSecurityComponentType(componentType)) {
388
- // security schemas must referenced from components
389
- delete openapi.components?.[componentType]?.[componentName];
390
- }
391
- }
392
- removeEmptyComponents(openapi, componentType);
393
- }
394
- }
395
- }
396
-
397
- export { iteratePathItems };
@@ -1,60 +0,0 @@
1
- import type { Oas2Definition } from '@redocly/openapi-core';
2
- import type { Oas3_1Definition, Oas3Definition } from '@redocly/openapi-core/lib/typings/openapi';
3
-
4
- export type Definition = Oas3_1Definition | Oas3Definition | Oas2Definition;
5
- export interface ComponentsFiles {
6
- [schemas: string]: any;
7
- }
8
- export interface RefObject {
9
- [$ref: string]: string;
10
- }
11
-
12
- export const COMPONENTS = 'components';
13
- export const PATHS = 'paths';
14
- export const WEBHOOKS = 'webhooks';
15
- export const xWEBHOOKS = 'x-webhooks';
16
- export enum OPENAPI3_METHOD {
17
- get = 'get',
18
- put = 'put',
19
- post = 'post',
20
- delete = 'delete',
21
- options = 'options',
22
- head = 'head',
23
- patch = 'patch',
24
- trace = 'trace',
25
- }
26
-
27
- export const OPENAPI3_METHOD_NAMES: OPENAPI3_METHOD[] = [
28
- OPENAPI3_METHOD.get,
29
- OPENAPI3_METHOD.put,
30
- OPENAPI3_METHOD.post,
31
- OPENAPI3_METHOD.delete,
32
- OPENAPI3_METHOD.options,
33
- OPENAPI3_METHOD.head,
34
- OPENAPI3_METHOD.patch,
35
- OPENAPI3_METHOD.trace,
36
- ];
37
-
38
- export enum OPENAPI3_COMPONENT {
39
- Schemas = 'schemas',
40
- Responses = 'responses',
41
- Parameters = 'parameters',
42
- Examples = 'examples',
43
- Headers = 'headers',
44
- RequestBodies = 'requestBodies',
45
- Links = 'links',
46
- Callbacks = 'callbacks',
47
- SecuritySchemes = 'securitySchemes',
48
- }
49
-
50
- export const OPENAPI3_COMPONENT_NAMES: OPENAPI3_COMPONENT[] = [
51
- OPENAPI3_COMPONENT.RequestBodies,
52
- OPENAPI3_COMPONENT.Schemas,
53
- OPENAPI3_COMPONENT.Responses,
54
- OPENAPI3_COMPONENT.Parameters,
55
- OPENAPI3_COMPONENT.Examples,
56
- OPENAPI3_COMPONENT.Headers,
57
- OPENAPI3_COMPONENT.Links,
58
- OPENAPI3_COMPONENT.Callbacks,
59
- OPENAPI3_COMPONENT.SecuritySchemes,
60
- ];
@@ -1,140 +0,0 @@
1
- import { performance } from 'perf_hooks';
2
- import * as colors from 'colorette';
3
- import {
4
- normalizeTypes,
5
- BaseResolver,
6
- resolveDocument,
7
- detectSpec,
8
- getTypes,
9
- normalizeVisitors,
10
- walkDocument,
11
- Stats,
12
- bundle,
13
- } from '@redocly/openapi-core';
14
- import { getFallbackApisOrExit, printExecutionTime } from '../utils/miscellaneous';
15
-
16
- import type {
17
- StatsAccumulator,
18
- StatsName,
19
- WalkContext,
20
- OutputFormat,
21
- StyleguideConfig,
22
- } from '@redocly/openapi-core';
23
- import type { CommandArgs } from '../wrapper';
24
- import type { VerifyConfigOptions } from '../types';
25
-
26
- const statsAccumulator: StatsAccumulator = {
27
- refs: { metric: '🚗 References', total: 0, color: 'red', items: new Set() },
28
- externalDocs: { metric: '📦 External Documents', total: 0, color: 'magenta' },
29
- schemas: { metric: '📈 Schemas', total: 0, color: 'white' },
30
- parameters: { metric: '👉 Parameters', total: 0, color: 'yellow', items: new Set() },
31
- links: { metric: '🔗 Links', total: 0, color: 'cyan', items: new Set() },
32
- pathItems: { metric: '🔀 Path Items', total: 0, color: 'green' },
33
- webhooks: { metric: '🎣 Webhooks', total: 0, color: 'green' },
34
- operations: { metric: '👷 Operations', total: 0, color: 'yellow' },
35
- tags: { metric: '🔖 Tags', total: 0, color: 'white', items: new Set() },
36
- };
37
-
38
- function printStatsStylish(statsAccumulator: StatsAccumulator) {
39
- for (const node in statsAccumulator) {
40
- const { metric, total, color } = statsAccumulator[node as StatsName];
41
- process.stderr.write(colors[color](`${metric}: ${total} \n`));
42
- }
43
- }
44
-
45
- function printStatsJson(statsAccumulator: StatsAccumulator) {
46
- const json: any = {};
47
- for (const key of Object.keys(statsAccumulator)) {
48
- json[key] = {
49
- metric: statsAccumulator[key as StatsName].metric,
50
- total: statsAccumulator[key as StatsName].total,
51
- };
52
- }
53
- process.stdout.write(JSON.stringify(json, null, 2));
54
- }
55
-
56
- function printStatsMarkdown(statsAccumulator: StatsAccumulator) {
57
- let output = '| Feature | Count |\n| --- | --- |\n';
58
- for (const key of Object.keys(statsAccumulator)) {
59
- output +=
60
- '| ' +
61
- statsAccumulator[key as StatsName].metric +
62
- ' | ' +
63
- statsAccumulator[key as StatsName].total +
64
- ' |\n';
65
- }
66
- process.stdout.write(output);
67
- }
68
-
69
- function printStats(
70
- statsAccumulator: StatsAccumulator,
71
- api: string,
72
- startedAt: number,
73
- format: string
74
- ) {
75
- switch (format) {
76
- case 'stylish':
77
- process.stderr.write(`Document: ${colors.magenta(api)} stats:\n\n`);
78
- printStatsStylish(statsAccumulator);
79
- printExecutionTime('stats', startedAt, api);
80
- break;
81
- case 'json':
82
- printStatsJson(statsAccumulator);
83
- break;
84
- case 'markdown':
85
- printStatsMarkdown(statsAccumulator);
86
- break;
87
- }
88
- }
89
-
90
- export type StatsOptions = {
91
- api?: string;
92
- format: OutputFormat;
93
- } & VerifyConfigOptions;
94
-
95
- export async function handleStats({ argv, config, collectSpecData }: CommandArgs<StatsOptions>) {
96
- const [{ path }] = await getFallbackApisOrExit(argv.api ? [argv.api] : [], config);
97
- const externalRefResolver = new BaseResolver(config.resolve);
98
- const { bundle: document } = await bundle({ config, ref: path });
99
- collectSpecData?.(document.parsed);
100
- const lintConfig: StyleguideConfig = config.styleguide;
101
- const specVersion = detectSpec(document.parsed);
102
- const types = normalizeTypes(
103
- lintConfig.extendTypes(getTypes(specVersion), specVersion),
104
- lintConfig
105
- );
106
-
107
- const startedAt = performance.now();
108
- const ctx: WalkContext = {
109
- problems: [],
110
- oasVersion: specVersion,
111
- visitorsData: {},
112
- };
113
-
114
- const resolvedRefMap = await resolveDocument({
115
- rootDocument: document,
116
- rootType: types.Root,
117
- externalRefResolver,
118
- });
119
-
120
- const statsVisitor = normalizeVisitors(
121
- [
122
- {
123
- severity: 'warn',
124
- ruleId: 'stats',
125
- visitor: Stats(statsAccumulator),
126
- },
127
- ],
128
- types
129
- );
130
-
131
- walkDocument({
132
- document,
133
- rootType: types.Root,
134
- normalizedVisitors: statsVisitor,
135
- resolvedRefMap,
136
- ctx,
137
- });
138
-
139
- printStats(statsAccumulator, path, startedAt, argv.format);
140
- }
@@ -1,32 +0,0 @@
1
- import { spawn } from 'child_process';
2
- import { getPlatformSpawnArgs, sanitizeLocale, sanitizePath } from '../utils/platform';
3
-
4
- import type { CommandArgs } from '../wrapper';
5
- import type { VerifyConfigOptions } from '../types';
6
-
7
- export type TranslationsOptions = {
8
- locale: string;
9
- 'project-dir'?: string;
10
- } & VerifyConfigOptions;
11
-
12
- export const handleTranslations = async ({ argv }: CommandArgs<TranslationsOptions>) => {
13
- process.stdout.write(`\nLaunching translate using NPX.\n\n`);
14
- const { npxExecutableName, sanitize, shell } = getPlatformSpawnArgs();
15
-
16
- const projectDir = sanitize(argv['project-dir'], sanitizePath);
17
- const locale = sanitize(argv.locale, sanitizeLocale);
18
-
19
- const child = spawn(
20
- npxExecutableName,
21
- ['-y', '@redocly/realm', 'translate', locale, `-d=${projectDir}`],
22
- {
23
- stdio: 'inherit',
24
- shell,
25
- }
26
- );
27
-
28
- child.on('error', (error) => {
29
- process.stderr.write(`Translate launch failed: ${error.message}`);
30
- throw new Error(`Translate launch failed.`);
31
- });
32
- };
package/src/custom.d.ts DELETED
@@ -1 +0,0 @@
1
- type GenericObject = Record<string, any>;