@redocly/cli 0.0.0-snapshot.1737554067
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/README.md +114 -0
- package/bin/cli.js +3 -0
- package/lib/__mocks__/@redocly/openapi-core.d.ts +99 -0
- package/lib/__mocks__/@redocly/openapi-core.js +84 -0
- package/lib/__mocks__/documents.d.ts +150 -0
- package/lib/__mocks__/documents.js +123 -0
- package/lib/__mocks__/fs.d.ts +8 -0
- package/lib/__mocks__/fs.js +9 -0
- package/lib/__mocks__/perf_hooks.d.ts +3 -0
- package/lib/__mocks__/perf_hooks.js +6 -0
- package/lib/__mocks__/redoc.d.ts +6 -0
- package/lib/__mocks__/redoc.js +5 -0
- package/lib/__tests__/commands/build-docs.test.d.ts +1 -0
- package/lib/__tests__/commands/build-docs.test.js +54 -0
- package/lib/__tests__/commands/bundle.test.d.ts +1 -0
- package/lib/__tests__/commands/bundle.test.js +235 -0
- package/lib/__tests__/commands/join.test.d.ts +1 -0
- package/lib/__tests__/commands/join.test.js +274 -0
- package/lib/__tests__/commands/lint.test.d.ts +1 -0
- package/lib/__tests__/commands/lint.test.js +149 -0
- package/lib/__tests__/commands/push-region.test.d.ts +1 -0
- package/lib/__tests__/commands/push-region.test.js +55 -0
- package/lib/__tests__/commands/push.test.d.ts +1 -0
- package/lib/__tests__/commands/push.test.js +463 -0
- package/lib/__tests__/fetch-with-timeout.test.d.ts +1 -0
- package/lib/__tests__/fetch-with-timeout.test.js +44 -0
- package/lib/__tests__/fixtures/config.d.ts +21 -0
- package/lib/__tests__/fixtures/config.js +24 -0
- package/lib/__tests__/spinner.test.d.ts +1 -0
- package/lib/__tests__/spinner.test.js +43 -0
- package/lib/__tests__/utils.test.d.ts +1 -0
- package/lib/__tests__/utils.test.js +593 -0
- package/lib/__tests__/wrapper.test.d.ts +1 -0
- package/lib/__tests__/wrapper.test.js +68 -0
- package/lib/cms/api/__tests__/api-keys.test.d.ts +1 -0
- package/lib/cms/api/__tests__/api-keys.test.js +26 -0
- package/lib/cms/api/__tests__/api.client.test.d.ts +1 -0
- package/lib/cms/api/__tests__/api.client.test.js +333 -0
- package/lib/cms/api/__tests__/domains.test.d.ts +1 -0
- package/lib/cms/api/__tests__/domains.test.js +13 -0
- package/lib/cms/api/api-client.d.ts +75 -0
- package/lib/cms/api/api-client.js +225 -0
- package/lib/cms/api/api-keys.d.ts +1 -0
- package/lib/cms/api/api-keys.js +23 -0
- package/lib/cms/api/domains.d.ts +1 -0
- package/lib/cms/api/domains.js +11 -0
- package/lib/cms/api/index.d.ts +3 -0
- package/lib/cms/api/index.js +19 -0
- package/lib/cms/api/types.d.ts +102 -0
- package/lib/cms/api/types.js +2 -0
- package/lib/cms/commands/__tests__/push-status.test.d.ts +1 -0
- package/lib/cms/commands/__tests__/push-status.test.js +563 -0
- package/lib/cms/commands/__tests__/push.test.d.ts +1 -0
- package/lib/cms/commands/__tests__/push.test.js +315 -0
- package/lib/cms/commands/__tests__/utils.test.d.ts +1 -0
- package/lib/cms/commands/__tests__/utils.test.js +51 -0
- package/lib/cms/commands/push-status.d.ts +23 -0
- package/lib/cms/commands/push-status.js +206 -0
- package/lib/cms/commands/push.d.ts +28 -0
- package/lib/cms/commands/push.js +142 -0
- package/lib/cms/commands/utils.d.ts +25 -0
- package/lib/cms/commands/utils.js +46 -0
- package/lib/cms/utils.d.ts +2 -0
- package/lib/cms/utils.js +6 -0
- package/lib/commands/build-docs/index.d.ts +3 -0
- package/lib/commands/build-docs/index.js +39 -0
- package/lib/commands/build-docs/template.hbs +23 -0
- package/lib/commands/build-docs/types.d.ts +23 -0
- package/lib/commands/build-docs/types.js +2 -0
- package/lib/commands/build-docs/utils.d.ts +7 -0
- package/lib/commands/build-docs/utils.js +87 -0
- package/lib/commands/bundle.d.ts +14 -0
- package/lib/commands/bundle.js +91 -0
- package/lib/commands/eject.d.ts +9 -0
- package/lib/commands/eject.js +28 -0
- package/lib/commands/join.d.ts +11 -0
- package/lib/commands/join.js +565 -0
- package/lib/commands/lint.d.ts +13 -0
- package/lib/commands/lint.js +108 -0
- package/lib/commands/login.d.ts +9 -0
- package/lib/commands/login.js +23 -0
- package/lib/commands/preview-docs/index.d.ts +12 -0
- package/lib/commands/preview-docs/index.js +127 -0
- package/lib/commands/preview-docs/preview-server/default.hbs +24 -0
- package/lib/commands/preview-docs/preview-server/hot.js +59 -0
- package/lib/commands/preview-docs/preview-server/oauth2-redirect.html +21 -0
- package/lib/commands/preview-docs/preview-server/preview-server.d.ts +5 -0
- package/lib/commands/preview-docs/preview-server/preview-server.js +113 -0
- package/lib/commands/preview-docs/preview-server/server.d.ts +22 -0
- package/lib/commands/preview-docs/preview-server/server.js +85 -0
- package/lib/commands/preview-project/constants.d.ts +14 -0
- package/lib/commands/preview-project/constants.js +22 -0
- package/lib/commands/preview-project/index.d.ts +3 -0
- package/lib/commands/preview-project/index.js +56 -0
- package/lib/commands/preview-project/types.d.ts +10 -0
- package/lib/commands/preview-project/types.js +2 -0
- package/lib/commands/push.d.ts +44 -0
- package/lib/commands/push.js +295 -0
- package/lib/commands/split/__tests__/index.test.d.ts +1 -0
- package/lib/commands/split/__tests__/index.test.js +91 -0
- package/lib/commands/split/index.d.ts +13 -0
- package/lib/commands/split/index.js +259 -0
- package/lib/commands/split/types.d.ts +36 -0
- package/lib/commands/split/types.js +51 -0
- package/lib/commands/stats.d.ts +8 -0
- package/lib/commands/stats.js +96 -0
- package/lib/commands/translations.d.ts +7 -0
- package/lib/commands/translations.js +20 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +733 -0
- package/lib/types.d.ts +43 -0
- package/lib/types.js +5 -0
- package/lib/utils/__mocks__/miscellaneous.d.ts +43 -0
- package/lib/utils/__mocks__/miscellaneous.js +24 -0
- package/lib/utils/assert-node-version.d.ts +1 -0
- package/lib/utils/assert-node-version.js +16 -0
- package/lib/utils/fetch-with-timeout.d.ts +7 -0
- package/lib/utils/fetch-with-timeout.js +26 -0
- package/lib/utils/getCommandNameFromArgs.d.ts +2 -0
- package/lib/utils/getCommandNameFromArgs.js +6 -0
- package/lib/utils/js-utils.d.ts +5 -0
- package/lib/utils/js-utils.js +28 -0
- package/lib/utils/miscellaneous.d.ts +81 -0
- package/lib/utils/miscellaneous.js +551 -0
- package/lib/utils/platform.d.ts +16 -0
- package/lib/utils/platform.js +34 -0
- package/lib/utils/spinner.d.ts +10 -0
- package/lib/utils/spinner.js +42 -0
- package/lib/utils/update-version-notifier.d.ts +3 -0
- package/lib/utils/update-version-notifier.js +102 -0
- package/lib/wrapper.d.ts +11 -0
- package/lib/wrapper.js +65 -0
- package/package.json +69 -0
|
@@ -0,0 +1,593 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const miscellaneous_1 = require("../utils/miscellaneous");
|
|
4
|
+
const platform_1 = require("../utils/platform");
|
|
5
|
+
const openapi_core_1 = require("@redocly/openapi-core");
|
|
6
|
+
const colorette_1 = require("colorette");
|
|
7
|
+
const fs_1 = require("fs");
|
|
8
|
+
const path = require("path");
|
|
9
|
+
const process = require("process");
|
|
10
|
+
jest.mock('os');
|
|
11
|
+
jest.mock('colorette');
|
|
12
|
+
jest.mock('fs');
|
|
13
|
+
describe('isSubdir', () => {
|
|
14
|
+
it('can correctly determine if subdir', () => {
|
|
15
|
+
[
|
|
16
|
+
['/foo', '/foo', false],
|
|
17
|
+
['/foo', '/bar', false],
|
|
18
|
+
['/foo', '/foobar', false],
|
|
19
|
+
['/foo', '/foo/bar', true],
|
|
20
|
+
['/foo', '/foo/../bar', false],
|
|
21
|
+
['/foo', '/foo/./bar', true],
|
|
22
|
+
['/bar/../foo', '/foo/bar', true],
|
|
23
|
+
['/foo', './bar', false],
|
|
24
|
+
['/foo', '/foo/..bar', true],
|
|
25
|
+
].forEach(([parent, child, expectRes]) => {
|
|
26
|
+
expect((0, miscellaneous_1.isSubdir)(parent, child)).toBe(expectRes);
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
it('can correctly determine if subdir for windows-based paths', () => {
|
|
30
|
+
const os = require('os');
|
|
31
|
+
os.platform.mockImplementation(() => 'win32');
|
|
32
|
+
[
|
|
33
|
+
['C:/Foo', 'C:/Foo/Bar', true],
|
|
34
|
+
['C:\\Foo', 'C:\\Bar', false],
|
|
35
|
+
['C:\\Foo', 'D:\\Foo\\Bar', false],
|
|
36
|
+
].forEach(([parent, child, expectRes]) => {
|
|
37
|
+
expect((0, miscellaneous_1.isSubdir)(parent, child)).toBe(expectRes);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
afterEach(() => {
|
|
41
|
+
jest.resetModules();
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
describe('pathToFilename', () => {
|
|
45
|
+
it('should use correct path separator', () => {
|
|
46
|
+
const processedPath = (0, miscellaneous_1.pathToFilename)('/user/createWithList', '_');
|
|
47
|
+
expect(processedPath).toEqual('user_createWithList');
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
describe('printConfigLintTotals', () => {
|
|
51
|
+
const totalProblemsMock = {
|
|
52
|
+
errors: 1,
|
|
53
|
+
warnings: 0,
|
|
54
|
+
ignored: 0,
|
|
55
|
+
};
|
|
56
|
+
const redColoretteMocks = colorette_1.red;
|
|
57
|
+
const yellowColoretteMocks = colorette_1.yellow;
|
|
58
|
+
beforeEach(() => {
|
|
59
|
+
yellowColoretteMocks.mockImplementation((text) => text);
|
|
60
|
+
redColoretteMocks.mockImplementation((text) => text);
|
|
61
|
+
jest.spyOn(process.stderr, 'write').mockImplementation(() => true);
|
|
62
|
+
});
|
|
63
|
+
it('should print errors if such exist', () => {
|
|
64
|
+
(0, miscellaneous_1.printConfigLintTotals)(totalProblemsMock);
|
|
65
|
+
expect(process.stderr.write).toHaveBeenCalledWith('❌ Your config has 1 error.');
|
|
66
|
+
expect(redColoretteMocks).toHaveBeenCalledWith('❌ Your config has 1 error.');
|
|
67
|
+
});
|
|
68
|
+
it('should print warnign if no error', () => {
|
|
69
|
+
(0, miscellaneous_1.printConfigLintTotals)({ ...totalProblemsMock, errors: 0, warnings: 2 });
|
|
70
|
+
expect(process.stderr.write).toHaveBeenCalledWith('⚠️ Your config has 2 warnings.\n');
|
|
71
|
+
expect(yellowColoretteMocks).toHaveBeenCalledWith('⚠️ Your config has 2 warnings.\n');
|
|
72
|
+
});
|
|
73
|
+
it('should print nothing if no error and no warnings', () => {
|
|
74
|
+
const result = (0, miscellaneous_1.printConfigLintTotals)({ ...totalProblemsMock, errors: 0 });
|
|
75
|
+
expect(result).toBeUndefined();
|
|
76
|
+
expect(process.stderr.write).toHaveBeenCalledTimes(0);
|
|
77
|
+
expect(yellowColoretteMocks).toHaveBeenCalledTimes(0);
|
|
78
|
+
expect(redColoretteMocks).toHaveBeenCalledTimes(0);
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
describe('getFallbackApisOrExit', () => {
|
|
82
|
+
const redColoretteMocks = colorette_1.red;
|
|
83
|
+
const yellowColoretteMocks = colorette_1.yellow;
|
|
84
|
+
const apis = {
|
|
85
|
+
main: {
|
|
86
|
+
root: 'someFile.yaml',
|
|
87
|
+
styleguide: {},
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
const config = { apis };
|
|
91
|
+
beforeEach(() => {
|
|
92
|
+
yellowColoretteMocks.mockImplementation((text) => text);
|
|
93
|
+
redColoretteMocks.mockImplementation((text) => text);
|
|
94
|
+
jest.spyOn(process.stderr, 'write').mockImplementation(() => true);
|
|
95
|
+
jest.spyOn(process, 'exit').mockImplementation();
|
|
96
|
+
});
|
|
97
|
+
afterEach(() => {
|
|
98
|
+
jest.clearAllMocks();
|
|
99
|
+
});
|
|
100
|
+
it('should exit with error because no path provided', async () => {
|
|
101
|
+
const apisConfig = {
|
|
102
|
+
apis: {},
|
|
103
|
+
};
|
|
104
|
+
expect.assertions(1);
|
|
105
|
+
try {
|
|
106
|
+
await (0, miscellaneous_1.getFallbackApisOrExit)([''], apisConfig);
|
|
107
|
+
}
|
|
108
|
+
catch (e) {
|
|
109
|
+
expect(e.message).toEqual('Path cannot be empty.');
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
it('should error if file from config do not exist', async () => {
|
|
113
|
+
fs_1.existsSync.mockImplementationOnce(() => false);
|
|
114
|
+
expect.assertions(3);
|
|
115
|
+
try {
|
|
116
|
+
await (0, miscellaneous_1.getFallbackApisOrExit)(undefined, config);
|
|
117
|
+
}
|
|
118
|
+
catch (e) {
|
|
119
|
+
expect(process.stderr.write).toHaveBeenCalledWith('\nsomeFile.yaml does not exist or is invalid.\n\n');
|
|
120
|
+
expect(process.stderr.write).toHaveBeenCalledWith('Please provide a valid path.\n\n');
|
|
121
|
+
expect(e.message).toEqual('Please provide a valid path.');
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
it('should return valid array with results if such file exist', async () => {
|
|
125
|
+
fs_1.existsSync.mockImplementationOnce(() => true);
|
|
126
|
+
jest.spyOn(path, 'resolve').mockImplementationOnce((_, path) => path);
|
|
127
|
+
const result = await (0, miscellaneous_1.getFallbackApisOrExit)(undefined, config);
|
|
128
|
+
expect(process.stderr.write).toHaveBeenCalledTimes(0);
|
|
129
|
+
expect(process.exit).toHaveBeenCalledTimes(0);
|
|
130
|
+
expect(result).toStrictEqual([
|
|
131
|
+
{
|
|
132
|
+
alias: 'main',
|
|
133
|
+
path: 'someFile.yaml',
|
|
134
|
+
output: undefined,
|
|
135
|
+
},
|
|
136
|
+
]);
|
|
137
|
+
});
|
|
138
|
+
it('should exit with error in case if invalid path provided as args', async () => {
|
|
139
|
+
const apisConfig = {
|
|
140
|
+
apis: {},
|
|
141
|
+
};
|
|
142
|
+
fs_1.existsSync.mockImplementationOnce(() => false);
|
|
143
|
+
expect.assertions(3);
|
|
144
|
+
try {
|
|
145
|
+
await (0, miscellaneous_1.getFallbackApisOrExit)(['someFile.yaml'], apisConfig);
|
|
146
|
+
}
|
|
147
|
+
catch (e) {
|
|
148
|
+
expect(process.stderr.write).toHaveBeenCalledWith('\nsomeFile.yaml does not exist or is invalid.\n\n');
|
|
149
|
+
expect(process.stderr.write).toHaveBeenCalledWith('Please provide a valid path.\n\n');
|
|
150
|
+
expect(e.message).toEqual('Please provide a valid path.');
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
it('should exit with error in case if invalid 2 path provided as args', async () => {
|
|
154
|
+
const apisConfig = {
|
|
155
|
+
apis: {},
|
|
156
|
+
};
|
|
157
|
+
fs_1.existsSync.mockImplementationOnce(() => false);
|
|
158
|
+
expect.assertions(3);
|
|
159
|
+
try {
|
|
160
|
+
await (0, miscellaneous_1.getFallbackApisOrExit)(['someFile.yaml', 'someFile2.yaml'], apisConfig);
|
|
161
|
+
}
|
|
162
|
+
catch (e) {
|
|
163
|
+
expect(process.stderr.write).toHaveBeenCalledWith('\nsomeFile.yaml does not exist or is invalid.\n\n');
|
|
164
|
+
expect(process.stderr.write).toHaveBeenCalledWith('Please provide a valid path.\n\n');
|
|
165
|
+
expect(e.message).toEqual('Please provide a valid path.');
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
it('should exit with error if only one file exist ', async () => {
|
|
169
|
+
const apisStub = {
|
|
170
|
+
...apis,
|
|
171
|
+
notExist: {
|
|
172
|
+
root: 'notExist.yaml',
|
|
173
|
+
styleguide: {},
|
|
174
|
+
},
|
|
175
|
+
};
|
|
176
|
+
const configStub = { apis: apisStub };
|
|
177
|
+
const existSyncMock = fs_1.existsSync.mockImplementation((path) => path.endsWith('someFile.yaml'));
|
|
178
|
+
expect.assertions(4);
|
|
179
|
+
try {
|
|
180
|
+
await (0, miscellaneous_1.getFallbackApisOrExit)(undefined, configStub);
|
|
181
|
+
}
|
|
182
|
+
catch (e) {
|
|
183
|
+
expect(process.stderr.write).toHaveBeenCalledWith('\nnotExist.yaml does not exist or is invalid.\n\n');
|
|
184
|
+
expect(process.stderr.write).toHaveBeenCalledWith('Please provide a valid path.\n\n');
|
|
185
|
+
expect(process.stderr.write).toHaveBeenCalledTimes(2);
|
|
186
|
+
expect(e.message).toEqual('Please provide a valid path.');
|
|
187
|
+
}
|
|
188
|
+
existSyncMock.mockClear();
|
|
189
|
+
});
|
|
190
|
+
it('should work ok if it is url passed', async () => {
|
|
191
|
+
fs_1.existsSync.mockImplementationOnce(() => false);
|
|
192
|
+
openapi_core_1.isAbsoluteUrl.mockImplementation(() => true);
|
|
193
|
+
const apisConfig = {
|
|
194
|
+
apis: {
|
|
195
|
+
main: {
|
|
196
|
+
root: 'https://someLinkt/petstore.yaml?main',
|
|
197
|
+
styleguide: {},
|
|
198
|
+
},
|
|
199
|
+
},
|
|
200
|
+
};
|
|
201
|
+
const result = await (0, miscellaneous_1.getFallbackApisOrExit)(undefined, apisConfig);
|
|
202
|
+
expect(process.stderr.write).toHaveBeenCalledTimes(0);
|
|
203
|
+
expect(result).toStrictEqual([
|
|
204
|
+
{
|
|
205
|
+
alias: 'main',
|
|
206
|
+
path: 'https://someLinkt/petstore.yaml?main',
|
|
207
|
+
output: undefined,
|
|
208
|
+
},
|
|
209
|
+
]);
|
|
210
|
+
openapi_core_1.isAbsoluteUrl.mockReset();
|
|
211
|
+
});
|
|
212
|
+
it('should find alias by filename', async () => {
|
|
213
|
+
fs_1.existsSync.mockImplementationOnce(() => true);
|
|
214
|
+
const entry = await (0, miscellaneous_1.getFallbackApisOrExit)(['./test.yaml'], {
|
|
215
|
+
apis: {
|
|
216
|
+
main: {
|
|
217
|
+
root: 'test.yaml',
|
|
218
|
+
styleguide: {},
|
|
219
|
+
},
|
|
220
|
+
},
|
|
221
|
+
});
|
|
222
|
+
expect(entry).toEqual([{ path: './test.yaml', alias: 'main' }]);
|
|
223
|
+
});
|
|
224
|
+
it('should return apis from config with paths and outputs resolved relatively to the config location', async () => {
|
|
225
|
+
fs_1.existsSync.mockImplementationOnce(() => true);
|
|
226
|
+
const entry = await (0, miscellaneous_1.getFallbackApisOrExit)(undefined, {
|
|
227
|
+
apis: {
|
|
228
|
+
main: {
|
|
229
|
+
root: 'test.yaml',
|
|
230
|
+
output: 'output/test.yaml',
|
|
231
|
+
styleguide: {},
|
|
232
|
+
},
|
|
233
|
+
},
|
|
234
|
+
configFile: 'project-folder/redocly.yaml',
|
|
235
|
+
});
|
|
236
|
+
expect(entry).toEqual([
|
|
237
|
+
{
|
|
238
|
+
path: expect.stringMatching(/project\-folder\/test\.yaml$/),
|
|
239
|
+
output: expect.stringMatching(/project\-folder\/output\/test\.yaml$/),
|
|
240
|
+
alias: 'main',
|
|
241
|
+
},
|
|
242
|
+
]);
|
|
243
|
+
});
|
|
244
|
+
});
|
|
245
|
+
describe('langToExt', () => {
|
|
246
|
+
it.each([
|
|
247
|
+
['php', '.php'],
|
|
248
|
+
['c#', '.cs'],
|
|
249
|
+
['shell', '.sh'],
|
|
250
|
+
['curl', '.sh'],
|
|
251
|
+
['bash', '.sh'],
|
|
252
|
+
['javascript', '.js'],
|
|
253
|
+
['js', '.js'],
|
|
254
|
+
['python', '.py'],
|
|
255
|
+
['c', '.c'],
|
|
256
|
+
['c++', '.cpp'],
|
|
257
|
+
['coffeescript', '.litcoffee'],
|
|
258
|
+
['dart', '.dart'],
|
|
259
|
+
['elixir', '.ex'],
|
|
260
|
+
['go', '.go'],
|
|
261
|
+
['groovy', '.groovy'],
|
|
262
|
+
['java', '.java'],
|
|
263
|
+
['kotlin', '.kt'],
|
|
264
|
+
['objective-c', '.m'],
|
|
265
|
+
['perl', '.pl'],
|
|
266
|
+
['powershell', '.ps1'],
|
|
267
|
+
['ruby', '.rb'],
|
|
268
|
+
['rust', '.rs'],
|
|
269
|
+
['scala', '.sc'],
|
|
270
|
+
['swift', '.swift'],
|
|
271
|
+
['typescript', '.ts'],
|
|
272
|
+
['tsx', '.tsx'],
|
|
273
|
+
])('should infer file extension from lang - %s', (lang, expected) => {
|
|
274
|
+
expect((0, miscellaneous_1.langToExt)(lang)).toBe(expected);
|
|
275
|
+
});
|
|
276
|
+
it('should ignore case when inferring file extension', () => {
|
|
277
|
+
expect((0, miscellaneous_1.langToExt)('JavaScript')).toBe('.js');
|
|
278
|
+
});
|
|
279
|
+
});
|
|
280
|
+
describe('sorTopLevelKeysForOas', () => {
|
|
281
|
+
it('should sort oas3 top level keys', () => {
|
|
282
|
+
const openApi = {
|
|
283
|
+
openapi: '3.0.0',
|
|
284
|
+
components: {},
|
|
285
|
+
security: [],
|
|
286
|
+
tags: [],
|
|
287
|
+
servers: [],
|
|
288
|
+
paths: {},
|
|
289
|
+
info: {},
|
|
290
|
+
externalDocs: {},
|
|
291
|
+
webhooks: [],
|
|
292
|
+
'x-webhooks': [],
|
|
293
|
+
jsonSchemaDialect: '',
|
|
294
|
+
};
|
|
295
|
+
const orderedKeys = [
|
|
296
|
+
'openapi',
|
|
297
|
+
'info',
|
|
298
|
+
'jsonSchemaDialect',
|
|
299
|
+
'servers',
|
|
300
|
+
'security',
|
|
301
|
+
'tags',
|
|
302
|
+
'externalDocs',
|
|
303
|
+
'paths',
|
|
304
|
+
'webhooks',
|
|
305
|
+
'x-webhooks',
|
|
306
|
+
'components',
|
|
307
|
+
];
|
|
308
|
+
const result = (0, miscellaneous_1.sortTopLevelKeysForOas)(openApi);
|
|
309
|
+
Object.keys(result).forEach((key, index) => {
|
|
310
|
+
expect(key).toEqual(orderedKeys[index]);
|
|
311
|
+
});
|
|
312
|
+
});
|
|
313
|
+
it('should sort oas2 top level keys', () => {
|
|
314
|
+
const openApi = {
|
|
315
|
+
swagger: '2.0.0',
|
|
316
|
+
security: [],
|
|
317
|
+
tags: [],
|
|
318
|
+
paths: {},
|
|
319
|
+
info: {},
|
|
320
|
+
externalDocs: {},
|
|
321
|
+
host: '',
|
|
322
|
+
basePath: '',
|
|
323
|
+
securityDefinitions: [],
|
|
324
|
+
schemes: [],
|
|
325
|
+
consumes: [],
|
|
326
|
+
parameters: [],
|
|
327
|
+
produces: [],
|
|
328
|
+
definitions: [],
|
|
329
|
+
responses: [],
|
|
330
|
+
};
|
|
331
|
+
const orderedKeys = [
|
|
332
|
+
'swagger',
|
|
333
|
+
'info',
|
|
334
|
+
'host',
|
|
335
|
+
'basePath',
|
|
336
|
+
'schemes',
|
|
337
|
+
'consumes',
|
|
338
|
+
'produces',
|
|
339
|
+
'security',
|
|
340
|
+
'tags',
|
|
341
|
+
'externalDocs',
|
|
342
|
+
'paths',
|
|
343
|
+
'definitions',
|
|
344
|
+
'parameters',
|
|
345
|
+
'responses',
|
|
346
|
+
'securityDefinitions',
|
|
347
|
+
];
|
|
348
|
+
const result = (0, miscellaneous_1.sortTopLevelKeysForOas)(openApi);
|
|
349
|
+
Object.keys(result).forEach((key, index) => {
|
|
350
|
+
expect(key).toEqual(orderedKeys[index]);
|
|
351
|
+
});
|
|
352
|
+
});
|
|
353
|
+
});
|
|
354
|
+
describe('handleErrors', () => {
|
|
355
|
+
const ref = 'openapi/test.yaml';
|
|
356
|
+
const redColoretteMocks = colorette_1.red;
|
|
357
|
+
const blueColoretteMocks = colorette_1.blue;
|
|
358
|
+
beforeEach(() => {
|
|
359
|
+
jest.spyOn(process.stderr, 'write').mockImplementation(() => true);
|
|
360
|
+
jest.spyOn(process, 'exit').mockImplementation((code) => code);
|
|
361
|
+
redColoretteMocks.mockImplementation((text) => text);
|
|
362
|
+
blueColoretteMocks.mockImplementation((text) => text);
|
|
363
|
+
});
|
|
364
|
+
afterEach(() => {
|
|
365
|
+
jest.clearAllMocks();
|
|
366
|
+
});
|
|
367
|
+
it('should handle ResolveError', () => {
|
|
368
|
+
const resolveError = new openapi_core_1.ResolveError(new Error('File not found.'));
|
|
369
|
+
expect(() => (0, miscellaneous_1.handleError)(resolveError, ref)).toThrowError(miscellaneous_1.HandledError);
|
|
370
|
+
expect(redColoretteMocks).toHaveBeenCalledTimes(1);
|
|
371
|
+
expect(process.stderr.write).toHaveBeenCalledWith(`Failed to resolve API description at openapi/test.yaml:\n\n - File not found.\n\n`);
|
|
372
|
+
});
|
|
373
|
+
it('should handle YamlParseError', () => {
|
|
374
|
+
const yamlParseError = new openapi_core_1.YamlParseError(new Error('Invalid yaml.'), {});
|
|
375
|
+
expect(() => (0, miscellaneous_1.handleError)(yamlParseError, ref)).toThrowError(miscellaneous_1.HandledError);
|
|
376
|
+
expect(redColoretteMocks).toHaveBeenCalledTimes(1);
|
|
377
|
+
expect(process.stderr.write).toHaveBeenCalledWith(`Failed to parse API description at openapi/test.yaml:\n\n - Invalid yaml.\n\n`);
|
|
378
|
+
});
|
|
379
|
+
it('should handle CircularJSONNotSupportedError', () => {
|
|
380
|
+
const circularError = new miscellaneous_1.CircularJSONNotSupportedError(new Error('Circular json'));
|
|
381
|
+
expect(() => (0, miscellaneous_1.handleError)(circularError, ref)).toThrowError(miscellaneous_1.HandledError);
|
|
382
|
+
expect(process.stderr.write).toHaveBeenCalledWith(`Detected circular reference which can't be converted to JSON.\n` +
|
|
383
|
+
`Try to use ${(0, colorette_1.blue)('yaml')} output or remove ${(0, colorette_1.blue)('--dereferenced')}.\n\n`);
|
|
384
|
+
});
|
|
385
|
+
it('should handle SyntaxError', () => {
|
|
386
|
+
const testError = new SyntaxError('Unexpected identifier');
|
|
387
|
+
testError.stack = 'test stack';
|
|
388
|
+
expect(() => (0, miscellaneous_1.handleError)(testError, ref)).toThrowError(miscellaneous_1.HandledError);
|
|
389
|
+
expect(process.stderr.write).toHaveBeenCalledWith('Syntax error: Unexpected identifier test stack\n\n');
|
|
390
|
+
});
|
|
391
|
+
it('should throw unknown error', () => {
|
|
392
|
+
const testError = new Error('Test error.');
|
|
393
|
+
expect(() => (0, miscellaneous_1.handleError)(testError, ref)).toThrowError(miscellaneous_1.HandledError);
|
|
394
|
+
expect(process.stderr.write).toHaveBeenCalledWith(`Something went wrong when processing openapi/test.yaml:\n\n - Test error.\n\n`);
|
|
395
|
+
});
|
|
396
|
+
});
|
|
397
|
+
describe('checkIfRulesetExist', () => {
|
|
398
|
+
beforeEach(() => {
|
|
399
|
+
jest.spyOn(process, 'exit').mockImplementation((code) => code);
|
|
400
|
+
});
|
|
401
|
+
afterEach(() => {
|
|
402
|
+
jest.clearAllMocks();
|
|
403
|
+
});
|
|
404
|
+
it('should throw an error if rules are not provided', () => {
|
|
405
|
+
const rules = {
|
|
406
|
+
oas2: {},
|
|
407
|
+
oas3_0: {},
|
|
408
|
+
oas3_1: {},
|
|
409
|
+
async2: {},
|
|
410
|
+
async3: {},
|
|
411
|
+
arazzo1: {},
|
|
412
|
+
};
|
|
413
|
+
expect(() => (0, miscellaneous_1.checkIfRulesetExist)(rules)).toThrowError('⚠️ No rules were configured. Learn how to configure rules: https://redocly.com/docs/cli/rules/');
|
|
414
|
+
});
|
|
415
|
+
it('should not throw an error if rules are provided', () => {
|
|
416
|
+
const rules = {
|
|
417
|
+
oas2: { 'operation-4xx-response': 'error' },
|
|
418
|
+
oas3_0: {},
|
|
419
|
+
oas3_1: {},
|
|
420
|
+
};
|
|
421
|
+
(0, miscellaneous_1.checkIfRulesetExist)(rules);
|
|
422
|
+
});
|
|
423
|
+
});
|
|
424
|
+
describe('cleanColors', () => {
|
|
425
|
+
it('should remove colors from string', () => {
|
|
426
|
+
const stringWithColors = `String for ${(0, colorette_1.red)('test')}`;
|
|
427
|
+
const result = (0, miscellaneous_1.cleanColors)(stringWithColors);
|
|
428
|
+
expect(result).not.toMatch(/\x1b\[\d+m/g);
|
|
429
|
+
});
|
|
430
|
+
});
|
|
431
|
+
describe('cleanArgs', () => {
|
|
432
|
+
beforeEach(() => {
|
|
433
|
+
// @ts-ignore
|
|
434
|
+
openapi_core_1.isAbsoluteUrl = jest.requireActual('@redocly/openapi-core').isAbsoluteUrl;
|
|
435
|
+
// @ts-ignore
|
|
436
|
+
fs_1.existsSync = (value) => jest.requireActual('fs').existsSync(path.resolve(__dirname, value));
|
|
437
|
+
// @ts-ignore
|
|
438
|
+
fs_1.statSync = (value) => jest.requireActual('fs').statSync(path.resolve(__dirname, value));
|
|
439
|
+
});
|
|
440
|
+
afterEach(() => {
|
|
441
|
+
jest.clearAllMocks();
|
|
442
|
+
});
|
|
443
|
+
it('should remove potentially sensitive data from args', () => {
|
|
444
|
+
const testArgs = {
|
|
445
|
+
config: './fixtures/redocly.yaml',
|
|
446
|
+
apis: ['main@v1', 'fixtures/openapi.yaml', 'http://some.url/openapi.yaml'],
|
|
447
|
+
format: 'codeframe',
|
|
448
|
+
};
|
|
449
|
+
expect((0, miscellaneous_1.cleanArgs)(testArgs)).toEqual({
|
|
450
|
+
config: 'file-yaml',
|
|
451
|
+
apis: ['api-name@api-version', 'file-yaml', 'http://url'],
|
|
452
|
+
format: 'codeframe',
|
|
453
|
+
});
|
|
454
|
+
});
|
|
455
|
+
it('should remove potentially sensitive data from a push destination', () => {
|
|
456
|
+
const testArgs = {
|
|
457
|
+
destination: '@org/name@version',
|
|
458
|
+
};
|
|
459
|
+
expect((0, miscellaneous_1.cleanArgs)(testArgs)).toEqual({
|
|
460
|
+
destination: '@organization/api-name@api-version',
|
|
461
|
+
});
|
|
462
|
+
});
|
|
463
|
+
});
|
|
464
|
+
describe('cleanRawInput', () => {
|
|
465
|
+
beforeEach(() => {
|
|
466
|
+
// @ts-ignore
|
|
467
|
+
openapi_core_1.isAbsoluteUrl = jest.requireActual('@redocly/openapi-core').isAbsoluteUrl;
|
|
468
|
+
// @ts-ignore
|
|
469
|
+
fs_1.existsSync = (value) => jest.requireActual('fs').existsSync(path.resolve(__dirname, value));
|
|
470
|
+
// @ts-ignore
|
|
471
|
+
fs_1.statSync = (value) => jest.requireActual('fs').statSync(path.resolve(__dirname, value));
|
|
472
|
+
});
|
|
473
|
+
afterEach(() => {
|
|
474
|
+
jest.clearAllMocks();
|
|
475
|
+
});
|
|
476
|
+
it('should remove potentially sensitive data from raw CLI input', () => {
|
|
477
|
+
const rawInput = [
|
|
478
|
+
'redocly',
|
|
479
|
+
'bundle',
|
|
480
|
+
'api-name@api-version',
|
|
481
|
+
'./fixtures/openapi.yaml',
|
|
482
|
+
'http://some.url/openapi.yaml',
|
|
483
|
+
'--config=fixtures/redocly.yaml',
|
|
484
|
+
'--output',
|
|
485
|
+
'fixtures',
|
|
486
|
+
];
|
|
487
|
+
expect((0, miscellaneous_1.cleanRawInput)(rawInput)).toEqual('redocly bundle api-name@api-version file-yaml http://url --config=file-yaml --output folder');
|
|
488
|
+
});
|
|
489
|
+
it('should preserve safe data from raw CLI input', () => {
|
|
490
|
+
const rawInput = [
|
|
491
|
+
'redocly',
|
|
492
|
+
'lint',
|
|
493
|
+
'./fixtures/openapi.json',
|
|
494
|
+
'--format',
|
|
495
|
+
'stylish',
|
|
496
|
+
'--extends=minimal',
|
|
497
|
+
'--skip-rule',
|
|
498
|
+
'operation-4xx-response',
|
|
499
|
+
];
|
|
500
|
+
expect((0, miscellaneous_1.cleanRawInput)(rawInput)).toEqual('redocly lint file-json --format stylish --extends=minimal --skip-rule operation-4xx-response');
|
|
501
|
+
});
|
|
502
|
+
describe('validateFileExtension', () => {
|
|
503
|
+
it('should return current file extension', () => {
|
|
504
|
+
expect((0, miscellaneous_1.getAndValidateFileExtension)('test.json')).toEqual('json');
|
|
505
|
+
});
|
|
506
|
+
it('should return yaml and print warning if file extension does not supported', () => {
|
|
507
|
+
const stderrMock = jest.spyOn(process.stderr, 'write').mockImplementation(() => true);
|
|
508
|
+
colorette_1.yellow.mockImplementation((text) => text);
|
|
509
|
+
expect((0, miscellaneous_1.getAndValidateFileExtension)('test.xml')).toEqual('yaml');
|
|
510
|
+
expect(stderrMock).toHaveBeenCalledWith(`Unsupported file extension: xml. Using yaml.\n`);
|
|
511
|
+
});
|
|
512
|
+
});
|
|
513
|
+
});
|
|
514
|
+
describe('writeToFileByExtension', () => {
|
|
515
|
+
beforeEach(() => {
|
|
516
|
+
jest.spyOn(process.stderr, 'write').mockImplementation(jest.fn());
|
|
517
|
+
colorette_1.yellow.mockImplementation((text) => text);
|
|
518
|
+
});
|
|
519
|
+
afterEach(() => {
|
|
520
|
+
jest.restoreAllMocks();
|
|
521
|
+
});
|
|
522
|
+
it('should call stringifyYaml function', () => {
|
|
523
|
+
(0, miscellaneous_1.writeToFileByExtension)('test data', 'test.yaml');
|
|
524
|
+
expect(openapi_core_1.stringifyYaml).toHaveBeenCalledWith('test data', { noRefs: false });
|
|
525
|
+
expect(process.stderr.write).toHaveBeenCalledWith(`test data`);
|
|
526
|
+
});
|
|
527
|
+
it('should call JSON.stringify function', () => {
|
|
528
|
+
const stringifySpy = jest.spyOn(JSON, 'stringify').mockImplementation((data) => data);
|
|
529
|
+
(0, miscellaneous_1.writeToFileByExtension)('test data', 'test.json');
|
|
530
|
+
expect(stringifySpy).toHaveBeenCalledWith('test data', null, 2);
|
|
531
|
+
expect(process.stderr.write).toHaveBeenCalledWith(`test data`);
|
|
532
|
+
});
|
|
533
|
+
});
|
|
534
|
+
describe('runtime platform', () => {
|
|
535
|
+
describe('sanitizePath', () => {
|
|
536
|
+
test.each([
|
|
537
|
+
['C:\\Program Files\\App', 'C:\\Program Files\\App'],
|
|
538
|
+
['/usr/local/bin/app', '/usr/local/bin/app'],
|
|
539
|
+
['invalid|path?name*', 'invalidpathname'],
|
|
540
|
+
['', ''],
|
|
541
|
+
['<>:"|?*', ':'],
|
|
542
|
+
['C:/Program Files\\App', 'C:/Program Files\\App'],
|
|
543
|
+
['path\nname\r', 'pathname'],
|
|
544
|
+
['/usr/local; rm -rf /', '/usr/local rm -rf /'],
|
|
545
|
+
['C:\\data&& dir', 'C:\\data dir'],
|
|
546
|
+
])('should sanitize path %s to %s', (input, expected) => {
|
|
547
|
+
expect((0, platform_1.sanitizePath)(input)).toBe(expected);
|
|
548
|
+
});
|
|
549
|
+
});
|
|
550
|
+
describe('sanitizeLocale', () => {
|
|
551
|
+
test.each([
|
|
552
|
+
['en-US', 'en-US'],
|
|
553
|
+
['fr_FR', 'fr_FR'],
|
|
554
|
+
['en<>US', 'enUS'],
|
|
555
|
+
['fr@FR', 'fr@FR'],
|
|
556
|
+
['en_US@#$%', 'en_US@'],
|
|
557
|
+
[' en-US ', 'en-US'],
|
|
558
|
+
['', ''],
|
|
559
|
+
])('should sanitize locale %s to %s', (input, expected) => {
|
|
560
|
+
expect((0, platform_1.sanitizeLocale)(input)).toBe(expected);
|
|
561
|
+
});
|
|
562
|
+
});
|
|
563
|
+
describe('getPlatformSpawnArgs', () => {
|
|
564
|
+
const originalPlatform = process.platform;
|
|
565
|
+
afterEach(() => {
|
|
566
|
+
Object.defineProperty(process, 'platform', {
|
|
567
|
+
value: originalPlatform,
|
|
568
|
+
});
|
|
569
|
+
});
|
|
570
|
+
it('should return args for Windows platform', () => {
|
|
571
|
+
Object.defineProperty(process, 'platform', {
|
|
572
|
+
value: 'win32',
|
|
573
|
+
});
|
|
574
|
+
const result = (0, platform_1.getPlatformSpawnArgs)();
|
|
575
|
+
expect(result).toEqual({
|
|
576
|
+
npxExecutableName: 'npx.cmd',
|
|
577
|
+
sanitize: expect.any(Function),
|
|
578
|
+
shell: true,
|
|
579
|
+
});
|
|
580
|
+
});
|
|
581
|
+
it('should return args for non-Windows platform', () => {
|
|
582
|
+
Object.defineProperty(process, 'platform', {
|
|
583
|
+
value: 'linux',
|
|
584
|
+
});
|
|
585
|
+
const result = (0, platform_1.getPlatformSpawnArgs)();
|
|
586
|
+
expect(result).toEqual({
|
|
587
|
+
npxExecutableName: 'npx',
|
|
588
|
+
sanitize: expect.any(Function),
|
|
589
|
+
shell: false,
|
|
590
|
+
});
|
|
591
|
+
});
|
|
592
|
+
});
|
|
593
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const miscellaneous_1 = require("../utils/miscellaneous");
|
|
4
|
+
const process = require("process");
|
|
5
|
+
const wrapper_1 = require("../wrapper");
|
|
6
|
+
const lint_1 = require("../commands/lint");
|
|
7
|
+
const push_1 = require("../commands/push");
|
|
8
|
+
const openapi_core_1 = require("@redocly/openapi-core");
|
|
9
|
+
jest.mock('node-fetch');
|
|
10
|
+
jest.mock('../utils/miscellaneous', () => ({
|
|
11
|
+
sendTelemetry: jest.fn(),
|
|
12
|
+
loadConfigAndHandleErrors: jest.fn(),
|
|
13
|
+
}));
|
|
14
|
+
jest.mock('../commands/lint', () => ({
|
|
15
|
+
handleLint: jest.fn().mockImplementation(({ collectSpecData }) => {
|
|
16
|
+
collectSpecData({ openapi: '3.1.0' });
|
|
17
|
+
}),
|
|
18
|
+
lintConfigCallback: jest.fn(),
|
|
19
|
+
}));
|
|
20
|
+
describe('commandWrapper', () => {
|
|
21
|
+
it('should send telemetry if there is "telemetry: on" in the config', async () => {
|
|
22
|
+
miscellaneous_1.loadConfigAndHandleErrors.mockImplementation(() => {
|
|
23
|
+
return { telemetry: 'on', styleguide: { recommendedFallback: true } };
|
|
24
|
+
});
|
|
25
|
+
openapi_core_1.detectSpec.mockImplementationOnce(() => {
|
|
26
|
+
return 'oas3_1';
|
|
27
|
+
});
|
|
28
|
+
process.env.REDOCLY_TELEMETRY = 'on';
|
|
29
|
+
const wrappedHandler = (0, wrapper_1.commandWrapper)(lint_1.handleLint);
|
|
30
|
+
await wrappedHandler({});
|
|
31
|
+
expect(lint_1.handleLint).toHaveBeenCalledTimes(1);
|
|
32
|
+
expect(miscellaneous_1.sendTelemetry).toHaveBeenCalledTimes(1);
|
|
33
|
+
expect(miscellaneous_1.sendTelemetry).toHaveBeenCalledWith({}, 0, false, 'oas3_1', 'openapi', '3.1.0');
|
|
34
|
+
});
|
|
35
|
+
it('should not collect spec version if the file is not parsed to json', async () => {
|
|
36
|
+
miscellaneous_1.loadConfigAndHandleErrors.mockImplementation(() => {
|
|
37
|
+
return { telemetry: 'on', styleguide: { recommendedFallback: true } };
|
|
38
|
+
});
|
|
39
|
+
lint_1.handleLint.mockImplementation(({ collectSpecData }) => {
|
|
40
|
+
collectSpecData();
|
|
41
|
+
});
|
|
42
|
+
process.env.REDOCLY_TELEMETRY = 'on';
|
|
43
|
+
const wrappedHandler = (0, wrapper_1.commandWrapper)(lint_1.handleLint);
|
|
44
|
+
await wrappedHandler({});
|
|
45
|
+
expect(lint_1.handleLint).toHaveBeenCalledTimes(1);
|
|
46
|
+
expect(miscellaneous_1.sendTelemetry).toHaveBeenCalledTimes(1);
|
|
47
|
+
expect(miscellaneous_1.sendTelemetry).toHaveBeenCalledWith({}, 0, false, undefined, undefined, undefined);
|
|
48
|
+
});
|
|
49
|
+
it('should NOT send telemetry if there is "telemetry: off" in the config', async () => {
|
|
50
|
+
miscellaneous_1.loadConfigAndHandleErrors.mockImplementation(() => {
|
|
51
|
+
return { telemetry: 'off', styleguide: { recommendedFallback: true } };
|
|
52
|
+
});
|
|
53
|
+
process.env.REDOCLY_TELEMETRY = 'on';
|
|
54
|
+
const wrappedHandler = (0, wrapper_1.commandWrapper)(lint_1.handleLint);
|
|
55
|
+
await wrappedHandler({});
|
|
56
|
+
expect(lint_1.handleLint).toHaveBeenCalledTimes(1);
|
|
57
|
+
expect(miscellaneous_1.sendTelemetry).toHaveBeenCalledTimes(0);
|
|
58
|
+
});
|
|
59
|
+
it('should pass files from arguments to config', async () => {
|
|
60
|
+
const filesToPush = ['test1.yaml', 'test2.yaml'];
|
|
61
|
+
const loadConfigMock = miscellaneous_1.loadConfigAndHandleErrors;
|
|
62
|
+
const argv = {
|
|
63
|
+
files: filesToPush,
|
|
64
|
+
};
|
|
65
|
+
await (0, wrapper_1.commandWrapper)(push_1.handlePush)(argv);
|
|
66
|
+
expect(loadConfigMock).toHaveBeenCalledWith(expect.objectContaining({ files: filesToPush }));
|
|
67
|
+
});
|
|
68
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|