diff options
| author | Soccera1 <[email protected]> | 2026-07-18 09:27:17 +1000 |
|---|---|---|
| committer | Soccera1 <[email protected]> | 2026-07-18 09:27:17 +1000 |
| commit | 6f5241afb5bd1d6a1a8724bf06cc6f205c279402 (patch) | |
| tree | 2aa852e968f6b89f2c8458333ce3e7c38b3c2450 /src/commands/sleep.js | |
| parent | a753f669c9bedfccb9f7f400da018bb487dca5d0 (diff) | |
| download | bnu-6f5241afb5bd1d6a1a8724bf06cc6f205c279402.tar.gz bnu-6f5241afb5bd1d6a1a8724bf06cc6f205c279402.zip | |
Refactor to single call
Diffstat (limited to 'src/commands/sleep.js')
| -rw-r--r-- | src/commands/sleep.js | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/src/commands/sleep.js b/src/commands/sleep.js new file mode 100644 index 0000000..81fa9a3 --- /dev/null +++ b/src/commands/sleep.js @@ -0,0 +1,97 @@ +#!/usr/bin/env bun + +import { invalidOptionMessage } from "../shared/common.js"; +import { InvocationError, UsageError, VERSION, stdout } from "../shared/diagnostics.js"; +import { parseDuration } from "../shared/process.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const SLEEP_LONG_OPTIONS = ["help", "version"]; + +export async function sleep(args) { + const meta = sleepMetaOption(args); + if (meta === "--help") { + showSleepHelp(); + return 0; + } + if (meta === "--version") { + stdout(`${VERSION}\n`); + return 0; + } + validateSleepOptions(args); + if (!args.length) throw new UsageError("missing operand", true); + if (args[0] === "--") { + args = args.slice(1); + if (!args.length) return 0; + } + let total = 0; + for (const value of args) { + try { + total += parseDuration(value); + } catch (error) { + if (error instanceof InvocationError) throw new UsageError(error.message, true); + throw error; + } + } + if (!Number.isFinite(total) || total > 2147483647) await new Promise(() => {}); + await Bun.sleep(total); + return 0; +} + +export function sleepMetaOption(args) { + for (const arg of args) { + if (arg === "--") return null; + if (!arg.startsWith("-") || arg === "-") continue; + if (!arg.startsWith("--")) return null; + const option = normalizeSleepLongOption(arg); + const name = option.slice(2).split("=", 1)[0]; + if (!SLEEP_LONG_OPTIONS.includes(name) || option.includes("=")) return null; + return option; + } + return null; +} + +export function validateSleepOptions(args) { + let end = false; + for (const arg of args) { + if (end) continue; + if (arg === "--") { + end = true; + continue; + } + if (arg === "-" || !arg.startsWith("-")) continue; + if (!arg.startsWith("--")) throw new UsageError(sleepInvalidOptionMessage(arg), true); + const option = normalizeSleepLongOption(arg); + const [name, inlineValue] = option.slice(2).split(/=(.*)/s, 2); + if (!SLEEP_LONG_OPTIONS.includes(name)) throw new UsageError(`unrecognized option '${arg}'`, true); + if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true); + } +} + +export function normalizeSleepLongOption(arg) { + const body = arg.slice(2); + const eq = body.indexOf("="); + const name = eq === -1 ? body : body.slice(0, eq); + if (!name) return arg; + const match = SLEEP_LONG_OPTIONS.find((option) => option.startsWith(name)); + if (!match) return arg; + return eq === -1 ? `--${match}` : `--${match}=${body.slice(eq + 1)}`; +} + +export function showSleepHelp() { + stdout("Usage: sleep NUMBER[SUFFIX]...\n"); + stdout(" or: sleep OPTION\n"); + stdout("Pause for NUMBER seconds, where NUMBER is an integer or floating-point.\n"); + stdout("SUFFIX may be 's','m','h', or 'd', for seconds, minutes, hours, days.\n"); + stdout("With multiple arguments, pause for the sum of their values.\n\n"); + stdout(" --help display this help and exit\n"); + stdout(" --version output version information and exit\n"); +} + +export function sleepInvalidOptionMessage(arg) { + return arg.startsWith("--") ? `unrecognized option '${arg}'` : invalidOptionMessage(arg); +} + +const singleCall = defineCommand("sleep", sleep, null); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); |
