@sanity/cli 3.52.4 → 3.53.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/_chunks-cjs/cli.js +3538 -5423
- package/lib/_chunks-cjs/cli.js.map +1 -1
- package/lib/_chunks-cjs/generateAction.js +17 -12
- package/lib/_chunks-cjs/generateAction.js.map +1 -1
- package/lib/workers/typegenGenerate.d.ts +6 -0
- package/lib/workers/typegenGenerate.js +13 -7
- package/lib/workers/typegenGenerate.js.map +1 -1
- package/package.json +7 -7
- package/src/actions/init-project/initProject.ts +6 -3
- package/src/actions/init-project/prompts/nextjs.ts +5 -5
- package/src/actions/init-project/templates/nextjs/index.ts +67 -17
- package/src/actions/init-project/templates/nextjs/schemaTypes/blog.ts +34 -243
- package/src/actions/typegen/generate.telemetry.ts +1 -0
- package/src/actions/typegen/generateAction.ts +27 -18
- package/src/outputters/cliOutputter.ts +2 -2
- package/src/workers/typegenGenerate.ts +23 -5
@@ -1,11 +1,13 @@
|
|
1
1
|
// Types
|
2
2
|
|
3
|
-
const
|
3
|
+
const authorType = `import {UserIcon} from '@sanity/icons'
|
4
|
+
import {defineArrayMember, defineField, defineType} from 'sanity'
|
4
5
|
|
5
6
|
export const authorType = defineType({
|
6
7
|
name: 'author',
|
7
8
|
title: 'Author',
|
8
9
|
type: 'document',
|
10
|
+
icon: UserIcon,
|
9
11
|
fields: [
|
10
12
|
defineField({
|
11
13
|
name: 'name',
|
@@ -16,7 +18,6 @@ export const authorType = defineType({
|
|
16
18
|
type: 'slug',
|
17
19
|
options: {
|
18
20
|
source: 'name',
|
19
|
-
maxLength: 96,
|
20
21
|
},
|
21
22
|
}),
|
22
23
|
defineField({
|
@@ -25,23 +26,16 @@ export const authorType = defineType({
|
|
25
26
|
options: {
|
26
27
|
hotspot: true,
|
27
28
|
},
|
28
|
-
fields: [
|
29
|
-
{
|
30
|
-
name: 'alt',
|
31
|
-
type: 'string',
|
32
|
-
title: 'Alternative Text',
|
33
|
-
}
|
34
|
-
]
|
35
29
|
}),
|
36
30
|
defineField({
|
37
31
|
name: 'bio',
|
38
32
|
type: 'array',
|
39
33
|
of: [
|
40
|
-
{
|
34
|
+
defineArrayMember({
|
41
35
|
type: 'block',
|
42
36
|
styles: [{title: 'Normal', value: 'normal'}],
|
43
37
|
lists: [],
|
44
|
-
},
|
38
|
+
}),
|
45
39
|
],
|
46
40
|
}),
|
47
41
|
],
|
@@ -54,59 +48,7 @@ export const authorType = defineType({
|
|
54
48
|
})
|
55
49
|
`
|
56
50
|
|
57
|
-
const
|
58
|
-
name: 'author',
|
59
|
-
title: 'Author',
|
60
|
-
type: 'document',
|
61
|
-
fields: [
|
62
|
-
{
|
63
|
-
name: 'name',
|
64
|
-
type: 'string',
|
65
|
-
},
|
66
|
-
{
|
67
|
-
name: 'slug',
|
68
|
-
type: 'slug',
|
69
|
-
options: {
|
70
|
-
source: 'name',
|
71
|
-
maxLength: 96,
|
72
|
-
},
|
73
|
-
},
|
74
|
-
{
|
75
|
-
name: 'image',
|
76
|
-
type: 'image',
|
77
|
-
options: {
|
78
|
-
hotspot: true,
|
79
|
-
},
|
80
|
-
fields: [
|
81
|
-
{
|
82
|
-
name: 'alt',
|
83
|
-
type: 'string',
|
84
|
-
title: 'Alternative Text',
|
85
|
-
}
|
86
|
-
]
|
87
|
-
},
|
88
|
-
{
|
89
|
-
name: 'bio',
|
90
|
-
type: 'array',
|
91
|
-
of: [
|
92
|
-
{
|
93
|
-
type: 'block',
|
94
|
-
styles: [{title: 'Normal', value: 'normal'}],
|
95
|
-
lists: [],
|
96
|
-
},
|
97
|
-
],
|
98
|
-
},
|
99
|
-
],
|
100
|
-
preview: {
|
101
|
-
select: {
|
102
|
-
title: 'name',
|
103
|
-
media: 'image',
|
104
|
-
},
|
105
|
-
},
|
106
|
-
}
|
107
|
-
`
|
108
|
-
|
109
|
-
const blockContentTS = `import {defineType, defineArrayMember} from 'sanity'
|
51
|
+
const blockContentType = `import {defineType, defineArrayMember} from 'sanity'
|
110
52
|
import {ImageIcon} from '@sanity/icons'
|
111
53
|
|
112
54
|
/**
|
@@ -184,94 +126,26 @@ export const blockContentType = defineType({
|
|
184
126
|
})
|
185
127
|
`
|
186
128
|
|
187
|
-
const
|
188
|
-
|
189
|
-
/**
|
190
|
-
* This is the schema type for block content used in the post document type
|
191
|
-
* Importing this type into the studio configuration's \`schema\` property
|
192
|
-
* lets you reuse it in other document types with:
|
193
|
-
* {
|
194
|
-
* name: 'someName',
|
195
|
-
* title: 'Some title',
|
196
|
-
* type: 'blockContent'
|
197
|
-
* }
|
198
|
-
*/
|
199
|
-
|
200
|
-
export const blockContentType = {
|
201
|
-
title: 'Block Content',
|
202
|
-
name: 'blockContent',
|
203
|
-
type: 'array',
|
204
|
-
of: [
|
205
|
-
{
|
206
|
-
title: 'Block',
|
207
|
-
type: 'block',
|
208
|
-
// Styles let you set what your user can mark up blocks with. These
|
209
|
-
// correspond with HTML tags, but you can set any title or value
|
210
|
-
// you want and decide how you want to deal with it where you want to
|
211
|
-
// use your content.
|
212
|
-
styles: [
|
213
|
-
{title: 'Normal', value: 'normal'},
|
214
|
-
{title: 'H1', value: 'h1'},
|
215
|
-
{title: 'H2', value: 'h2'},
|
216
|
-
{title: 'H3', value: 'h3'},
|
217
|
-
{title: 'H4', value: 'h4'},
|
218
|
-
{title: 'Quote', value: 'blockquote'},
|
219
|
-
],
|
220
|
-
lists: [{title: 'Bullet', value: 'bullet'}],
|
221
|
-
// Marks let you mark up inline text in the block editor.
|
222
|
-
marks: {
|
223
|
-
// Decorators usually describe a single property – e.g. a typographic
|
224
|
-
// preference or highlighting by editors.
|
225
|
-
decorators: [
|
226
|
-
{title: 'Strong', value: 'strong'},
|
227
|
-
{title: 'Emphasis', value: 'em'},
|
228
|
-
],
|
229
|
-
// Annotations can be any object structure – e.g. a link or a footnote.
|
230
|
-
annotations: [
|
231
|
-
{
|
232
|
-
title: 'URL',
|
233
|
-
name: 'link',
|
234
|
-
type: 'object',
|
235
|
-
fields: [
|
236
|
-
{
|
237
|
-
title: 'URL',
|
238
|
-
name: 'href',
|
239
|
-
type: 'url',
|
240
|
-
},
|
241
|
-
],
|
242
|
-
},
|
243
|
-
],
|
244
|
-
},
|
245
|
-
},
|
246
|
-
// You can add additional types here. Note that you can't use
|
247
|
-
// primitive types such as 'string' and 'number' in the same array
|
248
|
-
// as a block type.
|
249
|
-
{
|
250
|
-
type: 'image',
|
251
|
-
icon: ImageIcon,
|
252
|
-
options: {hotspot: true},
|
253
|
-
fields: [
|
254
|
-
{
|
255
|
-
name: 'alt',
|
256
|
-
type: 'string',
|
257
|
-
title: 'Alternative Text',
|
258
|
-
}
|
259
|
-
]
|
260
|
-
},
|
261
|
-
],
|
262
|
-
}
|
263
|
-
`
|
264
|
-
|
265
|
-
const categoryTS = `import {defineField, defineType} from 'sanity'
|
129
|
+
const categoryType = `import {TagIcon} from '@sanity/icons'
|
130
|
+
import {defineField, defineType} from 'sanity'
|
266
131
|
|
267
132
|
export const categoryType = defineType({
|
268
133
|
name: 'category',
|
134
|
+
title: 'Category',
|
269
135
|
type: 'document',
|
136
|
+
icon: TagIcon,
|
270
137
|
fields: [
|
271
138
|
defineField({
|
272
139
|
name: 'title',
|
273
140
|
type: 'string',
|
274
141
|
}),
|
142
|
+
defineField({
|
143
|
+
name: 'slug',
|
144
|
+
type: 'slug',
|
145
|
+
options: {
|
146
|
+
source: 'title',
|
147
|
+
},
|
148
|
+
}),
|
275
149
|
defineField({
|
276
150
|
name: 'description',
|
277
151
|
type: 'text',
|
@@ -280,29 +154,14 @@ export const categoryType = defineType({
|
|
280
154
|
})
|
281
155
|
`
|
282
156
|
|
283
|
-
const
|
284
|
-
|
285
|
-
title: 'Category',
|
286
|
-
type: 'document',
|
287
|
-
fields: [
|
288
|
-
{
|
289
|
-
name: 'title',
|
290
|
-
type: 'string',
|
291
|
-
},
|
292
|
-
{
|
293
|
-
name: 'description',
|
294
|
-
type: 'text',
|
295
|
-
},
|
296
|
-
],
|
297
|
-
}
|
298
|
-
`
|
299
|
-
|
300
|
-
const postTS = `import {defineField, defineType} from 'sanity'
|
157
|
+
const postType = `import {DocumentTextIcon} from '@sanity/icons'
|
158
|
+
import {defineArrayMember, defineField, defineType} from 'sanity'
|
301
159
|
|
302
160
|
export const postType = defineType({
|
303
161
|
name: 'post',
|
304
162
|
title: 'Post',
|
305
163
|
type: 'document',
|
164
|
+
icon: DocumentTextIcon,
|
306
165
|
fields: [
|
307
166
|
defineField({
|
308
167
|
name: 'title',
|
@@ -313,7 +172,6 @@ export const postType = defineType({
|
|
313
172
|
type: 'slug',
|
314
173
|
options: {
|
315
174
|
source: 'title',
|
316
|
-
maxLength: 96,
|
317
175
|
},
|
318
176
|
}),
|
319
177
|
defineField({
|
@@ -338,7 +196,7 @@ export const postType = defineType({
|
|
338
196
|
defineField({
|
339
197
|
name: 'categories',
|
340
198
|
type: 'array',
|
341
|
-
of: [{type: 'reference', to: {type: 'category'}}],
|
199
|
+
of: [defineArrayMember({type: 'reference', to: {type: 'category'}})],
|
342
200
|
}),
|
343
201
|
defineField({
|
344
202
|
name: 'publishedAt',
|
@@ -363,99 +221,32 @@ export const postType = defineType({
|
|
363
221
|
})
|
364
222
|
`
|
365
223
|
|
366
|
-
const postJS = `export const postType = {
|
367
|
-
name: 'post',
|
368
|
-
title: 'Post',
|
369
|
-
type: 'document',
|
370
|
-
fields: [
|
371
|
-
{
|
372
|
-
name: 'title',
|
373
|
-
type: 'string',
|
374
|
-
},
|
375
|
-
{
|
376
|
-
name: 'slug',
|
377
|
-
type: 'slug',
|
378
|
-
options: {
|
379
|
-
source: 'title',
|
380
|
-
maxLength: 96,
|
381
|
-
},
|
382
|
-
},
|
383
|
-
{
|
384
|
-
name: 'author',
|
385
|
-
type: 'reference',
|
386
|
-
to: {type: 'author'},
|
387
|
-
},
|
388
|
-
{
|
389
|
-
name: 'mainImage',
|
390
|
-
type: 'image',
|
391
|
-
options: {
|
392
|
-
hotspot: true,
|
393
|
-
},
|
394
|
-
fields: [
|
395
|
-
{
|
396
|
-
name: 'alt',
|
397
|
-
type: 'string',
|
398
|
-
title: 'Alternative text',
|
399
|
-
}
|
400
|
-
]
|
401
|
-
},
|
402
|
-
{
|
403
|
-
name: 'categories',
|
404
|
-
type: 'array',
|
405
|
-
of: [{type: 'reference', to: {type: 'category'}}],
|
406
|
-
},
|
407
|
-
{
|
408
|
-
name: 'publishedAt',
|
409
|
-
type: 'datetime',
|
410
|
-
},
|
411
|
-
{
|
412
|
-
name: 'body',
|
413
|
-
type: 'blockContent',
|
414
|
-
},
|
415
|
-
],
|
416
|
-
preview: {
|
417
|
-
select: {
|
418
|
-
title: 'title',
|
419
|
-
author: 'author.name',
|
420
|
-
media: 'mainImage',
|
421
|
-
},
|
422
|
-
prepare(selection) {
|
423
|
-
const {author} = selection
|
424
|
-
return {...selection, subtitle: author && \`by \${author}\`}
|
425
|
-
},
|
426
|
-
},
|
427
|
-
}
|
428
|
-
`
|
429
|
-
|
430
224
|
// Schema definition
|
431
|
-
|
432
225
|
export const blogSchemaTS = `import { type SchemaTypeDefinition } from 'sanity'
|
433
226
|
|
434
|
-
import {blockContentType} from './
|
435
|
-
import {categoryType} from './
|
436
|
-
import {postType} from './
|
437
|
-
import {authorType} from './
|
227
|
+
import {blockContentType} from './blockContentType'
|
228
|
+
import {categoryType} from './categoryType'
|
229
|
+
import {postType} from './postType'
|
230
|
+
import {authorType} from './authorType'
|
438
231
|
|
439
232
|
export const schema: { types: SchemaTypeDefinition[] } = {
|
440
233
|
types: [blockContentType, categoryType, postType, authorType],
|
441
234
|
}
|
442
235
|
`
|
443
236
|
|
444
|
-
export const blogSchemaJS = `import {blockContentType} from './
|
445
|
-
import {categoryType} from './
|
446
|
-
import {postType} from './
|
447
|
-
import {authorType} from './
|
237
|
+
export const blogSchemaJS = `import {blockContentType} from './blockContentType'
|
238
|
+
import {categoryType} from './categoryType'
|
239
|
+
import {postType} from './postType'
|
240
|
+
import {authorType} from './authorType'
|
448
241
|
|
449
242
|
export const schema = {
|
450
243
|
types: [blockContentType, categoryType, postType, authorType],
|
451
244
|
}
|
452
245
|
`
|
453
246
|
|
454
|
-
export const blogSchemaFolder
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
'postType.': useTypeScript ? postTS : postJS,
|
460
|
-
}
|
247
|
+
export const blogSchemaFolder: Record<string, string> = {
|
248
|
+
'authorType.': authorType,
|
249
|
+
'blockContentType.': blockContentType,
|
250
|
+
'categoryType.': categoryType,
|
251
|
+
'postType.': postType,
|
461
252
|
}
|
@@ -10,6 +10,7 @@ interface TypesGeneratedTraceAttrubutes {
|
|
10
10
|
unknownTypeNodesGenerated: number
|
11
11
|
unknownTypeNodesRatio: number
|
12
12
|
emptyUnionTypeNodesGenerated: number
|
13
|
+
configOverloadClientMethods: boolean
|
13
14
|
}
|
14
15
|
|
15
16
|
export const TypesGeneratedTrace = defineTrace<TypesGeneratedTraceAttrubutes>({
|
@@ -77,6 +77,7 @@ export default async function typegenGenerateAction(
|
|
77
77
|
workDir,
|
78
78
|
schemaPath: codegenConfig.schema,
|
79
79
|
searchPath: codegenConfig.path,
|
80
|
+
overloadClientMethods: codegenConfig.overloadClientMethods,
|
80
81
|
prettierConfig,
|
81
82
|
} satisfies TypegenGenerateTypesWorkerData,
|
82
83
|
// eslint-disable-next-line no-process-env
|
@@ -131,25 +132,32 @@ export default async function typegenGenerateAction(
|
|
131
132
|
return
|
132
133
|
}
|
133
134
|
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
135
|
+
if (msg.type === 'types') {
|
136
|
+
stats.queryFilesCount++
|
137
|
+
for (const {
|
138
|
+
queryName,
|
139
|
+
query,
|
140
|
+
type,
|
141
|
+
typeNodesGenerated,
|
142
|
+
unknownTypeNodesGenerated,
|
143
|
+
emptyUnionTypeNodesGenerated,
|
144
|
+
} of msg.types) {
|
145
|
+
fileTypeString += `// Variable: ${queryName}\n`
|
146
|
+
fileTypeString += `// Query: ${query.replace(/(\r\n|\n|\r)/gm, '')}\n`
|
147
|
+
fileTypeString += type
|
148
|
+
stats.queriesCount++
|
149
|
+
stats.typeNodesGenerated += typeNodesGenerated
|
150
|
+
stats.unknownTypeNodesGenerated += unknownTypeNodesGenerated
|
151
|
+
stats.emptyUnionTypeNodesGenerated += emptyUnionTypeNodesGenerated
|
152
|
+
}
|
153
|
+
typeFile.write(fileTypeString)
|
154
|
+
stats.size += Buffer.byteLength(fileTypeString)
|
155
|
+
}
|
156
|
+
|
157
|
+
if (msg.type === 'typemap') {
|
158
|
+
typeFile.write(msg.typeMap)
|
159
|
+
stats.size += Buffer.byteLength(msg.typeMap)
|
150
160
|
}
|
151
|
-
typeFile.write(fileTypeString)
|
152
|
-
stats.size += Buffer.byteLength(fileTypeString)
|
153
161
|
})
|
154
162
|
worker.addListener('error', reject)
|
155
163
|
})
|
@@ -167,6 +175,7 @@ export default async function typegenGenerateAction(
|
|
167
175
|
unknownTypeNodesRatio:
|
168
176
|
stats.typeNodesGenerated > 0 ? stats.unknownTypeNodesGenerated / stats.typeNodesGenerated : 0,
|
169
177
|
emptyUnionTypeNodesGenerated: stats.emptyUnionTypeNodesGenerated,
|
178
|
+
configOverloadClientMethods: codegenConfig.overloadClientMethods,
|
170
179
|
})
|
171
180
|
|
172
181
|
trace.complete()
|
@@ -1,6 +1,6 @@
|
|
1
1
|
/* eslint-disable no-console */
|
2
2
|
import chalk from 'chalk'
|
3
|
-
import ora, {type Ora} from 'ora'
|
3
|
+
import ora, {type Options, type Ora} from 'ora'
|
4
4
|
|
5
5
|
let isFirstClear = true
|
6
6
|
|
@@ -28,7 +28,7 @@ export default {
|
|
28
28
|
isFirstClear = false
|
29
29
|
},
|
30
30
|
|
31
|
-
spinner(options:
|
31
|
+
spinner(options: Options | string): Ora {
|
32
32
|
return ora(options)
|
33
33
|
},
|
34
34
|
}
|
@@ -21,6 +21,7 @@ export interface TypegenGenerateTypesWorkerData {
|
|
21
21
|
schemaPath: string
|
22
22
|
searchPath: string | string[]
|
23
23
|
prettierConfig: PrettierOptions | null
|
24
|
+
overloadClientMethods?: boolean
|
24
25
|
}
|
25
26
|
|
26
27
|
export type TypegenGenerateTypesWorkerMessage =
|
@@ -49,6 +50,11 @@ export type TypegenGenerateTypesWorkerMessage =
|
|
49
50
|
schema: string
|
50
51
|
length: number
|
51
52
|
}
|
53
|
+
| {
|
54
|
+
type: 'typemap'
|
55
|
+
filename: string
|
56
|
+
typeMap: string
|
57
|
+
}
|
52
58
|
| {
|
53
59
|
type: 'complete'
|
54
60
|
}
|
@@ -115,6 +121,8 @@ async function main() {
|
|
115
121
|
queryName: string
|
116
122
|
query: string
|
117
123
|
type: string
|
124
|
+
typeName: string
|
125
|
+
typeNode: TypeNode
|
118
126
|
unknownTypeNodesGenerated: number
|
119
127
|
typeNodesGenerated: number
|
120
128
|
emptyUnionTypeNodesGenerated: number
|
@@ -124,16 +132,17 @@ async function main() {
|
|
124
132
|
const ast = safeParseQuery(query)
|
125
133
|
const queryTypes = typeEvaluate(ast, schema)
|
126
134
|
|
127
|
-
const
|
128
|
-
|
129
|
-
|
130
|
-
)
|
135
|
+
const typeName = `${queryName}Result`
|
136
|
+
const type = typeGenerator.generateTypeNodeTypes(typeName, queryTypes)
|
137
|
+
const code = await maybeFormatCode(type.trim(), opts.prettierConfig)
|
131
138
|
|
132
139
|
const queryTypeStats = walkAndCountQueryTypeNodeStats(queryTypes)
|
133
140
|
fileQueryTypes.push({
|
134
141
|
queryName,
|
135
142
|
query,
|
136
|
-
|
143
|
+
typeName,
|
144
|
+
typeNode: queryTypes,
|
145
|
+
type: code,
|
137
146
|
unknownTypeNodesGenerated: queryTypeStats.unknownTypes,
|
138
147
|
typeNodesGenerated: queryTypeStats.allTypes,
|
139
148
|
emptyUnionTypeNodesGenerated: queryTypeStats.emptyUnions,
|
@@ -159,6 +168,15 @@ async function main() {
|
|
159
168
|
filename: result.filename,
|
160
169
|
} satisfies TypegenGenerateTypesWorkerMessage)
|
161
170
|
}
|
171
|
+
|
172
|
+
if (fileQueryTypes.length > 0 && opts.overloadClientMethods) {
|
173
|
+
const typeMap = typeGenerator.generateQueryMap(fileQueryTypes)
|
174
|
+
parentPort?.postMessage({
|
175
|
+
type: 'typemap',
|
176
|
+
filename: result.filename,
|
177
|
+
typeMap,
|
178
|
+
} satisfies TypegenGenerateTypesWorkerMessage)
|
179
|
+
}
|
162
180
|
}
|
163
181
|
|
164
182
|
parentPort?.postMessage({
|