Skip to content

Commit

Permalink
Merge pull request #293 from headwaymaps/mkirk/fixup-pelias-config
Browse files Browse the repository at this point in the history
fixup pelias config
  • Loading branch information
michaelkirk authored Oct 27, 2023
2 parents 468ce24 + f15f981 commit bf624b5
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 5 additions & 5 deletions services/pelias/generate_config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
2 changes: 1 addition & 1 deletion services/pelias/generate_config/src/Area.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as defaultConfig from "./defaultConfig.json";
import { default as defaultConfig } from "./defaultConfig.json";
import { PeliasConfig } from "./index";

type ImportsConfig = {
Expand Down
6 changes: 3 additions & 3 deletions services/pelias/generate_config/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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,
Expand Down
31 changes: 9 additions & 22 deletions services/pelias/generate_config/src/index.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <inputPath> <area> [<countries ...>]`;

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));

0 comments on commit bf624b5

Please sign in to comment.