Skip to content

feat: disable writing a log file via a --no-log-file flag #1877

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/dull-pianos-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hey-api/openapi-ts": patch
---

feat: ability to disable writing a log file via a `--no-log-file` flag or `logs.file` = `false`
7 changes: 7 additions & 0 deletions packages/openapi-ts/bin/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ const params = program
'DEPRECATED. Manually set base in OpenAPI config instead of inferring from server value',
)
.option('-s, --silent', 'Set log level to silent')
.option(
'--no-log-file',
'Disable writing a log file. Works like --silent but without supressing console output',
)
.option(
'-w, --watch [value]',
'Regenerate the client when the input file changes?',
Expand Down Expand Up @@ -83,6 +87,7 @@ async function start() {

userConfig = processParams(params, [
'dryRun',
'logFile',
'experimentalParser',
'exportCore',
'useOptions',
Expand Down Expand Up @@ -113,6 +118,8 @@ async function start() {
userConfig.logs.level = 'silent';
}

userConfig.logs.file = userConfig.logFile;

if (typeof params.watch === 'string') {
userConfig.watch = Number.parseInt(params.watch, 10);
}
Expand Down
1 change: 1 addition & 0 deletions packages/openapi-ts/src/generate/__tests__/class.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('generateLegacyClientClass', () => {
path: '',
},
logs: {
file: true,
level: 'info',
path: process.cwd(),
},
Expand Down
3 changes: 3 additions & 0 deletions packages/openapi-ts/src/generate/__tests__/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ describe('generateLegacyCore', () => {
path: '',
},
logs: {
file: true,
level: 'info',
path: process.cwd(),
},
Expand Down Expand Up @@ -127,6 +128,7 @@ describe('generateLegacyCore', () => {
path: '',
},
logs: {
file: true,
level: 'info',
path: process.cwd(),
},
Expand Down Expand Up @@ -202,6 +204,7 @@ describe('generateLegacyCore', () => {
path: '',
},
logs: {
file: true,
level: 'info',
path: process.cwd(),
},
Expand Down
1 change: 1 addition & 0 deletions packages/openapi-ts/src/generate/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('generateIndexFile', () => {
path: '',
},
logs: {
file: true,
level: 'info',
path: process.cwd(),
},
Expand Down
1 change: 1 addition & 0 deletions packages/openapi-ts/src/generate/__tests__/output.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('generateLegacyOutput', () => {
path: '',
},
logs: {
file: true,
level: 'info',
path: process.cwd(),
},
Expand Down
1 change: 1 addition & 0 deletions packages/openapi-ts/src/getLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Config, UserConfig } from './types/config';

export const getLogs = (userConfig: UserConfig | undefined): Config['logs'] => {
let logs: Config['logs'] = {
file: true,
level: 'info',
path: process.cwd(),
};
Expand Down
3 changes: 2 additions & 1 deletion packages/openapi-ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@
} catch (error) {
const config = configs[0] as Config | undefined;
const dryRun = config ? config.dryRun : resolvedConfig?.dryRun;

Check warning on line 70 in packages/openapi-ts/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/index.ts#L70

Added line #L70 was not covered by tests
// TODO: add setting for log output
if (!dryRun) {
const logs = config?.logs ?? getLogs(resolvedConfig);
if (logs.level !== 'silent') {
if (logs.level !== 'silent' && logs.file) {

Check warning on line 74 in packages/openapi-ts/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/openapi-ts/src/index.ts#L74

Added line #L74 was not covered by tests
const logName = `openapi-ts-error-${Date.now()}.log`;
const logsDir = path.resolve(process.cwd(), logs.path ?? '');
ensureDirSync(logsDir);
Expand Down
1 change: 1 addition & 0 deletions packages/openapi-ts/src/openApi/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ vi.mock('../3.1.x', () => ({
vi.mock('../../utils/config', () => {
const config: Partial<Config> = {
logs: {
file: false,
level: 'silent',
path: '',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('generateLegacySchemas', () => {
path: '',
},
logs: {
file: true,
level: 'info',
path: process.cwd(),
},
Expand Down Expand Up @@ -109,6 +110,7 @@ describe('generateLegacySchemas', () => {
path: '',
},
logs: {
file: true,
level: 'info',
path: process.cwd(),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('handlerLegacy', () => {
path: '',
},
logs: {
file: true,
level: 'info',
path: process.cwd(),
},
Expand Down Expand Up @@ -184,6 +185,7 @@ describe('methodNameBuilder', () => {
path: '',
},
logs: {
file: true,
level: 'info',
path: process.cwd(),
},
Expand Down Expand Up @@ -266,6 +268,7 @@ describe('methodNameBuilder', () => {
path: '',
},
logs: {
file: true,
level: 'info',
path: process.cwd(),
},
Expand Down Expand Up @@ -351,6 +354,7 @@ describe('methodNameBuilder', () => {
path: '',
},
logs: {
file: true,
level: 'info',
path: process.cwd(),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('generateLegacyTypes', () => {
path: '',
},
logs: {
file: true,
level: 'info',
path: process.cwd(),
},
Expand Down
7 changes: 7 additions & 0 deletions packages/openapi-ts/src/types/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ export interface UserConfig {
logs?:
| string
| {
/**
* Whether or not error logs should be written to a file or not
*
* @default true
* */
file?: boolean;
/**
* The logging level to control the verbosity of log output.
* Determines which messages are logged based on their severity.
Expand All @@ -168,6 +174,7 @@ export interface UserConfig {
| 'silent'
| 'trace'
| 'warn';

/**
* The relative location of the logs folder
*
Expand Down
2 changes: 2 additions & 0 deletions packages/openapi-ts/src/utils/__tests__/handlebars.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe('registerHandlebarHelpers', () => {
path: '',
},
logs: {
file: true,
level: 'info',
path: process.cwd(),
},
Expand Down Expand Up @@ -84,6 +85,7 @@ describe('registerHandlebarTemplates', () => {
path: '',
},
logs: {
file: true,
level: 'info',
path: process.cwd(),
},
Expand Down
1 change: 1 addition & 0 deletions packages/openapi-ts/src/utils/__tests__/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('operationNameFn', () => {
path: '',
},
logs: {
file: true,
level: 'info',
path: process.cwd(),
},
Expand Down