Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

[ -n "$CI" ] && exit 0

. "$(dirname -- "$0")/_/husky.sh"

npx lint-staged
3 changes: 3 additions & 0 deletions bin/generate-mapeo-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import path from 'node:path'
import { parseArgs } from 'node:util'

import { generate } from '../index.js'
import { assertValidSchemaName } from '../lib/schema.js'

const { values } = parseArgs({
strict: true,
Expand Down Expand Up @@ -42,6 +43,8 @@ if (values.version !== undefined && typeof values.version !== 'string')
// TODO: Ideally validate the parsed int
const count = values.count ? Number.parseInt(values.count, 10) : 1

assertValidSchemaName(values.schema)

const data = generate(values.schema, { count, version: values.version })

if (values.output) {
Expand Down
34 changes: 34 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { includeIgnoreFile } from '@eslint/compat'
import js from '@eslint/js'
import { defineConfig } from 'eslint/config'
import globals from 'globals'

const gitignorePath = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
'.gitignore',
)

const gitExcludePath = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
'.git',
'info',
'exclude',
)

export default defineConfig([
includeIgnoreFile(gitignorePath),
includeIgnoreFile(gitExcludePath),
js.configs.recommended,
{
name: 'node',
languageOptions: {
globals: {
...globals.commonjs,
...globals.node,
...globals.nodeBuiltin,
},
},
},
])
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { docSchemas } from '@comapeo/schema'
import { JSONSchemaFaker, createFakerSchema } from './lib/faker.js'
import { extractSchemaVersion, isValidSchemaName } from './lib/schema.js'
import { extractSchemaVersion } from './lib/schema.js'
/** @typedef {import('@comapeo/schema/dist/types.js').SchemaName} SchemaName */

export function listSchemas() {
Expand Down Expand Up @@ -36,8 +36,6 @@ function getFakerSchema(schemaName) {
* @returns {Array<Extract<import('@comapeo/schema').MapeoDoc, { schemaName: TSchemaName }>>}
*/
export function generate(schemaName, { count = 1 } = {}) {
isValidSchemaName(schemaName)

const schema = getFakerSchema(schemaName)

/** @type {Array<Extract<import('@comapeo/schema').MapeoDoc, { schemaName: TSchemaName }>>} */
Expand All @@ -46,7 +44,10 @@ export function generate(schemaName, { count = 1 } = {}) {
for (let i = 0; i < count; i++) {
result.push(
/** @type {Extract<import('@comapeo/schema').MapeoDoc, { schemaName: TSchemaName }>} */ (
JSONSchemaFaker.generate(schema)
JSONSchemaFaker.generate(
// @ts-expect-error
schema,
)
),
)
}
Expand Down
4 changes: 1 addition & 3 deletions lib/faker.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,7 @@ export { JSONSchemaFaker, createFakerSchema }
* @param {ValidSchema} schema
*/
function createFakerSchema(schema) {
// TODO: Ideally uses structured clone but not supported in Node 16
/** @type {import('type-fest').WritableDeep<ValidSchema>} */
const s = JSON.parse(JSON.stringify(schema))
const s = structuredClone(schema)

mutateWithFakerProperty(s.properties.docId, 'mapeo.id')
mutateWithFakerProperty(s.properties.versionId, 'mapeo.versionId')
Expand Down
11 changes: 8 additions & 3 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ import { docSchemas } from '@comapeo/schema'
* @returns {string}
*/
export function extractSchemaVersion(jsonSchemaId) {
const s = jsonSchemaId.split('/')
return s[s.length - 1].replace('.json', '')
const result = jsonSchemaId.split('/').at(-1)?.replace('.json', '')

if (!result) {
throw new Error('Unable to extract schema version')
}

return result
}

/**
Expand All @@ -15,7 +20,7 @@ export function extractSchemaVersion(jsonSchemaId) {
*
* @return {asserts name is import('@comapeo/schema/dist/types.js').SchemaName}
*/
export function isValidSchemaName(name) {
export function assertValidSchemaName(name) {
if (!(name in docSchemas)) {
throw new Error(`Could not find schema ${name}`)
}
Expand Down
4 changes: 4 additions & 0 deletions lint-staged.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @type {import('lint-staged').Configuration} */
export default {
'*.{js,md}': 'prettier --write',
}
Loading
Loading