diff --git a/Earthfile b/Earthfile index 43a93cd87..66e1b15f7 100644 --- a/Earthfile +++ b/Earthfile @@ -253,7 +253,7 @@ pelias-config: WORKDIR ./generate_config RUN yarn install && yarn build - RUN bin/generate-pelias-config "${area}" "${countries}" < areas.csv > pelias.json + RUN bin/generate-pelias-config areas.csv "${area}" "${countries}" > pelias.json SAVE ARTIFACT pelias.json /pelias.json pelias-import-base: diff --git a/services/pelias/generate_config/README.md b/services/pelias/generate_config/README.md index ca3fe6cd4..646a680d5 100644 --- a/services/pelias/generate_config/README.md +++ b/services/pelias/generate_config/README.md @@ -10,9 +10,9 @@ npm run build ## Usage ``` -bin/generate-pelias-config < areas.csv [area] [optional Country Code] -bin/generate-pelias-config < areas.csv Seattle -bin/generate-pelias-config < areas.csv Seattle CA -bin/generate-pelias-config < areas.csv Seattle "US CA" -bin/generate-pelias-config < areas.csv planet-v1.26 ALL +bin/generate-pelias-config areas.csv [area] [optional Country Code] +bin/generate-pelias-config areas.csv Seattle +bin/generate-pelias-config areas.csv Seattle CA +bin/generate-pelias-config areas.csv Seattle "US CA" +bin/generate-pelias-config areas.csv planet-v1.26 ALL ``` diff --git a/services/pelias/generate_config/src/Area.ts b/services/pelias/generate_config/src/Area.ts index 53e2f62ff..403fab608 100644 --- a/services/pelias/generate_config/src/Area.ts +++ b/services/pelias/generate_config/src/Area.ts @@ -1,4 +1,4 @@ -import * as defaultConfig from "./defaultConfig.json"; +import { default as defaultConfig } from "./defaultConfig.json"; import { PeliasConfig } from "./index"; type ImportsConfig = { diff --git a/services/pelias/generate_config/src/index.test.ts b/services/pelias/generate_config/src/index.test.ts index 9e54942f1..e352e81d0 100644 --- a/services/pelias/generate_config/src/index.test.ts +++ b/services/pelias/generate_config/src/index.test.ts @@ -5,7 +5,7 @@ import * as path from "path"; test("guesses country when missing", () => { const inputPath = path.join(path.resolve(__dirname), "../areas.csv"); const input = readFileSync(inputPath, "utf-8"); - const config = generate(input, { area: "Seattle", countries: [] }); + const config = generate(input, "Seattle", []); expect(config["imports"]["whosonfirst"]).toEqual({ countryCode: ["US"], datapath: "/data/whosonfirst", @@ -16,7 +16,7 @@ test("guesses country when missing", () => { test("use country when specified", () => { const inputPath = path.join(path.resolve(__dirname), "../areas.csv"); const input = readFileSync(inputPath, "utf-8"); - const config = generate(input, { area: "Seattle", countries: ["CA"] }); + const config = generate(input, "Seattle", ["CA"]); expect(config["imports"]["whosonfirst"]).toEqual({ countryCode: ["CA"], datapath: "/data/whosonfirst", @@ -27,7 +27,7 @@ test("use country when specified", () => { test("unknown area", () => { const inputPath = path.join(path.resolve(__dirname), "../areas.csv"); const input = readFileSync(inputPath, "utf-8"); - const config = generate(input, { area: "planet-v1.26", countries: ["ALL"] }); + const config = generate(input, "planet-v1.26", ["ALL"]); expect(config["imports"]["whosonfirst"]).toEqual({ datapath: "/data/whosonfirst", importPostalcodes: true, diff --git a/services/pelias/generate_config/src/index.ts b/services/pelias/generate_config/src/index.ts index dff884853..590fdd621 100644 --- a/services/pelias/generate_config/src/index.ts +++ b/services/pelias/generate_config/src/index.ts @@ -1,40 +1,27 @@ import { parse } from "csv-parse/sync"; import Area from "./Area"; -type Args = { - area: string; - countries: string[]; -}; - // TODO: add types export type PeliasConfig = any; // eslint-disable-line @typescript-eslint/no-explicit-any -export function parseArgs(): Args { - const args = process.argv.slice(2); - - const area = args[0]; - if (!area) { - throw Error("Missing area arg"); - } - - const countries = args.slice(1); - return { area, countries }; -} - -export function generate(input: string, args: Args): PeliasConfig { +export function generate( + input: string, + areaName: string, + countries: string[], +): PeliasConfig { const area = (() => { for (const record of parse(input, { columns: true })) { const area = Area.fromRecord(record); - if (area.name != args.area) { + if (area.name != areaName) { continue; } - if (args.countries.length > 0) { - area.countryCodes = args.countries; + if (countries.length > 0) { + area.countryCodes = countries; } return area; } - return new Area(args.area, args.countries, []); + return new Area(areaName, countries, []); })(); return area.peliasConfig(); diff --git a/services/pelias/generate_config/src/scripts/generatePeliasConfig.ts b/services/pelias/generate_config/src/scripts/generatePeliasConfig.ts index e8e630dea..615572b23 100644 --- a/services/pelias/generate_config/src/scripts/generatePeliasConfig.ts +++ b/services/pelias/generate_config/src/scripts/generatePeliasConfig.ts @@ -1,5 +1,28 @@ -import { generate, parseArgs } from "../index"; +import { generate } from "../index"; import { readFileSync } from "fs"; -const config = generate(readFileSync(0, "utf-8"), parseArgs()); +export function parseArgs(args: string[]): [string, string, string[]] { + const USAGE = `ARGS: \n []`; + + const inputPath = args.shift(); + if (!inputPath) { + console.error(USAGE); + throw Error("Missing `inputPath` arg"); + } + + const area = args.shift(); + if (!area) { + console.error(USAGE); + throw Error("Missing `area` arg"); + } + + const countries = args; + return [inputPath, area, countries]; +} + +// Get just the args from `node $cmd [args...]` +const args = process.argv.slice(2); +const [inputPath, area, countries] = parseArgs(args); +const input = readFileSync(inputPath, "utf-8"); +const config = generate(input, area, countries); console.log(JSON.stringify(config, undefined, 2));