@@ -5,13 +5,14 @@ import {
55 SystemMessagePromptTemplate ,
66} from 'langchain/prompts' ;
77import { LLMChain } from 'langchain/chains' ;
8- import { existsSync , readFileSync , writeFileSync } from 'fs' ;
8+ import { existsSync , mkdirSync , readFileSync , writeFileSync } from 'fs' ;
99import { StructureType , detectStructureType , getStructure } from './structure' ;
1010import { FormatterType , detectFormatterType , getFormatter } from './formatter' ;
1111import { Tiktoken , TiktokenModel , encoding_for_model } from '@dqbd/tiktoken' ;
1212import ora from 'ora' ;
1313import languageEncoding from 'detect-file-encoding-and-language' ;
1414import detectIndent from 'detect-indent' ;
15+ import { dirname } from 'path' ;
1516
1617// Build the chat model
1718function buildChain ( chat : ChatOpenAI , systemMessage : string ) : LLMChain {
@@ -103,6 +104,16 @@ function getEncModel(modelName: string): Tiktoken {
103104 return encoding_for_model ( modelName as TiktokenModel ) ;
104105}
105106
107+ function writeTo ( path : string , data : string ) {
108+ const dir = dirname ( path ) ;
109+
110+ if ( ! existsSync ( dir ) ) {
111+ mkdirSync ( dir , { recursive : true } ) ;
112+ }
113+
114+ writeFileSync ( path , data ) ;
115+ }
116+
106117export async function translate < T extends StructureType , F extends FormatterType > (
107118 type : T [ 'type' ] | 'auto' ,
108119 format : F [ 'type' ] | 'auto' ,
@@ -183,5 +194,5 @@ export async function translate<T extends StructureType, F extends FormatterType
183194 const translatedPatch = structure . join ( translated ) ;
184195 const translatedData = structure . merge ( keep , translatedPatch ) ;
185196
186- writeFileSync ( dstFile , fmt . marshal ( translatedData , dstIndent ) ) ;
197+ writeTo ( dstFile , fmt . marshal ( translatedData , dstIndent ) ) ;
187198}
0 commit comments