Skip to content

Commit

Permalink
feat: allow adding prefix to filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Dec 7, 2023
1 parent 58c468d commit 8b52786
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
15 changes: 15 additions & 0 deletions src/importSheet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
);
});
});
8 changes: 6 additions & 2 deletions src/importSheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ export async function importSheet(context: Context): Promise<void> {

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,
);
}),
);

Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface ImportSheetConfig {
columnKey?: string;
outputDir?: string;
prefix?: string;
filenamePrefix?: string;
sheetId?: number | string;
}

Expand Down
1 change: 1 addition & 0 deletions src/utils/createConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export function createConfig(
sheetId: 0,
outputDir: "translations",
prefix: "",
filenamePrefix: "",
...userConfig,
};
}
6 changes: 3 additions & 3 deletions src/utils/writeJsonFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { VerbosityLevel } from "../types";
import chalk from "chalk";

export const writeJsonFile = async (
key: string,
filename: string,
records: Record<string, string>,
{ config, debug, verbosity }: Context,
): Promise<void> => {
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), "");

Expand All @@ -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),
)}`,
),
Expand Down

0 comments on commit 8b52786

Please sign in to comment.