Skip to content

Commit 2afbc3b

Browse files
sgulsethkmelve
andauthoredMar 19, 2024··
fix(codegen): update help text and generated comments copy (#6059)
* fix(codegen): update help text and generated comments copy co-authored-by: Knut Melvær <knut@sanity.io> * fix: new lines Co-authored-by: Knut Melvær <knut@sanity.io> --------- Co-authored-by: Knut Melvær <knut@sanity.io>
1 parent c70653a commit 2afbc3b

File tree

2 files changed

+47
-21
lines changed

2 files changed

+47
-21
lines changed
 

‎packages/sanity/src/_internal/cli/actions/typegen/generateAction.ts

+20-6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@ export interface TypegenGenerateTypesCommandFlags {
1616
configPath?: string
1717
}
1818

19+
const generatedFileWarning = `/**
20+
* ---------------------------------------------------------------------------------
21+
* This file has been generated by Sanity TypeGen.
22+
* Command: \`sanity typegen generate\`
23+
*
24+
* Any modifications made directly to this file will be overwritten the next time
25+
* the TypeScript definitions are generated. Please make changes to the Sanity
26+
* schema definitions and/or GROQ queries if you need to update these types.
27+
*
28+
* For more information on how to use Sanity TypeGen, visit the official documentation:
29+
* https://www.sanity.io/docs/sanity-typegen
30+
* ---------------------------------------------------------------------------------
31+
*/\n\n`
32+
1933
export default async function typegenGenerateAction(
2034
args: CliCommandArguments<TypegenGenerateTypesCommandFlags>,
2135
context: CliCommandContext,
@@ -30,7 +44,7 @@ export default async function typegenGenerateAction(
3044

3145
const rootPkgPath = readPkgUp.sync({cwd: __dirname})?.path
3246
if (!rootPkgPath) {
33-
throw new Error('Could not find root directory for `sanity` package')
47+
throw new Error('Could not find the root directory for the `sanity` package')
3448
}
3549

3650
const workerPath = join(
@@ -60,7 +74,7 @@ export default async function typegenGenerateAction(
6074
constants.O_TRUNC | constants.O_CREAT | constants.O_WRONLY,
6175
)
6276

63-
typeFile.write('// This file is generated by `sanity typegen generate`\n')
77+
typeFile.write(generatedFileWarning)
6478

6579
const stats = {
6680
files: 0,
@@ -91,7 +105,7 @@ export default async function typegenGenerateAction(
91105
return
92106
}
93107

94-
let fileTypeString = `// ${msg.filename}\n`
108+
let fileTypeString = `// Source: ${msg.filename}\n`
95109

96110
if (msg.type === 'schema') {
97111
stats.schemas += msg.length
@@ -102,8 +116,8 @@ export default async function typegenGenerateAction(
102116

103117
stats.files++
104118
for (const {queryName, query, type, unknownTypes} of msg.types) {
105-
fileTypeString += `// ${queryName}\n`
106-
fileTypeString += `// ${query.replace(/(\r\n|\n|\r)/gm, '')}\n`
119+
fileTypeString += `// Variable: ${queryName}\n`
120+
fileTypeString += `// Query: ${query.replace(/(\r\n|\n|\r)/gm, '')}\n`
107121
fileTypeString += `${type}\n`
108122
stats.queries++
109123
stats.unknownTypes += unknownTypes
@@ -131,6 +145,6 @@ export default async function typegenGenerateAction(
131145
}
132146

133147
spinner.succeed(
134-
`Generated TypeScript types for ${stats.schemas} schema types and ${stats.queries} queries in ${stats.files} files into: ${codegenConfig.generates}`,
148+
`Generated TypeScript types for ${stats.schemas} schema types and ${stats.queries} GROQ queries in ${stats.files} files into: ${codegenConfig.generates}`,
135149
)
136150
}

‎packages/sanity/src/_internal/cli/commands/typegen/generateTypesCommand.ts

+27-15
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,39 @@
11
import {type CliCommandDefinition} from '@sanity/cli'
22

3-
const description = 'Generates types'
3+
const description = 'Generates TypeScript types from schema types and GROQ queries'
44

55
const helpText = `
6-
**Note**: This command is experimental and subject to change.
6+
Sanity TypeGen (Beta)
7+
This command is currently in beta and may undergo significant changes. Feedback is welcome!
78
8-
Options
9+
Usage
10+
sanity typegen generate [options]
11+
12+
Options:
913
--help, -h
10-
Show this help text.
14+
Displays this help message, providing information on command usage and options.
15+
16+
Examples:
17+
Generate TypeScript type definitions from a Sanity Studio schema extracted using the \`sanity schema extract\` command.
18+
$ sanity typegen generate
19+
20+
Configuration:
21+
This command can utilize configuration settings defined in a \`sanity-typegen.json\` file. These settings include:
22+
23+
- "path": Specifies a glob pattern to locate your TypeScript or JavaScript files.
24+
Default: "./src/**/*.{ts,tsx,js,jsx}"
25+
26+
- "schema": Defines the path to your Sanity schema file. This file should be generated using the \`sanity schema extract\` command.
27+
Default: "schema.json"
1128
12-
Examples
13-
# Generate types from a schema, generate schema with "sanity schema extract" first.
14-
sanity typegen generate
29+
- "generates": Indicates the path where the generated TypeScript type definitions will be saved.
30+
Default: "./sanity.types.ts"
1531
16-
Configuration
17-
The command uses the following configuration properties from sanity-typegen.json:
18-
{
19-
"path": "'./src/**/*.{ts,tsx,js,jsx}'" // glob pattern to your typescript files
20-
"schema": "schema.json", // path to your schema file, generated with 'sanity schema extract' command
21-
"generates": "./sanity.types.ts" // path to the file where the types will be generated
22-
}
32+
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.
2333
24-
The listed properties are the default values, and can be overridden in the configuration file.
34+
Note:
35+
- 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.
36+
- While this tool is in beta, we encourage you to experiment with these configurations and provide feedback to help improve its functionality and usability.
2537
`
2638

2739
const generateTypegenCommand: CliCommandDefinition = {

0 commit comments

Comments
 (0)
Please sign in to comment.