@sanity/cli 6.0.0-alpha.18 → 6.0.0-alpha.20
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/README.md +446 -446
- package/dist/actions/build/getViteConfig.js +47 -4
- package/dist/actions/build/getViteConfig.js.map +1 -1
- package/dist/actions/build/renderDocumentWorker/components/GlobalErrorHandler.js +1 -0
- package/dist/actions/build/renderDocumentWorker/components/GlobalErrorHandler.js.map +1 -1
- package/dist/actions/build/writeSanityRuntime.js +4 -3
- package/dist/actions/build/writeSanityRuntime.js.map +1 -1
- package/dist/actions/dev/getDashboardAppUrl.js +48 -0
- package/dist/actions/dev/getDashboardAppUrl.js.map +1 -0
- package/dist/actions/dev/getDevServerConfig.js +2 -1
- package/dist/actions/dev/getDevServerConfig.js.map +1 -1
- package/dist/actions/dev/startAppDevServer.js +3 -3
- package/dist/actions/dev/startAppDevServer.js.map +1 -1
- package/dist/actions/dev/startStudioDevServer.js +5 -10
- package/dist/actions/dev/startStudioDevServer.js.map +1 -1
- package/dist/actions/schema/extractSchemaWatcher.js +8 -6
- package/dist/actions/schema/extractSchemaWatcher.js.map +1 -1
- package/dist/actions/schema/matchSchemaPattern.js +22 -0
- package/dist/actions/schema/matchSchemaPattern.js.map +1 -0
- package/dist/actions/schema/runSchemaExtraction.js.map +1 -1
- package/dist/commands/backup/disable.js +0 -6
- package/dist/commands/backup/disable.js.map +1 -1
- package/dist/commands/backup/download.js +0 -6
- package/dist/commands/backup/download.js.map +1 -1
- package/dist/commands/backup/enable.js +0 -6
- package/dist/commands/backup/enable.js.map +1 -1
- package/dist/commands/backup/list.js +0 -6
- package/dist/commands/backup/list.js.map +1 -1
- package/dist/commands/cors/add.js +0 -6
- package/dist/commands/cors/add.js.map +1 -1
- package/dist/commands/cors/delete.js +0 -6
- package/dist/commands/cors/delete.js.map +1 -1
- package/dist/commands/cors/list.js +0 -6
- package/dist/commands/cors/list.js.map +1 -1
- package/dist/commands/dataset/alias/create.js +23 -7
- package/dist/commands/dataset/alias/create.js.map +1 -1
- package/dist/commands/dataset/alias/delete.js +17 -7
- package/dist/commands/dataset/alias/delete.js.map +1 -1
- package/dist/commands/dataset/alias/link.js +17 -7
- package/dist/commands/dataset/alias/link.js.map +1 -1
- package/dist/commands/dataset/alias/unlink.js +17 -7
- package/dist/commands/dataset/alias/unlink.js.map +1 -1
- package/dist/commands/dataset/copy.js +39 -29
- package/dist/commands/dataset/copy.js.map +1 -1
- package/dist/commands/dataset/create.js +17 -7
- package/dist/commands/dataset/create.js.map +1 -1
- package/dist/commands/dataset/delete.js +13 -7
- package/dist/commands/dataset/delete.js.map +1 -1
- package/dist/commands/dataset/embeddings/disable.js +19 -7
- package/dist/commands/dataset/embeddings/disable.js.map +1 -1
- package/dist/commands/dataset/embeddings/enable.js +17 -7
- package/dist/commands/dataset/embeddings/enable.js.map +1 -1
- package/dist/commands/dataset/embeddings/status.js +15 -7
- package/dist/commands/dataset/embeddings/status.js.map +1 -1
- package/dist/commands/dataset/export.js +30 -18
- package/dist/commands/dataset/export.js.map +1 -1
- package/dist/commands/dataset/list.js +19 -7
- package/dist/commands/dataset/list.js.map +1 -1
- package/dist/commands/dataset/visibility/get.js +15 -7
- package/dist/commands/dataset/visibility/get.js.map +1 -1
- package/dist/commands/dataset/visibility/set.js +19 -7
- package/dist/commands/dataset/visibility/set.js.map +1 -1
- package/dist/commands/documents/create.js +0 -6
- package/dist/commands/documents/create.js.map +1 -1
- package/dist/commands/documents/delete.js +0 -6
- package/dist/commands/documents/delete.js.map +1 -1
- package/dist/commands/documents/get.js +0 -6
- package/dist/commands/documents/get.js.map +1 -1
- package/dist/commands/documents/query.js +0 -6
- package/dist/commands/documents/query.js.map +1 -1
- package/dist/commands/graphql/list.js +0 -6
- package/dist/commands/graphql/list.js.map +1 -1
- package/dist/commands/graphql/undeploy.js +0 -6
- package/dist/commands/graphql/undeploy.js.map +1 -1
- package/dist/commands/hook/attempt.js +0 -6
- package/dist/commands/hook/attempt.js.map +1 -1
- package/dist/commands/hook/create.js +0 -6
- package/dist/commands/hook/create.js.map +1 -1
- package/dist/commands/hook/delete.js +0 -6
- package/dist/commands/hook/delete.js.map +1 -1
- package/dist/commands/hook/list.js +0 -6
- package/dist/commands/hook/list.js.map +1 -1
- package/dist/commands/hook/logs.js +0 -6
- package/dist/commands/hook/logs.js.map +1 -1
- package/dist/commands/media/delete-aspect.js +0 -6
- package/dist/commands/media/delete-aspect.js.map +1 -1
- package/dist/commands/media/deploy-aspect.js +1 -6
- package/dist/commands/media/deploy-aspect.js.map +1 -1
- package/dist/commands/media/export.js +0 -6
- package/dist/commands/media/export.js.map +1 -1
- package/dist/commands/media/import.js +0 -6
- package/dist/commands/media/import.js.map +1 -1
- package/dist/commands/schema/delete.js +0 -6
- package/dist/commands/schema/delete.js.map +1 -1
- package/dist/commands/tokens/add.js +0 -6
- package/dist/commands/tokens/add.js.map +1 -1
- package/dist/commands/tokens/delete.js +0 -6
- package/dist/commands/tokens/delete.js.map +1 -1
- package/dist/commands/tokens/list.js +0 -6
- package/dist/commands/tokens/list.js.map +1 -1
- package/dist/commands/users/invite.js +0 -6
- package/dist/commands/users/invite.js.map +1 -1
- package/dist/commands/users/list.js +0 -6
- package/dist/commands/users/list.js.map +1 -1
- package/dist/hooks/prerun/injectEnvVariables.js +9 -1
- package/dist/hooks/prerun/injectEnvVariables.js.map +1 -1
- package/dist/prompts/promptForProject.js +64 -0
- package/dist/prompts/promptForProject.js.map +1 -0
- package/dist/server/devServer.js +4 -2
- package/dist/server/devServer.js.map +1 -1
- package/dist/server/vite/plugin-schema-extraction.js +201 -0
- package/dist/server/vite/plugin-schema-extraction.js.map +1 -0
- package/dist/server/vite/plugin-typegen.js +217 -0
- package/dist/server/vite/plugin-typegen.js.map +1 -0
- package/dist/services/grants.js +13 -0
- package/dist/services/grants.js.map +1 -0
- package/dist/types/grants.js +3 -0
- package/dist/types/grants.js.map +1 -0
- package/dist/util/checkProjectPermissions.js +21 -0
- package/dist/util/checkProjectPermissions.js.map +1 -0
- package/dist/util/getSharedServerConfig.js +1 -0
- package/dist/util/getSharedServerConfig.js.map +1 -1
- package/dist/util/sharedFlags.js +19 -0
- package/dist/util/sharedFlags.js.map +1 -0
- package/dist/util/toForwardSlashes.js +8 -0
- package/dist/util/toForwardSlashes.js.map +1 -0
- package/oclif.manifest.json +362 -236
- package/package.json +18 -16
- package/dist/actions/dev/getCoreAppUrl.js +0 -10
- package/dist/actions/dev/getCoreAppUrl.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/commands/dataset/export.ts"],"sourcesContent":["import fs from 'node:fs/promises'\nimport path from 'node:path'\nimport {type Writable} from 'node:stream'\n\nimport {Args, Flags} from '@oclif/core'\nimport {getProjectCliClient, SanityCommand, subdebug} from '@sanity/cli-core'\nimport {boxen, input, spinner} from '@sanity/cli-core/ux'\nimport {type DatasetsResponse} from '@sanity/client'\nimport {exportDataset, type ExportOptions, type ExportProgress} from '@sanity/export'\nimport prettyMs from 'pretty-ms'\n\nimport {validateDatasetName} from '../../actions/dataset/validateDatasetName.js'\nimport {promptForDataset} from '../../prompts/promptForDataset.js'\nimport {listDatasets} from '../../services/datasets.js'\nimport {absolutify} from '../../util/absolutify.js'\nimport {NO_PROJECT_ID} from '../../util/errorMessages.js'\n\nconst noop = () => null\nconst exportDebug = subdebug('dataset:export')\n\nexport class DatasetExportCommand extends SanityCommand<typeof DatasetExportCommand> {\n static override args = {\n name: Args.string({\n description: 'Name of the dataset to export',\n }),\n // Args are order dependent\n // eslint-disable-next-line perfectionist/sort-objects\n destination: Args.string({\n description: 'Output destination file path',\n }),\n }\n\n static override description =\n 'Export dataset to local filesystem as a gzipped tarball. Assets failing with HTTP status codes 401, 403 and 404 upon download are ignored and excluded from export.'\n\n static override examples = [\n {\n command: '<%= config.bin %> <%= command.id %> moviedb localPath.tar.gz',\n description: 'Export dataset \"moviedb\" to localPath.tar.gz',\n },\n {\n command: '<%= config.bin %> <%= command.id %> moviedb assetless.tar.gz --no-assets',\n description: 'Export dataset without assets',\n },\n {\n command: '<%= config.bin %> <%= command.id %> staging staging.tar.gz --raw',\n description: 'Export raw documents without asset reference rewriting',\n },\n {\n command: '<%= config.bin %> <%= command.id %> staging staging.tar.gz --types products,shops',\n description: 'Export specific document types',\n },\n ]\n\n static override flags = {\n 'asset-concurrency': Flags.integer({\n default: 8,\n description: 'Concurrent number of asset downloads',\n }),\n mode: Flags.string({\n default: 'stream',\n description:\n 'Mode to export documents with `cursor` might be more performant for larger datasets, but might not be as accurate if the dataset is being modified during export',\n options: ['stream', 'cursor'],\n }),\n 'no-assets': Flags.boolean({\n default: false,\n description: 'Export only non-asset documents and remove references to image assets',\n }),\n 'no-compress': Flags.boolean({\n default: false,\n description: 'Skips compressing tarball entries (still generates a gzip file)',\n }),\n 'no-drafts': Flags.boolean({\n default: false,\n description: 'Export only published versions of documents',\n }),\n overwrite: Flags.boolean({\n default: false,\n description: 'Overwrite any file with the same name',\n }),\n raw: Flags.boolean({\n default: false,\n description: 'Extract only documents, without rewriting asset references',\n }),\n types: Flags.string({\n description: 'Defines which document types to export (comma-separated)',\n }),\n }\n\n public async run(): Promise<void> {\n const {args, flags} = await this.parse(DatasetExportCommand)\n const {destination: targetDestination, name: targetDataset} = args\n\n // Get project configuration\n const cliConfig = await this.getCliConfig()\n const projectId = await this.getProjectId()\n\n if (!projectId) {\n this.error(NO_PROJECT_ID, {\n exit: 1,\n })\n }\n\n const projectClient = await getProjectCliClient({\n apiVersion: '2023-05-26',\n projectId,\n requireUser: true,\n })\n\n let datasets: DatasetsResponse\n\n try {\n datasets = await listDatasets(projectId)\n } catch (error) {\n exportDebug('Error listing datasets', error)\n this.error(`Failed to list datasets:\\n${error instanceof Error ? error.message : error}`, {\n exit: 1,\n })\n }\n\n // Determine dataset name\n let dataset = targetDataset\n try {\n if (!dataset) {\n // Get default dataset from config\n const defaultDataset = cliConfig.api?.dataset\n if (defaultDataset) {\n dataset = defaultDataset\n this.log(`Using default dataset: ${dataset}`)\n } else {\n dataset = await promptForDataset({allowCreation: false, datasets})\n }\n }\n } catch (error) {\n exportDebug('Error selecting dataset', error)\n this.error(`Failed to select dataset:\\n${error instanceof Error ? error.message : error}`, {\n exit: 1,\n })\n }\n\n // Validate dataset name\n const dsError = validateDatasetName(dataset)\n if (dsError) {\n this.error(dsError, {exit: 1})\n }\n\n // Verify existence of dataset before trying to export from it\n if (!datasets.some((set) => set.name === dataset)) {\n this.error(`Dataset with name \"${dataset}\" not found`, {exit: 1})\n }\n\n this.log(\n boxen(\n `Exporting from:\nprojectId: ${projectId.padEnd(44)}\ndataset: ${dataset.padEnd(46)}`,\n {\n borderColor: 'yellow',\n borderStyle: 'round',\n },\n ),\n )\n\n // Determine output path\n let destinationPath = targetDestination\n if (!destinationPath) {\n destinationPath = await this.promptForDestination({dataset})\n }\n\n const outputPath = await this.getOutputPath(destinationPath, dataset, flags)\n if (!outputPath) {\n this.error('Cancelled', {exit: 1})\n }\n\n // Prepare export options\n const {fail, onProgress, succeed} = this.createProgressHandler()\n const exportOptions: ExportOptions = {\n assetConcurrency: flags['asset-concurrency'],\n assets: !flags['no-assets'],\n client: projectClient,\n compress: !flags['no-compress'],\n dataset,\n drafts: !flags['no-drafts'],\n mode: flags.mode === 'cursor' || flags.mode === 'stream' ? flags.mode : undefined,\n onProgress,\n outputPath,\n raw: flags.raw,\n types: flags.types ? flags.types.split(',') : undefined,\n }\n\n const start = Date.now()\n try {\n await exportDataset(exportOptions)\n succeed()\n this.log(`Export finished (${prettyMs(Date.now() - start)})`)\n } catch (error) {\n fail()\n const err = error instanceof Error ? error : new Error(String(error))\n exportDebug('Export failed', err)\n this.error(`Export failed: ${err.message}`, {exit: 1})\n }\n }\n\n private createProgressHandler() {\n let currentSpinner: ReturnType<typeof spinner> | null = null\n let currentStep = ''\n\n const onProgress = (progress: ExportProgress) => {\n if (progress.step !== currentStep) {\n // Complete previous step\n succeed()\n\n // Start new step\n currentStep = progress.step\n currentSpinner = spinner(progress.step).start()\n } else if (progress.step === currentStep && progress.update && currentSpinner) {\n // Update current step with progress info\n currentSpinner.text = `${progress.step} (${progress.current}/${progress.total})`\n }\n }\n\n const succeed = () => {\n currentSpinner?.succeed()\n }\n\n const fail = () => {\n currentSpinner?.fail()\n }\n\n return {fail, onProgress, succeed}\n }\n\n private async getOutputPath(\n destination: string,\n dataset: string,\n flags: {overwrite?: boolean},\n ): Promise<string | Writable> {\n if (destination === '-') {\n return process.stdout\n }\n\n const dstPath = path.isAbsolute(destination)\n ? destination\n : path.resolve(process.cwd(), destination)\n\n const dstStats = await fs.stat(dstPath).catch(noop)\n const looksLikeFile = dstStats ? dstStats.isFile() : path.basename(dstPath).includes('.')\n\n if (!dstStats) {\n const createPath = looksLikeFile ? path.dirname(dstPath) : dstPath\n try {\n await fs.mkdir(createPath, {recursive: true})\n } catch (error) {\n const err = error instanceof Error ? error : new Error(String(error))\n const errorWithCode = err as Error & {code?: string}\n if (errorWithCode.code === 'EACCES') {\n this.error(\n `Permission denied: Cannot create directory \"${createPath}\". Please check write permissions.`,\n {\n exit: 1,\n },\n )\n } else {\n this.error(`Failed to create directory \"${createPath}\": ${err.message}`, {\n exit: 1,\n })\n }\n }\n }\n\n const finalPath = looksLikeFile ? dstPath : path.join(dstPath, `${dataset}.tar.gz`)\n const finalPathStats = await fs.stat(finalPath).catch(noop)\n\n if (!flags.overwrite && finalPathStats && finalPathStats.isFile()) {\n this.error(`File \"${finalPath}\" already exists. Use --overwrite flag to overwrite it.`, {\n exit: 1,\n })\n }\n\n return finalPath\n }\n\n private promptForDestination(options: {dataset: string; workDir?: string}): Promise<string> {\n const {dataset, workDir = process.cwd()} = options\n\n const defaultPath = path.join(workDir, `${dataset}.tar.gz`)\n\n return input({\n default: defaultPath,\n message: 'Output path:',\n transformer: (value: string) => absolutify(value.trim()),\n validate: (value: string) => {\n const trimmed = value.trim()\n if (!trimmed) {\n return 'Please provide a valid output path'\n }\n return true\n },\n })\n }\n}\n"],"names":["fs","path","Args","Flags","getProjectCliClient","SanityCommand","subdebug","boxen","input","spinner","exportDataset","prettyMs","validateDatasetName","promptForDataset","listDatasets","absolutify","NO_PROJECT_ID","noop","exportDebug","DatasetExportCommand","args","name","string","description","destination","examples","command","flags","integer","default","mode","options","boolean","overwrite","raw","types","run","parse","targetDestination","targetDataset","cliConfig","getCliConfig","projectId","getProjectId","error","exit","projectClient","apiVersion","requireUser","datasets","Error","message","dataset","defaultDataset","api","log","allowCreation","dsError","some","set","padEnd","borderColor","borderStyle","destinationPath","promptForDestination","outputPath","getOutputPath","fail","onProgress","succeed","createProgressHandler","exportOptions","assetConcurrency","assets","client","compress","drafts","undefined","split","start","Date","now","err","String","currentSpinner","currentStep","progress","step","update","text","current","total","process","stdout","dstPath","isAbsolute","resolve","cwd","dstStats","stat","catch","looksLikeFile","isFile","basename","includes","createPath","dirname","mkdir","recursive","errorWithCode","code","finalPath","join","finalPathStats","workDir","defaultPath","transformer","value","trim","validate","trimmed"],"mappings":"AAAA,OAAOA,QAAQ,mBAAkB;AACjC,OAAOC,UAAU,YAAW;AAG5B,SAAQC,IAAI,EAAEC,KAAK,QAAO,cAAa;AACvC,SAAQC,mBAAmB,EAAEC,aAAa,EAAEC,QAAQ,QAAO,mBAAkB;AAC7E,SAAQC,KAAK,EAAEC,KAAK,EAAEC,OAAO,QAAO,sBAAqB;AAEzD,SAAQC,aAAa,QAAgD,iBAAgB;AACrF,OAAOC,cAAc,YAAW;AAEhC,SAAQC,mBAAmB,QAAO,+CAA8C;AAChF,SAAQC,gBAAgB,QAAO,oCAAmC;AAClE,SAAQC,YAAY,QAAO,6BAA4B;AACvD,SAAQC,UAAU,QAAO,2BAA0B;AACnD,SAAQC,aAAa,QAAO,8BAA6B;AAEzD,MAAMC,OAAO,IAAM;AACnB,MAAMC,cAAcZ,SAAS;AAE7B,OAAO,MAAMa,6BAA6Bd;IACxC,OAAgBe,OAAO;QACrBC,MAAMnB,KAAKoB,MAAM,CAAC;YAChBC,a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ises'\nimport path from 'node:path'\nimport {type Writable} from 'node:stream'\n\nimport {Args, Flags} from '@oclif/core'\nimport {\n getProjectCliClient,\n ProjectRootNotFoundError,\n SanityCommand,\n subdebug,\n} from '@sanity/cli-core'\nimport {boxen, input, spinner} from '@sanity/cli-core/ux'\nimport {type DatasetsResponse} from '@sanity/client'\nimport {exportDataset, type ExportOptions, type ExportProgress} from '@sanity/export'\nimport prettyMs from 'pretty-ms'\n\nimport {validateDatasetName} from '../../actions/dataset/validateDatasetName.js'\nimport {promptForDataset} from '../../prompts/promptForDataset.js'\nimport {promptForProject} from '../../prompts/promptForProject.js'\nimport {listDatasets} from '../../services/datasets.js'\nimport {absolutify} from '../../util/absolutify.js'\nimport {projectIdFlag} from '../../util/sharedFlags.js'\n\nconst noop = () => null\nconst exportDebug = subdebug('dataset:export')\n\nexport class DatasetExportCommand extends SanityCommand<typeof DatasetExportCommand> {\n static override args = {\n name: Args.string({\n description: 'Name of the dataset to export',\n }),\n // Args are order dependent\n // eslint-disable-next-line perfectionist/sort-objects\n destination: Args.string({\n description: 'Output destination file path',\n }),\n }\n\n static override description =\n 'Export dataset to local filesystem as a gzipped tarball. Assets failing with HTTP status codes 401, 403 and 404 upon download are ignored and excluded from export.'\n\n static override examples = [\n {\n command: '<%= config.bin %> <%= command.id %> moviedb localPath.tar.gz',\n description: 'Export dataset \"moviedb\" to localPath.tar.gz',\n },\n {\n command: '<%= config.bin %> <%= command.id %> moviedb assetless.tar.gz --no-assets',\n description: 'Export dataset without assets',\n },\n {\n command: '<%= config.bin %> <%= command.id %> staging staging.tar.gz --raw',\n description: 'Export raw documents without asset reference rewriting',\n },\n {\n command: '<%= config.bin %> <%= command.id %> staging staging.tar.gz --types products,shops',\n description: 'Export specific document types',\n },\n ]\n\n static override flags = {\n ...projectIdFlag,\n 'asset-concurrency': Flags.integer({\n default: 8,\n description: 'Concurrent number of asset downloads',\n }),\n mode: Flags.string({\n default: 'stream',\n description:\n 'Mode to export documents with `cursor` might be more performant for larger datasets, but might not be as accurate if the dataset is being modified during export',\n options: ['stream', 'cursor'],\n }),\n 'no-assets': Flags.boolean({\n default: false,\n description: 'Export only non-asset documents and remove references to image assets',\n }),\n 'no-compress': Flags.boolean({\n default: false,\n description: 'Skips compressing tarball entries (still generates a gzip file)',\n }),\n 'no-drafts': Flags.boolean({\n default: false,\n description: 'Export only published versions of documents',\n }),\n overwrite: Flags.boolean({\n default: false,\n description: 'Overwrite any file with the same name',\n }),\n raw: Flags.boolean({\n default: false,\n description: 'Extract only documents, without rewriting asset references',\n }),\n types: Flags.string({\n description: 'Defines which document types to export (comma-separated)',\n }),\n }\n\n public async run(): Promise<void> {\n const {args, flags} = await this.parse(DatasetExportCommand)\n const {destination: targetDestination, name: targetDataset} = args\n\n // Get project configuration\n const projectId = await this.getProjectId({\n fallback: () =>\n promptForProject({\n requiredPermissions: [{grant: 'read', permission: 'sanity.project.datasets'}],\n }),\n })\n\n const projectClient = await getProjectCliClient({\n apiVersion: '2023-05-26',\n projectId,\n requireUser: true,\n })\n\n let datasets: DatasetsResponse\n\n try {\n datasets = await listDatasets(projectId)\n } catch (error) {\n exportDebug('Error listing datasets', error)\n this.error(`Failed to list datasets:\\n${error instanceof Error ? error.message : error}`, {\n exit: 1,\n })\n }\n\n // Determine dataset name\n let dataset = targetDataset\n if (!dataset) {\n try {\n // Get default dataset from config (only available when running from a project directory)\n let defaultDataset: string | undefined\n try {\n const cliConfig = await this.getCliConfig()\n defaultDataset = cliConfig.api?.dataset\n } catch (err) {\n if (!(err instanceof ProjectRootNotFoundError)) throw err\n // Not inside a project directory — no default dataset available\n }\n\n if (defaultDataset) {\n dataset = defaultDataset\n this.log(`Using default dataset: ${dataset}`)\n } else {\n dataset = await promptForDataset({allowCreation: false, datasets})\n }\n } catch (error) {\n exportDebug('Error selecting dataset', error)\n this.error(`Failed to select dataset:\\n${error instanceof Error ? error.message : error}`, {\n exit: 1,\n })\n }\n }\n\n // Validate dataset name\n const dsError = validateDatasetName(dataset)\n if (dsError) {\n this.error(dsError, {exit: 1})\n }\n\n // Verify existence of dataset before trying to export from it\n if (!datasets.some((set) => set.name === dataset)) {\n this.error(`Dataset with name \"${dataset}\" not found`, {exit: 1})\n }\n\n this.log(\n boxen(\n `Exporting from:\nprojectId: ${projectId.padEnd(44)}\ndataset: ${dataset.padEnd(46)}`,\n {\n borderColor: 'yellow',\n borderStyle: 'round',\n },\n ),\n )\n\n // Determine output path\n let destinationPath = targetDestination\n if (!destinationPath) {\n destinationPath = await this.promptForDestination({dataset})\n }\n\n const outputPath = await this.getOutputPath(destinationPath, dataset, flags)\n if (!outputPath) {\n this.error('Cancelled', {exit: 1})\n }\n\n // Prepare export options\n const {fail, onProgress, succeed} = this.createProgressHandler()\n const exportOptions: ExportOptions = {\n assetConcurrency: flags['asset-concurrency'],\n assets: !flags['no-assets'],\n client: projectClient,\n compress: !flags['no-compress'],\n dataset,\n drafts: !flags['no-drafts'],\n mode: flags.mode === 'cursor' || flags.mode === 'stream' ? flags.mode : undefined,\n onProgress,\n outputPath,\n raw: flags.raw,\n types: flags.types ? flags.types.split(',') : undefined,\n }\n\n const start = Date.now()\n try {\n await exportDataset(exportOptions)\n succeed()\n this.log(`Export finished (${prettyMs(Date.now() - start)})`)\n } catch (error) {\n fail()\n const err = error instanceof Error ? error : new Error(String(error))\n exportDebug('Export failed', err)\n this.error(`Export failed: ${err.message}`, {exit: 1})\n }\n }\n\n private createProgressHandler() {\n let currentSpinner: ReturnType<typeof spinner> | null = null\n let currentStep = ''\n\n const onProgress = (progress: ExportProgress) => {\n if (progress.step !== currentStep) {\n // Complete previous step\n succeed()\n\n // Start new step\n currentStep = progress.step\n currentSpinner = spinner(progress.step).start()\n } else if (progress.step === currentStep && progress.update && currentSpinner) {\n // Update current step with progress info\n currentSpinner.text = `${progress.step} (${progress.current}/${progress.total})`\n }\n }\n\n const succeed = () => {\n currentSpinner?.succeed()\n }\n\n const fail = () => {\n currentSpinner?.fail()\n }\n\n return {fail, onProgress, succeed}\n }\n\n private async getOutputPath(\n destination: string,\n dataset: string,\n flags: {overwrite?: boolean},\n ): Promise<string | Writable> {\n if (destination === '-') {\n return process.stdout\n }\n\n const dstPath = path.isAbsolute(destination)\n ? destination\n : path.resolve(process.cwd(), destination)\n\n const dstStats = await fs.stat(dstPath).catch(noop)\n const looksLikeFile = dstStats ? dstStats.isFile() : path.basename(dstPath).includes('.')\n\n if (!dstStats) {\n const createPath = looksLikeFile ? path.dirname(dstPath) : dstPath\n try {\n await fs.mkdir(createPath, {recursive: true})\n } catch (error) {\n const err = error instanceof Error ? error : new Error(String(error))\n const errorWithCode = err as Error & {code?: string}\n if (errorWithCode.code === 'EACCES') {\n this.error(\n `Permission denied: Cannot create directory \"${createPath}\". Please check write permissions.`,\n {\n exit: 1,\n },\n )\n } else {\n this.error(`Failed to create directory \"${createPath}\": ${err.message}`, {\n exit: 1,\n })\n }\n }\n }\n\n const finalPath = looksLikeFile ? dstPath : path.join(dstPath, `${dataset}.tar.gz`)\n const finalPathStats = await fs.stat(finalPath).catch(noop)\n\n if (!flags.overwrite && finalPathStats && finalPathStats.isFile()) {\n this.error(`File \"${finalPath}\" already exists. Use --overwrite flag to overwrite it.`, {\n exit: 1,\n })\n }\n\n return finalPath\n }\n\n private promptForDestination(options: {dataset: string; workDir?: string}): Promise<string> {\n const {dataset, workDir = process.cwd()} = options\n\n const defaultPath = path.join(workDir, `${dataset}.tar.gz`)\n\n return input({\n default: defaultPath,\n message: 'Output path:',\n transformer: (value: string) => absolutify(value.trim()),\n validate: (value: string) => {\n const trimmed = value.trim()\n if (!trimmed) {\n return 'Please provide a valid output path'\n }\n return true\n },\n })\n }\n}\n"],"names":["fs","path","Args","Flags","getProjectCliClient","ProjectRootNotFoundError","SanityCommand","subdebug","boxen","input","spinner","exportDataset","prettyMs","validateDatasetName","promptForDataset","promptForProject","listDatasets","absolutify","projectIdFlag","noop","exportDebug","DatasetExportCommand","args","name","string","description","destination","examples","command","flags","integer","default","mode","options","boolean","overwrite","raw","types","run","parse","targetDestination","targetDataset","projectId","getProjectId","fallback","requiredPermissions","grant","permission","projectClient","apiVersion","requireUser","datasets","error","Error","message","exit","dataset","defaultDataset","cliConfig","getCliConfig","api","err","log","allowCreation","dsError","some","set","padEnd","borderColor","borderStyle","destinationPath","promptForDestination","outputPath","getOutputPath","fail","onProgress","succeed","createProgressHandler","exportOptions","assetConcurrency","assets","client","compress","drafts","undefined","split","start","Date","now","String","currentSpinner","currentStep","progress","step","update","text","current","total","process","stdout","dstPath","isAbsolute","resolve","cwd","dstStats","stat","catch","looksLikeFile","isFile","basename","includes","createPath","dirname","mkdir","recursive","errorWithCode","code","finalPath","join","finalPathStats","workDir","defaultPath","transformer","value","trim","validate","trimmed"],"mappings":"AAAA,OAAOA,QAAQ,mBAAkB;AACjC,OAAOC,UAAU,YAAW;AAG5B,SAAQC,IAAI,EAAEC,KAAK,QAAO,cAAa;AACvC,SACEC,mBAAmB,EACnBC,wBAAwB,EACxBC,aAAa,EACbC,QAAQ,QACH,mBAAkB;AACzB,SAAQC,KAAK,EAAEC,KAAK,EAAEC,OAAO,QAAO,sBAAqB;AAEzD,SAAQC,aAAa,QAAgD,iBAAgB;AACrF,OAAOC,cAAc,YAAW;AAEhC,SAAQC,mBAAmB,QAAO,+CAA8C;AAChF,SAAQC,gBAAgB,QAAO,oCAAmC;AAClE,SAAQC,gBAAgB,QAAO,oCAAmC;AAClE,SAAQC,YAAY,QAAO,6BAA4B;AACvD,SAAQC,UAAU,QAAO,2BAA0B;AACnD,SAAQC,aAAa,QAAO,4BAA2B;AAEvD,MAAMC,OAAO,IAAM;AACnB,MAAMC,cAAcb,SAAS;AAE7B,OAAO,MAAMc,6BAA6Bf;IACxC,OAAgBgB,OAAO;QACrBC,MAAMrB,KAAKsB,MAAM,CAAC;YAChBC,aAAa;QACf;QACA,2BAA2B;QAC3B,sDAAsD;QACtDC,aAAaxB,KAAKsB,MAAM,CAAC;YACvBC,aAAa;QACf;IACF,EAAC;IAED,OAAgBA,cACd,s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@
|
|
|
1
1
|
import { SanityCommand, subdebug } from '@sanity/cli-core';
|
|
2
|
+
import { promptForProject } from '../../prompts/promptForProject.js';
|
|
2
3
|
import { listDatasetAliases, listDatasets } from '../../services/datasets.js';
|
|
3
|
-
import {
|
|
4
|
+
import { projectIdFlag } from '../../util/sharedFlags.js';
|
|
4
5
|
const listDatasetDebug = subdebug('dataset:list');
|
|
5
6
|
export class ListDatasetCommand extends SanityCommand {
|
|
6
7
|
static description = 'List datasets of your project';
|
|
@@ -8,15 +9,26 @@ export class ListDatasetCommand extends SanityCommand {
|
|
|
8
9
|
{
|
|
9
10
|
command: '<%= config.bin %> <%= command.id %>',
|
|
10
11
|
description: 'List datasets of your project'
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
command: '<%= config.bin %> <%= command.id %> --project-id abc123',
|
|
15
|
+
description: 'List datasets for a specific project'
|
|
11
16
|
}
|
|
12
17
|
];
|
|
18
|
+
static flags = {
|
|
19
|
+
...projectIdFlag
|
|
20
|
+
};
|
|
13
21
|
async run() {
|
|
14
|
-
const projectId = await this.getProjectId(
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
22
|
+
const projectId = await this.getProjectId({
|
|
23
|
+
fallback: ()=>promptForProject({
|
|
24
|
+
requiredPermissions: [
|
|
25
|
+
{
|
|
26
|
+
grant: 'read',
|
|
27
|
+
permission: 'sanity.project.datasets'
|
|
28
|
+
}
|
|
29
|
+
]
|
|
30
|
+
})
|
|
31
|
+
});
|
|
20
32
|
const [datasets, aliases] = await Promise.allSettled([
|
|
21
33
|
listDatasets(projectId),
|
|
22
34
|
listDatasetAliases(projectId)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/commands/dataset/list.ts"],"sourcesContent":["import {SanityCommand, subdebug} from '@sanity/cli-core'\n\nimport {listDatasetAliases, listDatasets} from '../../services/datasets.js'\nimport {
|
|
1
|
+
{"version":3,"sources":["../../../src/commands/dataset/list.ts"],"sourcesContent":["import {SanityCommand, subdebug} from '@sanity/cli-core'\n\nimport {promptForProject} from '../../prompts/promptForProject.js'\nimport {listDatasetAliases, listDatasets} from '../../services/datasets.js'\nimport {projectIdFlag} from '../../util/sharedFlags.js'\n\nconst listDatasetDebug = subdebug('dataset:list')\n\nexport class ListDatasetCommand extends SanityCommand<typeof ListDatasetCommand> {\n static override description = 'List datasets of your project'\n\n static override examples = [\n {\n command: '<%= config.bin %> <%= command.id %>',\n description: 'List datasets of your project',\n },\n {\n command: '<%= config.bin %> <%= command.id %> --project-id abc123',\n description: 'List datasets for a specific project',\n },\n ]\n\n static override flags = {\n ...projectIdFlag,\n }\n\n public async run(): Promise<void> {\n const projectId = await this.getProjectId({\n fallback: () =>\n promptForProject({\n requiredPermissions: [{grant: 'read', permission: 'sanity.project.datasets'}],\n }),\n })\n\n const [datasets, aliases] = await Promise.allSettled([\n listDatasets(projectId),\n listDatasetAliases(projectId),\n ])\n\n if (datasets.status === 'rejected') {\n const err = datasets.reason as Error\n listDatasetDebug(`Error listing datasets for project ${projectId}`, err)\n this.error(`Dataset list retrieval failed: ${err.message}`, {exit: 1})\n }\n\n const datasetList = datasets.value\n if (datasetList.length === 0) {\n this.log('No datasets found for this project.')\n } else {\n for (const dataset of datasetList) {\n this.log(dataset.name)\n }\n }\n\n if (aliases.status === 'fulfilled' && aliases.value.length > 0) {\n for (const alias of aliases.value) {\n const targetDataset = alias.datasetName || '<unlinked>'\n this.log(`~${alias.name} -> ${targetDataset}`)\n }\n } else if (aliases.status === 'rejected') {\n listDatasetDebug(`Warning: Could not fetch aliases for project ${projectId}`, aliases.reason)\n }\n }\n}\n"],"names":["SanityCommand","subdebug","promptForProject","listDatasetAliases","listDatasets","projectIdFlag","listDatasetDebug","ListDatasetCommand","description","examples","command","flags","run","projectId","getProjectId","fallback","requiredPermissions","grant","permission","datasets","aliases","Promise","allSettled","status","err","reason","error","message","exit","datasetList","value","length","log","dataset","name","alias","targetDataset","datasetName"],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K,CAAE;gBACjC,MAAMM,gBAAgBD,MAAME,WAAW,IAAI;gBAC3C,IAAI,CAACL,GAAG,CAAC,CAAC,CAAC,EAAEG,MAAMD,IAAI,CAAC,IAAI,EAAEE,eAAe;YAC/C;QACF,OAAO,IAAIhB,QAAQG,MAAM,KAAK,YAAY;YACxCjB,iBAAiB,CAAC,6CAA6C,EAAEO,WAAW,EAAEO,QAAQK,MAAM;QAC9F;IACF;AACF"}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { Args } from '@oclif/core';
|
|
2
2
|
import { SanityCommand, subdebug } from '@sanity/cli-core';
|
|
3
3
|
import { validateDatasetName } from '../../../actions/dataset/validateDatasetName.js';
|
|
4
|
+
import { promptForProject } from '../../../prompts/promptForProject.js';
|
|
4
5
|
import { listDatasets } from '../../../services/datasets.js';
|
|
5
|
-
import {
|
|
6
|
+
import { projectIdFlag } from '../../../util/sharedFlags.js';
|
|
6
7
|
const getDebug = subdebug('dataset:visibility:get');
|
|
7
8
|
export class DatasetVisibilityGetCommand extends SanityCommand {
|
|
8
9
|
static args = {
|
|
@@ -18,15 +19,22 @@ export class DatasetVisibilityGetCommand extends SanityCommand {
|
|
|
18
19
|
description: 'Check the visibility of a dataset'
|
|
19
20
|
}
|
|
20
21
|
];
|
|
22
|
+
static flags = {
|
|
23
|
+
...projectIdFlag
|
|
24
|
+
};
|
|
21
25
|
async run() {
|
|
22
26
|
const { args } = await this.parse(DatasetVisibilityGetCommand);
|
|
23
27
|
const { dataset } = args;
|
|
24
|
-
const projectId = await this.getProjectId(
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
const projectId = await this.getProjectId({
|
|
29
|
+
fallback: ()=>promptForProject({
|
|
30
|
+
requiredPermissions: [
|
|
31
|
+
{
|
|
32
|
+
grant: 'read',
|
|
33
|
+
permission: 'sanity.project.datasets'
|
|
34
|
+
}
|
|
35
|
+
]
|
|
36
|
+
})
|
|
37
|
+
});
|
|
30
38
|
const dsError = validateDatasetName(dataset);
|
|
31
39
|
if (dsError) {
|
|
32
40
|
this.error(dsError, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/commands/dataset/visibility/get.ts"],"sourcesContent":["import {Args} from '@oclif/core'\nimport {SanityCommand, subdebug} from '@sanity/cli-core'\n\nimport {validateDatasetName} from '../../../actions/dataset/validateDatasetName.js'\nimport {listDatasets} from '../../../services/datasets.js'\nimport {
|
|
1
|
+
{"version":3,"sources":["../../../../src/commands/dataset/visibility/get.ts"],"sourcesContent":["import {Args} from '@oclif/core'\nimport {SanityCommand, subdebug} from '@sanity/cli-core'\n\nimport {validateDatasetName} from '../../../actions/dataset/validateDatasetName.js'\nimport {promptForProject} from '../../../prompts/promptForProject.js'\nimport {listDatasets} from '../../../services/datasets.js'\nimport {projectIdFlag} from '../../../util/sharedFlags.js'\n\nconst getDebug = subdebug('dataset:visibility:get')\n\nexport class DatasetVisibilityGetCommand extends SanityCommand<typeof DatasetVisibilityGetCommand> {\n static override args = {\n dataset: Args.string({\n description: 'The name of the dataset to get visibility for',\n required: true,\n }),\n }\n\n static override description = 'Get the visibility of a dataset'\n\n static override examples = [\n {\n command: '<%= config.bin %> <%= command.id %> my-dataset',\n description: 'Check the visibility of a dataset',\n },\n ]\n\n static override flags = {\n ...projectIdFlag,\n }\n\n public async run(): Promise<void> {\n const {args} = await this.parse(DatasetVisibilityGetCommand)\n const {dataset} = args\n\n const projectId = await this.getProjectId({\n fallback: () =>\n promptForProject({\n requiredPermissions: [{grant: 'read', permission: 'sanity.project.datasets'}],\n }),\n })\n\n const dsError = validateDatasetName(dataset)\n if (dsError) {\n this.error(dsError, {exit: 1})\n }\n\n let current\n try {\n const datasets = await listDatasets(projectId)\n current = datasets.find((curr: {name: string}) => curr.name === dataset)\n } catch (error) {\n getDebug(`Error listing datasets`, error)\n this.error(\n `Failed to list datasets: ${error instanceof Error ? error.message : String(error)}`,\n {exit: 1},\n )\n }\n\n if (!current) {\n this.error(`Dataset not found: ${dataset}`, {exit: 1})\n }\n\n this.log(current.aclMode)\n }\n}\n"],"names":["Args","SanityCommand","subdebug","validateDatasetName","promptForProject","listDatasets","projectIdFlag","getDebug","DatasetVisibilityGetCommand","args","dataset","string","description","required","examples","command","flags","run","parse","projectId","getProjectId","fallback","requiredPermissions","grant","permission","dsError","error","exit","current","datasets","find","curr","name","Error","message","String","log","aclMode"],"mappings":"AAAA,SAAQA,IAAI,QAAO,cAAa;AAChC,SAAQC,aAAa,EAAEC,QAAQ,QAAO,mBAAkB;AAExD,SAAQC,mBAAmB,QAAO,kDAAiD;AACnF,SAAQC,gBAAgB,QAAO,uCAAsC;AACrE,SAAQC,YAAY,QAAO,gCAA+B;AAC1D,SAAQC,aAAa,QAAO,+BAA8B;AAE1D,MAAMC,WAAWL,SAAS;AAE1B,OAAO,MAAMM,oCAAoCP;IAC/C,OAAgBQ,OAAO;QACrBC,SAASV,KAAKW,MAAM,CAAC;YACnBC,aAAa;YACbC,UAAU;QACZ;IACF,EAAC;IAED,OAAgBD,cAAc,kCAAiC;IAE/D,OAAgBE,WAAW;QACzB;YACEC,SAAS;YACTH,aAAa;QACf;KACD,CAAA;IAED,OAAgBI,QAAQ;QACtB,GAAGV,aAAa;IAClB,EAAC;IAED,MAAaW,MAAqB;QAChC,MAAM,EAACR,IAAI,EAAC,GAAG,MAAM,IAAI,CAACS,KAAK,CAACV;QAChC,MAAM,EAACE,OAAO,EAAC,GAAGD;QAElB,MAAMU,YAAY,MAAM,IAAI,CAACC,YAAY,CAAC;YACxCC,UAAU,IACRjB,iBAAiB;oBACfkB,qBAAqB;wBAAC;4BAACC,OAAO;4BAAQC,YAAY;wBAAyB;qBAAE;gBAC/E;QACJ;QAEA,MAAMC,UAAUtB,oBAAoBO;QACpC,IAAIe,SAAS;YACX,IAAI,CAACC,KAAK,CAACD,SAAS;gBAACE,MAAM;YAAC;QAC9B;QAEA,IAAIC;QACJ,IAAI;YACF,MAAMC,WAAW,MAAMxB,aAAac;YACpCS,UAAUC,SAASC,IAAI,CAAC,CAACC,OAAyBA,KAAKC,IAAI,KAAKtB;QAClE,EAAE,OAAOgB,OAAO;YACdnB,SAAS,CAAC,sBAAsB,CAAC,EAAEmB;YACnC,IAAI,CAACA,KAAK,CACR,CAAC,yBAAyB,EAAEA,iBAAiBO,QAAQP,MAAMQ,OAAO,GAAGC,OAAOT,QAAQ,EACpF;gBAACC,MAAM;YAAC;QAEZ;QAEA,IAAI,CAACC,SAAS;YACZ,IAAI,CAACF,KAAK,CAAC,CAAC,mBAAmB,EAAEhB,SAAS,EAAE;gBAACiB,MAAM;YAAC;QACtD;QAEA,IAAI,CAACS,GAAG,CAACR,QAAQS,OAAO;IAC1B;AACF"}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { Args } from '@oclif/core';
|
|
2
2
|
import { SanityCommand, subdebug } from '@sanity/cli-core';
|
|
3
3
|
import { validateDatasetName } from '../../../actions/dataset/validateDatasetName.js';
|
|
4
|
+
import { promptForProject } from '../../../prompts/promptForProject.js';
|
|
4
5
|
import { editDatasetAcl, listDatasets } from '../../../services/datasets.js';
|
|
5
|
-
import {
|
|
6
|
+
import { projectIdFlag } from '../../../util/sharedFlags.js';
|
|
6
7
|
const setDatasetVisibilityDebug = subdebug('dataset:visibility:set');
|
|
7
8
|
export class DatasetVisibilitySetCommand extends SanityCommand {
|
|
8
9
|
static args = {
|
|
@@ -30,15 +31,26 @@ export class DatasetVisibilitySetCommand extends SanityCommand {
|
|
|
30
31
|
description: 'Make a dataset public'
|
|
31
32
|
}
|
|
32
33
|
];
|
|
34
|
+
static flags = {
|
|
35
|
+
...projectIdFlag
|
|
36
|
+
};
|
|
33
37
|
async run() {
|
|
34
38
|
const { args } = await this.parse(DatasetVisibilitySetCommand);
|
|
35
39
|
const { dataset, mode } = args;
|
|
36
|
-
const projectId = await this.getProjectId(
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
const projectId = await this.getProjectId({
|
|
41
|
+
fallback: ()=>promptForProject({
|
|
42
|
+
requiredPermissions: [
|
|
43
|
+
{
|
|
44
|
+
grant: 'read',
|
|
45
|
+
permission: 'sanity.project.datasets'
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
grant: 'update',
|
|
49
|
+
permission: 'sanity.project.datasets'
|
|
50
|
+
}
|
|
51
|
+
]
|
|
52
|
+
})
|
|
53
|
+
});
|
|
42
54
|
const dsError = validateDatasetName(dataset);
|
|
43
55
|
if (dsError) {
|
|
44
56
|
this.error(dsError, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/commands/dataset/visibility/set.ts"],"sourcesContent":["import {Args} from '@oclif/core'\nimport {SanityCommand, subdebug} from '@sanity/cli-core'\nimport {DatasetsResponse} from '@sanity/client'\n\nimport {validateDatasetName} from '../../../actions/dataset/validateDatasetName.js'\nimport {editDatasetAcl, listDatasets} from '../../../services/datasets.js'\nimport {
|
|
1
|
+
{"version":3,"sources":["../../../../src/commands/dataset/visibility/set.ts"],"sourcesContent":["import {Args} from '@oclif/core'\nimport {SanityCommand, subdebug} from '@sanity/cli-core'\nimport {DatasetsResponse} from '@sanity/client'\n\nimport {validateDatasetName} from '../../../actions/dataset/validateDatasetName.js'\nimport {promptForProject} from '../../../prompts/promptForProject.js'\nimport {editDatasetAcl, listDatasets} from '../../../services/datasets.js'\nimport {projectIdFlag} from '../../../util/sharedFlags.js'\n\nconst setDatasetVisibilityDebug = subdebug('dataset:visibility:set')\n\nexport class DatasetVisibilitySetCommand extends SanityCommand<typeof DatasetVisibilitySetCommand> {\n static override args = {\n dataset: Args.string({\n description: 'The name of the dataset to set visibility for',\n required: true,\n }),\n mode: Args.string({\n description: 'The visibility mode to set',\n options: ['public', 'private'],\n required: true,\n }),\n }\n\n static override description = 'Set the visibility of a dataset'\n\n static override examples = [\n {\n command: '<%= config.bin %> <%= command.id %> my-dataset private',\n description: 'Make a dataset private',\n },\n {\n command: '<%= config.bin %> <%= command.id %> my-dataset public',\n description: 'Make a dataset public',\n },\n ]\n\n static override flags = {\n ...projectIdFlag,\n }\n\n public async run(): Promise<void> {\n const {args} = await this.parse(DatasetVisibilitySetCommand)\n const {dataset, mode} = args\n\n const projectId = await this.getProjectId({\n fallback: () =>\n promptForProject({\n requiredPermissions: [\n {grant: 'read', permission: 'sanity.project.datasets'},\n {grant: 'update', permission: 'sanity.project.datasets'},\n ],\n }),\n })\n\n const dsError = validateDatasetName(dataset)\n if (dsError) {\n this.error(dsError, {exit: 1})\n }\n\n let datasets: DatasetsResponse\n\n try {\n datasets = await listDatasets(projectId)\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error)\n setDatasetVisibilityDebug(`Failed to list datasets: ${message}`, error)\n this.error(`Failed to list datasets: ${message}`, {exit: 1})\n }\n\n const current = datasets.find((curr: {name: string}) => curr.name === dataset)\n\n if (!current) {\n this.error(`Dataset \"${dataset}\" not found`, {exit: 1})\n }\n\n if (current.aclMode === mode) {\n this.log(`Dataset already in \"${mode}\" mode`)\n return\n }\n\n if (mode === 'private') {\n this.log(\n 'Please note that while documents are private, assets (files and images) are still public',\n )\n }\n\n try {\n await editDatasetAcl({\n aclMode: mode as 'private' | 'public',\n datasetName: dataset,\n projectId,\n })\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error)\n setDatasetVisibilityDebug(`Failed to edit dataset: ${message}`, error)\n this.error(`Failed to edit dataset: ${message}`, {exit: 1})\n }\n this.log('Dataset visibility changed')\n }\n}\n"],"names":["Args","SanityCommand","subdebug","validateDatasetName","promptForProject","editDatasetAcl","listDatasets","projectIdFlag","setDatasetVisibilityDebug","DatasetVisibilitySetCommand","args","dataset","string","description","required","mode","options","examples","command","flags","run","parse","projectId","getProjectId","fallback","requiredPermissions","grant","permission","dsError","error","exit","datasets","message","Error","String","current","find","curr","name","aclMode","log","datasetName"],"mappings":"AAAA,SAAQA,IAAI,QAAO,cAAa;AAChC,SAAQC,a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AA0B,CAAC,yBAAyB,EAAEwB,SAAS,EAAEH;YACjE,IAAI,CAACA,KAAK,CAAC,CAAC,yBAAyB,EAAEG,SAAS,EAAE;gBAACF,MAAM;YAAC;QAC5D;QAEA,MAAMK,UAAUJ,SAASK,IAAI,CAAC,CAACC,OAAyBA,KAAKC,IAAI,KAAK3B;QAEtE,IAAI,CAACwB,SAAS;YACZ,IAAI,CAACN,KAAK,CAAC,CAAC,SAAS,EAAElB,QAAQ,WAAW,CAAC,EAAE;gBAACmB,MAAM;YAAC;QACvD;QAEA,IAAIK,QAAQI,OAAO,KAAKxB,MAAM;YAC5B,IAAI,CAACyB,GAAG,CAAC,CAAC,oBAAoB,EAAEzB,KAAK,MAAM,CAAC;YAC5C;QACF;QAEA,IAAIA,SAAS,WAAW;YACtB,IAAI,CAACyB,GAAG,CACN;QAEJ;QAEA,IAAI;YACF,MAAMnC,eAAe;gBACnBkC,SAASxB;gBACT0B,aAAa9B;gBACbW;YACF;QACF,EAAE,OAAOO,OAAO;YACd,MAAMG,UAAUH,iBAAiBI,QAAQJ,MAAMG,OAAO,GAAGE,OAAOL;YAChErB,0BAA0B,CAAC,wBAAwB,EAAEwB,SAAS,EAAEH;YAChE,IAAI,CAACA,KAAK,CAAC,CAAC,wBAAwB,EAAEG,SAAS,EAAE;gBAACF,MAAM;YAAC;QAC3D;QACA,IAAI,CAACU,GAAG,CAAC;IACX;AACF"}
|
|
@@ -11,7 +11,6 @@ import isEqual from 'lodash-es/isEqual.js';
|
|
|
11
11
|
import isPlainObject from 'lodash-es/isPlainObject.js';
|
|
12
12
|
import { DOCUMENTS_API_VERSION } from '../../actions/documents/constants.js';
|
|
13
13
|
import { getEditor, registerUnlinkOnSigInt } from '../../actions/documents/editor.js';
|
|
14
|
-
import { NO_PROJECT_ID } from '../../util/errorMessages.js';
|
|
15
14
|
import { isIdentifiedSanityDocument, isSanityDocumentish } from '../../util/isSanityDocumentish.js';
|
|
16
15
|
const createDocumentDebug = subdebug('documents:create');
|
|
17
16
|
export class CreateDocumentCommand extends SanityCommand {
|
|
@@ -68,11 +67,6 @@ export class CreateDocumentCommand extends SanityCommand {
|
|
|
68
67
|
const { dataset, id, json5: useJson5, missing, replace, watch } = flags;
|
|
69
68
|
const cliConfig = await this.getCliConfig();
|
|
70
69
|
const projectId = await this.getProjectId();
|
|
71
|
-
if (!projectId) {
|
|
72
|
-
this.error(NO_PROJECT_ID, {
|
|
73
|
-
exit: 1
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
70
|
if (!cliConfig.api?.dataset && !dataset) {
|
|
77
71
|
this.error('No dataset specified. Either configure a dataset in sanity.cli.ts or use the --dataset flag', {
|
|
78
72
|
exit: 1
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/commands/documents/create.ts"],"sourcesContent":["import {randomUUID} from 'node:crypto'\nimport fs from 'node:fs/promises'\nimport os from 'node:os'\nimport path from 'node:path'\n\nimport {Args, Flags} from '@oclif/core'\nimport {getProjectCliClient, SanityCommand, subdebug} from '@sanity/cli-core'\nimport {type MultipleMutationResult, type Mutation} from '@sanity/client'\nimport {watch as chokidarWatch} from 'chokidar'\nimport {execa, execaSync} from 'execa'\nimport json5 from 'json5'\nimport isEqual from 'lodash-es/isEqual.js'\nimport isPlainObject from 'lodash-es/isPlainObject.js'\n\nimport {DOCUMENTS_API_VERSION} from '../../actions/documents/constants.js'\nimport {getEditor, registerUnlinkOnSigInt} from '../../actions/documents/editor.js'\nimport {NO_PROJECT_ID} from '../../util/errorMessages.js'\nimport {isIdentifiedSanityDocument, isSanityDocumentish} from '../../util/isSanityDocumentish.js'\n\nexport type MutationOperationName = 'create' | 'createIfNotExists' | 'createOrReplace'\n\nconst createDocumentDebug = subdebug('documents:create')\n\nexport class CreateDocumentCommand extends SanityCommand<typeof CreateDocumentCommand> {\n static override args = {\n file: Args.string({\n description: 'JSON file to create document(s) from',\n required: false,\n }),\n }\n\n static override description = 'Create one or more documents'\n\n static override examples = [\n {\n command: '<%= config.bin %> <%= command.id %> myDocument.json',\n description: 'Create the document specified in \"myDocument.json\"',\n },\n {\n command: '<%= config.bin %> <%= command.id %>',\n description: 'Open configured $EDITOR and create the specified document(s)',\n },\n {\n command: '<%= config.bin %> <%= command.id %> --id myDocId --replace',\n description:\n 'Fetch document with the ID \"myDocId\" and open configured $EDITOR with the current document content (if any). Replace document with the edited version when the editor closes',\n },\n {\n command: '<%= config.bin %> <%= command.id %> --id myDocId --watch --replace --json5',\n description:\n 'Open configured $EDITOR and replace the document with the given content on each save. Use JSON5 file extension and parser for simplified syntax.',\n },\n ]\n\n static override flags = {\n dataset: Flags.string({\n char: 'd',\n description: 'Dataset to create document(s) in (overrides config)',\n }),\n id: Flags.string({\n description:\n 'Specify a document ID to use. Will fetch remote document ID and populate editor.',\n }),\n json5: Flags.boolean({\n description: 'Use JSON5 file type to allow a \"simplified\" version of JSON',\n }),\n missing: Flags.boolean({\n description: \"On duplicate document IDs, don't modify the target document(s)\",\n }),\n replace: Flags.boolean({\n description:\n 'On duplicate document IDs, replace existing document with specified document(s)',\n }),\n watch: Flags.boolean({\n description: 'Write the documents whenever the target file or buffer changes',\n }),\n }\n\n private client!: Awaited<ReturnType<typeof getProjectCliClient>>\n\n public async run(): Promise<void> {\n const {args, flags} = await this.parse(CreateDocumentCommand)\n const {file} = args\n const {dataset, id, json5: useJson5, missing, replace, watch} = flags\n const cliConfig = await this.getCliConfig()\n const projectId = await this.getProjectId()\n\n if (!projectId) {\n this.error(NO_PROJECT_ID, {exit: 1})\n }\n\n if (!cliConfig.api?.dataset && !dataset) {\n this.error(\n 'No dataset specified. Either configure a dataset in sanity.cli.ts or use the --dataset flag',\n {exit: 1},\n )\n }\n\n const targetDataset = dataset || cliConfig.api?.dataset\n\n this.client = await getProjectCliClient({\n apiVersion: DOCUMENTS_API_VERSION,\n dataset: targetDataset,\n projectId,\n requireUser: true,\n })\n\n if (replace && missing) {\n this.error('Cannot use both --replace and --missing', {exit: 1})\n }\n\n if (id && file) {\n this.error('Cannot use --id when specifying a file path', {exit: 1})\n }\n\n let operation: MutationOperationName = 'create'\n if (replace || missing) {\n operation = replace ? 'createOrReplace' : 'createIfNotExists'\n }\n\n if (file) {\n try {\n const contentPath = path.resolve(process.cwd(), file)\n const content = json5.parse(await fs.readFile(contentPath, 'utf8'))\n const result = await this.writeDocuments(content, operation)\n this.log(this.getResultMessage(result, operation))\n return\n } catch (error) {\n const err = error as Error\n createDocumentDebug(`Error creating documents from file ${file}`, err)\n this.error(`Failed to create documents: ${err.message}`, {exit: 1})\n }\n }\n\n try {\n // Create a temporary file and use that as source, opening an editor on it\n const docId = id || randomUUID()\n const ext = useJson5 ? 'json5' : 'json'\n // Add UUID prefix to prevent predictable file names and potential conflicts\n const tmpFile = path.join(os.tmpdir(), 'sanity-cli', `${randomUUID()}-${docId}.${ext}`)\n const stringify = useJson5 ? json5.stringify : JSON.stringify\n const defaultValue = (id && (await this.client.getDocument(id))) || {\n _id: docId,\n _type: 'specify-me',\n }\n\n // Create temp directory with restricted permissions (owner only)\n const tempDir = path.join(os.tmpdir(), 'sanity-cli')\n await fs.mkdir(tempDir, {\n mode: 0o700, // rwx------ (owner read/write/execute only)\n recursive: true,\n })\n\n // Write file with restricted permissions (owner read/write only)\n await fs.writeFile(tmpFile, stringify(defaultValue, null, 2), {\n encoding: 'utf8',\n mode: 0o600, // rw------- (owner read/write only)\n })\n\n const editor = getEditor()\n const readAndPerformCreatesFromFile = this.readAndPerformCreatesFromFile.bind(this, operation)\n\n if (watch) {\n // If we're in watch mode, we want to run the creation on each change (if it validates)\n registerUnlinkOnSigInt(tmpFile)\n this.log(`Watch mode: ${tmpFile}`)\n this.log('Watch mode: Will write documents on each save.')\n this.log('Watch mode: Press Ctrl + C to cancel watch mode.')\n\n // Add race condition protection\n let isProcessing = false\n chokidarWatch(tmpFile).on('change', async () => {\n if (isProcessing) {\n return // Skip if already processing\n }\n isProcessing = true\n\n this.log('')\n try {\n await readAndPerformCreatesFromFile(tmpFile, defaultValue)\n } finally {\n isProcessing = false\n }\n })\n execa(editor.bin, [...editor.args, tmpFile], {stdio: 'inherit'})\n } else {\n // While in normal mode, we just want to wait for the editor to close and run the thing once\n execaSync(editor.bin, [...editor.args, tmpFile], {stdio: 'inherit'})\n await readAndPerformCreatesFromFile(tmpFile, defaultValue)\n await fs.unlink(tmpFile).catch(() => {})\n }\n } catch (error) {\n const err = error as Error\n createDocumentDebug('Error in editor workflow', err)\n this.error(`Failed to create documents: ${err.message}`, {exit: 1})\n }\n }\n\n private getErrorMessage(message: string, index: number, isSingle: boolean): string {\n return isSingle ? `Document ${message}` : `Document at index ${index} ${message}`\n }\n\n /**\n * Formats the result message for document operations\n */\n private getResultMessage(\n result: MultipleMutationResult,\n operation: MutationOperationName,\n ): string {\n const joiner = '\\n - '\n if (operation === 'createOrReplace') {\n return `Upserted:\\n - ${result.results.map((res) => res.id).join(joiner)}`\n }\n\n if (operation === 'create') {\n return `Created:\\n - ${result.results.map((res) => res.id).join(joiner)}`\n }\n\n // \"Missing\" (createIfNotExists)\n const created: string[] = []\n const skipped: string[] = []\n for (const res of result.results) {\n if (res.operation === 'update') {\n skipped.push(res.id)\n } else {\n created.push(res.id)\n }\n }\n\n if (created.length > 0 && skipped.length > 0) {\n return [\n `Created:\\n - ${created.join(joiner)}`,\n `Skipped (already exists):${joiner}${skipped.join(joiner)}`,\n ].join('\\n\\n')\n } else if (created.length > 0) {\n return `Created:\\n - ${created.join(joiner)}`\n }\n\n return `Skipped (already exists):\\n - ${skipped.join(joiner)}`\n }\n\n /**\n * Reads and processes documents from a file for creation\n */\n private async readAndPerformCreatesFromFile(\n operation: MutationOperationName,\n filePath: string,\n defaultValue: unknown,\n ): Promise<void> {\n let content\n try {\n content = json5.parse(await fs.readFile(filePath, 'utf8'))\n } catch (err) {\n const error = err as Error\n createDocumentDebug(`Failed to read input file ${filePath}`, error)\n this.log(`Failed to read input: ${error.message}`)\n return\n }\n\n if (isEqual(content, defaultValue)) {\n this.log('Value not modified, doing nothing.')\n this.log('Modify document to trigger creation.')\n return\n }\n\n try {\n const writeResult = await this.writeDocuments(content, operation)\n this.log(this.getResultMessage(writeResult, operation))\n } catch (err) {\n const error = err as Error\n createDocumentDebug(`Failed to write documents`, error)\n let errorMessage = `Failed to write documents: ${error.message}`\n if (error.message.includes('already exists')) {\n errorMessage += '\\nPerhaps you want to use `--replace` or `--missing`?'\n }\n this.error(errorMessage, {exit: 1})\n }\n }\n\n /**\n * Validates a document for Sanity requirements\n */\n private validateDocument(doc: unknown, index: number, arr: unknown[]): void {\n const isSingle = arr.length === 1\n\n if (!isPlainObject(doc)) {\n throw new Error(this.getErrorMessage('must be an object', index, isSingle))\n }\n\n if (!isSanityDocumentish(doc)) {\n throw new Error(\n this.getErrorMessage('must have a `_type` property of type string', index, isSingle),\n )\n }\n\n // Enhanced validations for Sanity document constraints\n const docObj = doc as Record<string, unknown>\n\n // Validate _type is non-empty\n const typeValue = docObj._type?.toString().trim()\n if (!typeValue) {\n throw new Error(this.getErrorMessage('_type cannot be empty', index, isSingle))\n }\n\n // Validate _type format (alphanumeric, underscore, hyphen, dot)\n if (!/^[a-zA-Z][a-zA-Z0-9_.-]*$/.test(typeValue)) {\n throw new Error(\n this.getErrorMessage(\n '_type must start with a letter and contain only alphanumeric characters, underscores, hyphens, and dots',\n index,\n isSingle,\n ),\n )\n }\n\n // Validate _id format if present\n if (docObj._id && typeof docObj._id === 'string') {\n const idValue = docObj._id.trim()\n if (!idValue) {\n throw new Error(this.getErrorMessage('_id cannot be empty', index, isSingle))\n }\n\n // Sanity document IDs can contain alphanumeric chars, hyphens, underscores, and dots\n if (!/^[a-zA-Z0-9_.-]+$/.test(idValue)) {\n throw new Error(\n this.getErrorMessage(\n '_id can only contain alphanumeric characters, underscores, hyphens, and dots',\n index,\n isSingle,\n ),\n )\n }\n\n // Check length constraints (Sanity has reasonable limits)\n if (idValue.length > 200) {\n throw new Error(\n this.getErrorMessage('_id cannot be longer than 200 characters', index, isSingle),\n )\n }\n }\n\n // Warn about reserved fields (these are managed by Sanity)\n const reservedFields = ['_rev', '_updatedAt', '_createdAt']\n for (const field of reservedFields) {\n if (field in docObj) {\n // Note: We don't throw an error here as these might be present in fetched documents\n // that are being re-uploaded, but we could add a debug warning\n createDocumentDebug(\n `Warning: Document ${index} contains reserved field '${field}' which will be ignored by Sanity`,\n )\n }\n }\n }\n\n /**\n * Writes documents to Sanity using the specified operation\n */\n private async writeDocuments(\n documents: {_id?: string; _type: string} | {_id?: string; _type: string}[],\n operation: MutationOperationName,\n ): Promise<MultipleMutationResult> {\n const docs = Array.isArray(documents) ? documents : [documents]\n if (docs.length === 0) {\n throw new Error('No documents provided')\n }\n\n const mutations = docs.map((doc, index): Mutation => {\n this.validateDocument(doc, index, docs)\n if (operation === 'create') {\n return {create: doc}\n }\n\n if (operation === 'createIfNotExists') {\n if (isIdentifiedSanityDocument(doc)) {\n return {createIfNotExists: doc}\n }\n\n throw new Error(`Missing required _id attribute for ${operation}`)\n }\n\n if (operation === 'createOrReplace') {\n if (isIdentifiedSanityDocument(doc)) {\n return {createOrReplace: doc}\n }\n\n throw new Error(`Missing required _id attribute for ${operation}`)\n }\n\n throw new Error(`Unsupported operation ${operation}`)\n })\n\n return this.client.transaction(mutations).commit()\n }\n}\n"],"names":["randomUUID","fs","os","path","Args","Flags","getProjectCliClient","SanityCommand","subdebug","watch","chokidarWatch","execa","execaSync","json5","isEqual","isPlainObject","DOCUMENTS_API_VERSION","getEditor","registerUnlinkOnSigInt","NO_PROJECT_ID","isIdentifiedSanityDocument","isSanityDocumentish","createDocumentDebug","CreateDocumentCommand","args","file","string","description","required","examples","command","flags","dataset","char","id","boolean","missing","replace","client","run","parse","useJson5","cliConfig","getCliConfig","projectId","getProjectId","error","exit","api","targetDataset","apiVersion","requireUser","operation","contentPath","resolve","process","cwd","content","readFile","result","writeDocuments","log","getResultMessage","err","message","docId","ext","tmpFile","join","tmpdir","stringify","JSON","defaultValue","getDocument","_id","_type","tempDir","mkdir","mode","recursive","writeFile","encoding","editor","readAndPerformCreatesFromFile","bind","isProcessing","on","bin","stdio","unlink","catch","getErrorMessage","index","isSingle","joiner","results","map","res","created","skipped","push","length","filePath","writeResult","errorMessage","includes","validateDocument","doc","arr","Error","docObj","typeValue","toString","trim","test","idValue","reservedFields","field","documents","docs","Array","isArray","mutations","create","createIfNotExists","createOrReplace","transaction","commit"],"mappings":"AAAA,SAAQA,UAAU,QAAO,cAAa;AACtC,OAAOC,QAAQ,mBAAkB;AACjC,OAAOC,QAAQ,UAAS;AACxB,OAAOC,UAAU,YAAW;AAE5B,SAAQC,IAAI,EAAEC,KAAK,QAAO,cAAa;AACvC,SAAQC,mBAAmB,EAAEC,aAAa,EAAEC,QAAQ,QAAO,mBAAkB;AAE7E,SAAQC,SAASC,aAAa,QAAO,WAAU;AAC/C,SAAQC,KAAK,EAAEC,SAAS,QAAO,QAAO;AACtC,OAAOC,WAAW,QAAO;AACzB,OAAOC,aAAa,uBAAsB;AAC1C,OAAOC,mBAAmB,6BAA4B;AAEtD,SAAQC,qBAAqB,QAAO,uCAAsC;AAC1E,SAAQC,SAAS,EAAEC,sBAAsB,QAAO,oCAAmC;AACnF,SAAQC,aAAa,QAAO,8BAA6B;AACzD,SAAQC,0BAA0B,EAAEC,mBAAmB,QAAO,oCAAmC;AAIjG,MAAMC,sBAAsBd,SAAS;AAErC,OAAO,MAAMe,8BAA8BhB;IACzC,OAAgBiB,OAAO;QACrBC,MAAMrB,KAAKsB,MAAM,CAAC;YAChBC,aAAa;YACbC,UAAU;QACZ;IACF,EAAC;IAED,OAAgBD,cAAc,+BAA8B;IAE5D,OAAgBE,WAAW;QACzB;YACEC,SAAS;YACTH,aAAa;QACf;QACA;YACEG,SAAS;YACTH,aAAa;QACf;QACA;YACEG,SAAS;YACTH,aACE;QACJ;QACA;YACEG,SAAS;YACTH,aACE;QACJ;KACD,CAAA;IAED,OAAgBI,QAAQ;QACtBC,SAAS3B,MAAMqB,MAAM,CAAC;YACpBO,MAAM;YACNN,a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ClC,IAAInE,cAAc,UAAU;gBAC1B,OAAO;oBAACuE,QAAQhB;gBAAG;YACrB;YAEA,IAAIvD,cAAc,qBAAqB;gBACrC,IAAIhC,2BAA2BuF,MAAM;oBACnC,OAAO;wBAACiB,mBAAmBjB;oBAAG;gBAChC;gBAEA,MAAM,IAAIE,MAAM,CAAC,mCAAmC,EAAEzD,WAAW;YACnE;YAEA,IAAIA,cAAc,mBAAmB;gBACnC,IAAIhC,2BAA2BuF,MAAM;oBACnC,OAAO;wBAACkB,iBAAiBlB;oBAAG;gBAC9B;gBAEA,MAAM,IAAIE,MAAM,CAAC,mCAAmC,EAAEzD,WAAW;YACnE;YAEA,MAAM,IAAIyD,MAAM,CAAC,sBAAsB,EAAEzD,WAAW;QACtD;QAEA,OAAO,IAAI,CAACd,MAAM,CAACwF,WAAW,CAACJ,WAAWK,MAAM;IAClD;AACF"}
|
|
1
|
+
{"version":3,"sources":["../../../src/commands/documents/create.ts"],"sourcesContent":["import {randomUUID} from 'node:crypto'\nimport fs from 'node:fs/promises'\nimport os from 'node:os'\nimport path from 'node:path'\n\nimport {Args, Flags} from '@oclif/core'\nimport {getProjectCliClient, SanityCommand, subdebug} from '@sanity/cli-core'\nimport {type MultipleMutationResult, type Mutation} from '@sanity/client'\nimport {watch as chokidarWatch} from 'chokidar'\nimport {execa, execaSync} from 'execa'\nimport json5 from 'json5'\nimport isEqual from 'lodash-es/isEqual.js'\nimport isPlainObject from 'lodash-es/isPlainObject.js'\n\nimport {DOCUMENTS_API_VERSION} from '../../actions/documents/constants.js'\nimport {getEditor, registerUnlinkOnSigInt} from '../../actions/documents/editor.js'\nimport {isIdentifiedSanityDocument, isSanityDocumentish} from '../../util/isSanityDocumentish.js'\n\nexport type MutationOperationName = 'create' | 'createIfNotExists' | 'createOrReplace'\n\nconst createDocumentDebug = subdebug('documents:create')\n\nexport class CreateDocumentCommand extends SanityCommand<typeof CreateDocumentCommand> {\n static override args = {\n file: Args.string({\n description: 'JSON file to create document(s) from',\n required: false,\n }),\n }\n\n static override description = 'Create one or more documents'\n\n static override examples = [\n {\n command: '<%= config.bin %> <%= command.id %> myDocument.json',\n description: 'Create the document specified in \"myDocument.json\"',\n },\n {\n command: '<%= config.bin %> <%= command.id %>',\n description: 'Open configured $EDITOR and create the specified document(s)',\n },\n {\n command: '<%= config.bin %> <%= command.id %> --id myDocId --replace',\n description:\n 'Fetch document with the ID \"myDocId\" and open configured $EDITOR with the current document content (if any). Replace document with the edited version when the editor closes',\n },\n {\n command: '<%= config.bin %> <%= command.id %> --id myDocId --watch --replace --json5',\n description:\n 'Open configured $EDITOR and replace the document with the given content on each save. Use JSON5 file extension and parser for simplified syntax.',\n },\n ]\n\n static override flags = {\n dataset: Flags.string({\n char: 'd',\n description: 'Dataset to create document(s) in (overrides config)',\n }),\n id: Flags.string({\n description:\n 'Specify a document ID to use. Will fetch remote document ID and populate editor.',\n }),\n json5: Flags.boolean({\n description: 'Use JSON5 file type to allow a \"simplified\" version of JSON',\n }),\n missing: Flags.boolean({\n description: \"On duplicate document IDs, don't modify the target document(s)\",\n }),\n replace: Flags.boolean({\n description:\n 'On duplicate document IDs, replace existing document with specified document(s)',\n }),\n watch: Flags.boolean({\n description: 'Write the documents whenever the target file or buffer changes',\n }),\n }\n\n private client!: Awaited<ReturnType<typeof getProjectCliClient>>\n\n public async run(): Promise<void> {\n const {args, flags} = await this.parse(CreateDocumentCommand)\n const {file} = args\n const {dataset, id, json5: useJson5, missing, replace, watch} = flags\n const cliConfig = await this.getCliConfig()\n const projectId = await this.getProjectId()\n\n\n if (!cliConfig.api?.dataset && !dataset) {\n this.error(\n 'No dataset specified. Either configure a dataset in sanity.cli.ts or use the --dataset flag',\n {exit: 1},\n )\n }\n\n const targetDataset = dataset || cliConfig.api?.dataset\n\n this.client = await getProjectCliClient({\n apiVersion: DOCUMENTS_API_VERSION,\n dataset: targetDataset,\n projectId,\n requireUser: true,\n })\n\n if (replace && missing) {\n this.error('Cannot use both --replace and --missing', {exit: 1})\n }\n\n if (id && file) {\n this.error('Cannot use --id when specifying a file path', {exit: 1})\n }\n\n let operation: MutationOperationName = 'create'\n if (replace || missing) {\n operation = replace ? 'createOrReplace' : 'createIfNotExists'\n }\n\n if (file) {\n try {\n const contentPath = path.resolve(process.cwd(), file)\n const content = json5.parse(await fs.readFile(contentPath, 'utf8'))\n const result = await this.writeDocuments(content, operation)\n this.log(this.getResultMessage(result, operation))\n return\n } catch (error) {\n const err = error as Error\n createDocumentDebug(`Error creating documents from file ${file}`, err)\n this.error(`Failed to create documents: ${err.message}`, {exit: 1})\n }\n }\n\n try {\n // Create a temporary file and use that as source, opening an editor on it\n const docId = id || randomUUID()\n const ext = useJson5 ? 'json5' : 'json'\n // Add UUID prefix to prevent predictable file names and potential conflicts\n const tmpFile = path.join(os.tmpdir(), 'sanity-cli', `${randomUUID()}-${docId}.${ext}`)\n const stringify = useJson5 ? json5.stringify : JSON.stringify\n const defaultValue = (id && (await this.client.getDocument(id))) || {\n _id: docId,\n _type: 'specify-me',\n }\n\n // Create temp directory with restricted permissions (owner only)\n const tempDir = path.join(os.tmpdir(), 'sanity-cli')\n await fs.mkdir(tempDir, {\n mode: 0o700, // rwx------ (owner read/write/execute only)\n recursive: true,\n })\n\n // Write file with restricted permissions (owner read/write only)\n await fs.writeFile(tmpFile, stringify(defaultValue, null, 2), {\n encoding: 'utf8',\n mode: 0o600, // rw------- (owner read/write only)\n })\n\n const editor = getEditor()\n const readAndPerformCreatesFromFile = this.readAndPerformCreatesFromFile.bind(this, operation)\n\n if (watch) {\n // If we're in watch mode, we want to run the creation on each change (if it validates)\n registerUnlinkOnSigInt(tmpFile)\n this.log(`Watch mode: ${tmpFile}`)\n this.log('Watch mode: Will write documents on each save.')\n this.log('Watch mode: Press Ctrl + C to cancel watch mode.')\n\n // Add race condition protection\n let isProcessing = false\n chokidarWatch(tmpFile).on('change', async () => {\n if (isProcessing) {\n return // Skip if already processing\n }\n isProcessing = true\n\n this.log('')\n try {\n await readAndPerformCreatesFromFile(tmpFile, defaultValue)\n } finally {\n isProcessing = false\n }\n })\n execa(editor.bin, [...editor.args, tmpFile], {stdio: 'inherit'})\n } else {\n // While in normal mode, we just want to wait for the editor to close and run the thing once\n execaSync(editor.bin, [...editor.args, tmpFile], {stdio: 'inherit'})\n await readAndPerformCreatesFromFile(tmpFile, defaultValue)\n await fs.unlink(tmpFile).catch(() => {})\n }\n } catch (error) {\n const err = error as Error\n createDocumentDebug('Error in editor workflow', err)\n this.error(`Failed to create documents: ${err.message}`, {exit: 1})\n }\n }\n\n private getErrorMessage(message: string, index: number, isSingle: boolean): string {\n return isSingle ? `Document ${message}` : `Document at index ${index} ${message}`\n }\n\n /**\n * Formats the result message for document operations\n */\n private getResultMessage(\n result: MultipleMutationResult,\n operation: MutationOperationName,\n ): string {\n const joiner = '\\n - '\n if (operation === 'createOrReplace') {\n return `Upserted:\\n - ${result.results.map((res) => res.id).join(joiner)}`\n }\n\n if (operation === 'create') {\n return `Created:\\n - ${result.results.map((res) => res.id).join(joiner)}`\n }\n\n // \"Missing\" (createIfNotExists)\n const created: string[] = []\n const skipped: string[] = []\n for (const res of result.results) {\n if (res.operation === 'update') {\n skipped.push(res.id)\n } else {\n created.push(res.id)\n }\n }\n\n if (created.length > 0 && skipped.length > 0) {\n return [\n `Created:\\n - ${created.join(joiner)}`,\n `Skipped (already exists):${joiner}${skipped.join(joiner)}`,\n ].join('\\n\\n')\n } else if (created.length > 0) {\n return `Created:\\n - ${created.join(joiner)}`\n }\n\n return `Skipped (already exists):\\n - ${skipped.join(joiner)}`\n }\n\n /**\n * Reads and processes documents from a file for creation\n */\n private async readAndPerformCreatesFromFile(\n operation: MutationOperationName,\n filePath: string,\n defaultValue: unknown,\n ): Promise<void> {\n let content\n try {\n content = json5.parse(await fs.readFile(filePath, 'utf8'))\n } catch (err) {\n const error = err as Error\n createDocumentDebug(`Failed to read input file ${filePath}`, error)\n this.log(`Failed to read input: ${error.message}`)\n return\n }\n\n if (isEqual(content, defaultValue)) {\n this.log('Value not modified, doing nothing.')\n this.log('Modify document to trigger creation.')\n return\n }\n\n try {\n const writeResult = await this.writeDocuments(content, operation)\n this.log(this.getResultMessage(writeResult, operation))\n } catch (err) {\n const error = err as Error\n createDocumentDebug(`Failed to write documents`, error)\n let errorMessage = `Failed to write documents: ${error.message}`\n if (error.message.includes('already exists')) {\n errorMessage += '\\nPerhaps you want to use `--replace` or `--missing`?'\n }\n this.error(errorMessage, {exit: 1})\n }\n }\n\n /**\n * Validates a document for Sanity requirements\n */\n private validateDocument(doc: unknown, index: number, arr: unknown[]): void {\n const isSingle = arr.length === 1\n\n if (!isPlainObject(doc)) {\n throw new Error(this.getErrorMessage('must be an object', index, isSingle))\n }\n\n if (!isSanityDocumentish(doc)) {\n throw new Error(\n this.getErrorMessage('must have a `_type` property of type string', index, isSingle),\n )\n }\n\n // Enhanced validations for Sanity document constraints\n const docObj = doc as Record<string, unknown>\n\n // Validate _type is non-empty\n const typeValue = docObj._type?.toString().trim()\n if (!typeValue) {\n throw new Error(this.getErrorMessage('_type cannot be empty', index, isSingle))\n }\n\n // Validate _type format (alphanumeric, underscore, hyphen, dot)\n if (!/^[a-zA-Z][a-zA-Z0-9_.-]*$/.test(typeValue)) {\n throw new Error(\n this.getErrorMessage(\n '_type must start with a letter and contain only alphanumeric characters, underscores, hyphens, and dots',\n index,\n isSingle,\n ),\n )\n }\n\n // Validate _id format if present\n if (docObj._id && typeof docObj._id === 'string') {\n const idValue = docObj._id.trim()\n if (!idValue) {\n throw new Error(this.getErrorMessage('_id cannot be empty', index, isSingle))\n }\n\n // Sanity document IDs can contain alphanumeric chars, hyphens, underscores, and dots\n if (!/^[a-zA-Z0-9_.-]+$/.test(idValue)) {\n throw new Error(\n this.getErrorMessage(\n '_id can only contain alphanumeric characters, underscores, hyphens, and dots',\n index,\n isSingle,\n ),\n )\n }\n\n // Check length constraints (Sanity has reasonable limits)\n if (idValue.length > 200) {\n throw new Error(\n this.getErrorMessage('_id cannot be longer than 200 characters', index, isSingle),\n )\n }\n }\n\n // Warn about reserved fields (these are managed by Sanity)\n const reservedFields = ['_rev', '_updatedAt', '_createdAt']\n for (const field of reservedFields) {\n if (field in docObj) {\n // Note: We don't throw an error here as these might be present in fetched documents\n // that are being re-uploaded, but we could add a debug warning\n createDocumentDebug(\n `Warning: Document ${index} contains reserved field '${field}' which will be ignored by Sanity`,\n )\n }\n }\n }\n\n /**\n * Writes documents to Sanity using the specified operation\n */\n private async writeDocuments(\n documents: {_id?: string; _type: string} | {_id?: string; _type: string}[],\n operation: MutationOperationName,\n ): Promise<MultipleMutationResult> {\n const docs = Array.isArray(documents) ? documents : [documents]\n if (docs.length === 0) {\n throw new Error('No documents provided')\n }\n\n const mutations = docs.map((doc, index): Mutation => {\n this.validateDocument(doc, index, docs)\n if (operation === 'create') {\n return {create: doc}\n }\n\n if (operation === 'createIfNotExists') {\n if (isIdentifiedSanityDocument(doc)) {\n return {createIfNotExists: doc}\n }\n\n throw new Error(`Missing required _id attribute for ${operation}`)\n }\n\n if (operation === 'createOrReplace') {\n if (isIdentifiedSanityDocument(doc)) {\n return {createOrReplace: doc}\n }\n\n throw new Error(`Missing required _id attribute for ${operation}`)\n }\n\n throw new Error(`Unsupported operation ${operation}`)\n })\n\n return this.client.transaction(mutations).commit()\n }\n}\n"],"names":["randomUUID","fs","os","path","Args","Flags","getProjectCliClient","SanityCommand","subdebug","watch","chokidarWatch","execa","execaSync","json5","isEqual","isPlainObject","DOCUMENTS_API_VERSION","getEditor","registerUnlinkOnSigInt","isIdentifiedSanityDocument","isSanityDocumentish","createDocumentDebug","CreateDocumentCommand","args","file","string","description","required","examples","command","flags","dataset","char","id","boolean","missing","replace","client","run","parse","useJson5","cliConfig","getCliConfig","projectId","getProjectId","api","error","exit","targetDataset","apiVersion","requireUser","operation","contentPath","resolve","process","cwd","content","readFile","result","writeDocuments","log","getResultMessage","err","message","docId","ext","tmpFile","join","tmpdir","stringify","JSON","defaultValue","getDocument","_id","_type","tempDir","mkdir","mode","recursive","writeFile","encoding","editor","readAndPerformCreatesFromFile","bind","isProcessing","on","bin","stdio","unlink","catch","getErrorMessage","index","isSingle","joiner","results","map","res","created","skipped","push","length","filePath","writeResult","errorMessage","includes","validateDocument","doc","arr","Error","docObj","typeValue","toString","trim","test","idValue","reservedFields","field","documents","docs","Array","isArray","mutations","create","createIfNotExists","createOrReplace","transaction","commit"],"mappings":"AAAA,SAAQA,UAAU,QAAO,cAAa;AACtC,OAAOC,QAAQ,mBAAkB;AACjC,OAAOC,QAAQ,UAAS;AACxB,OAAOC,UAAU,YAAW;AAE5B,SAAQC,IAAI,EAAEC,KAAK,QAAO,cAAa;AACvC,SAAQC,mBAAmB,EAAEC,aAAa,EAAEC,QAAQ,QAAO,mBAAkB;AAE7E,SAAQC,SAASC,aAAa,QAAO,WAAU;AAC/C,SAAQC,KAAK,EAAEC,SAAS,QAAO,QAAO;AACtC,OAAOC,WAAW,QAAO;AACzB,OAAOC,aAAa,uBAAsB;AAC1C,OAAOC,mBAAmB,6BAA4B;AAEtD,SAAQC,qBAAqB,QAAO,uCAAsC;AAC1E,SAAQC,SAAS,EAAEC,sBAAsB,QAAO,oCAAmC;AACnF,SAAQC,0BAA0B,EAAEC,mBAAmB,QAAO,oCAAmC;AAIjG,MAAMC,sBAAsBb,SAAS;AAErC,OAAO,MAAMc,8BAA8Bf;IACzC,OAAgBgB,OAAO;QACrBC,MAAMpB,KAAKqB,MAAM,CAAC;YAChBC,aAAa;YACbC,UAAU;QACZ;IACF,EAAC;IAED,OAAgBD,cAAc,+BAA8B;IAE5D,OAAgBE,WAAW;QACzB;YACEC,SAAS;YACTH,aAAa;QACf;QACA;YACEG,SAAS;YACTH,aAAa;QACf;QACA;YACEG,SAAS;YACTH,aACE;QACJ;QACA;YACEG,SAAS;YACTH,aACE;QACJ;KACD,CAAA;IAED,OAAgBI,QAAQ;QACtBC,SAAS1B,MAAMoB,MAAM,CAAC;YACpBO,MAAM;YACNN,aAAa;QACf;QACAO,IAAI5B,MAAMoB,M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AAM;oBACnC,OAAO;wBAACiB,mBAAmBjB;oBAAG;gBAChC;gBAEA,MAAM,IAAIE,MAAM,CAAC,mCAAmC,EAAEzD,WAAW;YACnE;YAEA,IAAIA,cAAc,mBAAmB;gBACnC,IAAIhC,2BAA2BuF,MAAM;oBACnC,OAAO;wBAACkB,iBAAiBlB;oBAAG;gBAC9B;gBAEA,MAAM,IAAIE,MAAM,CAAC,mCAAmC,EAAEzD,WAAW;YACnE;YAEA,MAAM,IAAIyD,MAAM,CAAC,sBAAsB,EAAEzD,WAAW;QACtD;QAEA,OAAO,IAAI,CAACd,MAAM,CAACwF,WAAW,CAACJ,WAAWK,MAAM;IAClD;AACF"}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { Args, Flags } from '@oclif/core';
|
|
2
2
|
import { getProjectCliClient, SanityCommand, subdebug } from '@sanity/cli-core';
|
|
3
3
|
import { DOCUMENTS_API_VERSION } from '../../actions/documents/constants.js';
|
|
4
|
-
import { NO_PROJECT_ID } from '../../util/errorMessages.js';
|
|
5
4
|
const deleteDocumentDebug = subdebug('documents:delete');
|
|
6
5
|
export class DeleteDocumentCommand extends SanityCommand {
|
|
7
6
|
static args = {
|
|
@@ -58,11 +57,6 @@ export class DeleteDocumentCommand extends SanityCommand {
|
|
|
58
57
|
// Get project configuration
|
|
59
58
|
const cliConfig = await this.getCliConfig();
|
|
60
59
|
const projectId = await this.getProjectId();
|
|
61
|
-
if (!projectId) {
|
|
62
|
-
this.error(NO_PROJECT_ID, {
|
|
63
|
-
exit: 1
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
60
|
if (!cliConfig.api?.dataset && !dataset) {
|
|
67
61
|
this.error('No dataset specified. Either configure a dataset in sanity.cli.ts or use the --dataset flag', {
|
|
68
62
|
exit: 1
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/commands/documents/delete.ts"],"sourcesContent":["import {Args, Flags} from '@oclif/core'\nimport {getProjectCliClient, SanityCommand, subdebug} from '@sanity/cli-core'\n\nimport {DOCUMENTS_API_VERSION} from '../../actions/documents/constants.js'\
|
|
1
|
+
{"version":3,"sources":["../../../src/commands/documents/delete.ts"],"sourcesContent":["import {Args, Flags} from '@oclif/core'\nimport {getProjectCliClient, SanityCommand, subdebug} from '@sanity/cli-core'\n\nimport {DOCUMENTS_API_VERSION} from '../../actions/documents/constants.js'\n\nconst deleteDocumentDebug = subdebug('documents:delete')\n\nexport class DeleteDocumentCommand extends SanityCommand<typeof DeleteDocumentCommand> {\n static override args = {\n id: Args.string({\n description: 'Document ID to delete',\n required: true,\n }),\n ids: Args.string({\n description: 'Additional document IDs to delete',\n required: false,\n }),\n }\n\n static override description = 'Delete one or more documents from the projects configured dataset'\n\n static override examples = [\n {\n command: '<%= config.bin %> <%= command.id %> myDocId',\n description: 'Delete the document with the ID \"myDocId\"',\n },\n {\n command: \"<%= config.bin %> <%= command.id %> 'myDocId'\",\n description: 'ID wrapped in double or single quote works equally well',\n },\n {\n command: '<%= config.bin %> <%= command.id %> --dataset=blog someDocId',\n description: 'Delete document with ID \"someDocId\" from dataset \"blog\"',\n },\n {\n command: '<%= config.bin %> <%= command.id %> doc1 doc2',\n description: 'Delete the document with ID \"doc1\" and \"doc2\"',\n },\n ]\n\n static override flags = {\n dataset: Flags.string({\n description: 'NAME to override dataset',\n }),\n }\n\n // Disable strict mode to allow for more flexible input\n // This is needed for supporting multiple document IDs\n static override strict = false\n\n public async run(): Promise<void> {\n const {args, argv, flags} = await this.parse(DeleteDocumentCommand)\n const {id} = args\n const {dataset} = flags\n\n // Collect all document IDs from args and argv\n const ids = [id, ...argv.slice(1)].filter(Boolean) as string[]\n\n if (ids.length === 0) {\n this.error('Document ID must be specified', {exit: 1})\n }\n\n // Get project configuration\n const cliConfig = await this.getCliConfig()\n const projectId = await this.getProjectId()\n\n\n if (!cliConfig.api?.dataset && !dataset) {\n this.error(\n 'No dataset specified. Either configure a dataset in sanity.cli.ts or use the --dataset flag',\n {exit: 1},\n )\n }\n\n const targetDataset = dataset || cliConfig.api?.dataset\n\n try {\n const projectClient = await getProjectCliClient({\n apiVersion: DOCUMENTS_API_VERSION,\n dataset: targetDataset,\n projectId,\n requireUser: true,\n })\n\n const transaction = projectClient.transaction()\n for (const id of ids) {\n transaction.delete(id)\n }\n const {results} = await transaction.commit()\n const deleted = results.filter((res) => res.operation === 'delete').map((res) => res.id)\n const notFound = ids.filter((id) => !deleted.includes(id))\n\n if (deleted.length > 0) {\n this.log(`Deleted ${deleted.length} ${deleted.length === 1 ? 'document' : 'documents'}`)\n }\n\n if (notFound.length > 0) {\n this.error(\n `${notFound.length === 1 ? 'Document' : 'Documents'} not found: ${notFound.join(', ')}`,\n {\n exit: 1,\n },\n )\n }\n } catch (error) {\n const err = error as Error\n deleteDocumentDebug(`Error deleting documents ${ids.join(', ')}`, err)\n this.error(\n `Failed to delete ${ids.length} ${ids.length === 1 ? 'document' : 'documents'}: ${err.message}`,\n {\n exit: 1,\n },\n )\n }\n }\n}\n"],"names":["Args","Flags","getProjectCliClient","SanityCommand","subdebug","DOCUMENTS_API_VERSION","deleteDocumentDebug","DeleteDocumentCommand","args","id","string","description","required","ids","examples","command","flags","dataset","strict","run","argv","parse","slice","filter","Boolean","length","error","exit","cliConfig","getCliConfig","projectId","getProjectId","api","targetDataset","projectClient","apiVersion","requireUser","transaction","delete","results","commit","deleted","res","operation","map","notFound","includes","log","join","err","message"],"mappings":"AAAA,SAAQA,IAAI,EAAEC,KAAK,QAAO,cAAa;AACvC,SAAQC,mBAAmB,EAAEC,aAAa,EAAEC,QAAQ,QAAO,mBAAkB;AAE7E,SAAQC,qBAAqB,QAAO,uCAAsC;AAE1E,MAAMC,sBAAsBF,SAAS;AAErC,OAAO,MAAMG,8BAA8BJ;IACzC,OAAgBK,OAAO;QACrBC,IAAIT,KAAKU,MAAM,CAAC;YACdC,aAAa;YACbC,UAAU;QACZ;QACAC,KAAKb,KAAKU,MAAM,CAAC;YACfC,aAAa;YACbC,UAAU;QACZ;I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M,EAAC8B,OAAO,EAAC,GAAG,MAAMF,YAAYG,MAAM;YAC1C,MAAMC,UAAUF,QAAQhB,MAAM,CAAC,CAACmB,MAAQA,IAAIC,SAAS,KAAK,UAAUC,GAAG,CAAC,CAACF,MAAQA,IAAIjC,EAAE;YACvF,MAAMoC,WAAWhC,IAAIU,MAAM,CAAC,CAACd,KAAO,CAACgC,QAAQK,QAAQ,CAACrC;YAEtD,IAAIgC,QAAQhB,MAAM,GAAG,GAAG;gBACtB,IAAI,CAACsB,GAAG,CAAC,CAAC,QAAQ,EAAEN,QAAQhB,MAAM,CAAC,CAAC,EAAEgB,QAAQhB,MAAM,KAAK,IAAI,aAAa,aAAa;YACzF;YAEA,IAAIoB,SAASpB,MAAM,GAAG,GAAG;gBACvB,IAAI,CAACC,KAAK,CACR,GAAGmB,SAASpB,MAAM,KAAK,IAAI,aAAa,YAAY,YAAY,EAAEoB,SAASG,IAAI,CAAC,OAAO,EACvF;oBACErB,MAAM;gBACR;YAEJ;QACF,EAAE,OAAOD,OAAO;YACd,MAAMuB,MAAMvB;YACZpB,oBAAoB,CAAC,yBAAyB,EAAEO,IAAImC,IAAI,CAAC,OAAO,EAAEC;YAClE,IAAI,CAACvB,KAAK,CACR,CAAC,iBAAiB,EAAEb,IAAIY,MAAM,CAAC,CAAC,EAAEZ,IAAIY,MAAM,KAAK,IAAI,aAAa,YAAY,EAAE,EAAEwB,IAAIC,OAAO,EAAE,EAC/F;gBACEvB,MAAM;YACR;QAEJ;IACF;AACF"}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { Args, Flags } from '@oclif/core';
|
|
2
2
|
import { colorizeJson, getProjectCliClient, SanityCommand, subdebug } from '@sanity/cli-core';
|
|
3
3
|
import { DOCUMENTS_API_VERSION } from '../../actions/documents/constants.js';
|
|
4
|
-
import { NO_PROJECT_ID } from '../../util/errorMessages.js';
|
|
5
4
|
const getDocumentDebug = subdebug('documents:get');
|
|
6
5
|
export class GetDocumentCommand extends SanityCommand {
|
|
7
6
|
static args = {
|
|
@@ -42,11 +41,6 @@ export class GetDocumentCommand extends SanityCommand {
|
|
|
42
41
|
// Get project configuration
|
|
43
42
|
const cliConfig = await this.getCliConfig();
|
|
44
43
|
const projectId = await this.getProjectId();
|
|
45
|
-
if (!projectId) {
|
|
46
|
-
this.error(NO_PROJECT_ID, {
|
|
47
|
-
exit: 1
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
44
|
if (!cliConfig.api?.dataset && !dataset) {
|
|
51
45
|
this.error('No dataset specified. Either configure a dataset in sanity.cli.ts or use the --dataset flag', {
|
|
52
46
|
exit: 1
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/commands/documents/get.ts"],"sourcesContent":["import {Args, Flags} from '@oclif/core'\nimport {colorizeJson, getProjectCliClient, SanityCommand, subdebug} from '@sanity/cli-core'\n\nimport {DOCUMENTS_API_VERSION} from '../../actions/documents/constants.js'\
|
|
1
|
+
{"version":3,"sources":["../../../src/commands/documents/get.ts"],"sourcesContent":["import {Args, Flags} from '@oclif/core'\nimport {colorizeJson, getProjectCliClient, SanityCommand, subdebug} from '@sanity/cli-core'\n\nimport {DOCUMENTS_API_VERSION} from '../../actions/documents/constants.js'\n\nconst getDocumentDebug = subdebug('documents:get')\n\nexport class GetDocumentCommand extends SanityCommand<typeof GetDocumentCommand> {\n static override args = {\n documentId: Args.string({\n description: 'Document ID to retrieve',\n required: true,\n }),\n }\n\n static override description = 'Get and print a document by ID'\n\n static override examples = [\n {\n command: '<%= config.bin %> <%= command.id %> myDocId',\n description: 'Get the document with ID \"myDocId\"',\n },\n {\n command: '<%= config.bin %> <%= command.id %> myDocId --pretty',\n description: 'Get document with colorized JSON output',\n },\n {\n command: '<%= config.bin %> <%= command.id %> myDocId --dataset production',\n description: 'Get document from a specific dataset',\n },\n ]\n\n static override flags = {\n dataset: Flags.string({\n char: 'd',\n description: 'Dataset to get document from (overrides config)',\n }),\n pretty: Flags.boolean({\n default: false,\n description: 'Colorize JSON output',\n }),\n }\n\n public async run(): Promise<void> {\n const {args, flags} = await this.parse(GetDocumentCommand)\n const {documentId} = args\n const {dataset, pretty} = flags\n\n // Get project configuration\n const cliConfig = await this.getCliConfig()\n const projectId = await this.getProjectId()\n\n\n if (!cliConfig.api?.dataset && !dataset) {\n this.error(\n 'No dataset specified. Either configure a dataset in sanity.cli.ts or use the --dataset flag',\n {exit: 1},\n )\n }\n\n const targetDataset = dataset || cliConfig.api?.dataset\n\n try {\n const projectClient = await getProjectCliClient({\n apiVersion: DOCUMENTS_API_VERSION,\n dataset: targetDataset,\n projectId,\n requireUser: true,\n })\n\n const doc = await projectClient.getDocument(documentId)\n\n if (!doc) {\n this.error(`Document \"${documentId}\" not found in dataset \"${targetDataset}\"`, {exit: 1})\n }\n\n // Output the document\n if (pretty) {\n this.log(colorizeJson(doc))\n } else {\n this.log(JSON.stringify(doc, null, 2))\n }\n } catch (error) {\n const err = error as Error\n\n getDocumentDebug(`Error fetching document ${documentId}`, err)\n this.error(`Failed to fetch document: ${err.message}`, {exit: 1})\n }\n }\n}\n"],"names":["Args","Flags","colorizeJson","getProjectCliClient","SanityCommand","subdebug","DOCUMENTS_API_VERSION","getDocumentDebug","GetDocumentCommand","args","documentId","string","description","required","examples","command","flags","dataset","char","pretty","boolean","default","run","parse","cliConfig","getCliConfig","projectId","getProjectId","api","error","exit","targetDataset","projectClient","apiVersion","requireUser","doc","getDocument","log","JSON","stringify","err","message"],"mappings":"AAAA,SAAQA,IAAI,EAAEC,KAAK,QAAO,cAAa;AACvC,SAAQC,YAAY,EAAEC,mBAAmB,EAAEC,aAAa,EAAEC,QAAQ,QAAO,mBAAkB;AAE3F,SAAQC,qBAAqB,QAAO,uCAAsC;AAE1E,MAAMC,mBAAmBF,SAAS;AAElC,OAAO,MAAMG,2BAA2BJ;IACtC,OAAgBK,OAAO;QACrBC,YAAYV,KAAKW,MAAM,CAAC;YACtBC,aAAa;YACbC,UAAU;QACZ;IACF,EAAC;IAED,OAA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M;YACrC;QACF,EAAE,OAAON,OAAO;YACd,MAAMW,MAAMX;YAEZtB,iBAAiB,CAAC,wBAAwB,EAAEG,YAAY,EAAE8B;YAC1D,IAAI,CAACX,KAAK,CAAC,CAAC,0BAA0B,EAAEW,IAAIC,OAAO,EAAE,EAAE;gBAACX,MAAM;YAAC;QACjE;IACF;AACF"}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { Args, Flags } from '@oclif/core';
|
|
2
2
|
import { colorizeJson, getProjectCliClient, SanityCommand, subdebug } from '@sanity/cli-core';
|
|
3
3
|
import { DOCUMENTS_API_VERSION } from '../../actions/documents/constants.js';
|
|
4
|
-
import { NO_PROJECT_ID } from '../../util/errorMessages.js';
|
|
5
4
|
const queryDocumentDebug = subdebug('documents:query');
|
|
6
5
|
export class QueryDocumentCommand extends SanityCommand {
|
|
7
6
|
static args = {
|
|
@@ -56,11 +55,6 @@ export class QueryDocumentCommand extends SanityCommand {
|
|
|
56
55
|
const configProjectId = await this.getProjectId();
|
|
57
56
|
const targetProject = project || configProjectId;
|
|
58
57
|
const requireUser = !anonymous;
|
|
59
|
-
if (!targetProject) {
|
|
60
|
-
this.error(NO_PROJECT_ID, {
|
|
61
|
-
exit: 1
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
58
|
if (!cliConfig.api?.dataset && !dataset) {
|
|
65
59
|
this.error('No dataset specified. Either configure a dataset in sanity.cli.ts or use the --dataset flag', {
|
|
66
60
|
exit: 1
|