Skip to content

Commit ae7237a

Browse files
committed
refactor: follow oclif conventions around error handling
1 parent b08924b commit ae7237a

File tree

5 files changed

+16
-18
lines changed

5 files changed

+16
-18
lines changed

src/actions/streamProcessor.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
import {writeFile} from 'node:fs/promises'
22

3-
import {subdebug} from '@sanity/cli-core'
43
import {spinner} from '@sanity/cli-core/ux'
54
import {WorkerChannelReceiver} from '@sanity/worker-channels'
65
import {format, resolveConfig as resolvePrettierConfig} from 'prettier'
76

87
import {TypeGenConfig} from '../readConfig.js'
98
import {count} from '../utils/count.js'
9+
import {debug} from '../utils/debug.js'
1010
import {formatPath} from '../utils/formatPath.js'
1111
import {getMessage} from '../utils/getMessage.js'
1212
import {percent} from '../utils/percent.js'
1313
import {generatedFileWarning} from './generatedFileWarning.js'
1414
import {TypegenWorkerChannel} from './types.js'
1515

16-
const debug = subdebug('typegen:generate')
17-
1816
/**
1917
* Processes the event stream from a typegen worker thread.
2018
*

src/actions/typegenGenerate.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import {dirname, isAbsolute, join} from 'node:path'
33
import {env} from 'node:process'
44
import {Worker} from 'node:worker_threads'
55

6-
import {subdebug} from '@sanity/cli-core'
76
import {WorkerChannelReceiver} from '@sanity/worker-channels'
87

98
import {prepareConfig} from '../utils/config.js'
9+
import {debug} from '../utils/debug.js'
1010
import {processTypegenWorkerStream} from './streamProcessor.js'
1111
import {
1212
type GenerationResult,
@@ -15,8 +15,6 @@ import {
1515
TypegenWorkerChannel,
1616
} from './types.js'
1717

18-
const debug = subdebug('typegen:generate')
19-
2018
/**
2119
* Runs a single typegen generation.
2220
*

src/actions/typegenWatch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export function runTypegenWatcher(options: RunTypegenOptions): {
9393
stats.successfulDurations.push(duration)
9494
} catch (err) {
9595
const errorMessage = err instanceof Error ? err.message : err
96-
console.error(` ${chalk.red('›')} ${errorMessage}`)
96+
error(` ${chalk.red('›')} ${errorMessage}`)
9797
stats.failedCount++
9898
}
9999
})

src/commands/typegen/generate.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
import {stat} from 'node:fs/promises'
22

33
import {Flags} from '@oclif/core'
4-
import {SanityCommand, subdebug} from '@sanity/cli-core'
4+
import {SanityCommand} from '@sanity/cli-core'
55
import {chalk, spinner} from '@sanity/cli-core/ux'
66
import {omit, once} from 'lodash-es'
77

88
import {runTypegenGenerate} from '../../actions/typegenGenerate.js'
99
import {runTypegenWatcher} from '../../actions/typegenWatch.js'
1010
import {configDefinition, readConfig, type TypeGenConfig} from '../../readConfig.js'
1111
import {TypegenWatchModeTrace, TypesGeneratedTrace} from '../../typegen.telemetry.js'
12+
import {debug} from '../../utils/debug.js'
1213
import {promiseWithResolvers} from '../../utils/promiseWithResolvers.js'
1314
import {telemetry} from '../../utils/telemetryLogger.js'
1415

15-
const debug = subdebug('typegen:generate')
16-
1716
const description = `Sanity TypeGen (Beta)
1817
This command is currently in beta and may undergo significant changes. Feedback is welcome!
1918
@@ -89,21 +88,21 @@ export class TypegenGenerateCommand extends SanityCommand<typeof TypegenGenerate
8988
// check if the legacy config exist
9089
const legacyConfigPath = configPath || 'sanity-typegen.json'
9190
let hasLegacyConfig = false
91+
9292
try {
9393
const file = await stat(legacyConfigPath)
9494
hasLegacyConfig = file.isFile()
9595
} catch (err) {
9696
if (err instanceof Error && 'code' in err && err.code === 'ENOENT' && configPath) {
97-
throw new Error(`Typegen config file not found: ${configPath}`, {cause: err})
97+
spin.fail()
98+
this.error(`Typegen config file not found: ${configPath}`, {exit: 1})
9899
}
99100

100101
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-
)
102+
spin.fail()
103+
this.error(`Error when checking if typegen config file exists: ${legacyConfigPath}`, {
104+
exit: 1,
105+
})
107106
}
108107
}
109108

@@ -154,7 +153,7 @@ export class TypegenGenerateCommand extends SanityCommand<typeof TypegenGenerate
154153
}
155154
} catch (err) {
156155
spin.fail()
157-
throw err
156+
this.error(`An error occured during config loading ${err}`, {exit: 1})
158157
}
159158
}
160159

src/utils/debug.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import {subdebug} from '@sanity/cli-core'
2+
3+
export const debug = subdebug('typegen:generate')

0 commit comments

Comments
 (0)