-
Notifications
You must be signed in to change notification settings - Fork 421
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: add docgen script * fixup! chore: add docgen script * fixup! chore: add docgen script
- Loading branch information
1 parent
4d0fc23
commit 80c1045
Showing
10 changed files
with
285 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |