diff --git a/src/bin.ts b/src/bin.ts index 5401d44..2a78b45 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -37,6 +37,11 @@ import type { Context, ImportSheetConfig } from "./types"; describe: "Prefix to use for keys.", type: "string", }) + .option("filenamePrefix", { + default: "", + describe: "Prefix to use for filenames.", + type: "string", + }) .option("envFile", { alias: "e", default: ".env", diff --git a/src/importSheet.spec.ts b/src/importSheet.spec.ts index 7c94ab8..67e5732 100644 --- a/src/importSheet.spec.ts +++ b/src/importSheet.spec.ts @@ -88,4 +88,19 @@ describe("importSheet", () => { stream.emit("end"); }).rejects.toBeInstanceOf(Error); }); + + it("prepends filename with a prefix if one is passed", async () => { + expect.assertions(1); + getMock.mockResolvedValue("key,nl_NL,en_GB\nmy_translation,woord,word\n"); + + await importSheet(createMockContext({ filenamePrefix: "prefix_" })); + + expect(writeFileSpy).toHaveBeenNthCalledWith( + 1, + "/dist/prefix_nl_NL.json", + JSON.stringify({ my_translation: "woord", lang: "nl_NL" }, null, 2) + + "\n", + { encoding: "utf-8" }, + ); + }); }); diff --git a/src/importSheet.ts b/src/importSheet.ts index cd25145..3c2aab2 100644 --- a/src/importSheet.ts +++ b/src/importSheet.ts @@ -33,9 +33,13 @@ export async function importSheet(context: Context): Promise { await Promise.all( columns.map((record) => { - const { key, records } = formatRecords(record, keys, context); + const { key: filename, records } = formatRecords(record, keys, context); - return writeJsonFile(key, records, context); + return writeJsonFile( + (config.filenamePrefix ?? "") + filename, + records, + context, + ); }), ); diff --git a/src/types.ts b/src/types.ts index beaf702..7814814 100644 --- a/src/types.ts +++ b/src/types.ts @@ -21,6 +21,7 @@ export interface ImportSheetConfig { columnKey?: string; outputDir?: string; prefix?: string; + filenamePrefix?: string; sheetId?: number | string; } diff --git a/src/utils/createConfig.ts b/src/utils/createConfig.ts index e00b441..fba180d 100644 --- a/src/utils/createConfig.ts +++ b/src/utils/createConfig.ts @@ -11,6 +11,7 @@ export function createConfig( sheetId: 0, outputDir: "translations", prefix: "", + filenamePrefix: "", ...userConfig, }; } diff --git a/src/utils/writeJsonFile.ts b/src/utils/writeJsonFile.ts index de6f197..9919b5f 100644 --- a/src/utils/writeJsonFile.ts +++ b/src/utils/writeJsonFile.ts @@ -5,11 +5,11 @@ import { VerbosityLevel } from "../types"; import chalk from "chalk"; export const writeJsonFile = async ( - key: string, + filename: string, records: Record, { config, debug, verbosity }: Context, ): Promise => { - const filePath = path.resolve(config.outputDir, `${key}.json`); + const filePath = path.resolve(config.outputDir, `${filename}.json`); const jsonData = JSON.stringify(records, null, 2); const folder = filePath.replace(path.basename(filePath), ""); @@ -19,7 +19,7 @@ export const writeJsonFile = async ( if (verbosity >= VerbosityLevel.Info) { debug( chalk.green( - `Wrote file for key ${chalk.cyan(key)} to ${chalk.yellow( + `Wrote file for key ${chalk.cyan(filename)} to ${chalk.yellow( path.relative(process.cwd(), filePath), )}`, ),