|
| 1 | +import { getConfiguration, Structure } from "gruber"; |
| 2 | +import pkg from "./package.json" with { type: "json" }; |
| 3 | + |
| 4 | +const config = getConfiguration(); |
| 5 | + |
| 6 | +const UNSET = "gruber://unset"; |
| 7 | + |
| 8 | +const struct = config.object({ |
| 9 | + env: config.string({ variable: "NODE_ENV", fallback: "development" }), |
| 10 | + |
| 11 | + meta: config.object({ |
| 12 | + name: config.string({ variable: "APP_NAME", fallback: pkg.name }), |
| 13 | + version: config.string({ variable: "APP_VERSION", fallback: pkg.version }), |
| 14 | + }), |
| 15 | + |
| 16 | + targets: config.array( |
| 17 | + config.object({ |
| 18 | + name: Structure.string(), |
| 19 | + bbox: Structure.array(Structure.number()), |
| 20 | + }), |
| 21 | + ), |
| 22 | + |
| 23 | + protomaps: config.object({ |
| 24 | + metadata: config.url({ |
| 25 | + variable: "PROTOMAPS_METADATA_URL", |
| 26 | + fallback: "https://build-metadata.protomaps.dev/builds.json", |
| 27 | + }), |
| 28 | + builds: config.url({ |
| 29 | + variable: "PROTOMAPS_BUILDS_URL", |
| 30 | + fallback: "https://build.protomaps.com", |
| 31 | + }), |
| 32 | + }), |
| 33 | + |
| 34 | + s3: config.object({ |
| 35 | + prefix: config.string({ variable: "S3_PREFIX", fallback: "" }), |
| 36 | + accessKey: config.string({ variable: "S3_ACCESS_KEY", fallback: UNSET }), |
| 37 | + secretKey: config.string({ variable: "S3_SECRET_KEY", fallback: UNSET }), |
| 38 | + bucketName: config.string({ variable: "S3_BUCKET_NAME", fallback: UNSET }), |
| 39 | + endpoint: config.url({ variable: "S3_ENDPOINT", fallback: UNSET }), |
| 40 | + objectMetadata: Structure.any(), |
| 41 | + }), |
| 42 | +}); |
| 43 | + |
| 44 | +export async function loadConfiguration(path: string | URL) { |
| 45 | + const value = await config.load(path, struct); |
| 46 | + if (value.env === "production") { |
| 47 | + if (value.s3.accessKey === UNSET) throw new Error("s3.accessKey not set"); |
| 48 | + if (value.s3.secretKey === UNSET) throw new Error("s3.secretKey not set"); |
| 49 | + if (value.s3.bucketName === UNSET) throw new Error("s3.bucketName not set"); |
| 50 | + if (value.s3.endpoint.toString() === UNSET) { |
| 51 | + throw new Error("s3.endpoint not set"); |
| 52 | + } |
| 53 | + } |
| 54 | + for (const target of value.targets) { |
| 55 | + if (target.bbox.length !== 4) throw new Error("bbox must have 4 elements"); |
| 56 | + } |
| 57 | + return value; |
| 58 | +} |
| 59 | + |
| 60 | +export function outputConfiguration() { |
| 61 | + console.log(config.getUsage(struct, appConfig)); |
| 62 | +} |
| 63 | + |
| 64 | +export const appConfig = await loadConfiguration( |
| 65 | + new URL("./config.json", import.meta.url), |
| 66 | +); |
0 commit comments