|
| 1 | +import {constants, open} from 'node:fs/promises' |
| 2 | +import {dirname, join} from 'node:path' |
| 3 | + |
| 4 | +import {type CliCommandArguments, type CliCommandContext} from '@sanity/cli' |
| 5 | +import {readConfig} from '@sanity/codegen' |
| 6 | +import readPkgUp from 'read-pkg-up' |
| 7 | +import {Worker} from 'worker_threads' |
| 8 | + |
| 9 | +import { |
| 10 | + type CodegenGenerateTypesWorkerData, |
| 11 | + type CodegenGenerateTypesWorkerMessage, |
| 12 | +} from '../../threads/codegenGenerateTypes' |
| 13 | +import {TypesGeneratedTrace} from './generateTypes.telemetry' |
| 14 | + |
| 15 | +export interface CodegenGenerateTypesCommandFlags { |
| 16 | + configPath?: string |
| 17 | +} |
| 18 | + |
| 19 | +export default async function codegenGenerateAction( |
| 20 | + args: CliCommandArguments<CodegenGenerateTypesCommandFlags>, |
| 21 | + context: CliCommandContext, |
| 22 | +): Promise<void> { |
| 23 | + const flags = args.extOptions |
| 24 | + const {output, workDir, telemetry} = context |
| 25 | + |
| 26 | + const trace = telemetry.trace(TypesGeneratedTrace) |
| 27 | + trace.start() |
| 28 | + |
| 29 | + const codegenConfig = await readConfig(flags.configPath || 'sanity-codegen.json') |
| 30 | + |
| 31 | + const rootPkgPath = readPkgUp.sync({cwd: __dirname})?.path |
| 32 | + if (!rootPkgPath) { |
| 33 | + throw new Error('Could not find root directory for `sanity` package') |
| 34 | + } |
| 35 | + |
| 36 | + const workerPath = join( |
| 37 | + dirname(rootPkgPath), |
| 38 | + 'lib', |
| 39 | + '_internal', |
| 40 | + 'cli', |
| 41 | + 'threads', |
| 42 | + 'codegenGenerateTypes.js', |
| 43 | + ) |
| 44 | + |
| 45 | + const spinner = output.spinner({}).start('Generating types') |
| 46 | + |
| 47 | + const worker = new Worker(workerPath, { |
| 48 | + workerData: { |
| 49 | + workDir, |
| 50 | + schemaPath: codegenConfig.schema, |
| 51 | + searchPath: codegenConfig.path, |
| 52 | + } satisfies CodegenGenerateTypesWorkerData, |
| 53 | + // eslint-disable-next-line no-process-env |
| 54 | + env: process.env, |
| 55 | + }) |
| 56 | + |
| 57 | + const typeFile = await open( |
| 58 | + join(process.cwd(), codegenConfig.generates), |
| 59 | + // eslint-disable-next-line no-bitwise |
| 60 | + constants.O_TRUNC | constants.O_CREAT | constants.O_WRONLY, |
| 61 | + ) |
| 62 | + |
| 63 | + typeFile.write('// This file is generated by `sanity codegen generate`\n') |
| 64 | + |
| 65 | + const stats = { |
| 66 | + files: 0, |
| 67 | + errors: 0, |
| 68 | + queries: 0, |
| 69 | + schemas: 0, |
| 70 | + unknownTypes: 0, |
| 71 | + size: 0, |
| 72 | + } |
| 73 | + |
| 74 | + await new Promise<void>((resolve, reject) => { |
| 75 | + worker.addListener('message', (msg: CodegenGenerateTypesWorkerMessage) => { |
| 76 | + if (msg.type === 'error') { |
| 77 | + trace.error(msg.error) |
| 78 | + |
| 79 | + if (msg.fatal) { |
| 80 | + reject(msg.error) |
| 81 | + return |
| 82 | + } |
| 83 | + const errorMessage = msg.filename |
| 84 | + ? `${msg.error.message} in "${msg.filename}"` |
| 85 | + : msg.error.message |
| 86 | + spinner.fail(errorMessage) |
| 87 | + stats.errors++ |
| 88 | + return |
| 89 | + } |
| 90 | + if (msg.type === 'complete') { |
| 91 | + resolve() |
| 92 | + return |
| 93 | + } |
| 94 | + |
| 95 | + let fileTypeString = `// ${msg.filename}\n` |
| 96 | + |
| 97 | + if (msg.type === 'schema') { |
| 98 | + stats.schemas += msg.length |
| 99 | + fileTypeString += `${msg.schema}\n\n` |
| 100 | + typeFile.write(fileTypeString) |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + stats.files++ |
| 105 | + for (const {queryName, query, type, unknownTypes} of msg.types) { |
| 106 | + fileTypeString += `// ${queryName}\n` |
| 107 | + fileTypeString += `// ${query.replace(/(\r\n|\n|\r)/gm, '')}\n` |
| 108 | + fileTypeString += `${type}\n` |
| 109 | + stats.queries++ |
| 110 | + stats.unknownTypes += unknownTypes |
| 111 | + } |
| 112 | + typeFile.write(`${fileTypeString}\n`) |
| 113 | + stats.size += Buffer.byteLength(fileTypeString) |
| 114 | + }) |
| 115 | + worker.addListener('error', reject) |
| 116 | + }) |
| 117 | + |
| 118 | + typeFile.close() |
| 119 | + |
| 120 | + trace.log({ |
| 121 | + outputSize: stats.size, |
| 122 | + queryTypes: stats.queries, |
| 123 | + schemaTypes: stats.schemas, |
| 124 | + files: stats.files, |
| 125 | + filesWithErrors: stats.errors, |
| 126 | + unknownTypes: stats.unknownTypes, |
| 127 | + }) |
| 128 | + |
| 129 | + trace.complete() |
| 130 | + if (stats.errors > 0) { |
| 131 | + spinner.warn(`Encountered errors in ${stats.errors} files while generating types`) |
| 132 | + } |
| 133 | + |
| 134 | + spinner.succeed( |
| 135 | + `Generated TypeScript types for ${stats.schemas} schema types and ${stats.queries} queries in ${stats.files} files into: ${codegenConfig.generates}`, |
| 136 | + ) |
| 137 | +} |
0 commit comments