diff --git a/scripts/android-sync-versioning.ts b/scripts/android-sync-versioning.ts index 035bb4f6cdd2..e854628bc772 100644 --- a/scripts/android-sync-versioning.ts +++ b/scripts/android-sync-versioning.ts @@ -1,51 +1,9 @@ // Android Sync Versioning script supports OpenClaw repository automation. import path from "node:path"; import { syncAndroidVersioning } from "./lib/android-version.ts"; +import { parseVersionSyncArgs } from "./lib/version-script-args.ts"; -type Mode = "check" | "write"; - -export function parseArgs(argv: string[]): { help: boolean; mode: Mode; rootDir: string } { - let help = false; - let mode: Mode = "write"; - let rootDir = path.resolve("."); - - for (let index = 0; index < argv.length; index += 1) { - const arg = argv[index]; - switch (arg) { - case "--check": { - mode = "check"; - break; - } - case "--write": { - mode = "write"; - break; - } - case "--root": { - rootDir = path.resolve(readOptionValue(argv, index, "--root")); - index += 1; - break; - } - case "-h": - case "--help": { - help = true; - break; - } - default: { - throw new Error(`Unknown argument: ${arg}`); - } - } - } - - return { help, mode, rootDir }; -} - -function readOptionValue(argv: string[], index: number, flag: string): string { - const value = argv[index + 1]; - if (!value || value.startsWith("--")) { - throw new Error(`Missing value for ${flag}.`); - } - return value; -} +export { parseVersionSyncArgs as parseArgs } from "./lib/version-script-args.ts"; function printUsage(): void { process.stdout.write( @@ -54,7 +12,7 @@ function printUsage(): void { } function main(argv = process.argv.slice(2)): number { - const options = parseArgs(argv); + const options = parseVersionSyncArgs(argv); if (options.help) { printUsage(); return 0; diff --git a/scripts/android-version.ts b/scripts/android-version.ts index abf3beb2a26a..82ffdb2cee43 100644 --- a/scripts/android-version.ts +++ b/scripts/android-version.ts @@ -1,63 +1,6 @@ // Android Version script supports OpenClaw repository automation. -import path from "node:path"; import { resolveAndroidVersion } from "./lib/android-version.ts"; - -type CliOptions = { - field: string | null; - format: "json" | "shell"; - help: boolean; - rootDir: string; -}; - -function parseArgs(argv: string[]): CliOptions { - let field: string | null = null; - let format: "json" | "shell" = "json"; - let help = false; - let rootDir = path.resolve("."); - - for (let index = 0; index < argv.length; index += 1) { - const arg = argv[index]; - switch (arg) { - case "--field": { - field = readOptionValue(argv, index, "--field"); - index += 1; - break; - } - case "--json": { - format = "json"; - break; - } - case "--shell": { - format = "shell"; - break; - } - case "--root": { - const value = readOptionValue(argv, index, "--root"); - rootDir = path.resolve(value); - index += 1; - break; - } - case "-h": - case "--help": { - help = true; - break; - } - default: { - throw new Error(`Unknown argument: ${arg}`); - } - } - } - - return { field, format, help, rootDir }; -} - -function readOptionValue(argv: string[], index: number, flag: string): string { - const value = argv[index + 1]; - if (!value || value.startsWith("--")) { - throw new Error(`Missing value for ${flag}.`); - } - return value; -} +import { parseVersionQueryArgs } from "./lib/version-script-args.ts"; function printUsage(): void { process.stdout.write( @@ -66,7 +9,7 @@ function printUsage(): void { } function main(argv = process.argv.slice(2)): number { - const options = parseArgs(argv); + const options = parseVersionQueryArgs(argv); if (options.help) { printUsage(); return 0; diff --git a/scripts/ios-sync-versioning.ts b/scripts/ios-sync-versioning.ts index 45540734268c..cf3f201bf269 100644 --- a/scripts/ios-sync-versioning.ts +++ b/scripts/ios-sync-versioning.ts @@ -1,51 +1,9 @@ // Ios Sync Versioning script supports OpenClaw repository automation. import path from "node:path"; import { syncIosVersioning } from "./lib/ios-version.ts"; +import { parseVersionSyncArgs } from "./lib/version-script-args.ts"; -type Mode = "check" | "write"; - -export function parseArgs(argv: string[]): { help: boolean; mode: Mode; rootDir: string } { - let help = false; - let mode: Mode = "write"; - let rootDir = path.resolve("."); - - for (let index = 0; index < argv.length; index += 1) { - const arg = argv[index]; - switch (arg) { - case "--check": { - mode = "check"; - break; - } - case "--write": { - mode = "write"; - break; - } - case "--root": { - rootDir = path.resolve(readOptionValue(argv, index, "--root")); - index += 1; - break; - } - case "-h": - case "--help": { - help = true; - break; - } - default: { - throw new Error(`Unknown argument: ${arg}`); - } - } - } - - return { help, mode, rootDir }; -} - -function readOptionValue(argv: string[], index: number, flag: string): string { - const value = argv[index + 1]; - if (!value || value.startsWith("--")) { - throw new Error(`Missing value for ${flag}.`); - } - return value; -} +export { parseVersionSyncArgs as parseArgs } from "./lib/version-script-args.ts"; function printUsage(): void { process.stdout.write( @@ -54,7 +12,7 @@ function printUsage(): void { } function main(argv = process.argv.slice(2)): number { - const options = parseArgs(argv); + const options = parseVersionSyncArgs(argv); if (options.help) { printUsage(); return 0; diff --git a/scripts/ios-version.ts b/scripts/ios-version.ts index 231fa57d88fb..fd315af6c3a2 100644 --- a/scripts/ios-version.ts +++ b/scripts/ios-version.ts @@ -1,63 +1,6 @@ // Ios Version script supports OpenClaw repository automation. -import path from "node:path"; import { resolveIosVersion } from "./lib/ios-version.ts"; - -type CliOptions = { - field: string | null; - format: "json" | "shell"; - help: boolean; - rootDir: string; -}; - -function parseArgs(argv: string[]): CliOptions { - let field: string | null = null; - let format: "json" | "shell" = "json"; - let help = false; - let rootDir = path.resolve("."); - - for (let index = 0; index < argv.length; index += 1) { - const arg = argv[index]; - switch (arg) { - case "--field": { - field = readOptionValue(argv, index, "--field"); - index += 1; - break; - } - case "--json": { - format = "json"; - break; - } - case "--shell": { - format = "shell"; - break; - } - case "--root": { - const value = readOptionValue(argv, index, "--root"); - rootDir = path.resolve(value); - index += 1; - break; - } - case "-h": - case "--help": { - help = true; - break; - } - default: { - throw new Error(`Unknown argument: ${arg}`); - } - } - } - - return { field, format, help, rootDir }; -} - -function readOptionValue(argv: string[], index: number, flag: string): string { - const value = argv[index + 1]; - if (!value || value.startsWith("--")) { - throw new Error(`Missing value for ${flag}.`); - } - return value; -} +import { parseVersionQueryArgs } from "./lib/version-script-args.ts"; function printUsage(): void { process.stdout.write( @@ -66,7 +9,7 @@ function printUsage(): void { } function main(argv = process.argv.slice(2)): number { - const options = parseArgs(argv); + const options = parseVersionQueryArgs(argv); if (options.help) { printUsage(); return 0; diff --git a/scripts/lib/version-script-args.ts b/scripts/lib/version-script-args.ts new file mode 100644 index 000000000000..2458cfd4c0a0 --- /dev/null +++ b/scripts/lib/version-script-args.ts @@ -0,0 +1,100 @@ +import path from "node:path"; + +export type VersionScriptFormat = "json" | "shell"; +export type VersionQueryCliOptions = { + field: string | null; + format: VersionScriptFormat; + help: boolean; + rootDir: string; +}; +export type VersionSyncMode = "check" | "write"; +export type VersionSyncCliOptions = { + help: boolean; + mode: VersionSyncMode; + rootDir: string; +}; + +export function parseVersionQueryArgs(argv: string[]): VersionQueryCliOptions { + let field: string | null = null; + let format: VersionScriptFormat = "json"; + let help = false; + let rootDir = path.resolve("."); + + for (let index = 0; index < argv.length; index += 1) { + const arg = argv[index]; + switch (arg) { + case "--field": { + field = readOptionValue(argv, index, "--field"); + index += 1; + break; + } + case "--json": { + format = "json"; + break; + } + case "--shell": { + format = "shell"; + break; + } + case "--root": { + const value = readOptionValue(argv, index, "--root"); + rootDir = path.resolve(value); + index += 1; + break; + } + case "-h": + case "--help": { + help = true; + break; + } + default: { + throw new Error(`Unknown argument: ${arg}`); + } + } + } + + return { field, format, help, rootDir }; +} + +export function parseVersionSyncArgs(argv: string[]): VersionSyncCliOptions { + let help = false; + let mode: VersionSyncMode = "write"; + let rootDir = path.resolve("."); + + for (let index = 0; index < argv.length; index += 1) { + const arg = argv[index]; + switch (arg) { + case "--check": { + mode = "check"; + break; + } + case "--write": { + mode = "write"; + break; + } + case "--root": { + rootDir = path.resolve(readOptionValue(argv, index, "--root")); + index += 1; + break; + } + case "-h": + case "--help": { + help = true; + break; + } + default: { + throw new Error(`Unknown argument: ${arg}`); + } + } + } + + return { help, mode, rootDir }; +} + +function readOptionValue(argv: string[], index: number, flag: string): string { + const value = argv[index + 1]; + if (!value || value.startsWith("--")) { + throw new Error(`Missing value for ${flag}.`); + } + return value; +}