From 038d5d5b4245a5c29dd63653fb1a2a0b5332a882 Mon Sep 17 00:00:00 2001 From: joyqi Date: Tue, 12 Sep 2023 16:58:15 +0800 Subject: [PATCH] [fix] create un-exists directory automatically --- src/index.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 7792fac..74b9a65 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,13 +5,14 @@ import { SystemMessagePromptTemplate, } from 'langchain/prompts'; import { LLMChain } from 'langchain/chains'; -import { existsSync, readFileSync, writeFileSync } from 'fs'; +import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs'; import { StructureType, detectStructureType, getStructure } from './structure'; import { FormatterType, detectFormatterType, getFormatter } from './formatter'; import { Tiktoken, TiktokenModel, encoding_for_model } from '@dqbd/tiktoken'; import ora from 'ora'; import languageEncoding from 'detect-file-encoding-and-language'; import detectIndent from 'detect-indent'; +import { dirname } from 'path'; // Build the chat model function buildChain(chat: ChatOpenAI, systemMessage: string): LLMChain { @@ -103,6 +104,16 @@ function getEncModel(modelName: string): Tiktoken { return encoding_for_model(modelName as TiktokenModel); } +function writeTo(path: string, data: string) { + const dir = dirname(path); + + if (!existsSync(dir)) { + mkdirSync(dir, { recursive: true }); + } + + writeFileSync(path, data); +} + export async function translate( type: T['type'] | 'auto', format: F['type'] | 'auto', @@ -183,5 +194,5 @@ export async function translate