sanity 3.77.3-server-side-schemas.36 → 3.77.3-server-side-schemas.37

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.
@@ -3,18 +3,16 @@ import chalk from 'chalk'
3
3
  import uniq from 'lodash/uniq'
4
4
 
5
5
  import {isDefined} from '../../../manifest/manifestTypeHelpers'
6
- import {type ManifestWorkspaceFile} from '../../../manifest/manifestTypes'
7
6
  import {type SchemaStoreContext} from './schemaStoreTypes'
8
7
  import {createManifestExtractor, isManifestExtractSatisfied} from './utils/mainfestExtractor'
9
8
  import {createManifestReader} from './utils/manifestReader'
10
9
  import {createSchemaApiClient} from './utils/schemaApiClient'
11
10
  import {
12
- assetIdsMatchesWorkspaces,
13
11
  filterLogReadProjectIdMismatch,
14
12
  parseDeleteSchemasConfig,
15
13
  type StoreSchemaCommonFlags,
16
14
  } from './utils/schemaStoreValidation'
17
- import {getDatasetsOutString, getStringArrayOutString} from './utils/storeSchemaOutStrings'
15
+ import {getDatasetsOutString, getStringList} from './utils/storeSchemaOutStrings'
18
16
 
19
17
  export interface DeleteSchemaFlags extends StoreSchemaCommonFlags {
20
18
  ids?: string
@@ -22,19 +20,19 @@ export interface DeleteSchemaFlags extends StoreSchemaCommonFlags {
22
20
  }
23
21
 
24
22
  interface DeleteResult {
25
- workspace: ManifestWorkspaceFile
23
+ dataset: string
26
24
  schemaId: string
27
25
  deleted: boolean
28
26
  }
29
27
 
30
28
  class DeleteIdError extends Error {
31
29
  public id: string
32
- public workspace: ManifestWorkspaceFile
33
- constructor(id: string, workspace: ManifestWorkspaceFile, options?: ErrorOptions) {
30
+ public dataset: string
31
+ constructor(id: string, dataset: string, options?: ErrorOptions) {
34
32
  super((options?.cause as {message?: string})?.message, options)
35
33
  this.name = 'DeleteIdError'
36
34
  this.id = id
37
- this.workspace = workspace
35
+ this.dataset = dataset
38
36
  }
39
37
  }
40
38
 
@@ -76,76 +74,73 @@ export async function deleteSchemaAction(
76
74
  .filter((workspace) => !dataset || workspace.dataset === dataset)
77
75
  .filter((workspace) => filterLogReadProjectIdMismatch(workspace, projectId, output))
78
76
 
79
- assetIdsMatchesWorkspaces(
80
- ids.map((id) => id.schemaId),
81
- workspaces,
82
- )
77
+ const datasets = uniq(workspaces.map((w) => w.dataset))
83
78
 
84
79
  const results = await Promise.allSettled(
85
- workspaces.flatMap((workspace: ManifestWorkspaceFile) => {
86
- return ids
87
- .filter(({workspace: idWorkspace}) => idWorkspace === workspace.name)
88
- .map(async ({schemaId}): Promise<DeleteResult> => {
89
- try {
90
- const deletedSchema = await client
91
- .withConfig({dataset: workspace.dataset})
92
- .delete(schemaId)
93
- return {workspace, schemaId, deleted: deletedSchema.results.length}
94
- } catch (err) {
95
- throw new DeleteIdError(schemaId, workspace, {cause: err})
96
- }
97
- })
80
+ datasets.flatMap((targetDataset: string) => {
81
+ return ids.map(async ({schemaId}): Promise<DeleteResult> => {
82
+ try {
83
+ const deletedSchema = await client.withConfig({dataset: targetDataset}).delete(schemaId)
84
+ return {dataset: targetDataset, schemaId, deleted: deletedSchema.results.length}
85
+ } catch (err) {
86
+ throw new DeleteIdError(schemaId, targetDataset, {cause: err})
87
+ }
88
+ })
98
89
  }),
99
90
  )
100
91
 
101
92
  const deletedIds = results
102
93
  .filter((r): r is PromiseFulfilledResult<DeleteResult> => r.status === 'fulfilled')
103
94
  .filter((r) => r.value.deleted)
104
- .map((r) => r.value.schemaId)
95
+ .map((r) => r.value)
105
96
 
106
97
  const notFound = uniq(
107
98
  results
108
99
  .filter((r): r is PromiseFulfilledResult<DeleteResult> => r.status === 'fulfilled')
109
100
  .filter((r) => !r.value.deleted)
101
+ .filter((r) => !deletedIds.map(({schemaId}) => schemaId).includes(r.value.schemaId))
110
102
  .map((r) => r.value.schemaId),
111
103
  )
112
104
 
113
- const deleteFailureIds = results
114
- .filter((r) => r.status === 'rejected')
115
- .map((result) => {
116
- const error = result.reason
117
- if (error instanceof DeleteIdError) {
118
- output.error(
119
- chalk.red(
120
- `Failed to delete schema "${error.id}" in dataset "${error.workspace.dataset}":\n${error.message}`,
121
- ),
122
- )
123
- if (verbose) output.error(error)
124
- return error.id
125
- }
126
- //hubris inc: given the try-catch wrapping the full promise "this should never happen"
127
- throw error
128
- })
105
+ const deleteFailureIds = uniq(
106
+ results
107
+ .filter((r) => r.status === 'rejected')
108
+ .map((result) => {
109
+ const error = result.reason
110
+ if (error instanceof DeleteIdError) {
111
+ output.error(
112
+ chalk.red(
113
+ `Failed to delete schema "${error.id}" in dataset "${error.dataset}":\n${error.message}`,
114
+ ),
115
+ )
116
+ if (verbose) output.error(error)
117
+ return error.id
118
+ }
119
+ //hubris inc: given the try-catch wrapping the full promise "this should never happen"
120
+ throw error
121
+ }),
122
+ )
129
123
 
130
124
  const success = deletedIds.length === ids.length
131
125
  if (success) {
132
126
  output.success(`Successfully deleted ${deletedIds.length}/${ids.length} schemas`)
133
127
  } else {
134
- const datasets = uniq(workspaces.map((w) => w.dataset))
135
128
  output.error(
136
129
  [
137
130
  `Deleted ${deletedIds.length}/${ids.length} schemas.`,
138
131
  deletedIds.length
139
- ? `Successfully deleted ids:\n ${getStringArrayOutString(deletedIds)}`
132
+ ? `Successfully deleted ids:\n${deletedIds
133
+ .map(
134
+ ({schemaId, dataset: targetDataset}) =>
135
+ `- "${schemaId}" (in ${getDatasetsOutString([targetDataset])})`,
136
+ )
137
+ .join('\n')}`
140
138
  : undefined,
141
139
  notFound.length
142
- ? `Ids not found in ${getDatasetsOutString(datasets)}:\n ${getStringArrayOutString(notFound)}`
140
+ ? `Ids not found in ${getDatasetsOutString(datasets)}:\n${getStringList(notFound)}`
143
141
  : undefined,
144
142
  ...(deleteFailureIds.length
145
- ? [
146
- `Failed to delete ids:\n ${getStringArrayOutString(deleteFailureIds)}`,
147
- 'Check logs for errors.',
148
- ]
143
+ ? [`Failed to delete ids:\n${getStringList(deleteFailureIds)}`, 'Check logs for errors.']
149
144
  : []),
150
145
  ]
151
146
  .filter(isDefined)
@@ -13,7 +13,6 @@ import {createManifestExtractor, isManifestExtractSatisfied} from './utils/mainf
13
13
  import {createManifestReader} from './utils/manifestReader'
14
14
  import {createSchemaApiClient} from './utils/schemaApiClient'
15
15
  import {
16
- assetIdsMatchesWorkspaces,
17
16
  filterLogReadProjectIdMismatch,
18
17
  parseListSchemasConfig,
19
18
  type StoreSchemaCommonFlags,
@@ -68,8 +67,6 @@ export async function listSchemasAction(
68
67
  filterLogReadProjectIdMismatch(workspace, projectId, output),
69
68
  )
70
69
 
71
- if (id) assetIdsMatchesWorkspaces([id], workspaces)
72
-
73
70
  const datasets = uniq(workspaces.map((w) => w.dataset))
74
71
 
75
72
  const schemaResults = await Promise.allSettled(
@@ -94,13 +94,13 @@ function assertNoErrors(errors: string[]) {
94
94
  }
95
95
  }
96
96
 
97
- export function assetIdsMatchesWorkspaces(ids: string[], workspaces: {name: string}[]) {
97
+ export function assertIdsMatchesWorkspaces(ids: string[], workspaces: {name: string}[]) {
98
98
  const invalidIds = ids
99
99
  .map((id) => ({
100
100
  id,
101
101
  parsedId: parseWorkspaceSchemaId(id, []),
102
102
  }))
103
- .filter(({id, parsedId}) => !parsedId || !workspaces.some((w) => w.name === parsedId.workspace))
103
+ .filter(({parsedId}) => !parsedId || !workspaces.some((w) => w.name === parsedId.workspace))
104
104
 
105
105
  if (invalidIds.length)
106
106
  throw new FlagValidationError(
@@ -7,3 +7,7 @@ export function getDatasetsOutString(datasets: string[]) {
7
7
  export function getStringArrayOutString(array: string[]) {
8
8
  return `[${array.map((d) => `"${d}"`).join(',')}]`
9
9
  }
10
+
11
+ export function getStringList(array: string[]) {
12
+ return array.map((s) => `- "${s}"`).join('\n')
13
+ }
@@ -1,6 +1,6 @@
1
1
  import {type CliCommandDefinition} from '@sanity/cli'
2
2
 
3
- const description = 'Delete schema documents by id'
3
+ const description = 'Delete schema documents by id.'
4
4
 
5
5
  const helpText = `
6
6
  **Note**: This command is experimental and subject to change.