Skip to content

Commit

Permalink
chore(codegen): refactor codegen cli group to use typegen cli group (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sgulseth authored Mar 19, 2024
1 parent 2826c46 commit ff07ff2
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 37 deletions.
2 changes: 1 addition & 1 deletion packages/@sanity/codegen/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# @sanity/codegen

Codegen toolkit for Sanity.io. This package is currently in an experimental state, and subject to change.
Codegen toolkit for Sanity.io, used to generate Typescript types for a Sanity Schema & GROQ queries. This package is currently in an experimental state, and subject to change.
8 changes: 4 additions & 4 deletions packages/sanity/package.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ export default defineConfig({
require: './lib/_internal/cli/threads/extractSchema.js',
default: './lib/_internal/cli/threads/extractSchema.js',
},
'./_internal/cli/threads/codegenGenerateTypes': {
source: './src/_internal/cli/threads/codegenGenerateTypes.ts',
require: './lib/_internal/cli/threads/codegenGenerateTypes.js',
default: './lib/_internal/cli/threads/codegenGenerateTypes.js',
'./_internal/cli/threads/typegenGenerate': {
source: './src/_internal/cli/threads/typegenGenerate.ts',
require: './lib/_internal/cli/threads/typegenGenerate.js',
default: './lib/_internal/cli/threads/typegenGenerate.js',
},
}),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import readPkgUp from 'read-pkg-up'
import {Worker} from 'worker_threads'

import {
type CodegenGenerateTypesWorkerData,
type CodegenGenerateTypesWorkerMessage,
} from '../../threads/codegenGenerateTypes'
import {TypesGeneratedTrace} from './generateTypes.telemetry'
type TypegenGenerateTypesWorkerData,
type TypegenGenerateTypesWorkerMessage,
} from '../../threads/typegenGenerate'
import {TypesGeneratedTrace} from './generate.telemetry'

export interface CodegenGenerateTypesCommandFlags {
export interface TypegenGenerateTypesCommandFlags {
configPath?: string
}

export default async function codegenGenerateAction(
args: CliCommandArguments<CodegenGenerateTypesCommandFlags>,
export default async function typegenGenerateAction(
args: CliCommandArguments<TypegenGenerateTypesCommandFlags>,
context: CliCommandContext,
): Promise<void> {
const flags = args.extOptions
Expand All @@ -26,7 +26,7 @@ export default async function codegenGenerateAction(
const trace = telemetry.trace(TypesGeneratedTrace)
trace.start()

const codegenConfig = await readConfig(flags.configPath || 'sanity-codegen.json')
const codegenConfig = await readConfig(flags.configPath || 'sanity-typegen.json')

const rootPkgPath = readPkgUp.sync({cwd: __dirname})?.path
if (!rootPkgPath) {
Expand All @@ -39,7 +39,7 @@ export default async function codegenGenerateAction(
'_internal',
'cli',
'threads',
'codegenGenerateTypes.js',
'typegenGenerate.js',
)

const spinner = output.spinner({}).start('Generating types')
Expand All @@ -49,7 +49,7 @@ export default async function codegenGenerateAction(
workDir,
schemaPath: codegenConfig.schema,
searchPath: codegenConfig.path,
} satisfies CodegenGenerateTypesWorkerData,
} satisfies TypegenGenerateTypesWorkerData,
// eslint-disable-next-line no-process-env
env: process.env,
})
Expand All @@ -60,7 +60,7 @@ export default async function codegenGenerateAction(
constants.O_TRUNC | constants.O_CREAT | constants.O_WRONLY,
)

typeFile.write('// This file is generated by `sanity codegen generate`\n')
typeFile.write('// This file is generated by `sanity typegen generate`\n')

const stats = {
files: 0,
Expand All @@ -72,11 +72,10 @@ export default async function codegenGenerateAction(
}

await new Promise<void>((resolve, reject) => {
worker.addListener('message', (msg: CodegenGenerateTypesWorkerMessage) => {
worker.addListener('message', (msg: TypegenGenerateTypesWorkerMessage) => {
if (msg.type === 'error') {
trace.error(msg.error)

if (msg.fatal) {
trace.error(msg.error)
reject(msg.error)
return
}
Expand Down
4 changes: 2 additions & 2 deletions packages/sanity/src/_internal/cli/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import enableBackupCommand from './backup/enableBackupCommand'
import listBackupCommand from './backup/listBackupCommand'
import buildCommand from './build/buildCommand'
import checkCommand from './check/checkCommand'
import generateTypesCodegenCommand from './codegen/generateTypesCommand'
import configCheckCommand from './config/configCheckCommand'
import addCorsOriginCommand from './cors/addCorsOriginCommand'
import corsGroup from './cors/corsGroup'
Expand Down Expand Up @@ -51,6 +50,7 @@ import extractSchemaCommand from './schema/extractSchemaCommand'
import schemaGroup from './schema/schemaGroup'
import validateSchemaCommand from './schema/validateSchemaCommand'
import startCommand from './start/startCommand'
import generateTypegenCommand from './typegen/generateTypesCommand'
import uninstallCommand from './uninstall/uninstallCommand'
import inviteUserCommand from './users/inviteUserCommand'
import listUsersCommand from './users/listUsersCommand'
Expand Down Expand Up @@ -98,7 +98,7 @@ const commands: (CliCommandDefinition | CliCommandGroupDefinition)[] = [
queryDocumentsCommand,
deleteDocumentsCommand,
createDocumentsCommand,
generateTypesCodegenCommand,
generateTypegenCommand,
validateDocumentsCommand,
graphqlGroup,
listGraphQLAPIsCommand,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {type CliCommandDefinition} from '@sanity/cli'

const description = 'Generates codegen'
const description = 'Generates types'

const helpText = `
**Note**: This command is experimental and subject to change.
Expand All @@ -11,10 +11,10 @@ Options
Examples
# Generate types from a schema, generate schema with "sanity schema extract" first.
sanity codegen generate-types
sanity typegen generate
Configuration
The codegen command uses the following configuration properties from sanity-codegen.json:
The command uses the following configuration properties from sanity-typegen.json:
{
"path": "'./src/**/*.{ts,tsx,js,jsx}'" // glob pattern to your typescript files
"schema": "schema.json", // path to your schema file, generated with 'sanity schema extract' command
Expand All @@ -24,18 +24,18 @@ The codegen command uses the following configuration properties from sanity-code
The listed properties are the default values, and can be overridden in the configuration file.
`

const generateTypesCodegenCommand: CliCommandDefinition = {
name: 'generate-types',
group: 'codegen',
const generateTypegenCommand: CliCommandDefinition = {
name: 'generate',
group: 'typegen',
signature: '',
description,
helpText,
hideFromHelp: true,
action: async (args, context) => {
const mod = await import('../../actions/codegen/generateTypesAction')
const mod = await import('../../actions/typegen/generateAction')

return mod.default(args, context)
},
} satisfies CliCommandDefinition

export default generateTypesCodegenCommand
export default generateTypegenCommand
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import {isMainThread, parentPort, workerData as _workerData} from 'worker_thread

const $info = createDebug('sanity:codegen:generate:info')

export interface CodegenGenerateTypesWorkerData {
export interface TypegenGenerateTypesWorkerData {
workDir: string
workspaceName?: string
schemaPath: string
searchPath: string | string[]
}

export type CodegenGenerateTypesWorkerMessage =
export type TypegenGenerateTypesWorkerMessage =
| {
type: 'error'
error: Error
Expand Down Expand Up @@ -50,7 +50,7 @@ if (isMainThread || !parentPort) {
throw new Error('This module must be run as a worker thread')
}

const opts = _workerData as CodegenGenerateTypesWorkerData
const opts = _workerData as TypegenGenerateTypesWorkerData

registerBabel()

Expand All @@ -69,7 +69,7 @@ async function main() {
schema: schemaTypes,
filename: 'schema.json',
length: schema.length,
} satisfies CodegenGenerateTypesWorkerMessage)
} satisfies TypegenGenerateTypesWorkerMessage)

const queries = findQueriesInPath({
path: opts.searchPath,
Expand All @@ -83,7 +83,7 @@ async function main() {
error: result.error,
fatal: false,
filename: result.filename,
} satisfies CodegenGenerateTypesWorkerMessage)
} satisfies TypegenGenerateTypesWorkerMessage)
continue
}
$info(`Processing ${result.queries.length} queries in "${result.filename}"...`)
Expand Down Expand Up @@ -112,7 +112,7 @@ async function main() {
),
fatal: false,
query,
} satisfies CodegenGenerateTypesWorkerMessage)
} satisfies TypegenGenerateTypesWorkerMessage)
}
}

Expand All @@ -122,13 +122,13 @@ async function main() {
type: 'types',
types: fileQueryTypes,
filename: result.filename,
} satisfies CodegenGenerateTypesWorkerMessage)
} satisfies TypegenGenerateTypesWorkerMessage)
}
}

parentPort?.postMessage({
type: 'complete',
} satisfies CodegenGenerateTypesWorkerMessage)
} satisfies TypegenGenerateTypesWorkerMessage)
}

function countUnknownTypes(typeNode: TypeNode): number {
Expand Down

0 comments on commit ff07ff2

Please sign in to comment.