Skip to content

Commit

Permalink
fix(cli): improve error handling on missing extracted schema
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Apr 3, 2024
1 parent b641174 commit 9061cbc
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 1 deletion.
15 changes: 14 additions & 1 deletion packages/@sanity/cli/src/actions/typegen/generateAction.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {constants, open} from 'node:fs/promises'
import {constants, open, stat} from 'node:fs/promises'
import {join} from 'node:path'

import {readConfig} from '@sanity/codegen'
Expand Down Expand Up @@ -45,6 +45,19 @@ export default async function typegenGenerateAction(
flags.configPath || flags['config-path'] || 'sanity-typegen.json',
)

try {
const {isFile} = await stat(codegenConfig.schema)
if (!isFile) {
throw new Error(`Schema path is not a file: ${codegenConfig.schema}`)
}
} catch (err) {
if (err.code === 'ENOENT') {
// If the user has not provided a specific schema path (eg we're using the default), give some help
const hint =
codegenConfig.schema === './schema.json' ? ` - did you run "sanity schema extract"?` : ''
throw new Error(`Schema file not found: ${codegenConfig.schema}${hint}`)
}
}
const workerPath = await getCliWorkerPath('typegenGenerate')

const spinner = output.spinner({}).start('Generating types')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"schema": "./custom-schema.json"
}
84 changes: 84 additions & 0 deletions packages/@sanity/cli/test/__fixtures__/v3/working-schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
[
{
"name": "person",
"type": "document",
"attributes": {
"_id": {
"type": "objectAttribute",
"value": {
"type": "string"
}
},
"_type": {
"type": "objectAttribute",
"value": {
"type": "string",
"value": "person"
}
},
"_createdAt": {
"type": "objectAttribute",
"value": {
"type": "string"
}
},
"_updatedAt": {
"type": "objectAttribute",
"value": {
"type": "string"
}
},
"_rev": {
"type": "objectAttribute",
"value": {
"type": "string"
}
},
"name": {
"type": "objectAttribute",
"value": {
"type": "string"
},
"optional": true
},
"slug": {
"type": "objectAttribute",
"value": {
"type": "inline",
"name": "slug"
},
"optional": true
}
}
},
{
"name": "slug",
"type": "type",
"value": {
"type": "object",
"attributes": {
"_type": {
"type": "objectAttribute",
"value": {
"type": "string",
"value": "slug"
}
},
"current": {
"type": "objectAttribute",
"value": {
"type": "string"
},
"optional": true
},
"source": {
"type": "objectAttribute",
"value": {
"type": "string"
},
"optional": true
}
}
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"schema": "./working-schema.json"
}
37 changes: 37 additions & 0 deletions packages/@sanity/cli/test/typegen.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {expect, test} from '@jest/globals'

import {describeCliTest} from './shared/describe'
import {runSanityCmdCommand} from './shared/environment'

describeCliTest('CLI: `sanity typegen`', () => {
test('sanity typegen generate: missing schema, default path', async () => {
const err = await runSanityCmdCommand('v3', ['typegen', 'generate']).catch((error) => error)
expect(err.code).toBe(1)
expect(err.stderr).toContain('did you run "sanity schema extract"')
expect(err.stderr).toContain('Schema file not found')
})

test('sanity typegen generate: missing schema, custom path', async () => {
const err = await runSanityCmdCommand('v3', [
'typegen',
'generate',
'--config-path',
'missing-typegen.json',
]).catch((error) => error)
expect(err.code).toBe(1)
expect(err.stderr).not.toContain('did you run "sanity schema extract"')
expect(err.stderr).toContain('custom-schema.json')
})

test('sanity typegen generate: working schema', async () => {
const result = await runSanityCmdCommand('v3', [
'typegen',
'generate',
'--config-path',
'working-typegen.json',
])

expect(result.code).toBe(0)
expect(result.stdout).toContain('Generated TypeScript types for 2 schema types')
})
})

0 comments on commit 9061cbc

Please sign in to comment.