@@ -6,6 +6,10 @@ import {
6
6
type CliConfig ,
7
7
type CliOutputter ,
8
8
} from '@sanity/cli'
9
+ import { type SanityProject } from '@sanity/client'
10
+ import chalk from 'chalk'
11
+ import { hideBin } from 'yargs/helpers'
12
+ import yargs from 'yargs/yargs'
9
13
10
14
import { type DevServerOptions , startDevServer } from '../../server/devServer'
11
15
import { checkRequiredDependencies } from '../../util/checkRequiredDependencies'
@@ -14,17 +18,48 @@ import {getSharedServerConfig, gracefulServerDeath} from '../../util/servers'
14
18
import { getTimer } from '../../util/timing'
15
19
16
20
export interface StartDevServerCommandFlags {
17
- host ?: string
18
- port ?: string
21
+ 'host' ?: string
22
+ 'port' ?: string
23
+ 'load-in-dashboard' ?: boolean
24
+ 'force' ?: boolean
25
+ }
26
+
27
+ export const getCoreURL = ( ) : string => {
28
+ return process . env . SANITY_INTERNAL_ENV === 'staging'
29
+ ? 'https://core.sanity.work'
30
+ : 'https://core.sanity.io'
31
+ }
32
+
33
+ export const getCoreAppURL = ( {
34
+ organizationId,
35
+ httpHost = 'localhost' ,
36
+ httpPort = 3333 ,
37
+ } : {
38
+ organizationId : string
39
+ httpHost ?: string
40
+ httpPort ?: number
41
+ } ) : string => {
42
+ // <core-app-url>/<orgniazationId>?dev=<dev-server-url>
43
+ return `${ getCoreURL ( ) } /@${ organizationId } ?dev=http://${ httpHost } :${ httpPort } `
44
+ }
45
+
46
+ function parseCliFlags ( args : { argv ?: string [ ] } ) {
47
+ // Using slice(1) to remove the first argument, which is the command `dev` path to the CLI
48
+ return yargs ( hideBin ( args . argv || process . argv ) . slice ( 1 ) )
49
+ . options ( 'host' , { type : 'string' , default : 'localhost' } )
50
+ . options ( 'port' , { type : 'number' , default : 3333 } )
51
+ . option ( 'load-in-dashboard' , { type : 'boolean' , default : false } ) . argv
19
52
}
20
53
21
54
export default async function startSanityDevServer (
22
55
args : CliCommandArguments < StartDevServerCommandFlags > ,
23
56
context : CliCommandContext ,
24
57
) : Promise < void > {
25
58
const timers = getTimer ( )
26
- const flags = args . extOptions
27
- const { output, workDir, cliConfig} = context
59
+ const flags = await parseCliFlags ( args )
60
+ const { output, apiClient, workDir, cliConfig} = context
61
+
62
+ const { loadInDashboard} = flags
28
63
29
64
timers . start ( 'checkStudioDependencyVersions' )
30
65
checkStudioDependencyVersions ( workDir )
@@ -39,8 +74,54 @@ export default async function startSanityDevServer(
39
74
// Try to load CLI configuration from sanity.cli.(js|ts)
40
75
const config = getDevServerConfig ( { flags, workDir, cliConfig, output} )
41
76
77
+ const projectId = cliConfig ?. api ?. projectId
78
+ let organizationId : string | undefined | null
79
+
80
+ if ( loadInDashboard ) {
81
+ if ( ! projectId ) {
82
+ output . error ( 'Project Id is required to load in dashboard' )
83
+ process . exit ( 1 )
84
+ }
85
+
86
+ const client = apiClient ( {
87
+ requireUser : true ,
88
+ requireProject : true ,
89
+ } )
90
+
91
+ try {
92
+ const project = await client . request < SanityProject > ( { uri : `/projects/${ projectId } ` } )
93
+ organizationId = project . organizationId
94
+ } catch ( err ) {
95
+ output . error ( 'Failed to get organization Id from project Id' )
96
+ process . exit ( 1 )
97
+ }
98
+ }
99
+
42
100
try {
43
- await startDevServer ( config )
101
+ const spinner = output . spinner ( 'Starting dev server' ) . start ( )
102
+ await startDevServer ( { ...config , skipStartLog : loadInDashboard } )
103
+ spinner . succeed ( )
104
+
105
+ if ( loadInDashboard ) {
106
+ if ( ! organizationId ) {
107
+ output . error ( 'Organization Id not found for project' )
108
+ process . exit ( 1 )
109
+ }
110
+
111
+ output . print ( `Dev server started on ${ config . httpPort } port` )
112
+ output . print ( `View your app in the Sanity dashboard here:` )
113
+ output . print (
114
+ chalk . blue (
115
+ chalk . underline (
116
+ getCoreAppURL ( {
117
+ organizationId,
118
+ httpHost : config . httpHost ,
119
+ httpPort : config . httpPort ,
120
+ } ) ,
121
+ ) ,
122
+ ) ,
123
+ )
124
+ }
44
125
} catch ( err ) {
45
126
gracefulServerDeath ( 'dev' , config . httpHost , config . httpPort , err )
46
127
}
@@ -52,13 +133,20 @@ export function getDevServerConfig({
52
133
cliConfig,
53
134
output,
54
135
} : {
55
- flags : StartDevServerCommandFlags
136
+ flags : Awaited < ReturnType < typeof parseCliFlags > >
56
137
workDir : string
57
138
cliConfig ?: CliConfig
58
139
output : CliOutputter
59
140
} ) : DevServerOptions {
60
141
const configSpinner = output . spinner ( 'Checking configuration files...' )
61
- const baseConfig = getSharedServerConfig ( { flags, workDir, cliConfig} )
142
+ const baseConfig = getSharedServerConfig ( {
143
+ flags : {
144
+ host : flags . host ,
145
+ port : flags . port ,
146
+ } ,
147
+ workDir,
148
+ cliConfig,
149
+ } )
62
150
configSpinner . succeed ( )
63
151
64
152
const env = process . env // eslint-disable-line no-process-env
0 commit comments