From 18db4584ebdfb353b69a9feca33edc44012ee3b0 Mon Sep 17 00:00:00 2001 From: tomasciccola <117094913+tomasciccola@users.noreply.github.com> Date: Mon, 14 Oct 2024 12:43:57 -0300 Subject: [PATCH] chore: use `createRequire` for comapeo/geometry (#25) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * use `createRequire` for comapeo/geometry, add `resolveJsonModule` to tsconfig.json * Clean up Geometry dereferencing --------- Co-authored-by: Tomás Ciccola Co-authored-by: Evan Hahn --- lib/faker.js | 17 ++++++----------- tsconfig.json | 3 ++- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/lib/faker.js b/lib/faker.js index 235cafb..c1f1f00 100644 --- a/lib/faker.js +++ b/lib/faker.js @@ -1,13 +1,9 @@ import { JSONSchemaFaker } from 'json-schema-faker' import { faker } from '@faker-js/faker' import deref from 'dereference-json-schema' -import { readFile } from 'node:fs/promises' - -const geometryPath = new URL( - '../node_modules/@comapeo/geometry/json/geometry.json', - import.meta.url, -).pathname -const Geometry = JSON.parse(await readFile(geometryPath, 'utf-8')) +import { createRequire } from 'module' +const require = createRequire(import.meta.url) +const Geometry = require('@comapeo/geometry/json/geometry.json') /** * @typedef {typeof import('@comapeo/schema').docSchemas[import('@comapeo/schema/dist/types.js').SchemaName]} ValidSchema @@ -176,10 +172,9 @@ function createFakerSchema(schema) { return s } case 'RemoteDetectionAlert': - s.properties.geometry = { - $ref: Geometry.$id, - ...deref.dereferenceSync(Geometry), - } + // @ts-expect-error Dereferencing this causes a type error, but the data + // is generated correctly. + s.properties.geometry = deref.dereferenceSync(Geometry) return s } } diff --git a/tsconfig.json b/tsconfig.json index b63d8a2..bc6ce81 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -9,7 +9,8 @@ "emitDeclarationOnly": true, "noFallthroughCasesInSwitch": true, "strictNullChecks": true, - "declarationDir": "./types" + "declarationDir": "./types", + "resolveJsonModule": true }, "include": ["./index.js", "lib/**/*.js"], "exclude": ["bin"]