Skip to content

Commit

Permalink
chore: use createRequire for comapeo/geometry (#25)
Browse files Browse the repository at this point in the history
* use `createRequire` for comapeo/geometry, add `resolveJsonModule` to tsconfig.json

* Clean up Geometry dereferencing

---------

Co-authored-by: Tomás Ciccola <[email protected]>
Co-authored-by: Evan Hahn <[email protected]>
  • Loading branch information
3 people authored Oct 14, 2024
1 parent e327105 commit 18db458
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
17 changes: 6 additions & 11 deletions lib/faker.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
}
}
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"emitDeclarationOnly": true,
"noFallthroughCasesInSwitch": true,
"strictNullChecks": true,
"declarationDir": "./types"
"declarationDir": "./types",
"resolveJsonModule": true
},
"include": ["./index.js", "lib/**/*.js"],
"exclude": ["bin"]
Expand Down

0 comments on commit 18db458

Please sign in to comment.