@sanity/cli 6.1.8 → 6.2.1
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 +301 -294
- package/dist/actions/debug/gatherDebugInfo.js +130 -81
- package/dist/actions/debug/gatherDebugInfo.js.map +1 -1
- package/dist/actions/debug/output.js +25 -0
- package/dist/actions/debug/output.js.map +1 -0
- package/dist/actions/debug/types.js.map +1 -1
- package/dist/actions/manifest/__tests__/testHelpers.js +21 -0
- package/dist/actions/manifest/__tests__/testHelpers.js.map +1 -0
- package/dist/actions/manifest/schemaTypeTransformer.js +2 -1
- package/dist/actions/manifest/schemaTypeTransformer.js.map +1 -1
- package/dist/actions/mcp/setupMCP.js +5 -5
- package/dist/actions/mcp/setupMCP.js.map +1 -1
- package/dist/actions/schema/getExtractOptions.js.map +1 -1
- package/dist/commands/{backup → backups}/disable.js +3 -0
- package/dist/commands/backups/disable.js.map +1 -0
- package/dist/commands/{backup → backups}/download.js +3 -0
- package/dist/commands/backups/download.js.map +1 -0
- package/dist/commands/{backup → backups}/enable.js +3 -0
- package/dist/commands/backups/enable.js.map +1 -0
- package/dist/commands/{backup → backups}/list.js +3 -0
- package/dist/commands/backups/list.js.map +1 -0
- package/dist/commands/{dataset → datasets}/alias/create.js +3 -0
- package/dist/commands/datasets/alias/create.js.map +1 -0
- package/dist/commands/{dataset → datasets}/alias/delete.js +3 -0
- package/dist/commands/datasets/alias/delete.js.map +1 -0
- package/dist/commands/{dataset → datasets}/alias/link.js +3 -0
- package/dist/commands/datasets/alias/link.js.map +1 -0
- package/dist/commands/{dataset → datasets}/alias/unlink.js +3 -0
- package/dist/commands/datasets/alias/unlink.js.map +1 -0
- package/dist/commands/{dataset → datasets}/copy.js +3 -0
- package/dist/commands/datasets/copy.js.map +1 -0
- package/dist/commands/{dataset → datasets}/create.js +3 -0
- package/dist/commands/datasets/create.js.map +1 -0
- package/dist/commands/{dataset → datasets}/delete.js +3 -0
- package/dist/commands/datasets/delete.js.map +1 -0
- package/dist/commands/{dataset → datasets}/embeddings/disable.js +3 -0
- package/dist/commands/datasets/embeddings/disable.js.map +1 -0
- package/dist/commands/{dataset → datasets}/embeddings/enable.js +3 -0
- package/dist/commands/datasets/embeddings/enable.js.map +1 -0
- package/dist/commands/{dataset → datasets}/embeddings/status.js +3 -0
- package/dist/commands/datasets/embeddings/status.js.map +1 -0
- package/dist/commands/{dataset → datasets}/export.js +3 -0
- package/dist/commands/datasets/export.js.map +1 -0
- package/dist/commands/{dataset → datasets}/import.js +14 -10
- package/dist/commands/datasets/import.js.map +1 -0
- package/dist/commands/{dataset → datasets}/list.js +3 -0
- package/dist/commands/datasets/list.js.map +1 -0
- package/dist/commands/{dataset → datasets}/visibility/get.js +3 -0
- package/dist/commands/datasets/visibility/get.js.map +1 -0
- package/dist/commands/{dataset → datasets}/visibility/set.js +3 -0
- package/dist/commands/datasets/visibility/set.js.map +1 -0
- package/dist/commands/debug.js +189 -74
- package/dist/commands/debug.js.map +1 -1
- package/dist/commands/documents/create.js +3 -0
- package/dist/commands/documents/create.js.map +1 -1
- package/dist/commands/documents/delete.js +3 -0
- package/dist/commands/documents/delete.js.map +1 -1
- package/dist/commands/documents/get.js +3 -0
- package/dist/commands/documents/get.js.map +1 -1
- package/dist/commands/documents/query.js +3 -0
- package/dist/commands/documents/query.js.map +1 -1
- package/dist/commands/documents/validate.js +3 -0
- package/dist/commands/documents/validate.js.map +1 -1
- package/dist/commands/{hook → hooks}/attempt.js +3 -0
- package/dist/commands/hooks/attempt.js.map +1 -0
- package/dist/commands/{hook → hooks}/create.js +3 -0
- package/dist/commands/hooks/create.js.map +1 -0
- package/dist/commands/{hook → hooks}/delete.js +3 -0
- package/dist/commands/hooks/delete.js.map +1 -0
- package/dist/commands/{hook → hooks}/list.js +3 -0
- package/dist/commands/hooks/list.js.map +1 -0
- package/dist/commands/{hook → hooks}/logs.js +3 -0
- package/dist/commands/hooks/logs.js.map +1 -0
- package/dist/commands/init.js +65 -13
- package/dist/commands/init.js.map +1 -1
- package/dist/commands/mcp/configure.js +3 -2
- package/dist/commands/mcp/configure.js.map +1 -1
- package/dist/commands/preview.js +1 -0
- package/dist/commands/preview.js.map +1 -1
- package/dist/commands/projects/create.js +3 -0
- package/dist/commands/projects/create.js.map +1 -1
- package/dist/commands/projects/list.js +3 -0
- package/dist/commands/projects/list.js.map +1 -1
- package/dist/commands/{schema → schemas}/delete.js +3 -0
- package/dist/commands/schemas/delete.js.map +1 -0
- package/dist/commands/{schema → schemas}/deploy.js +3 -0
- package/dist/commands/schemas/deploy.js.map +1 -0
- package/dist/commands/{schema → schemas}/extract.js +3 -0
- package/dist/commands/schemas/extract.js.map +1 -0
- package/dist/commands/{schema → schemas}/list.js +3 -0
- package/dist/commands/schemas/list.js.map +1 -0
- package/dist/commands/{schema → schemas}/validate.js +3 -0
- package/dist/commands/schemas/validate.js.map +1 -0
- package/dist/commands/tokens/add.js +3 -0
- package/dist/commands/tokens/add.js.map +1 -1
- package/dist/commands/tokens/delete.js +3 -0
- package/dist/commands/tokens/delete.js.map +1 -1
- package/dist/commands/tokens/list.js +3 -0
- package/dist/commands/tokens/list.js.map +1 -1
- package/dist/commands/users/invite.js +3 -0
- package/dist/commands/users/invite.js.map +1 -1
- package/dist/commands/users/list.js +3 -0
- package/dist/commands/users/list.js.map +1 -1
- package/dist/hooks/commandNotFound/topicAliases.js +71 -0
- package/dist/hooks/commandNotFound/topicAliases.js.map +1 -0
- package/dist/services/telemetry.js +23 -2
- package/dist/services/telemetry.js.map +1 -1
- package/dist/topicAliases.js +51 -0
- package/dist/topicAliases.js.map +1 -0
- package/dist/util/packageManager/packageManagerChoice.js +1 -2
- package/dist/util/packageManager/packageManagerChoice.js.map +1 -1
- package/oclif.config.js +20 -7
- package/oclif.manifest.json +478 -362
- package/package.json +17 -16
- package/dist/actions/debug/formatters.js +0 -22
- package/dist/actions/debug/formatters.js.map +0 -1
- package/dist/actions/debug/getGlobalConfigLocation.js +0 -7
- package/dist/actions/debug/getGlobalConfigLocation.js.map +0 -1
- package/dist/commands/backup/disable.js.map +0 -1
- package/dist/commands/backup/download.js.map +0 -1
- package/dist/commands/backup/enable.js.map +0 -1
- package/dist/commands/backup/list.js.map +0 -1
- package/dist/commands/dataset/alias/create.js.map +0 -1
- package/dist/commands/dataset/alias/delete.js.map +0 -1
- package/dist/commands/dataset/alias/link.js.map +0 -1
- package/dist/commands/dataset/alias/unlink.js.map +0 -1
- package/dist/commands/dataset/copy.js.map +0 -1
- package/dist/commands/dataset/create.js.map +0 -1
- package/dist/commands/dataset/delete.js.map +0 -1
- package/dist/commands/dataset/embeddings/disable.js.map +0 -1
- package/dist/commands/dataset/embeddings/enable.js.map +0 -1
- package/dist/commands/dataset/embeddings/status.js.map +0 -1
- package/dist/commands/dataset/export.js.map +0 -1
- package/dist/commands/dataset/import.js.map +0 -1
- package/dist/commands/dataset/list.js.map +0 -1
- package/dist/commands/dataset/visibility/get.js.map +0 -1
- package/dist/commands/dataset/visibility/set.js.map +0 -1
- package/dist/commands/hook/attempt.js.map +0 -1
- package/dist/commands/hook/create.js.map +0 -1
- package/dist/commands/hook/delete.js.map +0 -1
- package/dist/commands/hook/list.js.map +0 -1
- package/dist/commands/hook/logs.js.map +0 -1
- package/dist/commands/schema/delete.js.map +0 -1
- package/dist/commands/schema/deploy.js.map +0 -1
- package/dist/commands/schema/extract.js.map +0 -1
- package/dist/commands/schema/list.js.map +0 -1
- package/dist/commands/schema/validate.js.map +0 -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 {promptForProject} from '../../prompts/promptForProject.js'\nimport {isIdentifiedSanityDocument, isSanityDocumentish} from '../../util/isSanityDocumentish.js'\nimport {getDatasetFlag, getProjectIdFlag} from '../../util/sharedFlags.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 command: '<%= config.bin %> <%= command.id %> myDocument.json --project-id abc123',\n description: 'Create documents in a specific project',\n },\n ]\n\n static override flags = {\n ...getProjectIdFlag({\n description: 'Project ID to create document(s) in',\n semantics: 'override',\n }),\n ...getDatasetFlag({\n description: 'Dataset to create document(s) in',\n semantics: 'override',\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\n const cliConfig = await this.tryGetCliConfig()\n\n const projectId = await this.getProjectId({fallback: () => promptForProject({})})\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","promptForProject","isIdentifiedSanityDocument","isSanityDocumentish","getDatasetFlag","getProjectIdFlag","createDocumentDebug","CreateDocumentCommand","args","file","string","description","required","examples","command","flags","semantics","id","boolean","missing","replace","client","run","parse","dataset","useJson5","cliConfig","tryGetCliConfig","projectId","getProjectId","fallback","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,OAA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OAAO,IAAI,CAAChB,MAAM,CAAC0F,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 {promptForProject} from '../../prompts/promptForProject.js'\nimport {isIdentifiedSanityDocument, isSanityDocumentish} from '../../util/isSanityDocumentish.js'\nimport {getDatasetFlag, getProjectIdFlag} from '../../util/sharedFlags.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 command: '<%= config.bin %> <%= command.id %> myDocument.json --project-id abc123',\n description: 'Create documents in a specific project',\n },\n ]\n\n static override flags = {\n ...getProjectIdFlag({\n description: 'Project ID to create document(s) in',\n semantics: 'override',\n }),\n ...getDatasetFlag({\n description: 'Dataset to create document(s) in',\n semantics: 'override',\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 static override hiddenAliases: string[] = ['document:create']\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\n const cliConfig = await this.tryGetCliConfig()\n\n const projectId = await this.getProjectId({fallback: () => promptForProject({})})\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","promptForProject","isIdentifiedSanityDocument","isSanityDocumentish","getDatasetFlag","getProjectIdFlag","createDocumentDebug","CreateDocumentCommand","args","file","string","description","required","examples","command","flags","semantics","id","boolean","missing","replace","hiddenAliases","client","run","parse","dataset","useJson5","cliConfig","tryGetCliConfig","projectId","getProjectId","fallback","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,gBAAgB,QAAO,oCAAmC;AAClE,SAAQC,0BAA0B,EAAEC,mBAAmB,QAAO,oCAAmC;AACjG,SAAQC,cAAc,EAAEC,gBAAgB,QAAO,4BAA2B;AAI1E,MAAMC,sBAAsBhB,SAAS;AAErC,OAAO,MAAMiB,8BAA8BlB;IACzC,OAAgBmB,OAAO;QACrBC,MAAMvB,KAAKwB,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;QACA;YACEG,SAAS;YACTH,aAAa;QACf;KACD,CAAA;IAED,OAAgBI,QAAQ;QACtB,GAAGV,iBAAiB;YAClBM,aAAa;YACbK,WAAW;QACb,EAAE;QACF,GAAGZ,eAAe;YAChBO,aAAa;YACbK,WAAW;QACb,EAAE;QACFC,IAAI9B,MAAMuB,M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AC1B,OAAO;oBAACuE,QAAQhB;gBAAG;YACrB;YAEA,IAAIvD,cAAc,qBAAqB;gBACrC,IAAIpC,2BAA2B2F,MAAM;oBACnC,OAAO;wBAACiB,mBAAmBjB;oBAAG;gBAChC;gBAEA,MAAM,IAAIE,MAAM,CAAC,mCAAmC,EAAEzD,WAAW;YACnE;YAEA,IAAIA,cAAc,mBAAmB;gBACnC,IAAIpC,2BAA2B2F,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,CAAChB,MAAM,CAAC0F,WAAW,CAACJ,WAAWK,MAAM;IAClD;AACF"}
|
|
@@ -48,6 +48,9 @@ export class DeleteDocumentCommand extends SanityCommand {
|
|
|
48
48
|
semantics: 'override'
|
|
49
49
|
})
|
|
50
50
|
};
|
|
51
|
+
static hiddenAliases = [
|
|
52
|
+
'document:delete'
|
|
53
|
+
];
|
|
51
54
|
// Disable strict mode to allow for more flexible input
|
|
52
55
|
// This is needed for supporting multiple document IDs
|
|
53
56
|
static strict = false;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/commands/documents/delete.ts"],"sourcesContent":["import {Args} from '@oclif/core'\nimport {getProjectCliClient, SanityCommand, subdebug} from '@sanity/cli-core'\n\nimport {DOCUMENTS_API_VERSION} from '../../actions/documents/constants.js'\nimport {promptForProject} from '../../prompts/promptForProject.js'\nimport {getDatasetFlag, getProjectIdFlag} from '../../util/sharedFlags.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 command: '<%= config.bin %> <%= command.id %> myDocId --project-id abc123',\n description: 'Delete a document from a specific project',\n },\n ]\n\n static override flags = {\n ...getProjectIdFlag({description: 'Project ID to delete from', semantics: 'override'}),\n ...getDatasetFlag({description: 'Dataset to delete from', semantics: 'override'}),\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 (may not exist when running outside a project directory)\n const cliConfig = await this.tryGetCliConfig()\n\n const projectId = await this.getProjectId({fallback: () => promptForProject({})})\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","getProjectCliClient","SanityCommand","subdebug","DOCUMENTS_API_VERSION","promptForProject","getDatasetFlag","getProjectIdFlag","deleteDocumentDebug","DeleteDocumentCommand","args","id","string","description","required","ids","examples","command","flags","semantics","strict","run","argv","parse","dataset","slice","filter","Boolean","length","error","exit","cliConfig","tryGetCliConfig","projectId","getProjectId","fallback","api","targetDataset","projectClient","apiVersion","requireUser","transaction","delete","results","commit","deleted","res","operation","map","notFound","includes","log","join","err","message"],"mappings":"AAAA,SAAQA,IAAI,QAAO,cAAa;AAChC,SAAQC,mBAAmB,EAAEC,aAAa,EAAEC,QAAQ,QAAO,mBAAkB;AAE7E,SAAQC,qBAAqB,QAAO,uCAAsC;AAC1E,SAAQC,gBAAgB,QAAO,oCAAmC;AAClE,SAAQC,cAAc,EAAEC,gBAAgB,QAAO,4BAA2B;AAE1E,MAAMC,sBAAsBL,SAAS;AAErC,OAAO,MAAMM,8BAA8BP;IACzC,OAAgBQ,OAAO;QACrBC,IAAIX,KAAKY,MAAM,CAAC;YACdC,aAAa;YACbC,UAAU;QACZ;QACAC,KAAKf,KAAKY,MAAM,CAAC;YACfC,aAAa;YACbC,UAAU;QACZ;IACF,EAAC;IAED,OAAgBD,cAAc,oEAAmE;IAEjG,OAAgBG,WAAW;QACzB;YACEC,SAAS;YACTJ,aAAa;QACf;QACA;YACEI,SAAS;YACTJ,aAAa;QACf;QACA;YACEI,SAAS;YACTJ,aAAa;QACf;QACA;YACEI,SAAS;YACTJ,aAAa;QACf;QACA;YACEI,SAAS;YACTJ,aAAa;QACf;KACD,CAAA;IAED,OAAgBK,QAAQ;QACtB,GAAGX,iBAAiB;YAACM,aAAa;YAA6BM,WAAW;QAAU,EAAE;QACtF,GAAGb,eAAe;YAACO,aAAa;YAA0BM,WAAW;QAAU,EAAE;IACnF,EAAC;IAED,uDAAuD;IACvD,sDAAsD;IACtD,OAAgBC,SAAS,MAAK;IAE9B,MAAaC,MAAqB;QAChC,MAAM,
|
|
1
|
+
{"version":3,"sources":["../../../src/commands/documents/delete.ts"],"sourcesContent":["import {Args} from '@oclif/core'\nimport {getProjectCliClient, SanityCommand, subdebug} from '@sanity/cli-core'\n\nimport {DOCUMENTS_API_VERSION} from '../../actions/documents/constants.js'\nimport {promptForProject} from '../../prompts/promptForProject.js'\nimport {getDatasetFlag, getProjectIdFlag} from '../../util/sharedFlags.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 command: '<%= config.bin %> <%= command.id %> myDocId --project-id abc123',\n description: 'Delete a document from a specific project',\n },\n ]\n\n static override flags = {\n ...getProjectIdFlag({description: 'Project ID to delete from', semantics: 'override'}),\n ...getDatasetFlag({description: 'Dataset to delete from', semantics: 'override'}),\n }\n\n static override hiddenAliases: string[] = ['document:delete']\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 (may not exist when running outside a project directory)\n const cliConfig = await this.tryGetCliConfig()\n\n const projectId = await this.getProjectId({fallback: () => promptForProject({})})\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","getProjectCliClient","SanityCommand","subdebug","DOCUMENTS_API_VERSION","promptForProject","getDatasetFlag","getProjectIdFlag","deleteDocumentDebug","DeleteDocumentCommand","args","id","string","description","required","ids","examples","command","flags","semantics","hiddenAliases","strict","run","argv","parse","dataset","slice","filter","Boolean","length","error","exit","cliConfig","tryGetCliConfig","projectId","getProjectId","fallback","api","targetDataset","projectClient","apiVersion","requireUser","transaction","delete","results","commit","deleted","res","operation","map","notFound","includes","log","join","err","message"],"mappings":"AAAA,SAAQA,IAAI,QAAO,cAAa;AAChC,SAAQC,mBAAmB,EAAEC,aAAa,EAAEC,QAAQ,QAAO,mBAAkB;AAE7E,SAAQC,qBAAqB,QAAO,uCAAsC;AAC1E,SAAQC,gBAAgB,QAAO,oCAAmC;AAClE,SAAQC,cAAc,EAAEC,gBAAgB,QAAO,4BAA2B;AAE1E,MAAMC,sBAAsBL,SAAS;AAErC,OAAO,MAAMM,8BAA8BP;IACzC,OAAgBQ,OAAO;QACrBC,IAAIX,KAAKY,MAAM,CAAC;YACdC,aAAa;YACbC,UAAU;QACZ;QACAC,KAAKf,KAAKY,MAAM,CAAC;YACfC,aAAa;YACbC,UAAU;QACZ;IACF,EAAC;IAED,OAAgBD,cAAc,oEAAmE;IAEjG,OAAgBG,WAAW;QACzB;YACEC,SAAS;YACTJ,aAAa;QACf;QACA;YACEI,SAAS;YACTJ,aAAa;QACf;QACA;YACEI,SAAS;YACTJ,aAAa;QACf;QACA;YACEI,SAAS;YACTJ,aAAa;QACf;QACA;YACEI,SAAS;YACTJ,aAAa;QACf;KACD,CAAA;IAED,OAAgBK,QAAQ;QACtB,GAAGX,iBAAiB;YAACM,aAAa;YAA6BM,WAAW;QAAU,EAAE;QACtF,GAAGb,eAAe;YAACO,aAAa;YAA0BM,WAAW;QAAU,EAAE;IACnF,EAAC;IAED,OAAgBC,gBAA0B;QAAC;KAAkB,CAAA;IAE7D,uDAAuD;IACvD,sDAAsD;IACtD,OAAgBC,SAAS,MAAK;IAE9B,MAAaC,MAAqB;QAChC,MAAM,EAACZ,IAAI,EAAEa,IAAI,EAAEL,KAAK,EAAC,GAAG,MAAM,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B,MAAM,KAAK,IAAI,aAAa,aAAa;YACzF;YAEA,IAAIqB,SAASrB,MAAM,GAAG,GAAG;gBACvB,IAAI,CAACC,KAAK,CACR,GAAGoB,SAASrB,MAAM,KAAK,IAAI,aAAa,YAAY,YAAY,EAAEqB,SAASG,IAAI,CAAC,OAAO,EACvF;oBACEtB,MAAM;gBACR;YAEJ;QACF,EAAE,OAAOD,OAAO;YACd,MAAMwB,MAAMxB;YACZtB,oBAAoB,CAAC,yBAAyB,EAAEO,IAAIsC,IAAI,CAAC,OAAO,EAAEC;YAClE,IAAI,CAACxB,KAAK,CACR,CAAC,iBAAiB,EAAEf,IAAIc,MAAM,CAAC,CAAC,EAAEd,IAAIc,MAAM,KAAK,IAAI,aAAa,YAAY,EAAE,EAAEyB,IAAIC,OAAO,EAAE,EAC/F;gBACExB,MAAM;YACR;QAEJ;IACF;AACF"}
|
|
@@ -44,6 +44,9 @@ export class GetDocumentCommand extends SanityCommand {
|
|
|
44
44
|
description: 'Colorize JSON output'
|
|
45
45
|
})
|
|
46
46
|
};
|
|
47
|
+
static hiddenAliases = [
|
|
48
|
+
'document:get'
|
|
49
|
+
];
|
|
47
50
|
async run() {
|
|
48
51
|
const { args, flags } = await this.parse(GetDocumentCommand);
|
|
49
52
|
const { documentId } = args;
|
|
@@ -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'\nimport {promptForProject} from '../../prompts/promptForProject.js'\nimport {getDatasetFlag, getProjectIdFlag} from '../../util/sharedFlags.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 command: '<%= config.bin %> <%= command.id %> myDocId --project-id abc123',\n description: 'Get a document from a specific project',\n },\n ]\n\n static override flags = {\n ...getProjectIdFlag({\n description: 'Project ID to get document from',\n semantics: 'override',\n }),\n ...getDatasetFlag({description: 'Dataset to get document from', semantics: 'override'}),\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 const cliConfig = await this.tryGetCliConfig()\n\n const projectId = await this.getProjectId({fallback: () => promptForProject({})})\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","promptForProject","getDatasetFlag","getProjectIdFlag","getDocumentDebug","GetDocumentCommand","args","documentId","string","description","required","examples","command","flags","semantics","pretty","boolean","default","run","parse","dataset","cliConfig","tryGetCliConfig","projectId","getProjectId","fallback","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;AAC1E,SAAQC,gBAAgB,QAAO,oCAAmC;AAClE,SAAQC,cAAc,EAAEC,gBAAgB,QAAO,4BAA2B;AAE1E,MAAMC,mBAAmBL,SAAS;AAElC,OAAO,MAAMM,2BAA2BP;IACtC,OAAgBQ,OAAO;QACrBC,YAAYb,KAAKc,MAAM,CAAC;YACtBC,aAAa;YACbC,UAAU;QACZ;IACF,EAAC;IAED,OAAgBD,cAAc,iCAAgC;IAE9D,OAAgBE,WAAW;QACzB;YACEC,SAAS;YACTH,aAAa;QACf;QACA;YACEG,SAAS;YACTH,aAAa;QACf;QACA;YACEG,SAAS;YACTH,aAAa;QACf;QACA;YACEG,SAAS;YACTH,aAAa;QACf;KACD,CAAA;IAED,OAAgBI,QAAQ;QACtB,GAAGV,iBAAiB;YAClBM,aAAa;YACbK,WAAW;QACb,EAAE;QACF,GAAGZ,eAAe;YAACO,aAAa;YAAgCK,WAAW;QAAU,EAAE;QACvFC,QAAQpB,MAAMqB,OAAO,CAAC;YACpBC,SAAS;YACTR,aAAa;QACf;IACF,EAAC;IAED,
|
|
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'\nimport {promptForProject} from '../../prompts/promptForProject.js'\nimport {getDatasetFlag, getProjectIdFlag} from '../../util/sharedFlags.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 command: '<%= config.bin %> <%= command.id %> myDocId --project-id abc123',\n description: 'Get a document from a specific project',\n },\n ]\n\n static override flags = {\n ...getProjectIdFlag({\n description: 'Project ID to get document from',\n semantics: 'override',\n }),\n ...getDatasetFlag({description: 'Dataset to get document from', semantics: 'override'}),\n pretty: Flags.boolean({\n default: false,\n description: 'Colorize JSON output',\n }),\n }\n\n static override hiddenAliases: string[] = ['document:get']\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 const cliConfig = await this.tryGetCliConfig()\n\n const projectId = await this.getProjectId({fallback: () => promptForProject({})})\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","promptForProject","getDatasetFlag","getProjectIdFlag","getDocumentDebug","GetDocumentCommand","args","documentId","string","description","required","examples","command","flags","semantics","pretty","boolean","default","hiddenAliases","run","parse","dataset","cliConfig","tryGetCliConfig","projectId","getProjectId","fallback","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;AAC1E,SAAQC,gBAAgB,QAAO,oCAAmC;AAClE,SAAQC,cAAc,EAAEC,gBAAgB,QAAO,4BAA2B;AAE1E,MAAMC,mBAAmBL,SAAS;AAElC,OAAO,MAAMM,2BAA2BP;IACtC,OAAgBQ,OAAO;QACrBC,YAAYb,KAAKc,MAAM,CAAC;YACtBC,aAAa;YACbC,UAAU;QACZ;IACF,EAAC;IAED,OAAgBD,cAAc,iCAAgC;IAE9D,OAAgBE,WAAW;QACzB;YACEC,SAAS;YACTH,aAAa;QACf;QACA;YACEG,SAAS;YACTH,aAAa;QACf;QACA;YACEG,SAAS;YACTH,aAAa;QACf;QACA;YACEG,SAAS;YACTH,aAAa;QACf;KACD,CAAA;IAED,OAAgBI,QAAQ;QACtB,GAAGV,iBAAiB;YAClBM,aAAa;YACbK,WAAW;QACb,EAAE;QACF,GAAGZ,eAAe;YAACO,aAAa;YAAgCK,WAAW;QAAU,EAAE;QACvFC,QAAQpB,MAAMqB,OAAO,CAAC;YACpBC,SAAS;YACTR,aAAa;QACf;IACF,EAAC;IAED,OAAgBS,gBAA0B;QAAC;KAAe,CAAA;IAE1D,MAAaC,MAAqB;QAChC,MAAM,EAACb,IAAI,EAAEO,KAAK,EAAC,GAAG,MAAM,IAAI,CAACO,KAAK,CAACf;QACvC,MAAM,EAACE,UAAU,EAAC,GAAGD;QACrB,MAAM,EAACe,OAAO,EAAEN,MAAM,EAAC,GAAGF;QAE1B,MAAMS,YAAY,MAAM,IAAI,CAACC,eAAe;QAE5C,MAAMC,YAAY,MAAM,IAAI,CAACC,YAAY,CAAC;YAACC,UAAU,IAAMzB,iBAAiB,CAAC;QAAE;QAE/E,IAAI,CAACqB,UAAUK,GAAG,EAAEN,WAAW,CAACA,SAAS;YACvC,IAAI,CAACO,KAAK,CACR,+FACA;gBAACC,MAAM;YAAC;QAEZ;QAEA,MAAMC,gBAAgBT,WAAWC,UAAUK,GAAG,EAAEN;QAEhD,IAAI;YACF,MAAMU,gBAAgB,MAAMlC,oBAAoB;gBAC9CmC,YAAYhC;gBACZqB,SAASS;gBACTN;gBACAS,aAAa;YACf;YAEA,MAAMC,MAAM,MAAMH,cAAcI,WAAW,CAAC5B;YAE5C,IAAI,CAAC2B,KAAK;gBACR,IAAI,CAACN,KAAK,CAAC,CAAC,UAAU,EAAErB,WAAW,wBAAwB,EAAEuB,cAAc,CAAC,CAAC,EAAE;oBAACD,MAAM;gBAAC;YACzF;YAEA,sBAAsB;YACtB,IAAId,QAAQ;gBACV,IAAI,CAACqB,GAAG,CAACxC,aAAasC;YACxB,OAAO;gBACL,IAAI,CAACE,GAAG,CAACC,KAAKC,SAAS,CAACJ,KAAK,MAAM;YACrC;QACF,EAAE,OAAON,OAAO;YACd,MAAMW,MAAMX;YAEZxB,iBAAiB,CAAC,wBAAwB,EAAEG,YAAY,EAAEgC;YAC1D,IAAI,CAACX,KAAK,CAAC,CAAC,0BAA0B,EAAEW,IAAIC,OAAO,EAAE,EAAE;gBAACX,MAAM;YAAC;QACjE;IACF;AACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/commands/documents/query.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'\nimport {promptForProject} from '../../prompts/promptForProject.js'\nimport {getDatasetFlag, getProjectIdFlag} from '../../util/sharedFlags.js'\n\nconst queryDocumentDebug = subdebug('documents:query')\n\nexport class QueryDocumentCommand extends SanityCommand<typeof QueryDocumentCommand> {\n static override args = {\n query: Args.string({\n description: 'GROQ query to run against the dataset',\n required: true,\n }),\n }\n\n static override description = 'Query for documents'\n\n static override examples = [\n {\n command: '<%= config.bin %> <%= command.id %> \\'*[_type == \"movie\"][0..4]\\'',\n description: 'Fetch 5 documents of type \"movie\"',\n },\n {\n command:\n '<%= config.bin %> <%= command.id %> \\'*[_type == \"movie\"]|order(releaseDate asc)[0]{title}\\' --dataset staging',\n description: 'Fetch title of the oldest movie in the dataset named \"staging\"',\n },\n {\n command:\n '<%= config.bin %> <%= command.id %> \\'*[_id == \"header\"] { \"headerText\": pt::text(body) }\\' --api-version v2021-06-07',\n description: 'Use API version v2021-06-07 and do a query',\n },\n {\n command:\n '<%= config.bin %> <%= command.id %> \\'*[_type == \"post\"]\\' --project-id abc123 --dataset production',\n description: 'Query documents in a specific project and dataset',\n },\n ]\n\n static override flags = {\n ...getProjectIdFlag({description: 'Project ID to query', semantics: 'override'}),\n ...getDatasetFlag({description: 'Dataset to query', semantics: 'override'}),\n anonymous: Flags.boolean({\n default: false,\n description: 'Send the query without any authorization token',\n }),\n 'api-version': Flags.string({\n description: `API version to use (defaults to ${DOCUMENTS_API_VERSION})`,\n env: 'SANITY_CLI_QUERY_API_VERSION',\n }),\n pretty: Flags.boolean({\n default: false,\n description: 'Colorize JSON output',\n }),\n project: Flags.string({\n deprecated: {to: 'project-id'},\n description: 'Project ID to query',\n hidden: true,\n }),\n }\n\n public async run(): Promise<void> {\n const {args, flags} = await this.parse(QueryDocumentCommand)\n const {query} = args\n const {anonymous, 'api-version': apiVersion, dataset, pretty} = flags\n\n const cliConfig = await this.tryGetCliConfig()\n\n const projectId = await this.getProjectId({\n deprecatedFlagName: 'project',\n fallback: () => promptForProject({}),\n })\n\n const requireUser = !anonymous\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 const targetApiVersion = apiVersion || DOCUMENTS_API_VERSION\n\n if (!apiVersion) {\n this.warn(`--api-version not specified, using \\`${DOCUMENTS_API_VERSION}\\``)\n }\n\n try {\n const projectClient = await getProjectCliClient({\n apiVersion: targetApiVersion,\n dataset: targetDataset,\n projectId,\n requireUser,\n })\n\n const docs = await projectClient.fetch(query)\n\n if (!docs) {\n this.error('Query returned no results', {exit: 1})\n }\n\n // Output the query results\n if (pretty) {\n this.log(colorizeJson(docs))\n } else {\n this.log(JSON.stringify(docs, null, 2))\n }\n } catch (error) {\n const err = error as Error\n\n queryDocumentDebug(`Error running query: ${query}`, err)\n\n // Provide more context in error messages\n const errorMsg = err.message.toLowerCase().includes('syntax')\n ? `Invalid GROQ query syntax: ${err.message}`\n : `Failed to run query: ${err.message}`\n\n this.error(`${errorMsg}\\nQuery: ${query}`, {exit: 1})\n }\n }\n}\n"],"names":["Args","Flags","colorizeJson","getProjectCliClient","SanityCommand","subdebug","DOCUMENTS_API_VERSION","promptForProject","getDatasetFlag","getProjectIdFlag","queryDocumentDebug","QueryDocumentCommand","args","query","string","description","required","examples","command","flags","semantics","anonymous","boolean","default","env","pretty","project","deprecated","to","hidden","run","parse","apiVersion","dataset","cliConfig","tryGetCliConfig","projectId","getProjectId","deprecatedFlagName","fallback","requireUser","api","error","exit","targetDataset","targetApiVersion","warn","projectClient","docs","fetch","log","JSON","stringify","err","errorMsg","message","toLowerCase","includes"],"mappings":"AAAA,SAAQA,IAAI,EAAEC,KAAK,QAAO,cAAa;AACvC,SAAQC,YAAY,EAAEC,mBAAmB,EAAEC,aAAa,EAAEC,QAAQ,QAAO,mBAAkB;AAE3F,SAAQC,qBAAqB,QAAO,uCAAsC;AAC1E,SAAQC,gBAAgB,QAAO,oCAAmC;AAClE,SAAQC,cAAc,EAAEC,gBAAgB,QAAO,4BAA2B;AAE1E,MAAMC,qBAAqBL,SAAS;AAEpC,OAAO,MAAMM,6BAA6BP;IACxC,OAAgBQ,OAAO;QACrBC,OAAOb,KAAKc,MAAM,CAAC;YACjBC,aAAa;YACbC,UAAU;QACZ;IACF,EAAC;IAED,OAAgBD,cAAc,sBAAqB;IAEnD,OAAgBE,WAAW;QACzB;YACEC,SAAS;YACTH,aAAa;QACf;QACA;YACEG,SACE;YACFH,aAAa;QACf;QACA;YACEG,SACE;YACFH,aAAa;QACf;QACA;YACEG,SACE;YACFH,aAAa;QACf;KACD,CAAA;IAED,OAAgBI,QAAQ;QACtB,GAAGV,iBAAiB;YAACM,aAAa;YAAuBK,WAAW;QAAU,EAAE;QAChF,GAAGZ,eAAe;YAACO,aAAa;YAAoBK,WAAW;QAAU,EAAE;QAC3EC,WAAWpB,MAAMqB,OAAO,CAAC;YACvBC,SAAS;YACTR,aAAa;QACf;QACA,eAAed,MAAMa,MAAM,CAAC;YAC1BC,aAAa,CAAC,gCAAgC,EAAET,sBAAsB,CAAC,CAAC;YACxEkB,KAAK;QACP;QACAC,QAAQxB,MAAMqB,OAAO,CAAC;YACpBC,SAAS;YACTR,aAAa;QACf;QACAW,SAASzB,MAAMa,MAAM,CAAC;YACpBa,YAAY;gBAACC,IAAI;YAAY;YAC7Bb,aAAa;YACbc,QAAQ;QACV;IACF,EAAC;IAED,MAAaC,MAAqB;QAChC,MAAM,
|
|
1
|
+
{"version":3,"sources":["../../../src/commands/documents/query.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'\nimport {promptForProject} from '../../prompts/promptForProject.js'\nimport {getDatasetFlag, getProjectIdFlag} from '../../util/sharedFlags.js'\n\nconst queryDocumentDebug = subdebug('documents:query')\n\nexport class QueryDocumentCommand extends SanityCommand<typeof QueryDocumentCommand> {\n static override args = {\n query: Args.string({\n description: 'GROQ query to run against the dataset',\n required: true,\n }),\n }\n\n static override description = 'Query for documents'\n\n static override examples = [\n {\n command: '<%= config.bin %> <%= command.id %> \\'*[_type == \"movie\"][0..4]\\'',\n description: 'Fetch 5 documents of type \"movie\"',\n },\n {\n command:\n '<%= config.bin %> <%= command.id %> \\'*[_type == \"movie\"]|order(releaseDate asc)[0]{title}\\' --dataset staging',\n description: 'Fetch title of the oldest movie in the dataset named \"staging\"',\n },\n {\n command:\n '<%= config.bin %> <%= command.id %> \\'*[_id == \"header\"] { \"headerText\": pt::text(body) }\\' --api-version v2021-06-07',\n description: 'Use API version v2021-06-07 and do a query',\n },\n {\n command:\n '<%= config.bin %> <%= command.id %> \\'*[_type == \"post\"]\\' --project-id abc123 --dataset production',\n description: 'Query documents in a specific project and dataset',\n },\n ]\n\n static override flags = {\n ...getProjectIdFlag({description: 'Project ID to query', semantics: 'override'}),\n ...getDatasetFlag({description: 'Dataset to query', semantics: 'override'}),\n anonymous: Flags.boolean({\n default: false,\n description: 'Send the query without any authorization token',\n }),\n 'api-version': Flags.string({\n description: `API version to use (defaults to ${DOCUMENTS_API_VERSION})`,\n env: 'SANITY_CLI_QUERY_API_VERSION',\n }),\n pretty: Flags.boolean({\n default: false,\n description: 'Colorize JSON output',\n }),\n project: Flags.string({\n deprecated: {to: 'project-id'},\n description: 'Project ID to query',\n hidden: true,\n }),\n }\n\n static override hiddenAliases: string[] = ['document:query']\n\n public async run(): Promise<void> {\n const {args, flags} = await this.parse(QueryDocumentCommand)\n const {query} = args\n const {anonymous, 'api-version': apiVersion, dataset, pretty} = flags\n\n const cliConfig = await this.tryGetCliConfig()\n\n const projectId = await this.getProjectId({\n deprecatedFlagName: 'project',\n fallback: () => promptForProject({}),\n })\n\n const requireUser = !anonymous\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 const targetApiVersion = apiVersion || DOCUMENTS_API_VERSION\n\n if (!apiVersion) {\n this.warn(`--api-version not specified, using \\`${DOCUMENTS_API_VERSION}\\``)\n }\n\n try {\n const projectClient = await getProjectCliClient({\n apiVersion: targetApiVersion,\n dataset: targetDataset,\n projectId,\n requireUser,\n })\n\n const docs = await projectClient.fetch(query)\n\n if (!docs) {\n this.error('Query returned no results', {exit: 1})\n }\n\n // Output the query results\n if (pretty) {\n this.log(colorizeJson(docs))\n } else {\n this.log(JSON.stringify(docs, null, 2))\n }\n } catch (error) {\n const err = error as Error\n\n queryDocumentDebug(`Error running query: ${query}`, err)\n\n // Provide more context in error messages\n const errorMsg = err.message.toLowerCase().includes('syntax')\n ? `Invalid GROQ query syntax: ${err.message}`\n : `Failed to run query: ${err.message}`\n\n this.error(`${errorMsg}\\nQuery: ${query}`, {exit: 1})\n }\n }\n}\n"],"names":["Args","Flags","colorizeJson","getProjectCliClient","SanityCommand","subdebug","DOCUMENTS_API_VERSION","promptForProject","getDatasetFlag","getProjectIdFlag","queryDocumentDebug","QueryDocumentCommand","args","query","string","description","required","examples","command","flags","semantics","anonymous","boolean","default","env","pretty","project","deprecated","to","hidden","hiddenAliases","run","parse","apiVersion","dataset","cliConfig","tryGetCliConfig","projectId","getProjectId","deprecatedFlagName","fallback","requireUser","api","error","exit","targetDataset","targetApiVersion","warn","projectClient","docs","fetch","log","JSON","stringify","err","errorMsg","message","toLowerCase","includes"],"mappings":"AAAA,SAAQA,IAAI,EAAEC,KAAK,QAAO,cAAa;AACvC,SAAQC,YAAY,EAAEC,mBAAmB,EAAEC,aAAa,EAAEC,QAAQ,QAAO,mBAAkB;AAE3F,SAAQC,qBAAqB,QAAO,uCAAsC;AAC1E,SAAQC,gBAAgB,QAAO,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sCommand extends SanityCommand {
|
|
|
72
72
|
description: 'Skips the first confirmation prompt'
|
|
73
73
|
})
|
|
74
74
|
};
|
|
75
|
+
static hiddenAliases = [
|
|
76
|
+
'document:validate'
|
|
77
|
+
];
|
|
75
78
|
async run() {
|
|
76
79
|
const { flags } = await this.parse(ValidateDocumentsCommand);
|
|
77
80
|
const { dataset, file, format, level, 'max-custom-validation-concurrency': maxCustomValidationConcurrency, 'max-fetch-concurrency': maxFetchConcurrency, 'project-id': projectId, workspace } = flags;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/commands/documents/validate.ts"],"sourcesContent":["import fs from 'node:fs'\nimport path from 'node:path'\nimport {styleText} from 'node:util'\n\nimport {Flags} from '@oclif/core'\nimport {type CliConfig, ProjectRootNotFoundError, SanityCommand} from '@sanity/cli-core'\nimport {confirm, logSymbols} from '@sanity/cli-core/ux'\n\nimport {type Level} from '../../actions/documents/types.js'\nimport {validateDocuments} from '../../actions/documents/validate.js'\nimport {reporters} from '../../actions/documents/validation/reporters/index.js'\nimport {getDatasetFlag, getProjectIdFlag} from '../../util/sharedFlags.js'\n\nexport class ValidateDocumentsCommand extends SanityCommand<typeof ValidateDocumentsCommand> {\n static description = 'Validate documents in a dataset against the studio schema'\n\n static examples = [\n {\n command: '<%= config.bin %> <%= command.id %> --workspace default',\n description: 'Validates all documents in a Sanity project with more than one workspace',\n },\n {\n command: '<%= config.bin %> <%= command.id %> --workspace default --dataset staging',\n description: 'Override the dataset specified in the workspace',\n },\n {\n command: '<%= config.bin %> <%= command.id %> --yes > report.txt',\n description: 'Save the results of the report into a file',\n },\n {\n command: '<%= config.bin %> <%= command.id %> --level info',\n description: 'Report out info level validation markers too',\n },\n {\n command: '<%= config.bin %> <%= command.id %> --project-id abc123 --dataset production',\n description: 'Validate documents in a specific project and dataset',\n },\n ]\n\n static flags = {\n ...getProjectIdFlag({\n description:\n 'Override the project ID used. By default, this is derived from the given workspace',\n semantics: 'specify',\n }),\n ...getDatasetFlag({\n description:\n 'Override the dataset used. By default, this is derived from the given workspace',\n semantics: 'specify',\n }),\n file: Flags.string({\n description:\n 'Provide a path to either an .ndjson file or a tarball containing an .ndjson file',\n }),\n format: Flags.string({\n description:\n 'The output format used to print the found validation markers and report progress',\n }),\n level: Flags.custom<Level>({\n default: 'warning',\n description: 'The minimum level reported. Defaults to warning',\n options: ['error', 'warning', 'info'],\n })(),\n 'max-custom-validation-concurrency': Flags.integer({\n default: 5,\n description: 'Specify how many custom validators can run concurrently',\n }),\n 'max-fetch-concurrency': Flags.integer({\n default: 25,\n description: 'Specify how many `client.fetch` requests are allowed to run concurrently',\n }),\n workspace: Flags.string({\n description: 'The name of the workspace to use when downloading and validating all documents',\n }),\n yes: Flags.boolean({\n char: 'y',\n default: false,\n description: 'Skips the first confirmation prompt',\n }),\n }\n\n public async run(): Promise<void> {\n const {flags} = await this.parse(ValidateDocumentsCommand)\n const {\n dataset,\n file,\n format,\n level,\n 'max-custom-validation-concurrency': maxCustomValidationConcurrency,\n 'max-fetch-concurrency': maxFetchConcurrency,\n 'project-id': projectId,\n workspace,\n } = flags\n const unattendedMode = Boolean(flags.yes)\n\n let workDir: string\n let cliConfig: CliConfig\n try {\n const root = await this.getProjectRoot()\n workDir = root.directory\n cliConfig = await this.getCliConfig()\n } catch (err) {\n if (err instanceof ProjectRootNotFoundError) {\n this.error(\n 'This command must be run from within a Sanity project directory (requires studio schema for validation)',\n {exit: 1},\n )\n }\n throw err\n }\n\n if (!unattendedMode) {\n this.log(\n `${styleText('yellow', `${logSymbols.warning} Warning:`)} This command ${\n file\n ? 'reads all documents from your input file'\n : 'downloads all documents from your dataset'\n } and processes them through your local schema within a ` +\n `simulated browser environment.\\n`,\n )\n this.log(`Potential pitfalls:\\n`)\n this.log(\n `- Processes all documents locally (excluding assets). Large datasets may require more resources.`,\n )\n this.log(\n `- Executes all custom validation functions. Some functions may need to be refactored for compatibility.`,\n )\n this.log(\n `- Not all standard browser features are available and may cause issues while loading your Studio.`,\n )\n this.log(\n `- Adheres to document permissions. Ensure this account can see all desired documents.`,\n )\n if (file) {\n this.log(\n `- Checks for missing document references against the live dataset if not found in your file.`,\n )\n }\n\n const confirmed = await confirm({\n default: true,\n message: `Are you sure you want to continue?`,\n })\n\n if (!confirmed) {\n this.error('User aborted', {exit: 1})\n }\n }\n\n if (format && !(format in reporters)) {\n const formatter = new Intl.ListFormat('en-US', {\n style: 'long',\n type: 'conjunction',\n })\n this.error(\n `Did not recognize format '${format}'. Available formats are ${formatter.format(\n Object.keys(reporters).map((key) => `'${key}'`),\n )}`,\n {exit: 1},\n )\n }\n\n let ndjsonFilePath\n if (file) {\n const filePath = path.resolve(workDir, file)\n\n const stat = await fs.promises.stat(filePath)\n if (!stat.isFile()) {\n this.error(`'--file' must point to a valid ndjson file or tarball`, {exit: 1})\n }\n\n ndjsonFilePath = filePath\n }\n\n try {\n const overallLevel = await validateDocuments({\n dataset,\n level,\n maxCustomValidationConcurrency,\n maxFetchConcurrency,\n ndjsonFilePath,\n projectId,\n reporter: (worker) => {\n const reporter =\n format && format in reporters\n ? reporters[format as keyof typeof reporters]\n : reporters.pretty\n\n return reporter({flags, output: this.output, worker})\n },\n studioHost: cliConfig.studioHost,\n workDir,\n workspace,\n })\n\n if (overallLevel === 'error') {\n this.exit(1)\n }\n } catch (err) {\n this.error(err instanceof Error ? err.message : String(err), {exit: 1})\n }\n }\n}\n"],"names":["fs","path","styleText","Flags","ProjectRootNotFoundError","SanityCommand","confirm","logSymbols","validateDocuments","reporters","getDatasetFlag","getProjectIdFlag","ValidateDocumentsCommand","description","examples","command","flags","semantics","file","string","format","level","custom","default","options","integer","workspace","yes","boolean","char","run","parse","dataset","maxCustomValidationConcurrency","maxFetchConcurrency","projectId","unattendedMode","Boolean","workDir","cliConfig","root","getProjectRoot","directory","getCliConfig","err","error","exit","log","warning","confirmed","message","formatter","Intl","ListFormat","style","type","Object","keys","map","key","ndjsonFilePath","filePath","resolve","stat","promises","isFile","overallLevel","reporter","worker","pretty","output","studioHost","Error","String"],"mappings":"AAAA,OAAOA,QAAQ,UAAS;AACxB,OAAOC,UAAU,YAAW;AAC5B,SAAQC,SAAS,QAAO,YAAW;AAEnC,SAAQC,KAAK,QAAO,cAAa;AACjC,SAAwBC,wBAAwB,EAAEC,aAAa,QAAO,mBAAkB;AACxF,SAAQC,OAAO,EAAEC,UAAU,QAAO,sBAAqB;AAGvD,SAAQC,iBAAiB,QAAO,sCAAqC;AACrE,SAAQC,SAAS,QAAO,wDAAuD;AAC/E,SAAQC,cAAc,EAAEC,gBAAgB,QAAO,4BAA2B;AAE1E,OAAO,MAAMC,iCAAiCP;IAC5C,OAAOQ,cAAc,4DAA2D;IAEhF,OAAOC,WAAW;QAChB;YACEC,SAAS;YACTF,aAAa;QACf;QACA;YACEE,SAAS;YACTF,aAAa;QACf;QACA;YACEE,SAAS;YACTF,aAAa;QACf;QACA;YACEE,SAAS;YACTF,aAAa;QACf;QACA;YACEE,SAAS;YACTF,aAAa;QACf;KACD,CAAA;IAED,OAAOG,QAAQ;QACb,GAAGL,iBAAiB;YAClBE,aACE;YACFI,WAAW;QACb,EAAE;QACF,GAAGP,eAAe;YAChBG,aACE;YACFI,WAAW;QACb,EAAE;QACFC,MAAMf,MAAMgB,MAAM,CAAC;YACjBN,aACE;QACJ;QACAO,QAAQjB,MAAMgB,MAAM,CAAC;YACnBN,aACE;QACJ;QACAQ,OAAOlB,MAAMmB,MAAM,CAAQ;YACzBC,SAAS;YACTV,aAAa;YACbW,SAAS;gBAAC;gBAAS;gBAAW;aAAO;QACvC;QACA,qCAAqCrB,MAAMsB,OAAO,CAAC;YACjDF,SAAS;YACTV,aAAa;QACf;QACA,yBAAyBV,MAAMsB,OAAO,CAAC;YACrCF,SAAS;YACTV,aAAa;QACf;QACAa,WAAWvB,MAAMgB,MAAM,CAAC;YACtBN,aAAa;QACf;QACAc,KAAKxB,MAAMyB,OAAO,CAAC;YACjBC,MAAM;YACNN,SAAS;YACTV,aAAa;QACf;I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OAAO7B,MAAM;gBAACE,MAAM;YAAC;QACvE;IACF;AACF"}
|
|
1
|
+
{"version":3,"sources":["../../../src/commands/documents/validate.ts"],"sourcesContent":["import fs from 'node:fs'\nimport path from 'node:path'\nimport {styleText} from 'node:util'\n\nimport {Flags} from '@oclif/core'\nimport {type CliConfig, ProjectRootNotFoundError, SanityCommand} from '@sanity/cli-core'\nimport {confirm, logSymbols} from '@sanity/cli-core/ux'\n\nimport {type Level} from '../../actions/documents/types.js'\nimport {validateDocuments} from '../../actions/documents/validate.js'\nimport {reporters} from '../../actions/documents/validation/reporters/index.js'\nimport {getDatasetFlag, getProjectIdFlag} from '../../util/sharedFlags.js'\n\nexport class ValidateDocumentsCommand extends SanityCommand<typeof ValidateDocumentsCommand> {\n static description = 'Validate documents in a dataset against the studio schema'\n\n static examples = [\n {\n command: '<%= config.bin %> <%= command.id %> --workspace default',\n description: 'Validates all documents in a Sanity project with more than one workspace',\n },\n {\n command: '<%= config.bin %> <%= command.id %> --workspace default --dataset staging',\n description: 'Override the dataset specified in the workspace',\n },\n {\n command: '<%= config.bin %> <%= command.id %> --yes > report.txt',\n description: 'Save the results of the report into a file',\n },\n {\n command: '<%= config.bin %> <%= command.id %> --level info',\n description: 'Report out info level validation markers too',\n },\n {\n command: '<%= config.bin %> <%= command.id %> --project-id abc123 --dataset production',\n description: 'Validate documents in a specific project and dataset',\n },\n ]\n\n static flags = {\n ...getProjectIdFlag({\n description:\n 'Override the project ID used. By default, this is derived from the given workspace',\n semantics: 'specify',\n }),\n ...getDatasetFlag({\n description:\n 'Override the dataset used. By default, this is derived from the given workspace',\n semantics: 'specify',\n }),\n file: Flags.string({\n description:\n 'Provide a path to either an .ndjson file or a tarball containing an .ndjson file',\n }),\n format: Flags.string({\n description:\n 'The output format used to print the found validation markers and report progress',\n }),\n level: Flags.custom<Level>({\n default: 'warning',\n description: 'The minimum level reported. Defaults to warning',\n options: ['error', 'warning', 'info'],\n })(),\n 'max-custom-validation-concurrency': Flags.integer({\n default: 5,\n description: 'Specify how many custom validators can run concurrently',\n }),\n 'max-fetch-concurrency': Flags.integer({\n default: 25,\n description: 'Specify how many `client.fetch` requests are allowed to run concurrently',\n }),\n workspace: Flags.string({\n description: 'The name of the workspace to use when downloading and validating all documents',\n }),\n yes: Flags.boolean({\n char: 'y',\n default: false,\n description: 'Skips the first confirmation prompt',\n }),\n }\n\n static override hiddenAliases: string[] = ['document:validate']\n\n public async run(): Promise<void> {\n const {flags} = await this.parse(ValidateDocumentsCommand)\n const {\n dataset,\n file,\n format,\n level,\n 'max-custom-validation-concurrency': maxCustomValidationConcurrency,\n 'max-fetch-concurrency': maxFetchConcurrency,\n 'project-id': projectId,\n workspace,\n } = flags\n const unattendedMode = Boolean(flags.yes)\n\n let workDir: string\n let cliConfig: CliConfig\n try {\n const root = await this.getProjectRoot()\n workDir = root.directory\n cliConfig = await this.getCliConfig()\n } catch (err) {\n if (err instanceof ProjectRootNotFoundError) {\n this.error(\n 'This command must be run from within a Sanity project directory (requires studio schema for validation)',\n {exit: 1},\n )\n }\n throw err\n }\n\n if (!unattendedMode) {\n this.log(\n `${styleText('yellow', `${logSymbols.warning} Warning:`)} This command ${\n file\n ? 'reads all documents from your input file'\n : 'downloads all documents from your dataset'\n } and processes them through your local schema within a ` +\n `simulated browser environment.\\n`,\n )\n this.log(`Potential pitfalls:\\n`)\n this.log(\n `- Processes all documents locally (excluding assets). Large datasets may require more resources.`,\n )\n this.log(\n `- Executes all custom validation functions. Some functions may need to be refactored for compatibility.`,\n )\n this.log(\n `- Not all standard browser features are available and may cause issues while loading your Studio.`,\n )\n this.log(\n `- Adheres to document permissions. Ensure this account can see all desired documents.`,\n )\n if (file) {\n this.log(\n `- Checks for missing document references against the live dataset if not found in your file.`,\n )\n }\n\n const confirmed = await confirm({\n default: true,\n message: `Are you sure you want to continue?`,\n })\n\n if (!confirmed) {\n this.error('User aborted', {exit: 1})\n }\n }\n\n if (format && !(format in reporters)) {\n const formatter = new Intl.ListFormat('en-US', {\n style: 'long',\n type: 'conjunction',\n })\n this.error(\n `Did not recognize format '${format}'. Available formats are ${formatter.format(\n Object.keys(reporters).map((key) => `'${key}'`),\n )}`,\n {exit: 1},\n )\n }\n\n let ndjsonFilePath\n if (file) {\n const filePath = path.resolve(workDir, file)\n\n const stat = await fs.promises.stat(filePath)\n if (!stat.isFile()) {\n this.error(`'--file' must point to a valid ndjson file or tarball`, {exit: 1})\n }\n\n ndjsonFilePath = filePath\n }\n\n try {\n const overallLevel = await validateDocuments({\n dataset,\n level,\n maxCustomValidationConcurrency,\n maxFetchConcurrency,\n ndjsonFilePath,\n projectId,\n reporter: (worker) => {\n const reporter =\n format && format in reporters\n ? reporters[format as keyof typeof reporters]\n : reporters.pretty\n\n return reporter({flags, output: this.output, worker})\n },\n studioHost: cliConfig.studioHost,\n workDir,\n workspace,\n })\n\n if (overallLevel === 'error') {\n this.exit(1)\n }\n } catch (err) {\n this.error(err instanceof Error ? err.message : String(err), {exit: 1})\n }\n }\n}\n"],"names":["fs","path","styleText","Flags","ProjectRootNotFoundError","SanityCommand","confirm","logSymbols","validateDocuments","reporters","getDatasetFlag","getProjectIdFlag","ValidateDocumentsCommand","description","examples","command","flags","semantics","file","string","format","level","custom","default","options","integer","workspace","yes","boolean","char","hiddenAliases","run","parse","dataset","maxCustomValidationConcurrency","maxFetchConcurrency","projectId","unattendedMode","Boolean","workDir","cliConfig","root","getProjectRoot","directory","getCliConfig","err","error","exit","log","warning","confirmed","message","formatter","Intl","ListFormat","style","type","Object","keys","map","key","ndjsonFilePath","filePath","resolve","stat","promises","isFile","overallLevel","reporter","worker","pretty","output","studioHost","Error","String"],"mappings":"AAAA,OAAOA,QAAQ,UAAS;AACxB,OAAOC,UAAU,YAAW;AAC5B,SAAQC,SAAS,QAAO,YAAW;AAEnC,SAAQC,KAAK,QAAO,cAAa;AACjC,SAAwBC,wBAAwB,EAAEC,aAAa,QAAO,mBAAkB;AACxF,SAAQC,OAAO,EAAEC,UAAU,QAAO,sBAAqB;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ACR;YACA,IAAI,CAACV,KAAK,CACR,CAAC,0BAA0B,EAAE1B,OAAO,yBAAyB,EAAEgC,UAAUhC,MAAM,CAC7EqC,OAAOC,IAAI,CAACjD,WAAWkD,GAAG,CAAC,CAACC,MAAQ,CAAC,CAAC,EAAEA,IAAI,CAAC,CAAC,IAC7C,EACH;gBAACb,MAAM;YAAC;QAEZ;QAEA,IAAIc;QACJ,IAAI3C,MAAM;YACR,MAAM4C,WAAW7D,KAAK8D,OAAO,CAACxB,SAASrB;YAEvC,MAAM8C,OAAO,MAAMhE,GAAGiE,QAAQ,CAACD,IAAI,CAACF;YACpC,IAAI,CAACE,KAAKE,MAAM,IAAI;gBAClB,IAAI,CAACpB,KAAK,CAAC,CAAC,qDAAqD,CAAC,EAAE;oBAACC,MAAM;gBAAC;YAC9E;YAEAc,iBAAiBC;QACnB;QAEA,IAAI;YACF,MAAMK,eAAe,MAAM3D,kBAAkB;gBAC3CyB;gBACAZ;gBACAa;gBACAC;gBACA0B;gBACAzB;gBACAgC,UAAU,CAACC;oBACT,MAAMD,WACJhD,UAAUA,UAAUX,YAChBA,SAAS,CAACW,OAAiC,GAC3CX,UAAU6D,MAAM;oBAEtB,OAAOF,SAAS;wBAACpD;wBAAOuD,QAAQ,IAAI,CAACA,MAAM;wBAAEF;oBAAM;gBACrD;gBACAG,YAAYhC,UAAUgC,UAAU;gBAChCjC;gBACAb;YACF;YAEA,IAAIyC,iBAAiB,SAAS;gBAC5B,IAAI,CAACpB,IAAI,CAAC;YACZ;QACF,EAAE,OAAOF,KAAK;YACZ,IAAI,CAACC,KAAK,CAACD,eAAe4B,QAAQ5B,IAAIM,OAAO,GAAGuB,OAAO7B,MAAM;gBAACE,MAAM;YAAC;QACvE;IACF;AACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/commands/hooks/attempt.ts"],"sourcesContent":["import {Args} from '@oclif/core'\nimport {SanityCommand, subdebug} from '@sanity/cli-core'\n\nimport {formatFailure} from '../../actions/hook/formatFailure.js'\nimport {type DeliveryAttempt} from '../../actions/hook/types.js'\nimport {promptForProject} from '../../prompts/promptForProject.js'\nimport {getHookAttempt} from '../../services/hooks.js'\nimport {getProjectIdFlag} from '../../util/sharedFlags.js'\n\nconst attemptDebug = subdebug('hook:attempt')\n\nexport class AttemptHookCommand extends SanityCommand<typeof AttemptHookCommand> {\n static override args = {\n attemptId: Args.string({\n description: 'The delivery attempt ID to get details for',\n required: true,\n }),\n }\n static override description = 'Print details of a given webhook delivery attempt'\n\n static override examples = [\n {\n command: '<%= config.bin %> <%= command.id %> abc123',\n description: 'Print details of webhook delivery attempt with ID abc123',\n },\n {\n command: '<%= config.bin %> <%= command.id %> abc123 --project-id projectId',\n description: 'Get attempt details for a specific project',\n },\n ]\n\n static override flags = {\n ...getProjectIdFlag({\n description: 'Project ID to view webhook attempt for',\n semantics: 'override',\n }),\n }\n\n static override hiddenAliases: string[] = ['hook:attempt']\n\n public async run() {\n const {args} = await this.parse(AttemptHookCommand)\n const {attemptId} = args\n\n const projectId = await this.getProjectId({\n fallback: () =>\n promptForProject({\n requiredPermissions: [{grant: 'read', permission: 'sanity.project.webhooks'}],\n }),\n })\n\n let attempt: DeliveryAttempt\n try {\n attempt = await getHookAttempt({attemptId, projectId})\n } catch (error) {\n const err = error as Error\n attemptDebug(`Error fetching hook attempt ${attemptId}`, err)\n this.error(`Hook attempt retrieval failed:\\n${err.message}`, {exit: 1})\n }\n\n const {createdAt, failureReason, inProgress, resultBody, resultCode} = attempt\n\n this.log(`Date: ${createdAt}`)\n this.log(`Status: ${this.getStatus(attempt)}`)\n this.log(`Status code: ${resultCode}`)\n\n if (attempt.isFailure) {\n this.log(`Failure: ${formatFailure(attempt)}`)\n }\n\n if (!inProgress && (!failureReason || failureReason === 'http')) {\n const body = resultBody ? `\\n---\\n${resultBody}\\n---\\n` : '<empty>'\n this.log(`Response body: ${body}`)\n }\n }\n\n private getStatus(attempt: DeliveryAttempt): string {\n if (attempt.isFailure) {\n return 'Failed'\n }\n\n if (attempt.inProgress) {\n return 'In progress'\n }\n\n return 'Delivered'\n }\n}\n"],"names":["Args","SanityCommand","subdebug","formatFailure","promptForProject","getHookAttempt","getProjectIdFlag","attemptDebug","AttemptHookCommand","args","attemptId","string","description","required","examples","command","flags","semantics","hiddenAliases","run","parse","projectId","getProjectId","fallback","requiredPermissions","grant","permission","attempt","error","err","message","exit","createdAt","failureReason","inProgress","resultBody","resultCode","log","getStatus","isFailure","body"],"mappings":"AAAA,SAAQA,IAAI,QAAO,cAAa;AAChC,SAAQC,aAAa,EAAEC,QAAQ,QAAO,mBAAkB;AAExD,SAAQC,aAAa,QAAO,sCAAqC;AAEjE,SAAQC,gBAAgB,QAAO,oCAAmC;AAClE,SAAQC,cAAc,QAAO,0BAAyB;AACtD,SAAQC,gBAAgB,QAAO,4BAA2B;AAE1D,MAAMC,eAAeL,SAAS;AAE9B,OAAO,MAAMM,2BAA2BP;IACtC,OAAgBQ,OAAO;QACrBC,WAAWV,KAAKW,MAAM,CAAC;YACrBC,aAAa;YACbC,UAAU;QACZ;IACF,EAAC;IACD,OAAgBD,cAAc,oDAAmD;IAEjF,OAAgBE,WAAW;QACzB;YACEC,SAAS;YACTH,aAAa;QACf;QACA;YACEG,SAAS;YACTH,aAAa;QACf;KACD,CAAA;IAED,OAAgBI,QAAQ;QACtB,GAAGV,iBAAiB;YAClBM,aAAa;YACbK,WAAW;QACb,EAAE;IACJ,EAAC;IAED,OAAgBC,gBAA0B;QAAC;KAAe,CAAA;IAE1D,MAAaC,MAAM;QACjB,MAAM,EAACV,IAAI,EAAC,GAAG,MAAM,IAAI,CAACW,KAAK,CAACZ;QAChC,MAAM,EAACE,SAAS,EAAC,GAAGD;QAEpB,MAAMY,YAAY,MAAM,IAAI,CAACC,YAAY,CAAC;YACxCC,UAAU,IACRnB,iBAAiB;oBACfoB,qBAAqB;wBAAC;4BAACC,OAAO;4BAAQC,YAAY;wBAAyB;qBAAE;gBAC/E;QACJ;QAEA,IAAIC;QACJ,IAAI;YACFA,UAAU,MAAMtB,eAAe;gBAACK;gBAAWW;YAAS;QACtD,EAAE,OAAOO,OAAO;YACd,MAAMC,MAAMD;YACZrB,aAAa,CAAC,4BAA4B,EAAEG,WAAW,EAAEmB;YACzD,IAAI,CAACD,KAAK,CAAC,CAAC,gCAAgC,EAAEC,IAAIC,OAAO,EAAE,EAAE;gBAACC,MAAM;YAAC;QACvE;QAEA,MAAM,EAACC,SAAS,EAAEC,aAAa,EAAEC,UAAU,EAAEC,UAAU,EAAEC,UAAU,EAAC,GAAGT;QAEvE,IAAI,CAACU,GAAG,CAAC,CAAC,MAAM,EAAEL,WAAW;QAC7B,IAAI,CAACK,GAAG,CAAC,CAAC,QAAQ,EAAE,IAAI,CAACC,SAAS,CAACX,UAAU;QAC7C,IAAI,CAACU,GAAG,CAAC,CAAC,aAAa,EAAED,YAAY;QAErC,IAAIT,QAAQY,SAAS,EAAE;YACrB,IAAI,CAACF,GAAG,CAAC,CAAC,SAAS,EAAElC,cAAcwB,UAAU;QAC/C;QAEA,IAAI,CAACO,cAAe,CAAA,CAACD,iBAAiBA,kBAAkB,MAAK,GAAI;YAC/D,MAAMO,OAAOL,aAAa,CAAC,OAAO,EAAEA,WAAW,OAAO,CAAC,GAAG;YAC1D,IAAI,CAACE,GAAG,CAAC,CAAC,eAAe,EAAEG,MAAM;QACnC;IACF;IAEQF,UAAUX,OAAwB,EAAU;QAClD,IAAIA,QAAQY,SAAS,EAAE;YACrB,OAAO;QACT;QAEA,IAAIZ,QAAQO,UAAU,EAAE;YACtB,OAAO;QACT;QAEA,OAAO;IACT;AACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/commands/hooks/create.ts"],"sourcesContent":["import {getSanityUrl, SanityCommand, subdebug} from '@sanity/cli-core'\nimport open from 'open'\n\nimport {promptForProject} from '../../prompts/promptForProject.js'\nimport {getProjectById} from '../../services/projects.js'\nimport {getProjectIdFlag} from '../../util/sharedFlags.js'\n\nconst createHookDebug = subdebug('hook:create')\n\nexport class CreateHookCommand extends SanityCommand<typeof CreateHookCommand> {\n static override description = 'Create a new webhook for the current project'\n static override examples = [\n {\n command: '<%= config.bin %> <%= command.id %>',\n description: 'Create a new webhook for the current project',\n },\n {\n command: '<%= config.bin %> <%= command.id %> --project-id abc123',\n description: 'Create a webhook for a specific project',\n },\n ]\n\n static override flags = {\n ...getProjectIdFlag({\n description: 'Project ID to create webhook for',\n semantics: 'override',\n }),\n }\n\n static override hiddenAliases: string[] = ['hook:create']\n\n public async run() {\n const projectId = await this.getProjectId({\n fallback: () =>\n promptForProject({requiredPermissions: [{grant: 'read', permission: 'sanity.project'}]}),\n })\n\n let projectInfo: {organizationId?: string | null}\n try {\n projectInfo = await getProjectById(projectId)\n } catch (error) {\n const err = error as Error\n createHookDebug(`Error fetching project info for project ${projectId}`, err)\n this.error(`Failed to fetch project information:\\n${err.message}`, {exit: 1})\n }\n\n const organizationId = projectInfo.organizationId || 'personal'\n const manageUrl = getSanityUrl(\n `/organizations/${organizationId}/project/${projectId}/api/webhooks/new`,\n )\n\n this.log(`Opening ${manageUrl}`)\n\n try {\n await open(manageUrl)\n } catch (error) {\n const err = error as Error\n createHookDebug('Error opening browser', err)\n this.error(`Failed to open browser:\\n${err.message}`, {exit: 1})\n }\n }\n}\n"],"names":["getSanityUrl","SanityCommand","subdebug","open","promptForProject","getProjectById","getProjectIdFlag","createHookDebug","CreateHookCommand","description","examples","command","flags","semantics","hiddenAliases","run","projectId","getProjectId","fallback","requiredPermissions","grant","permission","projectInfo","error","err","message","exit","organizationId","manageUrl","log"],"mappings":"AAAA,SAAQA,YAAY,EAAEC,aAAa,EAAEC,QAAQ,QAAO,mBAAkB;AACtE,OAAOC,UAAU,OAAM;AAEvB,SAAQC,gBAAgB,QAAO,oCAAmC;AAClE,SAAQC,cAAc,QAAO,6BAA4B;AACzD,SAAQC,gBAAgB,QAAO,4BAA2B;AAE1D,MAAMC,kBAAkBL,SAAS;AAEjC,OAAO,MAAMM,0BAA0BP;IACrC,OAAgBQ,cAAc,+CAA8C;IAC5E,OAAgBC,WAAW;QACzB;YACEC,SAAS;YACTF,aAAa;QACf;QACA;YACEE,SAAS;YACTF,aAAa;QACf;KACD,CAAA;IAED,OAAgBG,QAAQ;QACtB,GAAGN,iBAAiB;YAClBG,aAAa;YACbI,WAAW;QACb,EAAE;IACJ,EAAC;IAED,OAAgBC,gBAA0B;QAAC;KAAc,CAAA;IAEzD,MAAaC,MAAM;QACjB,MAAMC,YAAY,MAAM,IAAI,CAACC,YAAY,CAAC;YACxCC,UAAU,IACRd,iBAAiB;oBAACe,qBAAqB;wBAAC;4BAACC,OAAO;4BAAQC,YAAY;wBAAgB;qBAAE;gBAAA;QAC1F;QAEA,IAAIC;QACJ,IAAI;YACFA,cAAc,MAAMjB,eAAeW;QACrC,EAAE,OAAOO,OAAO;YACd,MAAMC,MAAMD;YACZhB,gBAAgB,CAAC,wCAAwC,EAAES,WAAW,EAAEQ;YACxE,IAAI,CAACD,KAAK,CAAC,CAAC,sCAAsC,EAAEC,IAAIC,OAAO,EAAE,EAAE;gBAACC,MAAM;YAAC;QAC7E;QAEA,MAAMC,iBAAiBL,YAAYK,cAAc,IAAI;QACrD,MAAMC,YAAY5B,aAChB,CAAC,eAAe,EAAE2B,eAAe,SAAS,EAAEX,UAAU,iBAAiB,CAAC;QAG1E,IAAI,CAACa,GAAG,CAAC,CAAC,QAAQ,EAAED,WAAW;QAE/B,IAAI;YACF,MAAMzB,KAAKyB;QACb,EAAE,OAAOL,OAAO;YACd,MAAMC,MAAMD;YACZhB,gBAAgB,yBAAyBiB;YACzC,IAAI,CAACD,KAAK,CAAC,CAAC,yBAAyB,EAAEC,IAAIC,OAAO,EAAE,EAAE;gBAACC,MAAM;YAAC;QAChE;IACF;AACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/commands/hooks/delete.ts"],"sourcesContent":["import {Args} from '@oclif/core'\nimport {SanityCommand, subdebug} from '@sanity/cli-core'\nimport {select} from '@sanity/cli-core/ux'\n\nimport {type Hook} from '../../actions/hook/types'\nimport {promptForProject} from '../../prompts/promptForProject.js'\nimport {deleteHookForProject, listHooksForProject} from '../../services/hooks.js'\nimport {getProjectIdFlag} from '../../util/sharedFlags.js'\n\nconst deleteHookDebug = subdebug('hook:delete')\n\nexport class Delete extends SanityCommand<typeof Delete> {\n static override args = {\n name: Args.string({\n description: 'Name of hook to delete (will prompt if not provided)',\n required: false,\n }),\n }\n\n static override description = 'Delete a hook within your project'\n\n static override examples = [\n {\n command: '<%= config.bin %> <%= command.id %>',\n description: 'Interactively select and delete a hook',\n },\n {\n command: '<%= config.bin %> <%= command.id %> my-hook',\n description: 'Delete a specific hook by name',\n },\n {\n command: '<%= config.bin %> <%= command.id %> --project-id abc123',\n description: 'Delete a hook from a specific project',\n },\n ]\n\n static override flags = {\n ...getProjectIdFlag({\n description: 'Project ID to delete webhook from',\n semantics: 'override',\n }),\n }\n\n static override hiddenAliases: string[] = ['hook:delete']\n\n public async run(): Promise<void> {\n const {args} = await this.parse(Delete)\n\n const projectId = await this.getProjectId({\n fallback: () =>\n promptForProject({\n requiredPermissions: [{grant: 'delete', permission: 'sanity.project.webhooks'}],\n }),\n })\n\n // Get the hook ID to delete\n const hookId = await this.promptForHook(args.name, projectId)\n\n try {\n await deleteHookForProject(projectId, hookId)\n\n this.log('Hook deleted')\n } catch (error) {\n const err = error as Error\n deleteHookDebug(`Error deleting hook ${hookId} for project ${projectId}`, err)\n this.error(`Hook deletion failed:\\n${err.message}`, {exit: 1})\n }\n }\n\n private async promptForHook(\n specifiedName: string | undefined,\n projectId: string,\n ): Promise<string> {\n let hooks: Hook[]\n try {\n hooks = await listHooksForProject(projectId)\n } catch (error) {\n const err = error as Error\n deleteHookDebug(`Error fetching hooks for project ${projectId}`, err)\n this.error(`Failed to fetch hooks:\\n${err.message}`, {exit: 1})\n }\n\n if (hooks.length === 0) {\n this.error('No hooks configured for this project.', {exit: 1})\n }\n\n // If hook name is specified, find it in the list\n if (specifiedName) {\n const specifiedNameLower = specifiedName.toLowerCase()\n const selectedHook = hooks.find((hook) => hook.name.toLowerCase() === specifiedNameLower)\n\n if (!selectedHook) {\n this.error(`Hook with name \"${specifiedName}\" not found`, {exit: 1})\n }\n\n return selectedHook.id\n }\n\n // If no hook name specified, prompt user to select one\n const choices = hooks.map((hook) => ({\n name: hook.name,\n value: hook.id,\n }))\n\n const selectedId = await select({\n choices,\n message: 'Select hook to delete',\n })\n\n return selectedId\n }\n}\n"],"names":["Args","SanityCommand","subdebug","select","promptForProject","deleteHookForProject","listHooksForProject","getProjectIdFlag","deleteHookDebug","Delete","args","name","string","description","required","examples","command","flags","semantics","hiddenAliases","run","parse","projectId","getProjectId","fallback","requiredPermissions","grant","permission","hookId","promptForHook","log","error","err","message","exit","specifiedName","hooks","length","specifiedNameLower","toLowerCase","selectedHook","find","hook","id","choices","map","value","selectedId"],"mappings":"AAAA,SAAQA,IAAI,QAAO,cAAa;AAChC,SAAQC,aAAa,EAAEC,QAAQ,QAAO,mBAAkB;AACxD,SAAQC,MAAM,QAAO,sBAAqB;AAG1C,SAAQC,gBAAgB,QAAO,oCAAmC;AAClE,SAAQC,oBAAoB,EAAEC,mBAAmB,QAAO,0BAAyB;AACjF,SAAQC,gBAAgB,QAAO,4BAA2B;AAE1D,MAAMC,kBAAkBN,SAAS;AAEjC,OAAO,MAAMO,eAAeR;IAC1B,OAAgBS,OAAO;QACrBC,MAAMX,KAAKY,MAAM,CAAC;YAChBC,aAAa;YACbC,UAAU;QACZ;IACF,EAAC;IAED,OAAgBD,cAAc,oCAAmC;IAEjE,OAAgBE,WAAW;QACzB;YACEC,SAAS;YACTH,aAAa;QACf;QACA;YACEG,SAAS;YACTH,aAAa;QACf;QACA;YACEG,SAAS;YACTH,aAAa;QACf;KACD,CAAA;IAED,OAAgBI,QAAQ;QACtB,GAAGV,iBAAiB;YAClBM,aAAa;YACbK,WAAW;QACb,EAAE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AAC4B,WAAW,OAAOD;YAEtE,IAAI,CAACE,cAAc;gBACjB,IAAI,CAACT,KAAK,CAAC,CAAC,gBAAgB,EAAEI,cAAc,WAAW,CAAC,EAAE;oBAACD,MAAM;gBAAC;YACpE;YAEA,OAAOM,aAAaG,EAAE;QACxB;QAEA,uDAAuD;QACvD,MAAMC,UAAUR,MAAMS,GAAG,CAAC,CAACH,OAAU,CAAA;gBACnC/B,MAAM+B,KAAK/B,IAAI;gBACfmC,OAAOJ,KAAKC,EAAE;YAChB,CAAA;QAEA,MAAMI,aAAa,MAAM5C,OAAO;YAC9ByC;YACAX,SAAS;QACX;QAEA,OAAOc;IACT;AACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/commands/hooks/list.ts"],"sourcesContent":["import {SanityCommand, subdebug} from '@sanity/cli-core'\n\nimport {type Hook} from '../../actions/hook/types'\nimport {promptForProject} from '../../prompts/promptForProject.js'\nimport {listHooksForProject} from '../../services/hooks.js'\nimport {getProjectIdFlag} from '../../util/sharedFlags.js'\n\nconst listHookDebug = subdebug('hook:list')\n\nexport class List extends SanityCommand<typeof List> {\n static override description = 'List hooks for a given project'\n static override examples = [\n {\n command: '<%= config.bin %> <%= command.id %>',\n description: 'List hooks for a given project',\n },\n {\n command: '<%= config.bin %> <%= command.id %> --project-id abc123',\n description: 'List hooks for a specific project',\n },\n ]\n\n static override flags = {\n ...getProjectIdFlag({\n description: 'Project ID to list webhooks for',\n semantics: 'override',\n }),\n }\n\n static override hiddenAliases: string[] = ['hook:list']\n\n public async run() {\n // Ensure we have project context\n const projectId = await this.getProjectId({\n fallback: () =>\n promptForProject({\n requiredPermissions: [{grant: 'read', permission: 'sanity.project.webhooks'}],\n }),\n })\n\n let hooks: Hook[]\n try {\n hooks = await listHooksForProject(projectId)\n } catch (error) {\n const err = error as Error\n\n listHookDebug(`Error fetching hooks for project ${projectId}`, err)\n this.error(`Hook list retrieval failed:\\n${err.message}`, {exit: 1})\n }\n\n for (const hook of hooks) {\n this.log(`Name: ${hook.name}`)\n this.log(`Dataset: ${hook.dataset}`)\n this.log(`URL: ${hook.url}`)\n\n if (hook.type === 'document') {\n this.log(`HTTP method: ${hook.httpMethod}`)\n\n if (hook.description) {\n this.log(`Description: ${hook.description}`)\n }\n }\n\n this.log('')\n }\n }\n}\n"],"names":["SanityCommand","subdebug","promptForProject","listHooksForProject","getProjectIdFlag","listHookDebug","List","description","examples","command","flags","semantics","hiddenAliases","run","projectId","getProjectId","fallback","requiredPermissions","grant","permission","hooks","error","err","message","exit","hook","log","name","dataset","url","type","httpMethod"],"mappings":"AAAA,SAAQA,aAAa,EAAEC,QAAQ,QAAO,mBAAkB;AAGxD,SAAQC,gBAAgB,QAAO,oCAAmC;AAClE,SAAQC,mBAAmB,QAAO,0BAAyB;AAC3D,SAAQC,gBAAgB,QAAO,4BAA2B;AAE1D,MAAMC,gBAAgBJ,SAAS;AAE/B,OAAO,MAAMK,aAAaN;IACxB,OAAgBO,cAAc,iCAAgC;IAC9D,OAAgBC,WAAW;QACzB;YACEC,SAAS;YACTF,aAAa;QACf;QACA;YACEE,SAAS;YACTF,aAAa;QACf;KACD,CAAA;IAED,OAAgBG,QAAQ;QACtB,GAAGN,iBAAiB;YAClBG,aAAa;YACbI,WAAW;QACb,EAAE;IACJ,EAAC;IAED,OAAgBC,gBAA0B;QAAC;KAAY,CAAA;IAEvD,MAAaC,MAAM;QACjB,iCAAiC;QACjC,MAAMC,YAAY,MAAM,IAAI,CAACC,YAAY,CAAC;YACxCC,UAAU,IACRd,iBAAiB;oBACfe,qBAAqB;wBAAC;4BAACC,OAAO;4BAAQC,YAAY;wBAAyB;qBAAE;gBAC/E;QACJ;QAEA,IAAIC;QACJ,IAAI;YACFA,QAAQ,MAAMjB,oBAAoBW;QACpC,EAAE,OAAOO,OAAO;YACd,MAAMC,MAAMD;YAEZhB,cAAc,CAAC,iCAAiC,EAAES,WAAW,EAAEQ;YAC/D,IAAI,CAACD,KAAK,CAAC,CAAC,6BAA6B,EAAEC,IAAIC,OAAO,EAAE,EAAE;gBAACC,MAAM;YAAC;QACpE;QAEA,KAAK,MAAMC,QAAQL,MAAO;YACxB,IAAI,CAACM,GAAG,CAAC,CAAC,MAAM,EAAED,KAAKE,IAAI,EAAE;YAC7B,IAAI,CAACD,GAAG,CAAC,CAAC,SAAS,EAAED,KAAKG,OAAO,EAAE;YACnC,IAAI,CAACF,GAAG,CAAC,CAAC,KAAK,EAAED,KAAKI,GAAG,EAAE;YAE3B,IAAIJ,KAAKK,IAAI,KAAK,YAAY;gBAC5B,IAAI,CAACJ,GAAG,CAAC,CAAC,aAAa,EAAED,KAAKM,UAAU,EAAE;gBAE1C,IAAIN,KAAKlB,WAAW,EAAE;oBACpB,IAAI,CAACmB,GAAG,CAAC,CAAC,aAAa,EAAED,KAAKlB,WAAW,EAAE;gBAC7C;YACF;YAEA,IAAI,CAACmB,GAAG,CAAC;QACX;IACF;AACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/commands/hooks/logs.ts"],"sourcesContent":["import {inspect, styleText} from 'node:util'\n\nimport {Args, Flags} from '@oclif/core'\nimport {SanityCommand, subdebug} from '@sanity/cli-core'\nimport {select} from '@sanity/cli-core/ux'\nimport groupBy from 'lodash-es/groupBy.js'\n\nimport {formatFailure} from '../../actions/hook/formatFailure.js'\nimport {type DeliveryAttempt, type Hook, type HookMessage} from '../../actions/hook/types'\nimport {promptForProject} from '../../prompts/promptForProject.js'\nimport {\n getHookAttemptsForProject,\n getHookMessagesForProject,\n getHooksForProject,\n} from '../../services/hooks.js'\nimport {getProjectIdFlag} from '../../util/sharedFlags.js'\n\nconst logsHookDebug = subdebug('hook:logs')\n\nexport class LogsHookCommand extends SanityCommand<typeof LogsHookCommand> {\n static override args = {\n name: Args.string({\n description: 'Name of the hook to show logs for',\n required: false,\n }),\n }\n\n static override description = 'List latest log entries for a given hook'\n\n static override examples = [\n {\n command: '<%= config.bin %> <%= command.id %>',\n description: 'List latest log entries for a given hook',\n },\n {\n command: '<%= config.bin %> <%= command.id %> [NAME]',\n description: 'List latest log entries for a specific hook by name',\n },\n {\n command: '<%= config.bin %> <%= command.id %> --project-id abc123',\n description: 'List hook logs for a specific project',\n },\n ]\n\n static override flags = {\n ...getProjectIdFlag({\n description: 'Project ID to view webhook logs for',\n semantics: 'override',\n }),\n detailed: Flags.boolean({\n description: 'Include detailed payload and attempts',\n required: false,\n }),\n }\n\n static override hiddenAliases: string[] = ['hook:logs']\n\n public async run() {\n const {args, flags} = await this.parse(LogsHookCommand)\n\n // Ensure we have project context\n const projectId = await this.getProjectId({\n fallback: () =>\n promptForProject({\n requiredPermissions: [{grant: 'read', permission: 'sanity.project.webhooks'}],\n }),\n })\n\n // Get hooks for the project\n let hooks: Hook[]\n try {\n hooks = await getHooksForProject(projectId)\n } catch (error) {\n const err = error as Error\n logsHookDebug(`Error fetching hooks for project ${projectId}`, err)\n this.error(`Hook list retrieval failed:\\n${err.message}`, {exit: 1})\n }\n\n if (hooks.length === 0) {\n this.error('No hooks currently registered', {exit: 1})\n }\n\n // If hook name is provided, find that specific hook\n let selectedHook: Hook | undefined\n if (args.name) {\n selectedHook = hooks.find((hook) => hook.name.toLowerCase() === args.name?.toLowerCase())\n if (!selectedHook) {\n this.error(`Hook with name \"${args.name}\" not found`, {exit: 1})\n }\n } else if (hooks.length === 1) {\n // If only one hook exists, use that\n selectedHook = hooks[0]\n } else {\n // Otherwise prompt user to select a hook\n selectedHook = await this.selectHook(hooks)\n }\n\n if (!selectedHook) {\n this.error('No hook selected', {exit: 1})\n }\n\n // Fetch messages and attempts for the selected hook\n let messages: HookMessage[]\n let attempts: DeliveryAttempt[] = []\n try {\n ;[messages, attempts] = await Promise.all([\n getHookMessagesForProject({\n hookId: selectedHook.id,\n projectId,\n }),\n getHookAttemptsForProject({\n hookId: selectedHook.id,\n projectId,\n }),\n ])\n } catch (error) {\n const err = error as Error\n logsHookDebug(`Error fetching logs for hook ${selectedHook.id}`, err)\n this.error(`Hook logs retrieval failed:\\n${err.message}`, {exit: 1})\n }\n\n // Group attempts by message ID\n const groupedAttempts = groupBy(attempts, 'messageId')\n\n // Populate messages with attempts\n const populated = messages.map((msg): HookMessage & {attempts: DeliveryAttempt[]} => ({\n ...msg,\n attempts: groupedAttempts[msg.id] || [],\n }))\n\n const totalMessages = messages.length - 1\n\n for (const [i, message] of populated.entries()) {\n this.printMessage(message, {detailed: flags.detailed})\n this.printSeparator(totalMessages === i)\n }\n }\n\n private formatAttemptDate(dateString: string): string {\n try {\n return new Date(dateString).toISOString().replace(/\\.\\d+Z$/, 'Z')\n } catch {\n return dateString // fallback to original if parsing fails\n }\n }\n\n private printMessage(\n message: HookMessage & {attempts: DeliveryAttempt[]},\n options: {detailed?: boolean},\n ) {\n const {detailed} = options\n\n this.log(`Date: ${message.createdAt}`)\n this.log(`Status: ${message.status}`)\n if (message.resultCode) {\n this.log(`Result code: ${message.resultCode}`)\n }\n\n if (message.failureCount > 0) {\n this.log(`Failures: ${message.failureCount}`)\n }\n\n if (detailed) {\n this.log('Payload:')\n try {\n const payload = JSON.parse(message.payload)\n this.log(inspect(payload, {colors: true}))\n } catch (error) {\n this.log(`Payload (raw): ${message.payload}`)\n logsHookDebug('Failed to parse payload JSON:', error)\n }\n }\n\n if (detailed && message.attempts && message.attempts.length > 0) {\n this.log('Attempts:')\n for (const attempt of message.attempts) {\n const date = this.formatAttemptDate(attempt.createdAt)\n const prefix = ` [${date}]`\n\n if (attempt.inProgress) {\n this.log(`${prefix} ${styleText('yellow', 'Pending')}`)\n } else if (attempt.isFailure) {\n const failure = formatFailure(attempt, {includeHelp: true})\n this.log(`${prefix} ${styleText('yellow', `Failure: ${failure}`)}`)\n } else {\n this.log(`${prefix} Success: HTTP ${attempt.resultCode} (${attempt.duration}ms)`)\n }\n }\n }\n\n // Leave some empty space between messages\n this.log('')\n }\n\n private printSeparator(skip: boolean) {\n if (!skip) {\n this.log('---\\n')\n }\n }\n\n private async selectHook(hooks: Hook[]) {\n const hookId = await select({\n choices: hooks.map((hook) => ({\n name: hook.name,\n value: hook.id,\n })),\n message: 'Select hook to list logs for',\n })\n\n return hooks.find((hook) => hook.id === hookId)\n }\n}\n"],"names":["inspect","styleText","Args","Flags","SanityCommand","subdebug","select","groupBy","formatFailure","promptForProject","getHookAttemptsForProject","getHookMessagesForProject","getHooksForProject","getProjectIdFlag","logsHookDebug","LogsHookCommand","args","name","string","description","required","examples","command","flags","semantics","detailed","boolean","hiddenAliases","run","parse","projectId","getProjectId","fallback","requiredPermissions","grant","permission","hooks","error","err","message","exit","length","selectedHook","find","hook","toLowerCase","selectHook","messages","attempts","Promise","all","hookId","id","groupedAttempts","populated","map","msg","totalMessages","i","entries","printMessage","printSeparator","formatAttemptDate","dateString","Date","toISOString","replace","options","log","createdAt","status","resultCode","failureCount","payload","JSON","colors","attempt","date","prefix","inProgress","isFailure","failure","includeHelp","duration","skip","choices","value"],"mappings":"AAAA,SAAQA,OAAO,EAAEC,SAAS,QAAO,YAAW;AAE5C,SAAQC,IAAI,EAAEC,KAAK,QAAO,cAAa;AACvC,SAAQC,aAAa,EAAEC,QAAQ,QAAO,mBAAkB;AACxD,SAAQC,MAAM,QAAO,sBAAqB;AAC1C,OAAOC,aAAa,uBAAsB;AAE1C,SAAQC,aAAa,QAAO,sCAAqC;AAEjE,SAAQC,gBAAgB,QAAO,oCAAmC;AAClE,SACEC,yBAAyB,EACzBC,yBAAyB,EACzBC,kBAAkB,QACb,0BAAyB;AAChC,SAAQC,gBAAgB,QAAO,4BAA2B;AAE1D,MAAMC,gBAAgBT,SAAS;AAE/B,OAAO,MAAMU,wBAAwBX;IACnC,OAAgBY,OAAO;QACrBC,MAAMf,KAAKgB,MAAM,CAAC;YAChBC,aAAa;YACbC,UAAU;QACZ;IACF,EAAC;IAED,OAAgBD,cAAc,2CAA0C;IAExE,OAAgBE,WAAW;QACzB;YACEC,SAAS;YACTH,aAAa;QACf;QACA;YACEG,SAAS;YACTH,aAAa;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