From ff07ff247ade43af1e3d6116b845aa74c5498773 Mon Sep 17 00:00:00 2001 From: Sindre Gulseth Date: Tue, 19 Mar 2024 16:15:40 +0100 Subject: [PATCH] chore(codegen): refactor codegen cli group to use typegen cli group (#6051) --- packages/@sanity/codegen/README.md | 2 +- packages/sanity/package.config.ts | 8 +++--- .../generate.telemetry.ts} | 0 .../generateAction.ts} | 27 +++++++++---------- .../src/_internal/cli/commands/index.ts | 4 +-- .../generateTypesCommand.ts | 16 +++++------ ...genGenerateTypes.ts => typegenGenerate.ts} | 16 +++++------ 7 files changed, 36 insertions(+), 37 deletions(-) rename packages/sanity/src/_internal/cli/actions/{codegen/generateTypes.telemetry.ts => typegen/generate.telemetry.ts} (100%) rename packages/sanity/src/_internal/cli/actions/{codegen/generateTypesAction.ts => typegen/generateAction.ts} (83%) rename packages/sanity/src/_internal/cli/commands/{codegen => typegen}/generateTypesCommand.ts (68%) rename packages/sanity/src/_internal/cli/threads/{codegenGenerateTypes.ts => typegenGenerate.ts} (89%) diff --git a/packages/@sanity/codegen/README.md b/packages/@sanity/codegen/README.md index 4cb7e1a8288..05bc1d49473 100644 --- a/packages/@sanity/codegen/README.md +++ b/packages/@sanity/codegen/README.md @@ -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. diff --git a/packages/sanity/package.config.ts b/packages/sanity/package.config.ts index 52b891b7098..3b9f66fec53 100644 --- a/packages/sanity/package.config.ts +++ b/packages/sanity/package.config.ts @@ -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', }, }), diff --git a/packages/sanity/src/_internal/cli/actions/codegen/generateTypes.telemetry.ts b/packages/sanity/src/_internal/cli/actions/typegen/generate.telemetry.ts similarity index 100% rename from packages/sanity/src/_internal/cli/actions/codegen/generateTypes.telemetry.ts rename to packages/sanity/src/_internal/cli/actions/typegen/generate.telemetry.ts diff --git a/packages/sanity/src/_internal/cli/actions/codegen/generateTypesAction.ts b/packages/sanity/src/_internal/cli/actions/typegen/generateAction.ts similarity index 83% rename from packages/sanity/src/_internal/cli/actions/codegen/generateTypesAction.ts rename to packages/sanity/src/_internal/cli/actions/typegen/generateAction.ts index b90fd40440f..e91c1e704b0 100644 --- a/packages/sanity/src/_internal/cli/actions/codegen/generateTypesAction.ts +++ b/packages/sanity/src/_internal/cli/actions/typegen/generateAction.ts @@ -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, +export default async function typegenGenerateAction( + args: CliCommandArguments, context: CliCommandContext, ): Promise { const flags = args.extOptions @@ -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) { @@ -39,7 +39,7 @@ export default async function codegenGenerateAction( '_internal', 'cli', 'threads', - 'codegenGenerateTypes.js', + 'typegenGenerate.js', ) const spinner = output.spinner({}).start('Generating types') @@ -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, }) @@ -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, @@ -72,11 +72,10 @@ export default async function codegenGenerateAction( } await new Promise((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 } diff --git a/packages/sanity/src/_internal/cli/commands/index.ts b/packages/sanity/src/_internal/cli/commands/index.ts index 051b9476e65..8263b97ded1 100644 --- a/packages/sanity/src/_internal/cli/commands/index.ts +++ b/packages/sanity/src/_internal/cli/commands/index.ts @@ -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' @@ -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' @@ -98,7 +98,7 @@ const commands: (CliCommandDefinition | CliCommandGroupDefinition)[] = [ queryDocumentsCommand, deleteDocumentsCommand, createDocumentsCommand, - generateTypesCodegenCommand, + generateTypegenCommand, validateDocumentsCommand, graphqlGroup, listGraphQLAPIsCommand, diff --git a/packages/sanity/src/_internal/cli/commands/codegen/generateTypesCommand.ts b/packages/sanity/src/_internal/cli/commands/typegen/generateTypesCommand.ts similarity index 68% rename from packages/sanity/src/_internal/cli/commands/codegen/generateTypesCommand.ts rename to packages/sanity/src/_internal/cli/commands/typegen/generateTypesCommand.ts index 41767fcfa8b..8393e4da45b 100644 --- a/packages/sanity/src/_internal/cli/commands/codegen/generateTypesCommand.ts +++ b/packages/sanity/src/_internal/cli/commands/typegen/generateTypesCommand.ts @@ -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. @@ -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 @@ -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 diff --git a/packages/sanity/src/_internal/cli/threads/codegenGenerateTypes.ts b/packages/sanity/src/_internal/cli/threads/typegenGenerate.ts similarity index 89% rename from packages/sanity/src/_internal/cli/threads/codegenGenerateTypes.ts rename to packages/sanity/src/_internal/cli/threads/typegenGenerate.ts index e94cb527db6..57f7f0818cb 100644 --- a/packages/sanity/src/_internal/cli/threads/codegenGenerateTypes.ts +++ b/packages/sanity/src/_internal/cli/threads/typegenGenerate.ts @@ -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 @@ -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() @@ -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, @@ -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}"...`) @@ -112,7 +112,7 @@ async function main() { ), fatal: false, query, - } satisfies CodegenGenerateTypesWorkerMessage) + } satisfies TypegenGenerateTypesWorkerMessage) } } @@ -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 {