Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add docgen script #7387

Merged
merged 3 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dev/test-studio/schema/debug/validation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ export default defineType({
title: 'A geopoint',
description: 'Required, must be in Norway somewhere',
validation: (Rule) =>
Rule.required().custom((geoPoint) => {
Rule.custom((geoPoint) => {
if (!geoPoint) {
return true
}
Expand Down
1 change: 1 addition & 0 deletions dev/test-studio/schema/species.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default {
title: 'Species',
type: 'string',
},
{name: 'description', type: 'text', title: 'Description'},
{
name: 'image',
title: 'Image',
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"deploy:movies": "pnpm build && cd examples/movies-studio && sanity deploy",
"deploy:test": "pnpm build && cd dev/test-studio && sanity deploy",
"dev": "pnpm dev:test-studio",
"generate:docs": "tsx --env-file=.env.local ./scripts/generate-documents/cli.ts",
"dev:cli": "pnpm watch",
"dev:design-studio": "pnpm --filter design-studio dev",
"dev:embedded-studio": "pnpm --filter embedded-studio dev",
Expand Down
123 changes: 123 additions & 0 deletions scripts/generate-documents/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import path from 'node:path'
import {parseArgs} from 'node:util'

import {createClient} from '@sanity/client'
import {tap} from 'rxjs'

import {readEnv} from '../utils/envVars'
import {run} from './run'
import {book} from './templates/book'
import {species} from './templates/species'
import {validation} from './templates/validation'

const {values: args} = parseArgs({
args: process.argv.slice(2),
options: {
number: {
type: 'string',
short: 'n',
default: '1',
},
dataset: {
type: 'string',
},
bundle: {
type: 'string',
},
draft: {
type: 'boolean',
},
published: {
type: 'boolean',
},
size: {
type: 'string',
},
concurrency: {
type: 'string',
short: 'c',
},
template: {
type: 'string',
short: 't',
},
help: {
type: 'boolean',
short: 'h',
},
},
})

const templates = {
validation: validation,
book: book,
species: species,
}

const HELP_TEXT = `Usage: tsx --env-file=.env.local ./${path.relative(process.cwd(), process.argv[1])} --template <template> [arguments]
Arguments:
--template, -t <template>: Template to use (required). Possible values: ${Object.keys(templates).join(', ')}
--dataset: Dataset to generate documents in (defaults to 'test')
--amount, -n <int>: Number of documents to generate
--draft: Generate draft documents
--published: Generate published documents
--bundle <string>: Bundle to generate documents in
--size <bytes>: Size (in bytes) of the generated document (will be approximated)
--concurrency, -c <int>: Number of concurrent requests
--help, -h: Show this help message
Add more templates by adding them to the "./${path.relative(process.cwd(), path.join(__dirname, './templates'))}" directory.
`

if (args.help) {
// eslint-disable-next-line no-console
console.log(HELP_TEXT)
process.exit(0)
}

if (!args.template) {
console.error('Error: Missing required `--template` argument\n')
console.error(HELP_TEXT)
process.exit(1)
}
if (!(args.template in templates)) {
console.error(`Error: Template "${args.template}" does not exist. Available templates: ${Object.keys(templates).join(', ')}
`)
console.error(HELP_TEXT)
process.exit(1)
}

const template = templates[args.template as keyof typeof templates]

type KnownEnvVar = 'TEST_STUDIO_WRITE_TOKEN'

const client = createClient({
projectId: 'ppsg7ml5',
dataset: args.dataset || 'test',
token: readEnv<KnownEnvVar>('TEST_STUDIO_WRITE_TOKEN'),
apiVersion: '2024-07-31',
useCdn: false,
})

run({
bundle: args.bundle,
draft: args.draft || true,
published: args.published,
concurrency: args.concurrency ? Number(args.concurrency) : undefined,
number: args.number ? Number(args.number) : undefined,
size: args.size ? Number(args.size) : undefined,
template,
client,
})
.pipe(
tap({
next: (doc) => {
// eslint-disable-next-line no-console
console.log('Created', doc._id)
},
error: console.error,
}),
)
.subscribe()
59 changes: 59 additions & 0 deletions scripts/generate-documents/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import {type SanityClient, type SanityDocument} from '@sanity/client'
import {mergeMap, range} from 'rxjs'

import {type DocGenTemplate} from './types'

export type ProgramArgs = {
client: SanityClient
number?: number
published?: boolean
size?: number
draft?: boolean
bundle?: string
template: DocGenTemplate
concurrency?: number
}

export function run(_args: ProgramArgs) {
const {client, bundle, draft, concurrency, published, template, number, size} = _args
const runId = Date.now()

return range(0, number || 1).pipe(
mergeMap((i) => {
const id = `${runId}-autogenerated-${i}`

const templateOptions = {
size: size ?? 256,
}

const title = `Generated #${runId.toString(32).slice(2)}/${i}`

const publishedDocument = published
? {...template({...templateOptions, title: `${title} - Published`}), _id: id}
: undefined

const draftDocument = draft
? {
...template({...templateOptions, title: `${title} - Published`}),
_id: `drafts.${id}`,
title: `${title} - Draft`,
}
: undefined

const bundleDocument = bundle
? {
...template({...templateOptions, title: `${title} - Published`}),
_id: `${bundle}.${id}`,
_version: {},
title: `${title} - Bundle: ${bundle}`,
}
: undefined

return [publishedDocument, draftDocument, bundleDocument].flatMap((d) => (d ? [d] : []))
}),
mergeMap((doc) => {
console.log('Creating', doc._id)
return client.observable.create(doc as SanityDocument, {autoGenerateArrayKeys: true})
}, concurrency ?? 2),
)
}
6 changes: 6 additions & 0 deletions scripts/generate-documents/templates/book.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {type DocGenTemplate} from '../types'

export const book: DocGenTemplate = () => ({
_type: 'book',
title: 'Generated book',
})
11 changes: 11 additions & 0 deletions scripts/generate-documents/templates/species.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {type DocGenTemplate} from '../types'
import {loremString} from '../utils/lorem'

export const species: DocGenTemplate = (options) => ({
_type: 'species',
name: options.title,
foo: 'bar',
genus: 'Foo',
species: 'Bar',
description: loremString(options.size),
})
49 changes: 49 additions & 0 deletions scripts/generate-documents/templates/validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {type DocGenTemplate} from '../types'
import {loremString} from '../utils/lorem'

export const validation: DocGenTemplate = (options) => ({
_type: 'validationTest',
arrayOfSlugs: [
{
_type: 'slugEmbed',
sku: {
_type: 'slug',
current: 'foo',
},
},
],
body: [
{
_type: 'block',
children: [
{
_type: 'span',
marks: [],
text: loremString(options.size / 2),
},
],
markDefs: [],
style: 'normal',
},
],
bymonth: [11],
checkbox: true,
dropdown: 'one',
infoValidation: 'moop',
intro: loremString(options.size / 2),
lowestTemperature: 50,
myFancyUrlField: 'http://www.sanity.io',
myUrlField: 'https://www.sanity.io',
radio: 'one',
relativeUrl: '/foo/bar',
slug: {
_type: 'slug',
current: 'slug',
},
switch: false,
title: options.title,
titleCase: 'bar',
translations: {
no: 'asdasdsad',
},
})
8 changes: 8 additions & 0 deletions scripts/generate-documents/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
interface DocGenTemplateOptions {
size: number
title: string
}

type TypedDocument = {_type: string} & Record<string, unknown>

export type DocGenTemplate = (options: DocGenTemplateOptions) => TypedDocument
26 changes: 26 additions & 0 deletions scripts/generate-documents/utils/lorem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const encoder = new TextEncoder()
const decoder = new TextDecoder()
const TEXT = encoder.encode(
`Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. `,
)

/**
* Generate a Uint8Array of lorem ipsum text of the given byte size
* @param byteSize - The size in bytes of the uint array to generate
* @internal
*/
export function loremBytes(byteSize: number): Uint8Array {
const result = new Uint8Array(byteSize)
for (let i = 0; i < byteSize; i++) {
result[i] = TEXT[i % TEXT.byteLength]
}
return result
}

/**
* Generate a lorem ipsum string of the given byte size
* @param byteSize - The size in bytes of the string to generate
*/
export function loremString(byteSize: number): string {
return decoder.decode(loremBytes(byteSize))
}
Loading