Skip to content

Commit 9afbf66

Browse files
author
Lubos ​
committed
fix: respect logs setting if initialization fails
1 parent 96053a5 commit 9afbf66

File tree

4 files changed

+53
-27
lines changed

4 files changed

+53
-27
lines changed

.changeset/mighty-bulldogs-kiss.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@hey-api/openapi-ts': patch
3+
---
4+
5+
fix: respect logs setting if initialization fails

packages/openapi-ts-tests/test/bin.test.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -164,23 +164,6 @@ describe('bin', () => {
164164
);
165165
});
166166

167-
it('throws error with wrong client', () => {
168-
const result = sync('node', [
169-
path.resolve(__dirname, '..', '..', 'openapi-ts', 'bin', 'index.cjs'),
170-
'--input',
171-
path.resolve(__dirname, 'spec', 'v3.json'),
172-
'--output',
173-
path.resolve(__dirname, 'generated', 'bin'),
174-
'--client',
175-
'invalid/client',
176-
'--dry-run',
177-
'true',
178-
]);
179-
expect(result.stdout.toString()).toBe('');
180-
expect(result.stderr.toString()).toContain('encountered an error');
181-
expect(result.stderr.toString()).toContain('client needs to be set');
182-
});
183-
184167
it('displays help', () => {
185168
const result = sync('node', [
186169
path.resolve(__dirname, '..', '..', 'openapi-ts', 'bin', 'index.cjs'),

packages/openapi-ts/src/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,18 @@ export const createClient = async (
3232
const resolvedConfig =
3333
typeof userConfig === 'function' ? await userConfig() : userConfig;
3434

35-
let configs: Config[] = [];
35+
const configs: Array<Config> = [];
3636

3737
try {
3838
Performance.start('createClient');
3939

4040
Performance.start('config');
41-
configs = await initConfigs(resolvedConfig);
41+
for (const result of await initConfigs(resolvedConfig)) {
42+
configs.push(result.config);
43+
if (result.errors.length) {
44+
throw result.errors[0];
45+
}
46+
}
4247
Performance.end('config');
4348

4449
Performance.start('handlebars');

packages/openapi-ts/src/initConfigs.ts

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,12 @@ const mergeConfigs = (
332332
*/
333333
export const initConfigs = async (
334334
userConfig: UserConfig | undefined,
335-
): Promise<Config[]> => {
335+
): Promise<
336+
ReadonlyArray<{
337+
config: Config;
338+
errors: ReadonlyArray<Error>;
339+
}>
340+
> => {
336341
let configurationFile: string | undefined = undefined;
337342
if (userConfig?.configFile) {
338343
const parts = userConfig.configFile.split('.');
@@ -350,7 +355,12 @@ export const initConfigs = async (
350355
? configFromFile.map((config) => mergeConfigs(config, userConfig))
351356
: [mergeConfigs(configFromFile, userConfig)];
352357

353-
return userConfigs.map((userConfig) => {
358+
const results: Array<{
359+
config: Config;
360+
errors: Array<Error>;
361+
}> = [];
362+
363+
for (const userConfig of userConfigs) {
354364
const {
355365
base,
356366
configFile = '',
@@ -362,6 +372,8 @@ export const initConfigs = async (
362372
useOptions = true,
363373
} = userConfig;
364374

375+
const errors: Array<Error> = [];
376+
365377
const logs = getLogs(userConfig);
366378

367379
if (logs.level === 'debug') {
@@ -372,13 +384,17 @@ export const initConfigs = async (
372384
const output = getOutput(userConfig);
373385

374386
if (!input.path) {
375-
throw new Error(
376-
'missing input - which OpenAPI specification should we use to generate your output?',
387+
errors.push(
388+
new Error(
389+
'missing input - which OpenAPI specification should we use to generate your output?',
390+
),
377391
);
378392
}
379393

380394
if (!output.path) {
381-
throw new Error('missing output - where should we generate your output?');
395+
errors.push(
396+
new Error('missing output - where should we generate your output?'),
397+
);
382398
}
383399

384400
if (!useOptions) {
@@ -389,8 +405,20 @@ export const initConfigs = async (
389405

390406
output.path = path.resolve(process.cwd(), output.path);
391407

408+
let plugins: Pick<Config, 'plugins' | 'pluginOrder'>;
409+
410+
try {
411+
plugins = getPlugins(userConfig);
412+
} catch (error) {
413+
errors.push(error);
414+
plugins = {
415+
pluginOrder: [],
416+
plugins: {},
417+
};
418+
}
419+
392420
const config = setConfig({
393-
...getPlugins(userConfig),
421+
...plugins,
394422
base,
395423
configFile,
396424
dryRun,
@@ -409,6 +437,11 @@ export const initConfigs = async (
409437
console.warn('config:', config);
410438
}
411439

412-
return config;
413-
});
440+
results.push({
441+
config,
442+
errors,
443+
});
444+
}
445+
446+
return results;
414447
};

0 commit comments

Comments
 (0)