@@ -4,7 +4,8 @@ import { syncAndroidVersioning } from "./lib/android-version.ts";
|
4 | 4 | |
5 | 5 | type Mode = "check" | "write"; |
6 | 6 | |
7 | | -export function parseArgs(argv: string[]): { mode: Mode; rootDir: string } { |
| 7 | +export function parseArgs(argv: string[]): { help: boolean; mode: Mode; rootDir: string } { |
| 8 | +let help = false; |
8 | 9 | let mode: Mode = "write"; |
9 | 10 | let rootDir = path.resolve("."); |
10 | 11 | |
@@ -20,39 +21,62 @@ export function parseArgs(argv: string[]): { mode: Mode; rootDir: string } {
|
20 | 21 | break; |
21 | 22 | } |
22 | 23 | case "--root": { |
23 | | -const value = argv[index + 1]; |
24 | | -if (!value) { |
25 | | -throw new Error("Missing value for --root."); |
26 | | -} |
27 | | -rootDir = path.resolve(value); |
| 24 | +rootDir = path.resolve(readOptionValue(argv, index, "--root")); |
28 | 25 | index += 1; |
29 | 26 | break; |
30 | 27 | } |
31 | 28 | case "-h": |
32 | 29 | case "--help": { |
33 | | -console.log( |
34 | | -"Usage: node --import tsx scripts/android-sync-versioning.ts [--write|--check] [--root dir]", |
35 | | -); |
36 | | -process.exit(0); |
| 30 | +help = true; |
| 31 | +break; |
37 | 32 | } |
38 | 33 | default: { |
39 | 34 | throw new Error(`Unknown argument: ${arg}`); |
40 | 35 | } |
41 | 36 | } |
42 | 37 | } |
43 | 38 | |
44 | | -return { mode, rootDir }; |
| 39 | +return { help, mode, rootDir }; |
45 | 40 | } |
46 | 41 | |
47 | | -const options = parseArgs(process.argv.slice(2)); |
48 | | -const result = syncAndroidVersioning({ mode: options.mode, rootDir: options.rootDir }); |
| 42 | +function readOptionValue(argv: string[], index: number, flag: string): string { |
| 43 | +const value = argv[index + 1]; |
| 44 | +if (!value || value.startsWith("--")) { |
| 45 | +throw new Error(`Missing value for ${flag}.`); |
| 46 | +} |
| 47 | +return value; |
| 48 | +} |
49 | 49 | |
50 | | -if (options.mode === "check") { |
51 | | -process.stdout.write("Android versioning artifacts are up to date.\n"); |
52 | | -} else if (result.updatedPaths.length === 0) { |
53 | | -process.stdout.write("Android versioning artifacts already up to date.\n"); |
54 | | -} else { |
| 50 | +function printUsage(): void { |
55 | 51 | process.stdout.write( |
56 | | -`Updated Android versioning artifacts:\n- ${result.updatedPaths.map((filePath) => path.relative(process.cwd(), filePath)).join("\n- ")}\n`, |
| 52 | +"Usage: node --import tsx scripts/android-sync-versioning.ts [--write|--check] [--root dir]\n", |
57 | 53 | ); |
58 | 54 | } |
| 55 | + |
| 56 | +function main(argv = process.argv.slice(2)): number { |
| 57 | +const options = parseArgs(argv); |
| 58 | +if (options.help) { |
| 59 | +printUsage(); |
| 60 | +return 0; |
| 61 | +} |
| 62 | + |
| 63 | +const result = syncAndroidVersioning({ mode: options.mode, rootDir: options.rootDir }); |
| 64 | + |
| 65 | +if (options.mode === "check") { |
| 66 | +process.stdout.write("Android versioning artifacts are up to date.\n"); |
| 67 | +} else if (result.updatedPaths.length === 0) { |
| 68 | +process.stdout.write("Android versioning artifacts already up to date.\n"); |
| 69 | +} else { |
| 70 | +process.stdout.write( |
| 71 | +`Updated Android versioning artifacts:\n- ${result.updatedPaths.map((filePath) => path.relative(process.cwd(), filePath)).join("\n- ")}\n`, |
| 72 | +); |
| 73 | +} |
| 74 | +return 0; |
| 75 | +} |
| 76 | + |
| 77 | +try { |
| 78 | +process.exitCode = main(); |
| 79 | +} catch (error) { |
| 80 | +process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`); |
| 81 | +process.exitCode = 1; |
| 82 | +} |