Skip to content

Commit a382ae6

Browse files
authored
Merge pull request #2335 from hey-api/chore/check-node-version
chore: check Node version
2 parents 6814224 + f35fad9 commit a382ae6

File tree

6 files changed

+70
-8
lines changed

6 files changed

+70
-8
lines changed

packages/openapi-ts/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
},
102102
"devDependencies": {
103103
"@config/vite-base": "workspace:*",
104+
"@types/bun": "1.2.19",
104105
"@types/cross-spawn": "6.0.6",
105106
"@types/express": "4.17.21",
106107
"axios": "1.8.2",
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { ConfigError } from '../error';
2+
3+
export const checkNodeVersion = () => {
4+
if (typeof Bun !== 'undefined') {
5+
const [major] = Bun.version.split('.').map(Number);
6+
if (major! < 1) {
7+
throw new ConfigError(
8+
`Unsupported Bun version ${Bun.version}. Please use Bun 1.0.0 or newer.`,
9+
);
10+
}
11+
} else if (typeof process !== 'undefined' && process.versions?.node) {
12+
const [major] = process.versions.node.split('.').map(Number);
13+
if (major! < 18) {
14+
throw new ConfigError(
15+
`Unsupported Node version ${process.versions.node}. Please use Node 18 or newer.`,
16+
);
17+
}
18+
}
19+
};

packages/openapi-ts/src/config/init.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import path from 'node:path';
22

33
import { loadConfig } from 'c12';
44

5+
import { ConfigError } from '../error';
56
import type { Config, UserConfig } from '../types/config';
67
import { isLegacyClient, setConfig } from '../utils/config';
78
import { getInput } from './input';
@@ -70,15 +71,17 @@ export const initConfigs = async (
7071

7172
if (!input.path) {
7273
errors.push(
73-
new Error(
74+
new ConfigError(
7475
'missing input - which OpenAPI specification should we use to generate your output?',
7576
),
7677
);
7778
}
7879

7980
if (!output.path) {
8081
errors.push(
81-
new Error('missing output - where should we generate your output?'),
82+
new ConfigError(
83+
'missing output - where should we generate your output?',
84+
),
8285
);
8386
}
8487

packages/openapi-ts/src/error.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { ensureDirSync } from './generate/utils';
88

99
export const isInteractive = process.stdin.isTTY && process.stdout.isTTY;
1010

11+
export class ConfigError extends Error {}
12+
1113
export class HeyApiError extends Error {
1214
args: ReadonlyArray<unknown>;
1315
event: string;
@@ -38,7 +40,14 @@ export class HeyApiError extends Error {
3840
}
3941
}
4042

41-
export const logCrashReport = (error: unknown, logsDir: string): string => {
43+
export const logCrashReport = (
44+
error: unknown,
45+
logsDir: string,
46+
): string | undefined => {
47+
if (error instanceof ConfigError) {
48+
return;
49+
}
50+
4251
const logName = `openapi-ts-error-${Date.now()}.log`;
4352
const fullDir = path.resolve(process.cwd(), logsDir);
4453
ensureDirSync(fullDir);
@@ -138,18 +147,23 @@ export const printCrashReport = ({
138147
`\n\n${colors.red('❗️ Error:')} ${colors.white(typeof error === 'string' ? error : error instanceof Error ? error.message : 'Unknown error')}` +
139148
(logPath
140149
? `\n\n${colors.cyan('📄 Crash log saved to:')} ${colors.gray(logPath)}`
141-
: ''),
150+
: '') +
151+
'\n',
142152
);
143153
};
144154

145-
export const shouldReportCrash = async (): Promise<boolean> => {
146-
if (!isInteractive) {
155+
export const shouldReportCrash = async ({
156+
error,
157+
}: {
158+
error: unknown;
159+
}): Promise<boolean> => {
160+
if (!isInteractive || error instanceof ConfigError) {
147161
return false;
148162
}
149163

150164
return new Promise((resolve) => {
151165
process.stdout.write(
152-
`${colors.yellow('\n\n📢 Open a GitHub issue with crash details?')} ${colors.yellow('(y/N):')}`,
166+
`${colors.yellow('\n📢 Open a GitHub issue with crash details?')} ${colors.yellow('(y/N):')}`,
153167
);
154168
process.stdin.setEncoding('utf8');
155169
process.stdin.once('data', (data: string) => {

packages/openapi-ts/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import colors from 'ansi-colors';
22
// @ts-expect-error
33
import colorSupport from 'color-support';
44

5+
import { checkNodeVersion } from './config/engine';
56
import { initConfigs } from './config/init';
67
import { getLogs } from './config/logs';
78
import { createClient as pCreateClient } from './createClient';
@@ -35,6 +36,8 @@ export const createClient = async (
3536
const configs: Array<Config> = [];
3637

3738
try {
39+
checkNodeVersion();
40+
3841
Performance.start('createClient');
3942

4043
Performance.start('config');
@@ -90,7 +93,7 @@ export const createClient = async (
9093

9194
if (logs.level !== 'silent') {
9295
printCrashReport({ error, logPath });
93-
if (await shouldReportCrash()) {
96+
if (await shouldReportCrash({ error })) {
9497
await openGitHubIssueWithCrashReport(error);
9598
}
9699
}

pnpm-lock.yaml

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)