Skip to content

Commit

Permalink
detect file indent automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
joyqi committed May 9, 2023
1 parent 2b51513 commit 2af13c9
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 10 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"@dqbd/tiktoken": "^1.0.7",
"deep-object-diff": "^1.1.9",
"detect-file-encoding-and-language": "^2.3.3",
"detect-indent": "^7.0.1",
"flat": "^5.0.2",
"js-yaml": "^4.1.0",
"langchain": "^0.0.70",
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions src/formatter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export type FormatterType = {

export interface Formatter {
unmarshal: (text: string) => any;
marshal: (data: any) => string;
marshal: (data: any, indent: number) => string;
}

function getFormatter<T extends FormatterType>(type: T['type']): T['formatter'] {
Expand Down Expand Up @@ -49,7 +49,8 @@ export function unmarshal<T extends FormatterType>(

export function marshal<T extends FormatterType>(
type: T['type'],
data: Parameters<T['formatter']['marshal']>[0]
data: Parameters<T['formatter']['marshal']>[0],
indent: Parameters<T['formatter']['marshal']>[1]
) {
return getFormatter(type).marshal(data);
return getFormatter(type).marshal(data, indent);
}
4 changes: 2 additions & 2 deletions src/formatter/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default class implements Formatter {
return text ? JSON.parse(text) : null;
}

marshal(data: any): string {
return JSON.stringify(data, null, 2);
marshal(data: any, indent: number): string {
return JSON.stringify(data, null, indent);
}
}
4 changes: 2 additions & 2 deletions src/formatter/yaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default class implements Formatter {
return text ? load(text) : null;
}

marshal(data: any): string {
return dump(data);
marshal(data: any, indent: number): string {
return dump(data, { indent });
}
}
9 changes: 6 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { FormatterType, unmarshal, marshal, detectFormatterType } from './format
import { TiktokenModel, encoding_for_model } from '@dqbd/tiktoken';
import ora from 'ora';
import languageEncoding from 'detect-file-encoding-and-language';
import detectIndent from 'detect-indent';

yargs(hideBin(process.argv))
.command('tc [file]', 'Translate formated file', (yargs) => {
Expand Down Expand Up @@ -140,6 +141,8 @@ export async function translate<T extends StructureType, F extends FormatterType

const srcText = readFileSync(srcFile, srcEnc);
const dstText = existsSync(dstFile) ? readFileSync(dstFile, dstEnc) : '';
const srcIndent = Math.max(2, detectIndent(srcText).amount);
const dstIndent = existsSync(dstFile) ? Math.max(2, detectIndent(dstText).amount) : srcIndent;
const src = unmarshal(format, srcText);
const dst = unmarshal(format, dstText);

Expand All @@ -155,8 +158,8 @@ export async function translate<T extends StructureType, F extends FormatterType
'Model': model,
'Format': format,
'Structure': type,
'Source File': srcFile + ` (${srcEnc})`,
'Destination File': dstFile + ` (${dstEnc})`,
'Source File': srcFile + ` (encoding:${srcEnc} indent:${srcIndent})`,
'Destination File': dstFile + ` (encoding:${dstEnc} indent:${dstIndent})`,
'Source Language': srcLang,
'Destination Language': dstLang,
'Chunk Size': chunkSize,
Expand All @@ -182,5 +185,5 @@ export async function translate<T extends StructureType, F extends FormatterType
const translatedPatch = join(type, translated);
const translatedData = merge(type, keep, translatedPatch);

writeFileSync(dstFile, marshal(format, translatedData));
writeFileSync(dstFile, marshal(format, translatedData, dstIndent));
}

0 comments on commit 2af13c9

Please sign in to comment.