@sanity/cli 3.88.0 → 3.88.1-typegen-experimental.0

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.
Files changed (37) hide show
  1. package/lib/_chunks-cjs/cli.js +58748 -56759
  2. package/lib/_chunks-cjs/cli.js.map +1 -1
  3. package/lib/_chunks-cjs/generateAction.js +113 -111
  4. package/lib/_chunks-cjs/generateAction.js.map +1 -1
  5. package/lib/_chunks-cjs/loadEnv.js +3 -3
  6. package/lib/_chunks-cjs/loadEnv.js.map +1 -1
  7. package/lib/_chunks-cjs/workerChannel.js +84 -0
  8. package/lib/_chunks-cjs/workerChannel.js.map +1 -0
  9. package/lib/workers/typegenGenerate.d.ts +144 -33
  10. package/lib/workers/typegenGenerate.js +83 -112
  11. package/lib/workers/typegenGenerate.js.map +1 -1
  12. package/package.json +19 -21
  13. package/src/actions/typegen/generate.telemetry.ts +9 -3
  14. package/src/actions/typegen/generateAction.ts +159 -152
  15. package/src/cli.ts +0 -0
  16. package/src/commands/blueprints/addBlueprintsCommand.ts +52 -56
  17. package/src/commands/blueprints/blueprintsGroup.ts +0 -1
  18. package/src/commands/blueprints/configBlueprintsCommand.ts +50 -74
  19. package/src/commands/blueprints/deployBlueprintsCommand.ts +41 -133
  20. package/src/commands/blueprints/destroyBlueprintsCommand.ts +76 -0
  21. package/src/commands/blueprints/infoBlueprintsCommand.ts +29 -51
  22. package/src/commands/blueprints/initBlueprintsCommand.ts +55 -73
  23. package/src/commands/blueprints/logsBlueprintsCommand.ts +43 -81
  24. package/src/commands/blueprints/planBlueprintsCommand.ts +26 -36
  25. package/src/commands/blueprints/stacksBlueprintsCommand.ts +43 -51
  26. package/src/commands/functions/devFunctionsCommand.ts +1 -2
  27. package/src/commands/functions/envFunctionsCommand.ts +55 -46
  28. package/src/commands/functions/functionsGroup.ts +1 -2
  29. package/src/commands/functions/logsFunctionsCommand.ts +101 -58
  30. package/src/commands/functions/testFunctionsCommand.ts +56 -36
  31. package/src/commands/index.ts +6 -4
  32. package/src/commands/projects/listProjectsCommand.ts +0 -0
  33. package/src/commands/projects/projectsGroup.ts +0 -0
  34. package/src/util/__tests__/workerChannel.test.ts +222 -0
  35. package/src/util/workerChannel.ts +312 -0
  36. package/src/workers/typegenGenerate.ts +181 -183
  37. package/templates/app-sanity-ui/src/ExampleComponent.tsx +1 -1
@@ -1,110 +1,72 @@
1
1
  import {type CliCommandDefinition} from '../../types'
2
2
 
3
3
  const helpText = `
4
- ${
5
- /*Options
6
- --watch, -w Watch for new logs (streaming mode)
7
- */ ''
8
- }
9
- Examples
4
+ Options
5
+ --watch, -w Watch for new logs (streaming mode)
6
+
7
+ Examples:
10
8
  # Show logs for the current Stack
11
9
  sanity blueprints logs
12
- ${
13
- /*
14
- # Watch for new logs
10
+
11
+ # Watch for new logs (streaming mode)
15
12
  sanity blueprints logs --watch
16
- */ ''
17
- }
18
13
  `
19
14
 
20
- // const defaultFlags = {watch: false}
15
+ export interface BlueprintsLogsFlags {
16
+ watch?: boolean
17
+ w?: boolean
18
+ }
21
19
 
22
- const logsBlueprintsCommand: CliCommandDefinition = {
20
+ const defaultFlags: BlueprintsLogsFlags = {
21
+ //
22
+ }
23
+
24
+ const logsBlueprintsCommand: CliCommandDefinition<BlueprintsLogsFlags> = {
23
25
  name: 'logs',
24
26
  group: 'blueprints',
25
27
  helpText,
26
- signature: '[--watch]',
28
+ signature: '[--watch] [-w]',
27
29
  description: 'Display logs for the current Blueprint Stack',
28
- hideFromHelp: true,
30
+
29
31
  async action(args, context) {
30
32
  const {apiClient, output} = context
31
- const {print} = output
32
- // const flags = {...defaultFlags, ...args.extOptions}
33
- // const watchMode = Boolean(flags.watch)
33
+ const flags = {...defaultFlags, ...args.extOptions}
34
34
 
35
- const client = apiClient({requireUser: true, requireProject: false})
35
+ const client = apiClient({
36
+ requireUser: true,
37
+ requireProject: false,
38
+ })
36
39
  const {token} = client.config()
40
+ if (!token) throw new Error('No API token found. Please run `sanity login`.')
37
41
 
38
- if (!token) {
39
- print('No API token found. Please run `sanity login` first.')
40
- return
41
- }
42
-
43
- const {blueprint: blueprintAction, logs: logsAction} = await import(
44
- '@sanity/runtime-cli/actions/blueprints'
45
- )
42
+ const {blueprintLogsCore} = await import('@sanity/runtime-cli/cores/blueprints')
43
+ const {getBlueprintAndStack} = await import('@sanity/runtime-cli/actions/blueprints')
46
44
  const {display} = await import('@sanity/runtime-cli/utils')
47
45
 
48
- let blueprint = null
49
- try {
50
- blueprint = await blueprintAction.readBlueprintOnDisk({token})
51
- } catch (error) {
52
- print('Unable to read Blueprint manifest file. Run `sanity blueprints init`')
53
- return
54
- }
46
+ const {localBlueprint, deployedStack, issues} = await getBlueprintAndStack({token})
55
47
 
56
- if (!blueprint) {
57
- print('Unable to read Blueprint manifest file. Run `sanity blueprints init`')
58
- return
48
+ if (issues) {
49
+ output.print(display.errors.presentBlueprintIssues(issues))
50
+ throw new Error('Unable to parse Blueprint file.')
59
51
  }
60
52
 
61
- const {errors, deployedStack} = blueprint
62
-
63
- if (errors && errors.length > 0) {
64
- print(errors)
65
- return
66
- }
53
+ const {projectId, stackId} = localBlueprint
54
+ const auth = {token, projectId}
67
55
 
68
- if (!deployedStack) {
69
- print('Stack not found')
70
- return
71
- }
56
+ const {success, streaming, error} = await blueprintLogsCore({
57
+ bin: 'sanity',
58
+ log: (message) => output.print(message),
59
+ auth,
60
+ stackId,
61
+ deployedStack,
62
+ flags: {
63
+ watch: flags.watch ?? flags.w,
64
+ },
65
+ })
72
66
 
73
- const {id: stackId, projectId, name} = deployedStack
74
- const auth = {token, projectId}
67
+ if (streaming) await streaming
75
68
 
76
- print(`Fetching logs for stack ${display.colors.yellow(`<${stackId}>`)}`)
77
-
78
- // enable watch mode here
79
-
80
- try {
81
- const {ok, logs, error} = await logsAction.getLogs(stackId, auth)
82
-
83
- if (!ok) {
84
- print(`${display.colors.red('Failed')} to retrieve logs`)
85
- print(`Error: ${error || 'Unknown error'}`)
86
- return
87
- }
88
-
89
- if (logs.length === 0) {
90
- print(`No logs found for Stack ${stackId}`)
91
- return
92
- }
93
-
94
- print(`${display.blueprintsFormatting.formatTitle('Blueprint', name)} Logs`)
95
- print(
96
- `Found ${display.colors.bold(logs.length.toString())} log entries for stack ${display.colors.yellow(stackId)}\n`,
97
- )
98
-
99
- // Organize and format logs by day
100
- const logsByDay = display.logsFormatting.organizeLogsByDay(logs)
101
- print(display.logsFormatting.formatLogsByDay(logsByDay))
102
- } catch (err) {
103
- print('Failed to retrieve logs')
104
- if (err instanceof Error) {
105
- print(`Error: ${err.message}`)
106
- }
107
- }
69
+ if (!success) throw new Error(error)
108
70
  },
109
71
  }
110
72
 
@@ -3,60 +3,50 @@ import {type CliCommandDefinition} from '../../types'
3
3
  const helpText = `
4
4
  Safe to run at any time. Will not modify any Resources.
5
5
 
6
- Examples
7
- # Show deployment plan
6
+ Examples:
7
+ # Show deployment plan for the current Blueprint
8
8
  sanity blueprints plan
9
9
  `
10
10
 
11
- const planBlueprintsCommand: CliCommandDefinition = {
11
+ export interface BlueprintsPlanFlags {
12
+ //
13
+ }
14
+
15
+ const planBlueprintsCommand: CliCommandDefinition<BlueprintsPlanFlags> = {
12
16
  name: 'plan',
13
17
  group: 'blueprints',
14
18
  helpText,
15
19
  signature: '',
16
20
  description: 'Enumerate Resources to be deployed',
17
- hideFromHelp: true,
21
+
18
22
  async action(args, context) {
19
23
  const {apiClient, output} = context
20
- const {print} = output
21
24
 
22
- const client = apiClient({requireUser: true, requireProject: false})
25
+ const client = apiClient({
26
+ requireUser: true,
27
+ requireProject: false,
28
+ })
23
29
  const {token} = client.config()
24
- const {blueprint: blueprintAction} = await import('@sanity/runtime-cli/actions/blueprints')
25
- const {display} = await import('@sanity/runtime-cli/utils')
26
-
27
- let blueprint = null
28
- try {
29
- blueprint = await blueprintAction.readBlueprintOnDisk({token})
30
- } catch (error) {
31
- print('Unable to read Blueprint manifest file. Run `sanity blueprints init`')
32
- return
33
- }
30
+ if (!token) throw new Error('No API token found. Please run `sanity login`.')
34
31
 
35
- if (!blueprint) {
36
- print('Unable to read Blueprint manifest file. Run `sanity blueprints init`')
37
- return
38
- }
32
+ const {blueprintPlanCore} = await import('@sanity/runtime-cli/cores/blueprints')
33
+ const {getBlueprintAndStack} = await import('@sanity/runtime-cli/actions/blueprints')
34
+ const {display} = await import('@sanity/runtime-cli/utils')
39
35
 
40
- const {errors, projectId, stackId, parsedBlueprint, fileInfo} = blueprint
36
+ const {localBlueprint, issues} = await getBlueprintAndStack({token})
41
37
 
42
- if (errors && errors.length > 0) {
43
- print(errors)
38
+ if (issues) {
39
+ // print issues and continue
40
+ output.print(display.errors.presentBlueprintIssues(issues))
44
41
  }
45
42
 
46
- const resources = parsedBlueprint.resources || []
47
-
48
- if (!projectId) {
49
- print('Unable to determine Project ID.')
50
- print('To configure this Blueprint, run `sanity blueprints config`')
51
- // continue to show the plan
52
- }
43
+ const {success, error} = await blueprintPlanCore({
44
+ bin: 'sanity',
45
+ log: (message) => output.print(message),
46
+ blueprint: localBlueprint,
47
+ })
53
48
 
54
- const name = stackId || 'Unknown'
55
- print(`${display.blueprintsFormatting.formatTitle('Blueprint', name)} Plan\n`)
56
- print(`Blueprint document: (${fileInfo.fileName})`)
57
- print('')
58
- print(display.blueprintsFormatting.formatResourceTree(resources))
59
- print('\nRun `sanity blueprints deploy` to deploy these changes')
49
+ if (!success) throw new Error(error)
60
50
  },
61
51
  }
62
52
 
@@ -1,75 +1,67 @@
1
1
  import {type CliCommandDefinition} from '../../types'
2
2
 
3
3
  const helpText = `
4
- Examples
4
+ Options
5
+ --project-id <id> Project ID to use
6
+
7
+ Examples:
5
8
  # List all Stacks for the current Project
6
9
  sanity blueprints stacks
10
+
11
+ # List Stacks for a specific project
12
+ sanity blueprints stacks --project-id abc123
7
13
  `
8
14
 
9
- const stacksBlueprintsCommand: CliCommandDefinition = {
15
+ export interface BlueprintsStacksFlags {
16
+ 'project-id'?: string
17
+ 'projectId'?: string
18
+ 'project'?: string
19
+ }
20
+
21
+ const defaultFlags: BlueprintsStacksFlags = {
22
+ //
23
+ }
24
+
25
+ const stacksBlueprintsCommand: CliCommandDefinition<BlueprintsStacksFlags> = {
10
26
  name: 'stacks',
11
27
  group: 'blueprints',
12
28
  helpText,
13
- signature: '',
29
+ signature: '[--project-id <id>]',
14
30
  description: 'List all Blueprint Stacks for the current Project',
15
- hideFromHelp: true,
31
+
16
32
  async action(args, context) {
17
33
  const {apiClient, output} = context
18
- const {print} = output
19
- const client = apiClient({requireUser: true, requireProject: false})
20
- const {token} = client.config()
34
+ const flags = {...defaultFlags, ...args.extOptions}
21
35
 
22
- if (!token) {
23
- print('No API token found. Please run `sanity login` first.')
24
- return
25
- }
36
+ const client = apiClient({
37
+ requireUser: true,
38
+ requireProject: false,
39
+ })
40
+ const {token} = client.config()
41
+ if (!token) throw new Error('No API token found. Please run `sanity login`.')
26
42
 
27
- const {blueprint: blueprintAction, stacks: stacksAction} = await import(
28
- '@sanity/runtime-cli/actions/blueprints'
29
- )
43
+ const {blueprintStacksCore} = await import('@sanity/runtime-cli/cores/blueprints')
44
+ const {getBlueprintAndStack} = await import('@sanity/runtime-cli/actions/blueprints')
30
45
  const {display} = await import('@sanity/runtime-cli/utils')
31
46
 
32
- let blueprint = null
33
- try {
34
- blueprint = await blueprintAction.readBlueprintOnDisk({token})
35
- } catch (error) {
36
- print('Unable to read Blueprint manifest file. Run `sanity blueprints init`')
37
- return
38
- }
39
-
40
- if (!blueprint) {
41
- print('Unable to read Blueprint manifest file. Run `sanity blueprints init`')
42
- return
43
- }
44
-
45
- const {errors, projectId, stackId} = blueprint
46
-
47
- if (errors && errors.length > 0) {
48
- print(errors)
49
- return
50
- }
47
+ const {localBlueprint, issues} = await getBlueprintAndStack({token})
51
48
 
52
- if (!projectId) {
53
- print('Blueprint is not configured for a Project. Run `sanity blueprints config`')
54
- return
49
+ if (issues) {
50
+ // print issues and continue
51
+ output.print(display.errors.presentBlueprintIssues(issues))
55
52
  }
56
53
 
57
- const auth = {token, projectId}
58
- const {ok, stacks, error} = await stacksAction.listStacks(auth)
59
-
60
- if (!ok) {
61
- print(error || 'Failed to list Stacks')
62
- return
63
- }
64
-
65
- if (!stacks || stacks.length === 0) {
66
- print('No Stacks found')
67
- return
68
- }
54
+ const {success, error} = await blueprintStacksCore({
55
+ bin: 'sanity',
56
+ log: (message) => output.print(message),
57
+ token,
58
+ blueprint: localBlueprint,
59
+ flags: {
60
+ projectId: flags['project-id'] ?? flags.projectId ?? flags.project,
61
+ },
62
+ })
69
63
 
70
- const {bold, yellow} = display.colors
71
- print(`${bold('Project')} <${yellow(projectId)}> ${bold('Stacks')}:\n`)
72
- print(display.blueprintsFormatting.formatStacksListing(stacks, stackId))
64
+ if (!success) throw new Error(error)
73
65
  },
74
66
  }
75
67
 
@@ -22,9 +22,8 @@ const devFunctionsCommand: CliCommandDefinition = {
22
22
  name: 'dev',
23
23
  group: 'functions',
24
24
  helpText,
25
- signature: '',
25
+ signature: '[--port <port>]',
26
26
  description: 'Start the Sanity Function emulator',
27
- hideFromHelp: true,
28
27
  async action(args, context) {
29
28
  const {output} = context
30
29
  const {print} = output
@@ -1,48 +1,47 @@
1
- import {type types} from '@sanity/runtime-cli/utils'
2
-
3
1
  import {type CliCommandDefinition} from '../../types'
4
2
 
5
- type StackFunctionResource = types.StackFunctionResource
6
-
7
3
  const helpText = `
8
- Arguments
9
- [add] Add or update an environment variable
10
- [remove] Remove an environment variable
4
+ Commands
5
+ add Add or update an environment variable
6
+ list List the environment variables
7
+ remove Remove an environment variable
11
8
 
12
- Options
13
- --name <name> The name of the function
14
- --key <key> The name of the environment variable
15
- --value <value> The value of the environment variable
9
+ Arguments
10
+ <name> The name of the function
11
+ <key> The name of the environment variable
12
+ <value> The value of the environment variable
16
13
 
17
14
  Examples
18
15
  # Add or update an environment variable
19
- sanity functions env add --name echo --key API_URL --value https://api.example.com/
16
+ sanity functions env add echo API_URL https://api.example.com/
20
17
 
21
18
  # Remove an environment variable
22
- sanity functions env remove --name echo --key API_URL
23
- `
19
+ sanity functions env remove echo API_URL
24
20
 
25
- const defaultFlags = {
26
- name: '',
27
- key: '',
28
- value: '',
29
- }
21
+ # List environment variables
22
+ sanity functions env list echo
23
+ `
30
24
 
31
25
  const envFunctionsCommand: CliCommandDefinition = {
32
26
  name: 'env',
33
27
  group: 'functions',
34
28
  helpText,
35
- signature: '',
36
- description: 'Add or remove an environment variable for a Sanity function',
37
- hideFromHelp: true,
29
+ signature: '<add|list|remove> <name> [key] [value]',
30
+ description:
31
+ 'Add or remove an environment variable or list environment variables for a Sanity function',
38
32
  async action(args, context) {
39
33
  const {apiClient, output} = context
40
34
  const {print} = output
41
- const [subCommand] = args.argsWithoutOptions
42
- const flags = {...defaultFlags, ...args.extOptions}
35
+ const [subCommand, name, key, value] = args.argsWithoutOptions
36
+
37
+ if (!subCommand || !['add', 'list', 'remove'].includes(subCommand)) {
38
+ throw new Error('You must specify if you want to list, add or remove')
39
+ }
43
40
 
44
- if (!subCommand || !['add', 'remove'].includes(subCommand)) {
45
- throw new Error('You must specify if you wish to add or remove an environment variable')
41
+ if (subCommand === 'add' && (!key || !value)) {
42
+ throw new Error('You must specify the name, key and value arguments')
43
+ } else if (subCommand === 'remove' && !key) {
44
+ throw new Error('You must specify the name and key arguments')
46
45
  }
47
46
 
48
47
  const client = apiClient({
@@ -50,19 +49,18 @@ const envFunctionsCommand: CliCommandDefinition = {
50
49
  requireProject: false,
51
50
  })
52
51
 
53
- if (flags.name === '') {
54
- throw new Error('You must provide a function name via the --name flag')
52
+ if (name === '') {
53
+ throw new Error('You must provide a function name as the first argument')
55
54
  }
56
55
 
57
56
  const token = client.config().token
58
- const {env} = await import('@sanity/runtime-cli/actions/functions')
59
- const {blueprint} = await import('@sanity/runtime-cli/actions/blueprints')
57
+ if (!token) throw new Error('No API token found. Please run `sanity login`.')
58
+
59
+ const {env: envAction} = await import('@sanity/runtime-cli/actions/functions')
60
+ const {blueprint, getBlueprintAndStack} = await import('@sanity/runtime-cli/actions/blueprints')
60
61
  const {findFunction} = await import('@sanity/runtime-cli/utils')
61
62
 
62
- const {deployedStack} = await blueprint.readBlueprintOnDisk({
63
- getStack: true,
64
- token,
65
- })
63
+ const {deployedStack} = await getBlueprintAndStack({token})
66
64
 
67
65
  if (!deployedStack) {
68
66
  throw new Error('Stack not found')
@@ -71,34 +69,45 @@ const envFunctionsCommand: CliCommandDefinition = {
71
69
  const blueprintConfig = blueprint.readConfigFile()
72
70
  const projectId = blueprintConfig?.projectId
73
71
 
74
- const {externalId} = findFunction.findFunctionByName(
75
- deployedStack,
76
- flags.name,
77
- ) as StackFunctionResource
72
+ const {externalId} = findFunction.findFunctionByName(deployedStack, name)
78
73
 
79
74
  if (token && projectId) {
80
75
  if (subCommand === 'add') {
81
- print(`Updating "${flags.key}" environment variable in "${flags.name}"`)
82
- const result = await env.update.update(externalId, flags.key, flags.value, {
76
+ print(`Updating "${key}" environment variable in "${name}"`)
77
+ const result = await envAction.update(externalId, key, value, {
83
78
  token,
84
79
  projectId,
85
80
  })
86
81
  if (result.ok) {
87
- print(`Update of ${flags.key} succeeded`)
82
+ print(`Update of "${key}" succeeded`)
88
83
  } else {
89
- print(`Failed to update ${flags.key}`)
84
+ print(`Failed to update "${key}"`)
90
85
  print(`Error: ${result.error || 'Unknown error'}`)
91
86
  }
92
87
  } else if (subCommand === 'remove') {
93
- print(`Removing "${flags.key}" environment variable in "${flags.name}"`)
94
- const result = await env.remove.remove(externalId, flags.key, {
88
+ print(`Removing "${key}" environment variable in "${name}"`)
89
+ const result = await envAction.remove(externalId, key, {
95
90
  token,
96
91
  projectId,
97
92
  })
98
93
  if (result.ok) {
99
- print(`Remove of ${flags.key} succeeded`)
94
+ print(`Removal of "${key}" succeeded`)
95
+ } else {
96
+ print(`Failed to remove "${key}"`)
97
+ print(`Error: ${result.error || 'Unknown error'}`)
98
+ }
99
+ } else if (subCommand === 'list') {
100
+ print(`Environment variables in "${name}"`)
101
+ const result = await envAction.list(externalId, {
102
+ token,
103
+ projectId,
104
+ })
105
+ if (result.ok && Array.isArray(result.envvars)) {
106
+ for (const envVarKey of result.envvars) {
107
+ print(envVarKey)
108
+ }
100
109
  } else {
101
- print(`Failed to remove ${flags.key}`)
110
+ print(`Failed to list environment variables in "${key}"`)
102
111
  print(`Error: ${result.error || 'Unknown error'}`)
103
112
  }
104
113
  }
@@ -4,8 +4,7 @@ const functionsGroup: CliCommandGroupDefinition = {
4
4
  name: 'functions',
5
5
  signature: '[COMMAND]',
6
6
  isGroupRoot: true,
7
- description: 'Test Sanity Functions locally and retrieve logs',
8
- hideFromHelp: true,
7
+ description: 'Test Sanity Functions locally, update environment variables and retrieve logs',
9
8
  }
10
9
 
11
10
  export default functionsGroup