#!/usr/bin/env node // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial /** * `mailwoman-un-locode` — build the lookup DB, or look up a UN/locode by name or coordinate. * * ```sh * mailwoman-un-locode build --csv code-list.csv --out un-locode.db * mailwoman-un-locode --db un-locode.db --country US --name "New York" * mailwoman-un-locode --db un-locode.db -- 40.7128 -74.0060 * ``` * * The `--` separates flags from coordinates so negative coordinates parse as positionals * (the coordinate form is the nearest-code lookup, formerly `--near`). */ import { parseArguments } from "@mailwoman/core/scripting/arguments" import { buildUNLocodeDB } from "#build" import { UNLocodeLookup } from "#index" const { values, positionals } = parseArguments({ options: { csv: { type: "string" }, out: { type: "string" }, db: { type: "string" }, country: { type: "string" }, name: { type: "string" }, }, allowPositionals: true, }) if (positionals[0] === "build") { if (!values.csv || !values.out) { console.error("Usage: mailwoman-un-locode build --csv --out ") process.exit(1) } const { rows, withCoords } = await buildUNLocodeDB(values.csv, values.out) console.error(`built ${values.out} (${rows} rows, ${withCoords} with coordinates)`) } else { const lat = Number(positionals[0]) const lon = Number(positionals[1]) const byName = Boolean(values.country && values.name) const byCoord = Number.isFinite(lat) && Number.isFinite(lon) if (!values.db || (!byName && !byCoord)) { console.error("Usage: mailwoman-un-locode --db (--country CC --name NAME | -- )") process.exit(1) } using lookup = new UNLocodeLookup({ databasePath: values.db }) const code = byName ? lookup.byName(values.country!, values.name!) : lookup.nearest(lat, lon) console.log(JSON.stringify({ unLocode: code })) }