Skip to content

Commit

Permalink
fix(cz-git,cli): node v12 v14 startup error (#200)
Browse files Browse the repository at this point in the history
1. node-fetch using dynamic import. ensure node >= v12.20 can be work
2. AI mode (need use node-fetch) add require node version >= 16.5 alert
  • Loading branch information
Zhengqbbb authored Nov 5, 2024
1 parent d3ee198 commit a760f62
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/recipes/openai.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# OpenAI <Badge type="info" text="`gpt-4o-mini` Model「default」" />
# OpenAI <Badge type="info" text="`gpt-4o-mini` Model「default」" /><Badge type="tip" text="Node.js >= 16.5.0+" />
Let the AI generate your **git commit message** subject <sup>(short description)</sup>

![demo-gif](https://user-images.githubusercontent.com/40693636/219867044-3ca9823d-9294-4e02-9a5b-624578844168.gif) <!-- size=720x309 -->
Expand Down
2 changes: 1 addition & 1 deletion docs/zh/recipes/openai.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
outline: [2, 3]
---

# OpenAI <Badge type="info" text="`gpt-4o-mini` 模型 「默认」" />
# OpenAI <Badge type="info" text="`gpt-4o-mini` 模型 「默认」" /><Badge type="tip" text="Node.js >= 16.5.0+" />

让 AI 生成你的 git commit 提交信息简短描述

Expand Down
10 changes: 7 additions & 3 deletions packages/cz-git/src/generator/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import url from 'node:url'
import process from 'node:process'
import { style } from '@cz-git/inquirer'
import HttpsProxyAgent from 'https-proxy-agent'
import fetch from 'node-fetch-cjs'
import { log } from '../shared'
import { isNodeVersionInRange, log } from '../shared'
import type { CommitizenGitOptions } from '../shared'

export async function fetchOpenAIMessage(options: CommitizenGitOptions, prompt: string) {
Expand All @@ -23,7 +22,9 @@ export async function fetchOpenAIMessage(options: CommitizenGitOptions, prompt:
agent = new HttpsProxyAgent(proxyUrl)
agent.path = agent?.pathname
}

try {
const { default: fetch } = await import('node-fetch-cjs')
const response = await fetch(aiContext.url, {
agent,
headers: {
Expand All @@ -32,7 +33,7 @@ export async function fetchOpenAIMessage(options: CommitizenGitOptions, prompt:
},
method: 'POST',
body: JSON.stringify(aiContext.payload),
signal: AbortSignal.timeout(10 * 1000),
signal: isNodeVersionInRange(18) ? AbortSignal?.timeout(10 * 1000) : undefined,
})

if (
Expand All @@ -59,6 +60,9 @@ export async function fetchOpenAIMessage(options: CommitizenGitOptions, prompt:
if (err.type === 'request-timeout')
errorMsg += `. ${style.bold(style.underline('Request Timeout'))} \n${style.yellow('[tip]>>>: If your country is unable to request the OpenAI API.\nCLI support for using http proxy like \`http_proxy\`, \`all_proxy\`.\nOr setup proxy e.g')} ${style.cyan('\`npx czg --api-proxy="http://127.0.0.1:1080"\`')}`

if (!isNodeVersionInRange(16, 5))
errorMsg = 'Node.js version >= v16.5.0 is required'

log('err', errorMsg)
throw new Error(err.message)
}
Expand Down
11 changes: 11 additions & 0 deletions packages/cz-git/src/shared/utils/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* @license MIT
*/

import process from 'node:process'
import { style } from '@cz-git/inquirer'
import type { Answers, CommitizenGitOptions, Option, ScopesType } from '../types'

Expand Down Expand Up @@ -237,3 +238,13 @@ export function previewMessage(msg: string, confirmColorize = false) {
: '###--------------------------------------------------------###'
console.info(`\n${SEP}\n${msg}\n${SEP}\n`)
}

export function isNodeVersionInRange(pMajor = 12, pMinor?: number) {
if (!process.version.startsWith('v'))
return false
const major = process.version.split('.')[0].slice(1)
const minor = process.version.split('.')[1]
return !pMinor
? Number(major) >= pMajor
: Number(major) >= pMajor && Number(minor) >= pMinor
}
6 changes: 2 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
{
"compilerOptions": {
"target": "es2017",
"target": "ES2017",
"jsx": "preserve",
"lib": [
"esnext"
],
"lib": ["ES2017"],
"module": "commonjs",
"moduleResolution": "node",
"resolveJsonModule": true,
Expand Down

0 comments on commit a760f62

Please sign in to comment.