Skip to content

Commit b08924b

Browse files
committed
fix: use correct logger and make output more helpful
1 parent 66ccda1 commit b08924b

File tree

3 files changed

+106
-103
lines changed

3 files changed

+106
-103
lines changed

src/actions/streamProcessor.ts

Lines changed: 37 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -34,64 +34,56 @@ export async function processTypegenWorkerStream(
3434
let code = ''
3535

3636
const spin = spinner().start(`Loading schema…`)
37+
3738
try {
38-
try {
39-
await receiver.event.loadedSchema()
40-
spin.succeed(`Schema loaded from ${formatPath(schema ?? '')}`)
41-
} catch (err) {
42-
throw new Error(`Schema processing failed ${err}`, {cause: err})
43-
}
39+
await receiver.event.loadedSchema()
40+
spin.succeed(`Schema loaded from ${formatPath(schema ?? '')}`)
4441

4542
spin.start('Generating schema types…')
43+
const {expectedFileCount} = await receiver.event.typegenStarted()
44+
const {schemaTypeDeclarations} = await receiver.event.generatedSchemaTypes()
45+
const schemaTypesCount = schemaTypeDeclarations.length
46+
47+
spin.text = 'Generating query types…'
48+
4649
let queriesCount = 0
4750
let evaluatedFiles = 0
4851
let filesWithErrors = 0
4952
let queryFilesCount = 0
5053
let typeNodesGenerated = 0
5154
let unknownTypeNodesGenerated = 0
5255
let emptyUnionTypeNodesGenerated = 0
53-
let schemaTypesCount = 0
54-
55-
try {
56-
const {expectedFileCount} = await receiver.event.typegenStarted()
57-
const {schemaTypeDeclarations} = await receiver.event.generatedSchemaTypes()
58-
schemaTypesCount = schemaTypeDeclarations.length
59-
60-
spin.text = 'Generating query types…'
61-
62-
for await (const {errors, queries} of receiver.stream.evaluatedModules()) {
63-
evaluatedFiles++
64-
queriesCount += queries.length
65-
queryFilesCount += queries.length > 0 ? 1 : 0
66-
filesWithErrors += errors.length > 0 ? 1 : 0
67-
68-
for (const {stats} of queries) {
69-
typeNodesGenerated += stats.allTypes
70-
unknownTypeNodesGenerated += stats.unknownTypes
71-
emptyUnionTypeNodesGenerated += stats.emptyUnions
72-
}
73-
74-
for (const error of errors) {
75-
spin.fail(getMessage(error))
76-
}
77-
78-
if (!spin.isSpinning) {
79-
spin.start()
80-
}
81-
82-
spin.text =
83-
`Generating query types… (${percent(evaluatedFiles / expectedFileCount)})\n` +
84-
` └─ Processed ${count(evaluatedFiles)} of ${count(expectedFileCount, 'files')}. ` +
85-
`Found ${count(queriesCount, 'queries', 'query')} from ${count(queryFilesCount, 'files')}.`
56+
57+
for await (const {errors, queries} of receiver.stream.evaluatedModules()) {
58+
evaluatedFiles++
59+
queriesCount += queries.length
60+
queryFilesCount += queries.length > 0 ? 1 : 0
61+
filesWithErrors += errors.length > 0 ? 1 : 0
62+
63+
for (const {stats} of queries) {
64+
typeNodesGenerated += stats.allTypes
65+
unknownTypeNodesGenerated += stats.unknownTypes
66+
emptyUnionTypeNodesGenerated += stats.emptyUnions
8667
}
8768

88-
const result = await receiver.event.typegenComplete()
89-
code = `${generatedFileWarning}${result.code}`
90-
await writeFile(generates, code)
91-
} catch (err) {
92-
throw new Error('Type generation failed', {cause: err})
69+
for (const error of errors) {
70+
spin.fail(getMessage(error))
71+
}
72+
73+
if (!spin.isSpinning) {
74+
spin.start()
75+
}
76+
77+
spin.text =
78+
`Generating query types… (${percent(evaluatedFiles / expectedFileCount)})\n` +
79+
` └─ Processed ${count(evaluatedFiles)} of ${count(expectedFileCount, 'files')}. ` +
80+
`Found ${count(queriesCount, 'queries', 'query')} from ${count(queryFilesCount, 'files')}.`
9381
}
9482

83+
const result = await receiver.event.typegenComplete()
84+
code = `${generatedFileWarning}${result.code}`
85+
await writeFile(generates, code)
86+
9587
let formattingError = false
9688
if (formatGeneratedCode) {
9789
spin.text = `Formatting generated types with prettier…`
@@ -143,7 +135,7 @@ export async function processTypegenWorkerStream(
143135
code,
144136
}
145137
} catch (err) {
146-
spin.fail(`${err}`)
138+
spin.fail()
147139
debug('error generating types', err)
148140
throw err
149141
} finally {

src/actions/typegenWatch.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {error, log} from 'node:console'
22
import {isAbsolute, join, relative} from 'node:path'
33

4+
import {chalk} from '@sanity/cli-core/ux'
45
import chokidar, {FSWatcher} from 'chokidar'
56
import {debounce, mean} from 'lodash-es'
67

@@ -88,9 +89,11 @@ export function runTypegenWatcher(options: RunTypegenOptions): {
8889

8990
const {runGeneration} = createTypegenRunner(async () => {
9091
try {
91-
const {duration} = await runTypegenGenerate(options)
92+
const {duration} = await runTypegenGenerate({...options})
9293
stats.successfulDurations.push(duration)
93-
} catch {
94+
} catch (err) {
95+
const errorMessage = err instanceof Error ? err.message : err
96+
console.error(` ${chalk.red('›')} ${errorMessage}`)
9497
stats.failedCount++
9598
}
9699
})

src/commands/typegen/generate.ts

Lines changed: 64 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -78,75 +78,83 @@ export class TypegenGenerateCommand extends SanityCommand<typeof TypegenGenerate
7878
}> {
7979
const spin = spinner({}).start('Loading config…')
8080

81-
const {flags} = await this.parse(TypegenGenerateCommand)
82-
const rootDir = await this.getProjectRoot()
83-
const config = await this.getCliConfig()
84-
85-
const configPath = flags['config-path']
86-
const workDir = rootDir.directory
87-
88-
// check if the legacy config exist
89-
const legacyConfigPath = configPath || 'sanity-typegen.json'
90-
let hasLegacyConfig = false
9181
try {
92-
const file = await stat(legacyConfigPath)
93-
hasLegacyConfig = file.isFile()
94-
} catch (err) {
95-
if (err instanceof Error && 'code' in err && err.code === 'ENOENT' && configPath) {
96-
throw new Error(`Typegen config file not found: ${configPath}`, {cause: err})
82+
const {flags} = await this.parse(TypegenGenerateCommand)
83+
const rootDir = await this.getProjectRoot()
84+
const config = await this.getCliConfig()
85+
86+
const configPath = flags['config-path']
87+
const workDir = rootDir.directory
88+
89+
// check if the legacy config exist
90+
const legacyConfigPath = configPath || 'sanity-typegen.json'
91+
let hasLegacyConfig = false
92+
try {
93+
const file = await stat(legacyConfigPath)
94+
hasLegacyConfig = file.isFile()
95+
} catch (err) {
96+
if (err instanceof Error && 'code' in err && err.code === 'ENOENT' && configPath) {
97+
throw new Error(`Typegen config file not found: ${configPath}`, {cause: err})
98+
}
99+
100+
if (err instanceof Error && 'code' in err && err.code !== 'ENOENT') {
101+
throw new Error(
102+
`Error when checking if typegen config file exists: ${legacyConfigPath}`,
103+
{
104+
cause: err,
105+
},
106+
)
107+
}
97108
}
98109

99-
if (err instanceof Error && 'code' in err && err.code !== 'ENOENT') {
100-
throw new Error(`Error when checking if typegen config file exists: ${legacyConfigPath}`, {
101-
cause: err,
102-
})
110+
// we have both legacy and cli config with typegen
111+
if (config?.typegen && hasLegacyConfig) {
112+
spin.warn(
113+
chalk.yellow(
114+
`You've specified typegen in your Sanity CLI config, but also have a typegen config.
115+
116+
The config from the Sanity CLI config is used.
117+
`,
118+
),
119+
)
120+
121+
return {
122+
config: configDefinition.parse(config.typegen || {}),
123+
path: rootDir.path,
124+
type: 'cli',
125+
workDir,
126+
}
103127
}
104-
}
105128

106-
// we have both legacy and cli config with typegen
107-
if (config?.typegen && hasLegacyConfig) {
108-
spin.warn(
109-
chalk.yellow(
110-
`You've specified typegen in your Sanity CLI config, but also have a typegen config.
129+
// we only have legacy typegen config
130+
if (hasLegacyConfig) {
131+
spin.warn(
132+
chalk.yellow(
133+
`The separate typegen config has been deprecated. Use \`typegen\` in the sanity CLI config instead.
134+
135+
See: https://www.sanity.io/docs/help/configuring-typegen-in-sanity-cli-config`,
136+
),
137+
)
138+
return {
139+
config: await readConfig(legacyConfigPath),
140+
path: legacyConfigPath,
141+
type: 'legacy',
142+
workDir,
143+
}
144+
}
111145

112-
The config from the Sanity CLI config is used.
113-
`,
114-
),
115-
)
146+
spin.succeed(`Config loaded from sanity.cli.ts`)
116147

148+
// we only have cli config
117149
return {
118150
config: configDefinition.parse(config.typegen || {}),
119151
path: rootDir.path,
120152
type: 'cli',
121153
workDir,
122154
}
123-
}
124-
125-
// we only have legacy typegen config
126-
if (hasLegacyConfig) {
127-
spin.warn(
128-
chalk.yellow(
129-
`The separate typegen config has been deprecated. Use \`typegen\` in the sanity CLI config instead.
130-
131-
See: https://www.sanity.io/docs/help/configuring-typegen-in-sanity-cli-config`,
132-
),
133-
)
134-
return {
135-
config: await readConfig(legacyConfigPath),
136-
path: legacyConfigPath,
137-
type: 'legacy',
138-
workDir,
139-
}
140-
}
141-
142-
spin.succeed(`Config loaded from sanity.cli.ts`)
143-
144-
// we only have cli config
145-
return {
146-
config: configDefinition.parse(config.typegen || {}),
147-
path: rootDir.path,
148-
type: 'cli',
149-
workDir,
155+
} catch (err) {
156+
spin.fail()
157+
throw err
150158
}
151159
}
152160

0 commit comments

Comments
 (0)