Skip to content

Commit

Permalink
[fix] create un-exists directory automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
joyqi committed Sep 12, 2023
1 parent 528fc27 commit 038d5d5
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<T extends StructureType, F extends FormatterType>(
type: T['type'] | 'auto',
format: F['type'] | 'auto',
Expand Down Expand Up @@ -183,5 +194,5 @@ export async function translate<T extends StructureType, F extends FormatterType
const translatedPatch = structure.join(translated);
const translatedData = structure.merge(keep, translatedPatch);

writeFileSync(dstFile, fmt.marshal(translatedData, dstIndent));
writeTo(dstFile, fmt.marshal(translatedData, dstIndent));
}

0 comments on commit 038d5d5

Please sign in to comment.