@redocly/cli 1.0.0-beta.96

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 (79) hide show
  1. package/README.md +39 -0
  2. package/bin/cli.js +3 -0
  3. package/lib/__mocks__/utils.d.ts +17 -0
  4. package/lib/__mocks__/utils.js +14 -0
  5. package/lib/__tests__/commands/bundle.test.d.ts +1 -0
  6. package/lib/__tests__/commands/bundle.test.js +92 -0
  7. package/lib/__tests__/commands/push-region.test.d.ts +1 -0
  8. package/lib/__tests__/commands/push-region.test.js +55 -0
  9. package/lib/__tests__/commands/push.test.d.ts +1 -0
  10. package/lib/__tests__/commands/push.test.js +153 -0
  11. package/lib/__tests__/utils.test.d.ts +1 -0
  12. package/lib/__tests__/utils.test.js +41 -0
  13. package/lib/assert-node-version.d.ts +1 -0
  14. package/lib/assert-node-version.js +10 -0
  15. package/lib/commands/bundle.d.ts +19 -0
  16. package/lib/commands/bundle.js +128 -0
  17. package/lib/commands/join.d.ts +7 -0
  18. package/lib/commands/join.js +421 -0
  19. package/lib/commands/lint.d.ts +11 -0
  20. package/lib/commands/lint.js +80 -0
  21. package/lib/commands/login.d.ts +6 -0
  22. package/lib/commands/login.js +28 -0
  23. package/lib/commands/preview-docs/index.d.ts +12 -0
  24. package/lib/commands/preview-docs/index.js +141 -0
  25. package/lib/commands/preview-docs/preview-server/default.hbs +24 -0
  26. package/lib/commands/preview-docs/preview-server/hot.js +42 -0
  27. package/lib/commands/preview-docs/preview-server/oauth2-redirect.html +21 -0
  28. package/lib/commands/preview-docs/preview-server/preview-server.d.ts +5 -0
  29. package/lib/commands/preview-docs/preview-server/preview-server.js +120 -0
  30. package/lib/commands/preview-docs/preview-server/server.d.ts +23 -0
  31. package/lib/commands/preview-docs/preview-server/server.js +85 -0
  32. package/lib/commands/push.d.ts +25 -0
  33. package/lib/commands/push.js +247 -0
  34. package/lib/commands/split/__tests__/index.test.d.ts +1 -0
  35. package/lib/commands/split/__tests__/index.test.js +70 -0
  36. package/lib/commands/split/index.d.ts +8 -0
  37. package/lib/commands/split/index.js +279 -0
  38. package/lib/commands/split/types.d.ts +37 -0
  39. package/lib/commands/split/types.js +52 -0
  40. package/lib/commands/stats.d.ts +5 -0
  41. package/lib/commands/stats.js +92 -0
  42. package/lib/index.d.ts +2 -0
  43. package/lib/index.js +269 -0
  44. package/lib/js-utils.d.ts +3 -0
  45. package/lib/js-utils.js +16 -0
  46. package/lib/types.d.ts +13 -0
  47. package/lib/types.js +5 -0
  48. package/lib/utils.d.ts +28 -0
  49. package/lib/utils.js +260 -0
  50. package/package.json +54 -0
  51. package/src/__mocks__/utils.ts +11 -0
  52. package/src/__tests__/commands/bundle.test.ts +120 -0
  53. package/src/__tests__/commands/push-region.test.ts +51 -0
  54. package/src/__tests__/commands/push.test.ts +156 -0
  55. package/src/__tests__/utils.test.ts +50 -0
  56. package/src/assert-node-version.ts +8 -0
  57. package/src/commands/bundle.ts +178 -0
  58. package/src/commands/join.ts +488 -0
  59. package/src/commands/lint.ts +110 -0
  60. package/src/commands/login.ts +19 -0
  61. package/src/commands/preview-docs/index.ts +188 -0
  62. package/src/commands/preview-docs/preview-server/default.hbs +24 -0
  63. package/src/commands/preview-docs/preview-server/hot.js +42 -0
  64. package/src/commands/preview-docs/preview-server/oauth2-redirect.html +21 -0
  65. package/src/commands/preview-docs/preview-server/preview-server.ts +150 -0
  66. package/src/commands/preview-docs/preview-server/server.ts +91 -0
  67. package/src/commands/push.ts +355 -0
  68. package/src/commands/split/__tests__/fixtures/spec.json +70 -0
  69. package/src/commands/split/__tests__/fixtures/webhooks.json +88 -0
  70. package/src/commands/split/__tests__/index.test.ts +96 -0
  71. package/src/commands/split/index.ts +349 -0
  72. package/src/commands/split/types.ts +73 -0
  73. package/src/commands/stats.ts +115 -0
  74. package/src/index.ts +311 -0
  75. package/src/js-utils.ts +12 -0
  76. package/src/types.ts +13 -0
  77. package/src/utils.ts +300 -0
  78. package/tsconfig.json +9 -0
  79. package/tsconfig.tsbuildinfo +1 -0
@@ -0,0 +1,50 @@
1
+ import { isSubdir, pathToFilename } from '../utils';
2
+
3
+ jest.mock("os");
4
+
5
+ describe('isSubdir', () => {
6
+ it('can correctly determine if subdir', () => {
7
+ (
8
+ [
9
+ ['/foo', '/foo', false],
10
+ ['/foo', '/bar', false],
11
+ ['/foo', '/foobar', false],
12
+ ['/foo', '/foo/bar', true],
13
+ ['/foo', '/foo/../bar', false],
14
+ ['/foo', '/foo/./bar', true],
15
+ ['/bar/../foo', '/foo/bar', true],
16
+ ['/foo', './bar', false],
17
+ ['/foo', '/foo/..bar', true],
18
+ ] as [string, string, boolean][]
19
+ ).forEach(([parent, child, expectRes]) => {
20
+ expect(isSubdir(parent, child)).toBe(expectRes);
21
+ });
22
+ });
23
+
24
+ it('can correctly determine if subdir for windows-based paths', () => {
25
+ const os = require('os');
26
+ os.platform.mockImplementation(() => 'win32');
27
+
28
+ (
29
+ [
30
+ ['C:/Foo', 'C:/Foo/Bar', true],
31
+ ['C:\\Foo', 'C:\\Bar', false],
32
+ ['C:\\Foo', 'D:\\Foo\\Bar', false],
33
+ ] as [string, string, boolean][]
34
+ ).forEach(([parent, child, expectRes]) => {
35
+ expect(isSubdir(parent, child)).toBe(expectRes);
36
+ });
37
+ });
38
+
39
+ afterEach(() => {
40
+ jest.resetModules()
41
+ })
42
+ });
43
+
44
+
45
+ describe('pathToFilename', () => {
46
+ it('should use correct path separator', () => {
47
+ const processedPath = pathToFilename('/user/createWithList', '_');
48
+ expect(processedPath).toEqual('user_createWithList');
49
+ });
50
+ });
@@ -0,0 +1,8 @@
1
+ import * as path from 'path';
2
+ import { exitWithError } from './utils';
3
+
4
+ try {
5
+ require('assert-node-version')(path.join(__dirname, '../'));
6
+ } catch (err) {
7
+ exitWithError(err.message);
8
+ }
@@ -0,0 +1,178 @@
1
+ import {
2
+ formatProblems,
3
+ getTotals,
4
+ loadConfig,
5
+ getMergedConfig,
6
+ OutputFormat,
7
+ lint,
8
+ bundle,
9
+ } from '@redocly/openapi-core';
10
+ import {
11
+ dumpBundle,
12
+ getExecutionTime,
13
+ getFallbackEntryPointsOrExit,
14
+ getOutputFileName,
15
+ handleError,
16
+ printUnusedWarnings,
17
+ saveBundle,
18
+ printLintTotals,
19
+ } from '../utils';
20
+ import { OutputExtensions, Totals } from '../types';
21
+ import { performance } from 'perf_hooks';
22
+ import { blue, gray, green, red, yellow } from 'colorette';
23
+ import { writeFileSync } from 'fs';
24
+
25
+ export async function handleBundle(
26
+ argv: {
27
+ entrypoints: string[];
28
+ output?: string;
29
+ ext: OutputExtensions;
30
+ 'max-problems'?: number;
31
+ 'skip-rule'?: string[];
32
+ 'skip-preprocessor'?: string[];
33
+ 'skip-decorator'?: string[];
34
+ dereferenced?: boolean;
35
+ force?: boolean;
36
+ config?: string;
37
+ lint?: boolean;
38
+ format: OutputFormat;
39
+ metafile?: string;
40
+ extends?: string[];
41
+ 'remove-unused-components'?: boolean;
42
+ },
43
+ version: string,
44
+ ) {
45
+ const config = await loadConfig(argv.config, argv.extends);
46
+ const removeUnusedComponents =
47
+ argv['remove-unused-components'] &&
48
+ !config.rawConfig.lint?.decorators?.hasOwnProperty('remove-unused-components');
49
+ const entrypoints = await getFallbackEntryPointsOrExit(argv.entrypoints, config);
50
+ const totals: Totals = { errors: 0, warnings: 0, ignored: 0 };
51
+ const maxProblems = argv['max-problems'];
52
+
53
+ for (const { path, alias } of entrypoints) {
54
+ try {
55
+ const startedAt = performance.now();
56
+ const resolvedConfig = getMergedConfig(config, alias);
57
+ resolvedConfig.lint.skipRules(argv['skip-rule']);
58
+ resolvedConfig.lint.skipPreprocessors(argv['skip-preprocessor']);
59
+ resolvedConfig.lint.skipDecorators(argv['skip-decorator']);
60
+
61
+ if (argv.lint) {
62
+ if (config.lint.recommendedFallback) {
63
+ process.stderr.write(
64
+ `No configurations were defined in extends -- using built in ${blue(
65
+ 'recommended',
66
+ )} configuration by default.\n${red(
67
+ 'Warning! This default behavior is going to be deprecated soon.',
68
+ )}\n\n`,
69
+ );
70
+ }
71
+ const results = await lint({
72
+ ref: path,
73
+ config: resolvedConfig,
74
+ });
75
+ const fileLintTotals = getTotals(results);
76
+
77
+ totals.errors += fileLintTotals.errors;
78
+ totals.warnings += fileLintTotals.warnings;
79
+ totals.ignored += fileLintTotals.ignored;
80
+
81
+ formatProblems(results, {
82
+ format: argv.format || 'codeframe',
83
+ totals: fileLintTotals,
84
+ version,
85
+ maxProblems,
86
+ });
87
+ printLintTotals(fileLintTotals, 2);
88
+ }
89
+
90
+ process.stderr.write(gray(`bundling ${path}...\n`));
91
+
92
+ const {
93
+ bundle: result,
94
+ problems,
95
+ ...meta
96
+ } = await bundle({
97
+ config: resolvedConfig,
98
+ ref: path,
99
+ dereference: argv.dereferenced,
100
+ removeUnusedComponents,
101
+ });
102
+
103
+ const fileTotals = getTotals(problems);
104
+ const { outputFile, ext } = getOutputFileName(
105
+ path,
106
+ entrypoints.length,
107
+ argv.output,
108
+ argv.ext,
109
+ );
110
+
111
+ if (fileTotals.errors === 0 || argv.force) {
112
+ if (!argv.output) {
113
+ const output = dumpBundle(result.parsed, argv.ext || 'yaml', argv.dereferenced);
114
+ process.stdout.write(output);
115
+ } else {
116
+ const output = dumpBundle(result.parsed, ext, argv.dereferenced);
117
+ saveBundle(outputFile, output);
118
+ }
119
+ }
120
+
121
+ totals.errors += fileTotals.errors;
122
+ totals.warnings += fileTotals.warnings;
123
+ totals.ignored += fileTotals.ignored;
124
+
125
+ formatProblems(problems, {
126
+ format: argv.format,
127
+ maxProblems,
128
+ totals: fileTotals,
129
+ version,
130
+ });
131
+
132
+ if (argv.metafile) {
133
+ if (entrypoints.length > 1) {
134
+ process.stderr.write(
135
+ yellow(`[WARNING] "--metafile" cannot be used with multiple entrypoints. Skipping...`),
136
+ );
137
+ }
138
+ {
139
+ writeFileSync(argv.metafile, JSON.stringify(meta), 'utf-8');
140
+ }
141
+ }
142
+
143
+ const elapsed = getExecutionTime(startedAt);
144
+ if (fileTotals.errors > 0) {
145
+ if (argv.force) {
146
+ process.stderr.write(
147
+ `❓ Created a bundle for ${blue(path)} at ${blue(outputFile)} with errors ${green(
148
+ elapsed,
149
+ )}.\n${yellow('Errors ignored because of --force')}.\n`,
150
+ );
151
+ } else {
152
+ process.stderr.write(
153
+ `❌ Errors encountered while bundling ${blue(
154
+ path,
155
+ )}: bundle not created (use --force to ignore errors).\n`,
156
+ );
157
+ }
158
+ } else {
159
+ process.stderr.write(
160
+ `📦 Created a bundle for ${blue(path)} at ${blue(outputFile)} ${green(elapsed)}.\n`,
161
+ );
162
+ }
163
+
164
+ const removedCount = meta.visitorsData?.['remove-unused-components']?.removedCount;
165
+ if (removedCount) {
166
+ process.stderr.write(gray(`🧹 Removed ${removedCount} unused components.\n`));
167
+ }
168
+ } catch (e) {
169
+ handleError(e, path);
170
+ }
171
+ }
172
+
173
+ printUnusedWarnings(config.lint);
174
+
175
+ // defer process exit to allow STDOUT pipe to flush
176
+ // see https://github.com/nodejs/node-v0.x-archive/issues/3737#issuecomment-19156072
177
+ process.once('exit', () => process.exit(totals.errors === 0 || argv.force ? 0 : 1));
178
+ }