ICS / generate-data.mjs
stat2025's picture
Upload 6 files
dba6001 verified
Raw
History Blame Contribute Delete
4.26 kB
import fs from "node:fs/promises";
import crypto from "node:crypto";
import { FileBlob, SpreadsheetFile } from "@oai/artifact-tool";
const [inputPath, outputPath, password = "20302030"] = process.argv.slice(2);
const CITY_INFO = new Map([
["المدينة الصناعية الأولى بالدمام", { region: "منطقة الدمام", city: "الصناعية الأولى بالدمام" }],
["المدينة صناعية الثانية بالدمام", { region: "منطقة الدمام", city: "الصناعية الثانية بالدمام" }],
["المدينة صناعية الثالثة بالدمام", { region: "منطقة الدمام", city: "الصناعية الثالثة بالدمام" }],
["المدينة صناعية الأولى بالأحساء", { region: "منطقة الأحساء", city: "المدينة صناعية الأولى بالأحساء" }],
["المدينة الصناعية بحفر الباطن", { region: "منطقة حفر الباطن", city: "المدينة الصناعية بحفر الباطن" }],
['مدينة الملك سلمان "سبارك"', { region: "سبارك", city: "مدينة الملك سلمان" }],
]);
const REGIONS = ["منطقة الدمام", "منطقة الأحساء", "منطقة حفر الباطن", "سبارك"];
function normalizeHeader(value) {
return String(value ?? "")
.replace(/\s+/g, " ")
.trim();
}
function getByHeader(row, headers, candidates) {
for (const candidate of candidates) {
const index = headers.findIndex((header) => normalizeHeader(header) === candidate);
if (index !== -1) return row[index] ?? "";
}
return "";
}
function clean(value) {
if (value === null || value === undefined) return "";
return String(value).trim();
}
if (!inputPath || !outputPath) {
console.error("Usage: node generate-data.mjs <input.xlsx> <output.js> [password]");
process.exitCode = 1;
} else {
const blob = await FileBlob.load(inputPath);
const workbook = await SpreadsheetFile.importXlsx(blob);
const rows = [];
for (const sheet of workbook.worksheets.items) {
const info = CITY_INFO.get(sheet.name);
if (!info) continue;
const { region, city } = info;
const values = sheet.getUsedRange(true).values;
if (!values?.length) continue;
const headers = values[0].map(normalizeHeader);
for (const sourceRow of values.slice(1)) {
const establishmentName = clean(getByHeader(sourceRow, headers, ["إسم المنشأة", "اسم المنشأة"]));
if (!establishmentName) continue;
rows.push({
region,
city,
establishmentName,
commercialRecord: clean(getByHeader(sourceRow, headers, ["السجل التجاري من الاطار", "السجل التجاري"])),
complianceStatus: clean(getByHeader(sourceRow, headers, ["حالة الاستيفاء"])),
cityClarification: clean(getByHeader(sourceRow, headers, ["توضيح المدينة", "توضيح المدينة الصناعية"])),
coordinates: clean(getByHeader(sourceRow, headers, ["الاحداثيات", "الإحداثية", "الإحداثيات"])),
});
}
}
const payload = JSON.stringify({
version: 2,
generatedAt: new Date().toISOString(),
regions: REGIONS,
rows,
});
const iterations = 310000;
const salt = crypto.randomBytes(16);
const iv = crypto.randomBytes(12);
const key = crypto.pbkdf2Sync(password, salt, iterations, 32, "sha256");
const cipher = crypto.createCipheriv("aes-256-gcm", key, iv);
const encrypted = Buffer.concat([cipher.update(payload, "utf8"), cipher.final()]);
const authTag = cipher.getAuthTag();
const combined = Buffer.concat([encrypted, authTag]);
const output = [
"/* Generated encrypted data. Replace this file using generate-data.mjs when the workbook changes. */",
"const ENCRYPTED_DATA = Object.freeze({",
` iterations: ${iterations},`,
` salt: "${salt.toString("base64")}",`,
` iv: "${iv.toString("base64")}",`,
` payload: "${combined.toString("base64")}",`,
"});",
"",
].join("\n");
await fs.writeFile(outputPath, output, "utf8");
console.log(JSON.stringify({ rows: rows.length, regions: REGIONS.length, outputPath }));
}