import * as Effect from "effect/Effect"; import type { PlatformFilter, PlatformTarget } from "@packall/core"; import { allPlatforms, parsePlatformTarget, platformTargets } from "@packall/core"; /** * Parses `--platform win32,linux`, `--platform "win32 linux"`, and * `--platform win32 --platform linux` — all to the same thing. * * Whitespace counts as a separator because PowerShell turns an unquoted * `win32,linux` into the single argument `"win32 linux"`: a comma builds an * array literal there, and arrays are space-joined on their way to a native * command. Accepting that costs nothing — no platform token contains a space — * and it means the obvious command works in the shell most of these users are * sitting in, rather than failing with a confusing quoting error. */ export const parsePlatforms = ( inputs: ReadonlyArray, ): Effect.Effect => Effect.gen(function* () { const targets: Array = []; const invalid: Array = []; for (const input of inputs) { for (const piece of input.split(/[,\s]+/)) { const trimmed = piece.trim(); if (trimmed.length === 0) continue; const target = parsePlatformTarget(trimmed); if (target === null) invalid.push(trimmed); else targets.push(target); } } if (invalid.length > 0) { return yield* Effect.fail( new Error( `Unrecognised --platform value(s): ${invalid.join(", ")}\n` + " Expected [-][-], where is one of aix, android, cygwin,\n" + " darwin, freebsd, haiku, linux, netbsd, openbsd, sunos, win32.\n" + " e.g. win32,linux (every arch) · linux-x64 · darwin-arm64 · linux-x64-musl", ), ); } return targets.length === 0 ? allPlatforms : platformTargets(targets); });