Skip to content

Commit 038d5d5

Browse files
committed
[fix] create un-exists directory automatically
1 parent 528fc27 commit 038d5d5

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/index.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import {
55
SystemMessagePromptTemplate,
66
} from 'langchain/prompts';
77
import { LLMChain } from 'langchain/chains';
8-
import { existsSync, readFileSync, writeFileSync } from 'fs';
8+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
99
import { StructureType, detectStructureType, getStructure } from './structure';
1010
import { FormatterType, detectFormatterType, getFormatter } from './formatter';
1111
import { Tiktoken, TiktokenModel, encoding_for_model } from '@dqbd/tiktoken';
1212
import ora from 'ora';
1313
import languageEncoding from 'detect-file-encoding-and-language';
1414
import detectIndent from 'detect-indent';
15+
import { dirname } from 'path';
1516

1617
// Build the chat model
1718
function 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+
106117
export 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

Comments
 (0)