Skip to content

Commit a1298e6

Browse files
committed
refactor: rename argument name
1 parent 59f4452 commit a1298e6

14 files changed

Lines changed: 235 additions & 81 deletions

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ pnpm i -g ff-diff
2929
$ ff-diff
3030
Usage:
3131
ff-diff clean [--keep version1,version2] [--keep-archives] [--keep-sources]
32-
ff-diff diff <old-version> <new-version> [--clean-archives] [--clean-sources] [--do-not-print-in-console] [--save-output-in-file] [--compare-userjs path] [--hide-common-changed-values] [--debug-firefox-version]
33-
ff-diff default-prefs [--do-not-print-in-console] [--save-output-in-file] [--firefox-path path] [--firefox-version version] [--debug-firefox-version]
34-
ff-diff unused-prefs-userjs path [--firefox-path path] [--force-default-profile] [--profile-path] [--firefox-version version] [--debug-firefox-version]
35-
ff-diff default-prefs-userjs path [--firefox-path path] [--do-not-print-in-console] [--save-output-in-file] [--force-default-profile] [--profile-path] [--firefox-version version] [--debug-firefox-version]
32+
ff-diff diff <old-version> <new-version> [--clean-archives] [--clean-sources] [--do-not-print-in-console] [--save-output-in-file] [--compare-userjs path] [--hide-common-changed-values] [--debug-firefox-capabilities]
33+
ff-diff default-prefs [--do-not-print-in-console] [--save-output-in-file] [--firefox-path path] [--firefox-version version] [--debug-firefox-capabilities]
34+
ff-diff unused-prefs-userjs path [--firefox-path path] [--force-default-profile] [--profile-path] [--firefox-version version] [--debug-firefox-capabilities]
35+
ff-diff default-prefs-userjs path [--firefox-path path] [--do-not-print-in-console] [--save-output-in-file] [--force-default-profile] [--profile-path] [--firefox-version version] [--debug-firefox-capabilities]
3636

3737
Options:
3838
-v, --version Print version info and exit

src/cli/index.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { diff } from "$commands/diff";
55
import { unusedPrefs } from "$commands/unused-prefs";
66
import { getArgumentValue, parseKeepArgument } from "$lib/cli";
77
import { getFirefoxReleaseProfilePath } from "$lib/firefox";
8-
import { startsWithNumberDotNumber, warnIncorrectOldVersion } from "$lib/helpers";
8+
import { exit, startsWithNumberDotNumber, warnIncorrectOldVersion } from "$lib/helpers";
99
import { styleText } from "node:util";
1010

1111
interface SourceCleanupOptions {
@@ -36,7 +36,7 @@ export const CLI_ARGS = {
3636
CLEAN_ARCHIVES: "--clean-archives",
3737
CLEAN_SOURCES: "--clean-sources",
3838
COMPARE_USERJS: "--compare-userjs",
39-
DEBUG_FIREFOX_CAPABILITIES: "--debug-firefox-version",
39+
DEBUG_FIREFOX_CAPABILITIES: "--debug-firefox-capabilities",
4040
DO_NOT_PRINT_IN_CONSOLE: "--do-not-print-in-console",
4141
FIREFOX_PATH: "--firefox-path",
4242
FIREFOX_VERSION: "--firefox-version",
@@ -109,8 +109,7 @@ const FFVersion: CliOption = {
109109
};
110110

111111
const exitWithError = (firstArg: string, secondArg: string) => {
112-
console.error(`You can't have ${firstArg} and ${secondArg} set at the same time`);
113-
process.exit(1);
112+
exit(`You can't have ${firstArg} and ${secondArg} set at the same time`);
114113
};
115114

116115
const getUserJSBasedCommands = () => {
@@ -214,10 +213,7 @@ abstract class BaseCli {
214213
}
215214

216215
if (this.fail) {
217-
if (this.error) {
218-
console.error(this.error);
219-
}
220-
process.exit(1);
216+
exit(this.error);
221217
}
222218
}
223219

@@ -355,7 +351,7 @@ class DefaultPrefsCommand extends BaseCli {
355351
if (firefoxInstallPath && firefoxVersion) {
356352
exitWithError(CLI_ARGS.FIREFOX_VERSION, CLI_ARGS.PROFILE_PATH);
357353
}
358-
const profilePath = firefoxInstallPath ?? getFirefoxReleaseProfilePath()?.profilePath;
354+
const profilePath = firefoxInstallPath ?? (await getFirefoxReleaseProfilePath())?.profilePath;
359355
if (profilePath === undefined) {
360356
this.usage();
361357
return;

src/commands/clean.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { readdir, rm } from "node:fs/promises";
22
import { join } from "node:path";
33
import { keepOptions } from "$cli";
44
import { installDir } from "$lib/install";
5+
import { exit } from "$lib/helpers";
56

67
export const clean = async (keptVersions: number[]) => {
78
try {
@@ -40,7 +41,6 @@ export const clean = async (keptVersions: number[]) => {
4041
console.log("No archives/sources has been removed");
4142
}
4243
} catch (error) {
43-
console.error("Error reading dist/ directory:", error);
44-
process.exit(1);
44+
exit(`Error reading dist/ directory: ${String(error)}`);
4545
}
4646
};

src/commands/default-prefs-userjs.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
import { defaultsUserJSDir, getPrefsFromInstalledVersion, installDir } from "$lib/install";
1212
import { Format, formatTicks, formatValue } from "$lib/format";
1313
import { parseUserPrefs } from "$lib/prefs";
14-
import { gettingPrefsMessage, gettingVersionMessage } from "$lib/helpers";
14+
import { exit, getPathType, gettingPrefsMessage, gettingVersionMessage } from "$lib/helpers";
1515
import { UserJSBasedCommands } from "$commands";
1616
import { type InspectColor, styleText } from "node:util";
1717

@@ -72,7 +72,7 @@ export const defaultPrefsUserJS = async (opts: UserJSBasedCommands) => {
7272
const userJsContent = readFileSync(opts.compareUserjs, "utf8");
7373

7474
const profilePath = opts.forceDefaultProfile
75-
? getFirefoxDefaultProfile().profilePath
75+
? (await getFirefoxDefaultProfile()).profilePath
7676
: opts.profilePath;
7777
console.log(gettingPrefsMessage);
7878
console.log(gettingVersionMessage);
@@ -129,9 +129,12 @@ export const defaultPrefsUserJS = async (opts: UserJSBasedCommands) => {
129129
if (printOptions.saveOutput) {
130130
const outputMD = generateOutput(Format.Markdown, wrongDefault, alreadyDefault);
131131
const title = `# Default in your user.js and in Firefox ${version}\n\n`;
132+
const pathType = await getPathType(defaultsUserJSDir);
132133
if (!existsSync(defaultsUserJSDir)) {
133134
console.log("creating diffs directory");
134135
mkdirSync(defaultsUserJSDir);
136+
} else if (pathType !== "directory") {
137+
exit(`there's already something here \`${defaultsUserJSDir}\``);
135138
}
136139
const diffsPath = join(defaultsUserJSDir, `default-userjs-${version}.md`);
137140
console.log(`${printOptions.doNotPrintConsole ? "" : "\n"}writing diffs to ${diffsPath}`);

src/commands/diff.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
comparePrefs,
1111
} from "$lib/firefox";
1212
import { type AllFormatted, Format, formatTicks, formatValue, type PrintDiff } from "$lib/format";
13-
import { isUnitDifferenceOne } from "$lib/helpers";
13+
import { exit, getPathType, isUnitDifferenceOne } from "$lib/helpers";
1414
import { commonChangedValuesForKeys, parseUserPrefs } from "$lib/prefs";
1515
import { diffsDir, getPrefsFromInstalledVersion, installDir } from "$lib/install";
1616
import { styleText } from "node:util";
@@ -199,17 +199,20 @@ const generateOutput = (format: Format, sections: PrintDiff[], newVersion: strin
199199
return lines;
200200
};
201201

202-
const handleOutputDiff = (sections: PrintDiff[], newVersion: string, oldVersion: string) => {
202+
const handleOutputDiff = async (sections: PrintDiff[], newVersion: string, oldVersion: string) => {
203203
if (!printOptions.doNotPrintConsole) {
204204
const outputTXT = generateOutput(Format.Text, sections, newVersion);
205205
console.log(outputTXT.join("\n"));
206206
}
207207
if (printOptions.saveOutput) {
208208
const outputMD = generateOutput(Format.Markdown, sections, newVersion);
209209
const title = `# Diffs Firefox ${oldVersion}-${newVersion}\n\n`;
210+
const pathType = await getPathType(diffsDir);
210211
if (!existsSync(diffsDir)) {
211212
console.log("creating diffs directory");
212213
mkdirSync(diffsDir);
214+
} else if (pathType !== "directory") {
215+
exit(`there's already something here \`${diffsDir}\``);
213216
}
214217
const diffsPath = join(diffsDir, `${oldVersion}-${newVersion}.md`);
215218
console.log(`\nwriting diffs to ${diffsPath}`);
@@ -219,9 +222,11 @@ const handleOutputDiff = (sections: PrintDiff[], newVersion: string, oldVersion:
219222

220223
export const diff = async (args: Diff) => {
221224
console.info(`Installing Firefox ${args.oldVersion} and ${args.newVersion} in "${installDir}"`);
222-
225+
const pathType = await getPathType(installDir);
223226
if (!existsSync(installDir)) {
224227
mkdirSync(installDir, { recursive: true });
228+
} else if (pathType !== "directory") {
229+
console.error(`there's already something here \`${installDir}\``);
225230
}
226231

227232
const versions = [args.oldVersion, args.newVersion];
@@ -260,7 +265,7 @@ export const diff = async (args: Diff) => {
260265

261266
const sections = getSections(configDiff);
262267

263-
handleOutputDiff(sections, args.newVersion, args.oldVersion);
268+
await handleOutputDiff(sections, args.newVersion, args.oldVersion);
264269

265270
if (args.compareUserJS) {
266271
handleCompareUsersJS(args.compareUserJS, configDiff, args.oldVersion, args.newVersion);

src/commands/unused-prefs.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ describe("unusedPrefs", () => {
187187
const mockDefaultProfile = { profilePath: "/default/profile" };
188188

189189
vi.mocked(readFileSync).mockReturnValue(mockUserJsContent);
190-
vi.mocked(firefox.getFirefoxDefaultProfile).mockReturnValue(mockDefaultProfile);
190+
vi.mocked(firefox.getFirefoxDefaultProfile).mockResolvedValue(mockDefaultProfile);
191191
vi.mocked(firefox.getPrefs).mockResolvedValue(mockFirefoxPrefs);
192192

193193
await unusedPrefs({

src/commands/unused-prefs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const unusedPrefs = async (opts: UserJSBasedCommands) => {
1111
const userJsContent = readFileSync(opts.compareUserjs, "utf8");
1212

1313
const profilePath = opts.forceDefaultProfile
14-
? getFirefoxDefaultProfile().profilePath
14+
? (await getFirefoxDefaultProfile()).profilePath
1515
: opts.profilePath;
1616
console.log(gettingPrefsMessage);
1717

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
hasAnyArg,
88
ALL_COMMANDS,
99
} from "$cli";
10+
import { exit } from "$lib/helpers";
1011
import { styleText } from "node:util";
1112

1213
const showVersion = (): void => {
@@ -48,8 +49,7 @@ const main = async (): Promise<void> => {
4849
};
4950

5051
const printErrorAndQuit = (message: string, err?: Error): void => {
51-
console.error(err ? `${message} ${err}` : message);
52-
process.exit(1);
52+
exit(err ? `${message} ${err}` : message);
5353
};
5454

5555
const mainWithErrorHandling = async (): Promise<void> => {

src/lib/cli.test.ts

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { describe, it, expect, vi, beforeEach } from "vitest";
22
import { getArgumentValue, getArgumentValues } from "$lib/cli";
3+
import * as helpers from "$lib/helpers";
34

4-
const expectExitError = "process.exit";
5-
const mockProcessExit = vi.spyOn(process, "exit").mockImplementation(() => {
5+
const expectExitError = `process.exit unexpectedly called with "1"`;
6+
7+
// Mock the exit function from helpers
8+
const mockExit = vi.spyOn(helpers, "exit").mockImplementation(() => {
69
throw new Error(expectExitError);
710
});
811

9-
const mockConsoleError = vi.spyOn(console, "error").mockImplementation(() => {});
10-
1112
describe("getArgumentValue", () => {
1213
beforeEach(() => {
1314
vi.resetAllMocks();
@@ -19,36 +20,34 @@ describe("getArgumentValue", () => {
1920
const result = getArgumentValue("--config");
2021

2122
expect(result).toBe("config.json");
22-
expect(mockProcessExit).not.toHaveBeenCalled();
23+
expect(mockExit).not.toHaveBeenCalled();
2324
});
2425

25-
it("should return null when argument does not exist", () => {
26+
it("should return undefined when argument does not exist", () => {
2627
process.argv = ["node", "script.js", "--other", "value"];
2728

2829
const result = getArgumentValue("--config");
2930

3031
expect(result).toBe(undefined);
31-
expect(mockProcessExit).not.toHaveBeenCalled();
32+
expect(mockExit).not.toHaveBeenCalled();
3233
});
3334

34-
it("should throw error when argument is at the end of argv", () => {
35+
it("should call exit when argument is at the end of argv", () => {
3536
process.argv = ["node", "script.js", "--config"];
3637

3738
expect(() => getArgumentValue("--config")).toThrow(expectExitError);
38-
expect(mockConsoleError).toHaveBeenCalledWith(
39+
expect(mockExit).toHaveBeenCalledWith(
3940
'Error: Argument "--config" is provided but has no value.',
4041
);
41-
expect(mockProcessExit).toHaveBeenCalledWith(1);
4242
});
4343

44-
it("should throw error when argument value starts with --", () => {
44+
it("should call exit when argument value starts with --", () => {
4545
process.argv = ["node", "script.js", "--config", "--another-flag"];
4646

4747
expect(() => getArgumentValue("--config")).toThrow(expectExitError);
48-
expect(mockConsoleError).toHaveBeenCalledWith(
48+
expect(mockExit).toHaveBeenCalledWith(
4949
'Error: Argument "--config" is provided but has no value.',
5050
);
51-
expect(mockProcessExit).toHaveBeenCalledWith(1);
5251
});
5352

5453
it("should return the correct value when multiple arguments exist", () => {
@@ -57,7 +56,7 @@ describe("getArgumentValue", () => {
5756
const result = getArgumentValue("--config");
5857

5958
expect(result).toBe("config.json");
60-
expect(mockProcessExit).not.toHaveBeenCalled();
59+
expect(mockExit).not.toHaveBeenCalled();
6160
});
6261

6362
it("should return the first occurrence when argument appears multiple times", () => {
@@ -66,7 +65,7 @@ describe("getArgumentValue", () => {
6665
const result = getArgumentValue("--config");
6766

6867
expect(result).toBe("first.json");
69-
expect(mockProcessExit).not.toHaveBeenCalled();
68+
expect(mockExit).not.toHaveBeenCalled();
7069
});
7170

7271
it("should handle arguments with special characters", () => {
@@ -75,7 +74,7 @@ describe("getArgumentValue", () => {
7574
const result = getArgumentValue("--path");
7675

7776
expect(result).toBe("/path/to/file.json");
78-
expect(mockProcessExit).not.toHaveBeenCalled();
77+
expect(mockExit).not.toHaveBeenCalled();
7978
});
8079

8180
it("should handle empty string values", () => {
@@ -84,7 +83,7 @@ describe("getArgumentValue", () => {
8483
const result = getArgumentValue("--config");
8584

8685
expect(result).toBe("");
87-
expect(mockProcessExit).not.toHaveBeenCalled();
86+
expect(mockExit).not.toHaveBeenCalled();
8887
});
8988

9089
it("should handle arguments with equals-like values", () => {
@@ -93,7 +92,7 @@ describe("getArgumentValue", () => {
9392
const result = getArgumentValue("--url");
9493

9594
expect(result).toBe("http://example.com?param=value");
96-
expect(mockProcessExit).not.toHaveBeenCalled();
95+
expect(mockExit).not.toHaveBeenCalled();
9796
});
9897

9998
it("should handle single character flags", () => {
@@ -102,17 +101,14 @@ describe("getArgumentValue", () => {
102101
const result = getArgumentValue("-v");
103102

104103
expect(result).toBe("1.0.0");
105-
expect(mockProcessExit).not.toHaveBeenCalled();
104+
expect(mockExit).not.toHaveBeenCalled();
106105
});
107106

108-
it("should throw error when single character flag has no value", () => {
107+
it("should call exit when single character flag has no value", () => {
109108
process.argv = ["node", "script.js", "", "-v"];
110109

111110
expect(() => getArgumentValue("-v")).toThrow(expectExitError);
112-
expect(mockConsoleError).toHaveBeenCalledWith(
113-
'Error: Argument "-v" is provided but has no value.',
114-
);
115-
expect(mockProcessExit).toHaveBeenCalledWith(1);
111+
expect(mockExit).toHaveBeenCalledWith('Error: Argument "-v" is provided but has no value.');
116112
});
117113
});
118114

@@ -194,8 +190,11 @@ describe("getArgumentValues", () => {
194190
expect(result).toEqual(["urgent"]);
195191
});
196192

197-
it("should handle mixed valid and invalid occurrences", () => {
193+
it("should call exit for mixed valid and invalid occurrences", () => {
198194
process.argv = ["node", "script.js", "--files", "file1.js", "--files"];
199195
expect(() => getArgumentValues("--files")).toThrow(expectExitError);
196+
expect(mockExit).toHaveBeenCalledWith(
197+
'Error: Argument "--files" is provided but has no value.',
198+
);
200199
});
201200
});

src/lib/cli.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { CLI_ARGS } from "$cli";
2+
import { exit } from "$lib/helpers";
23

34
const argumentWithoutValue = (argument: string) => {
4-
console.error(`Error: Argument "${argument}" is provided but has no value.`);
5-
process.exit(1);
5+
exit(`Error: Argument "${argument}" is provided but has no value.`);
66
};
77

88
export const getArgumentValue = (argument: string): undefined | string => {
@@ -55,8 +55,7 @@ export const parseKeepArgument = (): number[] => {
5555
const versions = args.map((value) => {
5656
const version = Number.parseInt(value, 10);
5757
if (isNaN(version)) {
58-
console.error(`Error: Invalid version '${value}' provided.`);
59-
process.exit(1);
58+
argumentWithoutValue(`Error: Invalid version '${value}' provided.`);
6059
}
6160
return version;
6261
});

0 commit comments

Comments
 (0)