psgc-areas
Version:
Provides the full Philippine Standard Geographic Code (PSGC) dataset in JSON with multiple formats (hierarchy, flat list of all areas, regions, provinces, cities, municipalities, municipal districts, and barangays).
31 lines (27 loc) • 1.17 kB
JavaScript
import fs from "node:fs/promises";
import buffer from "node:buffer";
import path from "node:path";
import ExcelJS from "exceljs";
import { PSGC_WORKBOOK_PATH, PSGC_WORKSHEET_NAME } from "./psgc.config.js";
import { PSGCDataset } from "./parser.js";
import { resolvePath } from "./utils.js";
// Read workbook from file and use target worksheet.
const workbook = new ExcelJS.Workbook();
const workbookPath = resolvePath(PSGC_WORKBOOK_PATH);
await workbook.xlsx.readFile(workbookPath);
const psgcWorksheet = workbook.getWorksheet(PSGC_WORKSHEET_NAME);
// Read the PSGC dataset using our own parser.
const psgcDataset = new PSGCDataset(psgcWorksheet);
// Export the complete PSGC dataset to JSON.
const arraysToExport = Object.entries(psgcDataset);
arraysToExport.forEach(async function(aKeyValuePair) {
try {
const filename = aKeyValuePair[0] + ".json";
const filepath = resolvePath(path.join("./data", filename));
const data = JSON.stringify(aKeyValuePair[1]);
const arrayBuffer = new Uint8Array(buffer.Buffer.from(data));
await fs.writeFile(filepath, arrayBuffer);
} catch (e) {
console.error(e);
}
});