Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(status): add failOnChanges option #3

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/commands/status.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { exit } from "node:process";
import type { I18nCliConfig } from "@/types/cli.js";
import type { Languages } from "@/types/language.js";
import { computeTranslationFolderRequiredChanges } from "@/utils/compute-translation-folder-required-changes.js";
Expand All @@ -9,6 +10,7 @@ import chalk from "chalk";
export type StatusCommandOptions = {
verbose?: boolean;
veryVerbose?: boolean;
failOnChanges?: boolean;
};

export async function statusCommand(config: I18nCliConfig, options: StatusCommandOptions) {
Expand Down Expand Up @@ -47,4 +49,9 @@ export async function statusCommand(config: I18nCliConfig, options: StatusComman
)}`} ${`${chalk.red(`- ${tokenPathsToDeleteMap.size}`)}`}`,
);
}

if (options.failOnChanges) {
const changesCount = tokenPathsToCreateMap.size + tokenPathsToDeleteMap.size;
if (changesCount) exit(1);
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export function createI18nCli(params: CreateI18nCliParams) {
"-vv, --very-verbose",
"Show the tokens that require changes and the associated languages",
)
.option("-foc, --fail-on-changes", "Fail if there are tokens that require changes")
.action((options) => {
statusCommand(config, options);
});
Expand Down
26 changes: 26 additions & 0 deletions tests/commands/status.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { exit } from "node:process";
import { type StatusCommandOptions, statusCommand } from "@/commands/status.js";
import type { CreateI18nCliParams } from "@/index.js";
import type { TranslationFolder } from "@/types/translation.js";
Expand All @@ -10,6 +11,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("@/utils/create-translation-folder.js");
vi.mock("@/utils/compute-translation-folder-required-changes.js");
vi.mock("@/utils/group-translation-folder-required-changes-by-token-path.js");
vi.mock("node:process");

const config: CreateI18nCliParams = {
i18nFolderAbsolutePath: "/path/to/translations",
Expand Down Expand Up @@ -109,4 +111,28 @@ describe(statusCommand.name, () => {
[chalk.red("- inner.deepest.deep in [es]")],
]);
});

it("exits with code 1 if failOnChanges is set and there are changes", async () => {
const logger = { log: vi.fn() };
const options: StatusCommandOptions = { failOnChanges: true };

await statusCommand({ ...config, logger }, options);

expect(exit).toHaveBeenCalledWith(1);
});

it("does not exit with code 1 if failOnChanges is set and there are no changes", async () => {
const logger = { log: vi.fn() };
const options: StatusCommandOptions = { failOnChanges: true };

vi.mocked(computeTranslationFolderRequiredChanges).mockReturnValue(new Map());
vi.mocked(groupTranslationFolderRequiredChangesByTokenPath).mockReturnValue({
tokenPathsToCreateMap: new Map(),
tokenPathsToDeleteMap: new Map(),
});

await statusCommand({ ...config, logger }, options);

expect(exit).not.toHaveBeenCalled();
});
});
Loading