-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathcities.ts
More file actions
100 lines (86 loc) · 2.83 KB
/
Copy pathcities.ts
File metadata and controls
100 lines (86 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env node
import { writeFile } from "node:fs/promises";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { fetchWithRetry } from "../src/_internals/fetch-with-retry/fetch-with-retry.ts";
const scriptsDir = dirname(fileURLToPath(import.meta.url));
type City = {
id: number;
nome: string;
microrregiao: {
id: number;
nome: string;
mesorregiao: {
id: number;
nome: string;
UF: {
id: number;
sigla: string;
nome: string;
regiao: {
id: number;
sigla: string;
nome: string;
};
};
};
};
};
const main = async () => {
const response = await fetchWithRetry(
"https://servicodados.ibge.gov.br/api/v1/localidades/municipios",
);
if (!response.ok) {
throw new Error(`IBGE municipalities request failed with status ${response.status}`);
}
const json = (await response.json()) as City[];
const cities = Object.fromEntries(
Object.entries(
json.reduce(
(acc, city) => {
const stateInitials = city?.microrregiao?.mesorregiao?.UF?.sigla;
if (!stateInitials) return acc;
if (!acc[stateInitials]) {
acc[stateInitials] = [];
}
acc[stateInitials].push(city.nome);
return acc;
},
{} as Record<string, string[]>,
),
)
.sort(([a], [b]) => a.localeCompare(b))
.map(([state, cityNames]) => [state, cityNames.sort((a, b) => a.localeCompare(b))]),
);
await writeFile(
resolve(scriptsDir, "..", "./src/_internals/constants/cities.ts"),
`/**
* A collection of Brazilian cities categorized by their respective states.
*
* @constant
* @type {Object}
* @property {string[]} GO - Cities in the state of Goiás.
* @property {string[]} MG - Cities in the state of Minas Gerais.
* @property {string[]} PA - Cities in the state of Pará.
* @property {string[]} CE - Cities in the state of Ceará.
* @property {string[]} BA - Cities in the state of Bahia.
* @property {string[]} PR - Cities in the state of Paraná.
* @property {string[]} SC - Cities in the state of Santa Catarina.
* @property {string[]} PE - Cities in the state of Pernambuco.
* @property {string[]} TO - Cities in the state of Tocantins.
* @property {string[]} RN - Cities in the state of Rio Grande do Norte.
* @property {string[]} PI - Cities in the state of Piauí.
* @property {string[]} RS - Cities in the state of Rio Grande do Sul.
* @property {string[]} MT - Cities in the state of Mato Grosso.
* @property {string[]} AC - Cities in the state of Acre.
* @property {string[]} SP - Cities in the state of São Paulo.
* @property {string[]} ES - Cities in the state of Espírito Santo.
* @property {string[]} MA - Cities in the state of Maranhão.
*/
export const DATA = ${JSON.stringify(cities)} as const`,
);
};
await main().catch((error) => {
console.error(error instanceof Error ? error.message : error);
process.exit(1);
});