Skip to content

Commit

Permalink
refactor: use geonames source data and export used fields only
Browse files Browse the repository at this point in the history
  • Loading branch information
gbicou committed Jan 8, 2024
1 parent 535620d commit 6a207a9
Show file tree
Hide file tree
Showing 10 changed files with 633 additions and 5,321 deletions.
6 changes: 6 additions & 0 deletions .changeset/many-spiders-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@bicou/countries-server-schema": patch
"@bicou/countries-server-data": patch
---

use geonames source data and export used fields only
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
dist
.nitro
.output
data/src/countries.ts
34 changes: 34 additions & 0 deletions data/build-countries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { createReadStream, createWriteStream } from "node:fs";
import { createInterface } from "node:readline";
import { once } from "node:events";
import { type GeoCountry } from "./src/geo-country.js";

const source = createWriteStream("src/countries.ts", { flags: "w" });
source.write('import { type GeoCountry } from "./geo-country.js";\n');
source.write('export { type GeoCountry } from "./geo-country.js";\n');
source.write("export const countries: GeoCountry[] = [\n");

const tsv = createInterface({
input: createReadStream("countryInfo.txt"),
crlfDelay: Number.POSITIVE_INFINITY,
});

tsv.on("line", (line) => {
if (line.length > 0 && !line.startsWith("#")) {
const data = line.split("\t");
const country: GeoCountry = {
ISO: data[0],
ISO3: data[1],
"ISO-Numeric": data[2],
Country: data[4],
tld: data[9],
neighbours: (data[17].length > 0 && data[17].split(",")) || [],
};
source.write("\t" + JSON.stringify(country) + ",\n");
}
});

await once(tsv, "close");

source.write("];\n");
source.end();
302 changes: 302 additions & 0 deletions data/countryInfo.txt

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion data/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"scripts": {
"dev": "tsup --watch",
"stub": "tsup --dts-only",
"build": "tsup --minify"
"build": "tsup --minify",
"build:countries": "tsx build-countries.ts"
},
"publishConfig": {
"access": "public"
Expand Down
Loading

1 comment on commit 6a207a9

@vercel
Copy link

@vercel vercel bot commented on 6a207a9 Jan 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.