Skip to content

Commit d0ec162

Browse files
authored
Merge pull request #68 from retejs/fix/node-compatibility
fix(ai): use dynamic import for markdown-to-ansi in older node.js
2 parents d728b60 + 0f1ea70 commit d0ec162

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

src/ai/guidance.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { readFileSync } from 'fs'
2-
import markdownToAnsiFactory from 'markdown-to-ansi'
32
import { join } from 'path'
43

54
import { assetsAI } from '../consts'
@@ -11,13 +10,19 @@ import { logger } from './logger'
1110
*
1211
* @returns Formatted guidance text with ANSI codes, or empty string if file doesn't exist
1312
*/
14-
function getGuidance(): string {
13+
async function getGuidance(): Promise<string> {
1514
const guidancePath = join(assetsAI, 'guidance.md')
1615

1716
try {
1817
const guidanceTemplate = readFileSync(guidancePath, 'utf-8')
1918

20-
// markdown-to-ansi exports a factory function that returns a converter function
19+
/*
20+
* Dynamic import is used because markdown-to-ansi is an ES module.
21+
* When compiled to CommonJS, static imports would be transformed to require(),
22+
* which doesn't work for ES modules in Node.js 18 and below.
23+
*/
24+
const markdownToAnsiModule = await import('markdown-to-ansi')
25+
const markdownToAnsiFactory = markdownToAnsiModule.default ?? markdownToAnsiModule
2126
const converter = markdownToAnsiFactory()
2227
// Convert markdown to ANSI-formatted terminal text (supports bold, italic, code, etc.)
2328

@@ -39,7 +44,7 @@ function getGuidance(): string {
3944
* Used for missing parameter errors to provide helpful guidance to users and AI assistants.
4045
*/
4146
export class GuidanceError extends Error {
42-
public readonly guidance: string
47+
public readonly guidance: Promise<string>
4348

4449
constructor(message: string) {
4550
super(message)

src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,10 @@ program
106106
await buildInstructions(options.tool, options.context, options.force, interactive)
107107
} catch (error) {
108108
if (error instanceof GuidanceError) {
109-
if (error.guidance) {
109+
const guidance = await error.guidance
110+
if (guidance) {
110111
console.log() // Add spacing before guidance
111-
logger.info(error.guidance)
112+
logger.info(guidance)
112113
}
113114
throwError(error.message)
114115
}

0 commit comments

Comments
 (0)