Skip to content

Commit efd7a95

Browse files
authored
fix(cli): bump cli dependencies (#43)
1 parent 801eaed commit efd7a95

File tree

9 files changed

+374
-295
lines changed

9 files changed

+374
-295
lines changed

bin/run.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env node
22
import {execute} from '@oclif/core'
3+
import {CLI_TELEMETRY_SYMBOL} from '@sanity/cli-core'
34

45
const err = '\u001B[31m\u001B[1mERROR:\u001B[22m\u001B[39m '
56
const nodeVersionParts = process.version.replace(/^v/i, '').split('.').map(Number)
@@ -21,11 +22,30 @@ function isSupportedNodeVersion(major, minor, patch) {
2122
}
2223

2324
if (!isSupportedNodeVersion(majorVersion, minorVersion, patchVersion)) {
25+
// eslint-disable-next-line no-console
2426
console.error(
2527
`${err}Node.js version >=20.19.1 <22 or >=22.12 required. You are running ${process.version}`,
2628
)
29+
// eslint-disable-next-line no-console
2730
console.error('')
2831
process.exit(1)
2932
}
3033

34+
if (process.env.NODE_ENV !== 'production') {
35+
/**
36+
* Telemetry is added via a plugin in the main CLI.
37+
* This adds a mock implementation of the telemetry API to allow running this CLI for testing.
38+
*
39+
* We won't be exposing this API to the public, so it's ok to use a globalThis assignment.
40+
*/
41+
globalThis[CLI_TELEMETRY_SYMBOL] = {
42+
trace: () => ({
43+
complete: () => {},
44+
error: () => {},
45+
log: () => {},
46+
start: () => {},
47+
}),
48+
}
49+
}
50+
3151
await execute({dir: import.meta.url})

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@
6666
"@babel/types": "^7.28.6",
6767
"@oclif/core": "^4.8.0",
6868
"@oclif/plugin-help": "^6.2.36",
69-
"@sanity/cli-core": "^0.1.0-alpha.8",
69+
"@sanity/cli-core": "^0.1.0-alpha.10",
7070
"@sanity/worker-channels": "^1.1.0",
71+
"chokidar": "^3.6.0",
7172
"debug": "^4.4.3",
7273
"globby": "^11.1.0",
7374
"groq": "^5.2.0",
@@ -77,16 +78,15 @@
7778
"prettier": "^3.7.4",
7879
"reselect": "^5.1.1",
7980
"tsconfig-paths": "^4.2.0",
80-
"zod": "^4.3.6",
81-
"chokidar": "^3.6.0"
81+
"zod": "^4.3.6"
8282
},
8383
"devDependencies": {
8484
"@eslint/compat": "^2.0.1",
85-
"@sanity/telemetry": "^0.8.0",
8685
"@microsoft/api-extractor": "^7.55.2",
8786
"@oclif/test": "^4.1.15",
88-
"@sanity/cli-test": "^0.0.2-alpha.7",
89-
"@sanity/eslint-config-cli": "0.0.0-alpha.1",
87+
"@sanity/cli-test": "^0.0.2-alpha.9",
88+
"@sanity/eslint-config-cli": "0.0.0-alpha.2",
89+
"@sanity/telemetry": "^0.8.0",
9090
"@swc/cli": "^0.7.9",
9191
"@swc/core": "^1.15.8",
9292
"@types/babel__core": "^7.20.5",

pnpm-lock.yaml

Lines changed: 305 additions & 230 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/actions/typegenWatch.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {error, log} from 'node:console'
22
import {isAbsolute, join, relative} from 'node:path'
3+
import {styleText} from 'node:util'
34

4-
import {chalk} from '@sanity/cli-core/ux'
55
import chokidar, {FSWatcher} from 'chokidar'
66
import {debounce, mean} from 'lodash-es'
77

@@ -19,7 +19,7 @@ const IGNORED_PATTERNS = [
1919
]
2020

2121
/** State for tracking generation status */
22-
export interface WatchState {
22+
interface WatchState {
2323
isGenerating: boolean
2424
pendingGeneration: boolean
2525
}
@@ -37,7 +37,7 @@ type WatcherStats = Omit<Extract<TypegenWatchModeTraceAttributes, {step: 'stoppe
3737
* If generation is already running, queues one more generation to run after completion.
3838
* Multiple queued requests are coalesced into a single pending generation.
3939
*/
40-
export function createTypegenRunner(onGenerate: () => Promise<unknown>): TypegenRunner {
40+
function createTypegenRunner(onGenerate: () => Promise<unknown>): TypegenRunner {
4141
const state: WatchState = {
4242
isGenerating: false,
4343
pendingGeneration: false,
@@ -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-
error(` ${chalk.red('›')} ${errorMessage}`)
96+
error(` ${styleText('red', '›')} ${errorMessage}`)
9797
stats.failedCount++
9898
}
9999
})

src/actions/types.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {WorkerChannel} from '@sanity/worker-channels'
33

44
import {TypeGenConfig} from '../readConfig.js'
55
import {type TypegenWorkerChannel as CodegenTypegenWorkerChannel} from '../typescript/typeGenerator.js'
6-
import {telemetry} from '../utils/telemetryLogger.js'
76

87
/**
98
* Data passed to the typegen worker thread.
@@ -47,9 +46,6 @@ export interface RunTypegenOptions {
4746

4847
/** Optional spinner instance for progress display */
4948
spin?: ReturnType<typeof spinner>
50-
51-
/** Optional telemetry instance for tracking usage */
52-
telemetry?: typeof telemetry
5349
}
5450

5551
/**

src/commands/typegen/__tests__/generate.test.ts

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {readFile, writeFile} from 'node:fs/promises'
33
import {join} from 'node:path'
44

55
import {runCommand} from '@oclif/test'
6-
import {testCommand, testExample} from '@sanity/cli-test'
6+
import {testCommand, testFixture} from '@sanity/cli-test'
77
import {once} from 'lodash-es'
88
import {beforeEach, describe, expect, test, vi} from 'vitest'
99

@@ -20,15 +20,6 @@ const mockTrace = {
2020
start: vi.fn(),
2121
}
2222

23-
vi.mock('../../../utils/telemetryLogger.js', () => ({
24-
telemetry: {
25-
trace: vi.fn(() => mockTrace),
26-
},
27-
}))
28-
29-
// Import mock after vi.mock to access it in tests
30-
const {telemetry} = await import('../../../utils/telemetryLogger.js')
31-
3223
describe('#typegen:generate', () => {
3324
beforeEach(() => {
3425
vi.clearAllMocks()
@@ -95,7 +86,7 @@ describe('#typegen:generate', () => {
9586
})
9687

9788
test('should error when no extracted schema is found', async () => {
98-
const cwd = await testExample('basic-studio')
89+
const cwd = await testFixture('basic-studio')
9990
process.chdir(cwd)
10091

10192
const {error} = await testCommand(TypegenGenerateCommand, [])
@@ -107,7 +98,7 @@ describe('#typegen:generate', () => {
10798
})
10899

109100
test('should generate types from queries', async () => {
110-
const cwd = await testExample('dev')
101+
const cwd = await testFixture('dev')
111102
process.chdir(cwd)
112103

113104
const {error, stderr} = await testCommand(TypegenGenerateCommand, [])
@@ -129,7 +120,7 @@ describe('#typegen:generate', () => {
129120
})
130121

131122
test('should generate types when schema is an absolute path', async () => {
132-
const cwd = await testExample('dev')
123+
const cwd = await testFixture('dev')
133124
process.chdir(cwd)
134125

135126
// Create config with absolute schema path
@@ -155,7 +146,7 @@ describe('#typegen:generate', () => {
155146
})
156147

157148
test('does not format generated types when formatGeneratedCode is false', async () => {
158-
const cwd = await testExample('dev')
149+
const cwd = await testFixture('dev')
159150
process.chdir(cwd)
160151

161152
await writeFile(
@@ -178,15 +169,21 @@ describe('#typegen:generate', () => {
178169
})
179170

180171
test('emits TypesGeneratedTrace telemetry on successful generation', async () => {
181-
const cwd = await testExample('dev')
172+
const cwd = await testFixture('dev')
182173
process.chdir(cwd)
183174

184-
const {error} = await testCommand(TypegenGenerateCommand, [])
175+
const mockTelemetry = vi.fn(() => mockTrace)
176+
177+
const {error} = await testCommand(TypegenGenerateCommand, [], {
178+
mocks: {
179+
trace: mockTelemetry,
180+
},
181+
})
185182

186183
expect(error).toBeUndefined()
187184

188185
// Verify telemetry.trace was called with TypesGeneratedTrace
189-
expect(telemetry.trace).toHaveBeenCalledWith(TypesGeneratedTrace)
186+
expect(mockTelemetry).toHaveBeenCalledWith(TypesGeneratedTrace)
190187

191188
// Verify the trace lifecycle methods were called in order
192189
expect(mockTrace.start).toHaveBeenCalled()
@@ -205,23 +202,29 @@ describe('#typegen:generate', () => {
205202
})
206203

207204
test('emits TypesGeneratedTrace error on failed generation', async () => {
208-
const cwd = await testExample('basic-studio')
205+
const cwd = await testFixture('basic-studio')
209206
process.chdir(cwd)
210207

211-
const {error} = await testCommand(TypegenGenerateCommand, [])
208+
const mockTelemetry = vi.fn(() => mockTrace)
209+
210+
const {error} = await testCommand(TypegenGenerateCommand, [], {
211+
mocks: {
212+
trace: mockTelemetry,
213+
},
214+
})
212215

213216
expect(error).toBeDefined()
214217

215218
// Verify telemetry.trace was called with TypesGeneratedTrace
216-
expect(telemetry.trace).toHaveBeenCalledWith(TypesGeneratedTrace)
219+
expect(mockTelemetry).toHaveBeenCalledWith(TypesGeneratedTrace)
217220

218221
// Verify error was logged
219222
expect(mockTrace.error).toHaveBeenCalledWith(expect.any(Error))
220223
expect(mockTrace.complete).not.toHaveBeenCalled()
221224
})
222225

223226
test('shows warning when legacy config and cli config are present', async () => {
224-
const cwd = await testExample('dev')
227+
const cwd = await testFixture('dev')
225228
process.chdir(cwd)
226229

227230
await writeFile(
@@ -255,7 +258,7 @@ describe('#typegen:generate', () => {
255258
})
256259

257260
test('shows warning when only legacy config is present', async () => {
258-
const cwd = await testExample('dev')
261+
const cwd = await testFixture('dev')
259262
process.chdir(cwd)
260263

261264
await writeFile(
@@ -279,7 +282,7 @@ describe('#typegen:generate', () => {
279282
})
280283

281284
test('shows an error when the legacy config file passed as a flag does not exist', async () => {
282-
const cwd = await testExample('dev')
285+
const cwd = await testFixture('dev')
283286
process.chdir(cwd)
284287

285288
const {error} = await testCommand(TypegenGenerateCommand, ['--config-path', 'typegen.json'])
@@ -291,7 +294,7 @@ describe('#typegen:generate', () => {
291294

292295
describe('watch mode', () => {
293296
test('generates on startup', async () => {
294-
const cwd = await testExample('dev')
297+
const cwd = await testFixture('dev')
295298
process.chdir(cwd)
296299

297300
await testLongRunning(['typegen', 'generate', '--watch'], {
@@ -305,7 +308,7 @@ describe('#typegen:generate', () => {
305308
})
306309

307310
test('generates when a file is created', async () => {
308-
const cwd = await testExample('dev')
311+
const cwd = await testFixture('dev')
309312
process.chdir(cwd)
310313

311314
const randomFilename = `${Math.random().toFixed(18)}file.ts`

src/commands/typegen/generate.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import {stat} from 'node:fs/promises'
2+
import {styleText} from 'node:util'
23

34
import {Flags} from '@oclif/core'
45
import {SanityCommand} from '@sanity/cli-core'
5-
import {chalk, spinner} from '@sanity/cli-core/ux'
6+
import {spinner} from '@sanity/cli-core/ux'
67
import {omit, once} from 'lodash-es'
78

89
import {runTypegenGenerate} from '../../actions/typegenGenerate.js'
@@ -11,12 +12,11 @@ import {configDefinition, readConfig, type TypeGenConfig} from '../../readConfig
1112
import {TypegenWatchModeTrace, TypesGeneratedTrace} from '../../typegen.telemetry.js'
1213
import {debug} from '../../utils/debug.js'
1314
import {promiseWithResolvers} from '../../utils/promiseWithResolvers.js'
14-
import {telemetry} from '../../utils/telemetryLogger.js'
1515

1616
const description = `Sanity TypeGen (Beta)
1717
This command is currently in beta and may undergo significant changes. Feedback is welcome!
1818
19-
${chalk.bold('Configuration:')}
19+
${styleText('bold', 'Configuration:')}
2020
This command can utilize configuration settings defined in a \`sanity-typegen.json\` file. These settings include:
2121
2222
- "path": Specifies a glob pattern to locate your TypeScript or JavaScript files.
@@ -30,7 +30,7 @@ This command can utilize configuration settings defined in a \`sanity-typegen.js
3030
3131
The default configuration values listed above are used if not overridden in your \`sanity-typegen.json\` configuration file. To customize the behavior of the type generation, adjust these properties in the configuration file according to your project's needs.
3232
33-
${chalk.bold('Note:')}
33+
${styleText('bold', 'Note:')}
3434
- The \`sanity schema extract\` command is a prerequisite for extracting your Sanity Studio schema into a \`schema.json\` file, which is then used by the \`sanity typegen generate\` command to generate type definitions.
3535
- While this tool is in beta, we encourage you to experiment with these configurations and provide feedback to help improve its functionality and usability.`.trim()
3636

@@ -109,7 +109,8 @@ export class TypegenGenerateCommand extends SanityCommand<typeof TypegenGenerate
109109
// we have both legacy and cli config with typegen
110110
if (config?.typegen && hasLegacyConfig) {
111111
spin.warn(
112-
chalk.yellow(
112+
styleText(
113+
'yellow',
113114
`You've specified typegen in your Sanity CLI config, but also have a typegen config.
114115
115116
The config from the Sanity CLI config is used.
@@ -128,7 +129,8 @@ export class TypegenGenerateCommand extends SanityCommand<typeof TypegenGenerate
128129
// we only have legacy typegen config
129130
if (hasLegacyConfig) {
130131
spin.warn(
131-
chalk.yellow(
132+
styleText(
133+
'yellow',
132134
`The separate typegen config has been deprecated. Use \`typegen\` in the sanity CLI config instead.
133135
134136
See: https://www.sanity.io/docs/help/configuring-typegen-in-sanity-cli-config`,
@@ -158,7 +160,7 @@ export class TypegenGenerateCommand extends SanityCommand<typeof TypegenGenerate
158160
}
159161

160162
private async runSingle() {
161-
const trace = telemetry.trace(TypesGeneratedTrace)
163+
const trace = this.telemetry.trace(TypesGeneratedTrace)
162164

163165
try {
164166
const {config: typegenConfig, type: typegenConfigMethod, workDir} = await this.getConfig()
@@ -187,7 +189,7 @@ export class TypegenGenerateCommand extends SanityCommand<typeof TypegenGenerate
187189
}
188190

189191
private async runWatcher() {
190-
const trace = telemetry.trace(TypegenWatchModeTrace)
192+
const trace = this.telemetry.trace(TypegenWatchModeTrace)
191193

192194
try {
193195
const {config: typegenConfig, workDir} = await this.getConfig()

src/utils/telemetryLogger.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

test/cliTestSetup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {type TestProject} from 'vitest/node'
33

44
export function setup(project: TestProject) {
55
return cliSetup(project, {
6-
additionalExamples: ['dev'],
6+
additionalFixtures: ['dev'],
77
})
88
}
99

0 commit comments

Comments
 (0)