Skip to content

Commit b0529c2

Browse files
refactor: use stderr for all output except for the output for programmatic use (#2033)
1 parent 04baaec commit b0529c2

File tree

39 files changed

+223
-252
lines changed

39 files changed

+223
-252
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
12
Found nested/redocly.yaml and using theme.openapi options
23
Prerendering docs
34

45
🎉 bundled successfully in: nested/redoc-static.html (36 KiB) [⏱ <test>ms].
5-
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
12
Found undefined and using theme.openapi options
23
Prerendering docs
34

45
🎉 bundled successfully in: redoc-static.html (330 KiB) [⏱ <test>ms].
5-
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
2-
Document: openapi.yaml stats:
3-
41
🚗 References: 3
52
📦 External Documents: 0
63
📈 Schemas: 1
@@ -11,5 +8,8 @@ Document: openapi.yaml stats:
118
👷 Operations: 2
129
🔖 Tags: 0
1310

11+
Document: openapi.yaml stats:
12+
13+
1414
openapi.yaml: stats processed in <test>ms
1515

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
2-
Document: museum.yaml stats:
3-
41
🚗 References: 43
52
📦 External Documents: 0
63
📈 Schemas: 23
@@ -11,5 +8,8 @@ Document: museum.yaml stats:
118
👷 Operations: 8
129
🔖 Tags: 3
1310

11+
Document: museum.yaml stats:
12+
13+
1414
museum.yaml: stats processed in <test>ms
1515

packages/cli/src/__tests__/commands/bundle.test.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { bundle, getTotals, getMergedConfig, Config } from '@redocly/openapi-core';
1+
import { bundle, getTotals, getMergedConfig, Config, logger } from '@redocly/openapi-core';
22

33
import { BundleOptions, handleBundle } from '../../commands/bundle.js';
44
import {
@@ -17,16 +17,12 @@ import { type Arguments } from 'yargs';
1717
describe('bundle', () => {
1818
let processExitMock: MockInstance;
1919
let exitCb: any;
20-
let stderrWriteMock: any;
21-
let stdoutWriteMock: any;
2220
beforeEach(async () => {
2321
processExitMock = vi.spyOn(process, 'exit').mockImplementation(vi.fn() as any);
2422
vi.spyOn(process, 'once').mockImplementation((_e, cb) => {
2523
exitCb = cb;
2624
return process.on(_e, cb);
2725
});
28-
stderrWriteMock = vi.spyOn(process.stderr, 'write').mockImplementation(vi.fn());
29-
stdoutWriteMock = vi.spyOn(process.stdout, 'write').mockImplementation(vi.fn());
3026

3127
vi.mock('@redocly/openapi-core');
3228
vi.mocked(bundle).mockImplementation(
@@ -209,7 +205,7 @@ describe('bundle', () => {
209205

210206
expect(saveBundle).toBeCalledTimes(1);
211207
expect(saveBundle).toHaveBeenCalledWith('output/foo.yaml', expect.any(String));
212-
expect(process.stdout.write).toHaveBeenCalledTimes(1);
208+
expect(logger.output).toHaveBeenCalledTimes(1);
213209
});
214210

215211
it('should NOT store bundled API descriptions in the output files described in the apis section of config IF there is a positional api provided', async () => {
@@ -241,7 +237,7 @@ describe('bundle', () => {
241237
});
242238

243239
expect(saveBundle).toBeCalledTimes(0);
244-
expect(process.stdout.write).toHaveBeenCalledTimes(1);
240+
expect(logger.output).toHaveBeenCalledTimes(1);
245241
});
246242

247243
it('should store bundled API descriptions in the directory specified in argv IF multiple positional apis provided AND --output specified', async () => {

packages/cli/src/__tests__/commands/lint.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
getTotals,
66
formatProblems,
77
doesYamlFileExist,
8+
logger,
89
type Totals,
910
} from '@redocly/openapi-core';
1011
import {
@@ -183,7 +184,7 @@ describe('handleLint', () => {
183184
};
184185
});
185186
await commandWrapper(handleLint)(argvMock);
186-
expect(process.stderr.write).toHaveBeenCalledWith(
187+
expect(logger.info).toHaveBeenCalledWith(
187188
`No configurations were provided -- using built in ${blue(
188189
'recommended'
189190
)} configuration by default.\n\n`

packages/cli/src/__tests__/spinner.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import { Spinner } from '../utils/spinner.js';
33
import * as process from 'node:process';
44

55
describe('Spinner', () => {
6-
const IS_TTY = process.stdout.isTTY;
6+
const IS_TTY = process.stderr.isTTY;
77

88
let writeMock: MockInstance;
99
let spinner: Spinner;
1010

1111
beforeEach(() => {
1212
vi.useFakeTimers();
13-
process.stdout.isTTY = true;
14-
writeMock = vi.spyOn(process.stdout, 'write').mockImplementation(vi.fn());
13+
process.stderr.isTTY = true;
14+
writeMock = vi.spyOn(process.stderr, 'write').mockImplementation(vi.fn());
1515
spinner = new Spinner();
1616
});
1717

@@ -20,7 +20,7 @@ describe('Spinner', () => {
2020
});
2121

2222
afterAll(() => {
23-
process.stdout.isTTY = IS_TTY;
23+
process.stderr.isTTY = IS_TTY;
2424
});
2525

2626
it('starts the spinner', () => {
@@ -42,7 +42,7 @@ describe('Spinner', () => {
4242
});
4343

4444
it('should call write 1 times if CI set to true', () => {
45-
process.stdout.isTTY = false;
45+
process.stderr.isTTY = false;
4646
spinner.start('Loading');
4747
vi.advanceTimersByTime(300);
4848
expect(writeMock).toHaveBeenCalledTimes(1);

packages/cli/src/__tests__/utils.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,8 @@ describe('checkIfRulesetExist', () => {
462462
});
463463

464464
it('should throw an error if rules are not provided', () => {
465+
vi.mocked(red).mockImplementation((text) => text as string);
466+
465467
const rules = {
466468
oas2: {},
467469
oas3_0: {},

packages/cli/src/auth/device-flow.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { blue, green } from 'colorette';
22
import * as childProcess from 'node:child_process';
3+
import { logger } from '@redocly/openapi-core';
34
import { ReuniteApiClient } from '../reunite/api/api-client.js';
45

56
export type AuthToken = {
@@ -18,17 +19,17 @@ export class RedoclyOAuthDeviceFlow {
1819

1920
async run() {
2021
const code = await this.getDeviceCode();
21-
process.stdout.write(
22+
logger.output(
2223
'Attempting to automatically open the SSO authorization page in your default browser.\n'
2324
);
24-
process.stdout.write(
25+
logger.output(
2526
'If the browser does not open or you wish to use a different device to authorize this request, open the following URL:\n\n'
2627
);
27-
process.stdout.write(blue(code.verificationUri));
28-
process.stdout.write(`\n\n`);
29-
process.stdout.write(`Then enter the code:\n\n`);
30-
process.stdout.write(blue(code.userCode));
31-
process.stdout.write(`\n\n`);
28+
logger.output(blue(code.verificationUri));
29+
logger.output(`\n\n`);
30+
logger.output(`Then enter the code:\n\n`);
31+
logger.output(blue(code.userCode));
32+
logger.output(`\n\n`);
3233

3334
this.openBrowser(code.verificationUriComplete);
3435

@@ -37,7 +38,7 @@ export class RedoclyOAuthDeviceFlow {
3738
code.interval,
3839
code.expiresIn
3940
);
40-
process.stdout.write(green('✅ Logged in\n\n'));
41+
logger.output(green('✅ Logged in\n\n'));
4142

4243
return accessToken;
4344
}

packages/cli/src/auth/oauth-client.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as path from 'node:path';
33
import { mkdirSync, existsSync, writeFileSync, readFileSync, rmSync } from 'node:fs';
44
import * as crypto from 'node:crypto';
55
import { Buffer } from 'node:buffer';
6+
import { logger } from '@redocly/openapi-core';
67
import { type AuthToken, RedoclyOAuthDeviceFlow } from './device-flow.js';
78

89
const SALT = '4618dbc9-8aed-4e27-aaf0-225f4603e5a4';
@@ -88,7 +89,7 @@ export class RedoclyOAuthClient {
8889
this.cipher.update(JSON.stringify(token), 'utf8', 'hex') + this.cipher.final('hex');
8990
writeFileSync(path.join(this.dir, 'auth.json'), encrypted);
9091
} catch (error) {
91-
process.stderr.write('Error saving tokens:', error);
92+
logger.error(`Error saving tokens: ${error}`);
9293
}
9394
}
9495

0 commit comments

Comments
 (0)