|
| 1 | +import path from 'node:path' |
| 2 | +import {parseArgs} from 'node:util' |
| 3 | + |
| 4 | +import {createClient} from '@sanity/client' |
| 5 | +import {tap} from 'rxjs' |
| 6 | + |
| 7 | +import {readEnv} from '../utils/envVars' |
| 8 | +import {run} from './run' |
| 9 | +import {book} from './templates/book' |
| 10 | +import {species} from './templates/species' |
| 11 | +import {validation} from './templates/validation' |
| 12 | + |
| 13 | +const {values: args} = parseArgs({ |
| 14 | + args: process.argv.slice(2), |
| 15 | + options: { |
| 16 | + number: { |
| 17 | + type: 'string', |
| 18 | + short: 'n', |
| 19 | + default: '1', |
| 20 | + }, |
| 21 | + dataset: { |
| 22 | + type: 'string', |
| 23 | + }, |
| 24 | + bundle: { |
| 25 | + type: 'string', |
| 26 | + }, |
| 27 | + draft: { |
| 28 | + type: 'boolean', |
| 29 | + }, |
| 30 | + published: { |
| 31 | + type: 'boolean', |
| 32 | + }, |
| 33 | + size: { |
| 34 | + type: 'string', |
| 35 | + }, |
| 36 | + concurrency: { |
| 37 | + type: 'string', |
| 38 | + short: 'c', |
| 39 | + }, |
| 40 | + template: { |
| 41 | + type: 'string', |
| 42 | + short: 't', |
| 43 | + }, |
| 44 | + help: { |
| 45 | + type: 'boolean', |
| 46 | + short: 'h', |
| 47 | + }, |
| 48 | + }, |
| 49 | +}) |
| 50 | + |
| 51 | +const templates = { |
| 52 | + validation: validation, |
| 53 | + book: book, |
| 54 | + species: species, |
| 55 | +} |
| 56 | + |
| 57 | +const HELP_TEXT = `Usage: tsx --env-file=.env.local ./${path.relative(process.cwd(), process.argv[1])} --template <template> [arguments] |
| 58 | +
|
| 59 | + Arguments: |
| 60 | + --template, -t <template>: Template to use (required). Possible values: ${Object.keys(templates).join(', ')} |
| 61 | +
|
| 62 | + --dataset: Dataset to generate documents in (defaults to 'test') |
| 63 | + --amount, -n <int>: Number of documents to generate |
| 64 | + --draft: Generate draft documents |
| 65 | + --published: Generate published documents |
| 66 | + --bundle <string>: Bundle to generate documents in |
| 67 | + --size <bytes>: Size (in bytes) of the generated document (will be approximated) |
| 68 | + --concurrency, -c <int>: Number of concurrent requests |
| 69 | + --help, -h: Show this help message |
| 70 | +
|
| 71 | + Add more templates by adding them to the "./${path.relative(process.cwd(), path.join(__dirname, './templates'))}" directory. |
| 72 | + ` |
| 73 | + |
| 74 | +if (args.help) { |
| 75 | + // eslint-disable-next-line no-console |
| 76 | + console.log(HELP_TEXT) |
| 77 | + process.exit(0) |
| 78 | +} |
| 79 | + |
| 80 | +if (!args.template) { |
| 81 | + console.error('Error: Missing required `--template` argument\n') |
| 82 | + console.error(HELP_TEXT) |
| 83 | + process.exit(1) |
| 84 | +} |
| 85 | +if (!(args.template in templates)) { |
| 86 | + console.error(`Error: Template "${args.template}" does not exist. Available templates: ${Object.keys(templates).join(', ')} |
| 87 | +`) |
| 88 | + console.error(HELP_TEXT) |
| 89 | + process.exit(1) |
| 90 | +} |
| 91 | + |
| 92 | +const template = templates[args.template as keyof typeof templates] |
| 93 | + |
| 94 | +type KnownEnvVar = 'TEST_STUDIO_WRITE_TOKEN' |
| 95 | + |
| 96 | +const client = createClient({ |
| 97 | + projectId: 'ppsg7ml5', |
| 98 | + dataset: args.dataset || 'test', |
| 99 | + token: readEnv<KnownEnvVar>('TEST_STUDIO_WRITE_TOKEN'), |
| 100 | + apiVersion: '2024-07-31', |
| 101 | + useCdn: false, |
| 102 | +}) |
| 103 | + |
| 104 | +run({ |
| 105 | + bundle: args.bundle, |
| 106 | + draft: args.draft || true, |
| 107 | + published: args.published, |
| 108 | + concurrency: args.concurrency ? Number(args.concurrency) : undefined, |
| 109 | + number: args.number ? Number(args.number) : undefined, |
| 110 | + size: args.size ? Number(args.size) : undefined, |
| 111 | + template, |
| 112 | + client, |
| 113 | +}) |
| 114 | + .pipe( |
| 115 | + tap({ |
| 116 | + next: (doc) => { |
| 117 | + // eslint-disable-next-line no-console |
| 118 | + console.log('Created', doc._id) |
| 119 | + }, |
| 120 | + error: console.error, |
| 121 | + }), |
| 122 | + ) |
| 123 | + .subscribe() |
0 commit comments