@@ -26,10 +26,10 @@ export const builder = {
26
26
describe : 'Open web version (even if desktop app available)' ,
27
27
type : 'boolean' ,
28
28
} ,
29
- serverOnly : {
29
+ 'server-only' : {
30
30
describe : 'Run only server' ,
31
31
type : 'boolean' ,
32
- 'default' : ' false'
32
+ 'default' : false
33
33
}
34
34
}
35
35
@@ -42,36 +42,27 @@ function randomString(len = 32) {
42
42
. replace ( / \/ / g, '0' )
43
43
}
44
44
45
- export async function handler (
46
- context : Context ,
47
- argv : { endpoint : string ; port : string ; web : boolean , serverOnly : boolean } ,
48
- ) {
49
- const localPlaygroundPath = `/Applications/GraphQL\ Playground.app/Contents/MacOS/GraphQL\ Playground`
50
- if ( fs . existsSync ( localPlaygroundPath ) && ! argv . web ) {
51
- const envPath = path . join ( os . tmpdir ( ) , `${ randomString ( ) } .json` )
52
- fs . writeFileSync ( envPath , JSON . stringify ( process . env ) )
53
- const url = `graphql-playground://?cwd=${ process . cwd ( ) } &envPath=${ envPath } `
54
- opn ( url , { wait : false } )
55
- } else {
45
+ const startServer = async ( { context, endpoint, port = 3000 } : { context : Context , endpoint : string , port : string } ) =>
46
+ new Promise < string > ( async ( resolve , reject ) => {
56
47
const app = express ( )
57
-
58
48
const config = await context . getConfig ( )
59
49
const projects = config . getProjects ( )
60
50
61
51
if ( projects === undefined ) {
62
52
const projectConfig = await context . getProjectConfig ( )
53
+
63
54
if ( ! projectConfig . endpointsExtension ) {
64
55
throw noEndpointError
65
56
}
66
- const endpoint = projectConfig . endpointsExtension . getEndpoint (
67
- argv . endpoint ,
57
+ const { url , headers } = projectConfig . endpointsExtension . getEndpoint (
58
+ endpoint ,
68
59
)
69
60
70
61
app . use (
71
62
'/graphql' ,
72
63
requestProxy ( {
73
- url : endpoint . url ,
74
- headers : endpoint . headers ,
64
+ url,
65
+ headers,
75
66
} ) ,
76
67
)
77
68
@@ -89,18 +80,40 @@ export async function handler(
89
80
)
90
81
}
91
82
92
- const port = argv . port || 3000
93
-
94
83
const listener = app . listen ( port , ( ) => {
95
84
let host = listener . address ( ) . address
96
85
if ( host === '::' ) {
97
86
host = 'localhost'
98
87
}
99
88
const link = `http://${ host } :${ port } /playground`
100
89
console . log ( 'Serving playground at %s' , chalk . blue ( link ) )
101
- if ( ! argv . serverOnly ) {
102
- opn ( link )
103
- }
90
+
91
+ resolve ( link )
104
92
} )
93
+ } )
94
+
95
+ export async function handler (
96
+ context : Context ,
97
+ argv : { endpoint : string ; port : string ; web : boolean , serverOnly : boolean } ,
98
+ ) {
99
+ const localPlaygroundPath = `/Applications/GraphQL\ Playground.app/Contents/MacOS/GraphQL\ Playground`
100
+
101
+ const isLocalPlaygroundAvailable = fs . existsSync ( localPlaygroundPath )
102
+
103
+ const shouldStartServer = argv . serverOnly || argv . web || ! isLocalPlaygroundAvailable
104
+
105
+ const shouldOpenBrowser = ! argv . serverOnly
106
+
107
+ if ( shouldStartServer ) {
108
+ const link = await startServer ( { context, endpoint : argv . endpoint , port : argv . port } )
109
+
110
+ if ( shouldOpenBrowser ) {
111
+ opn ( link )
112
+ }
113
+ } else {
114
+ const envPath = path . join ( os . tmpdir ( ) , `${ randomString ( ) } .json` )
115
+ fs . writeFileSync ( envPath , JSON . stringify ( process . env ) )
116
+ const url = `graphql-playground://?cwd=${ process . cwd ( ) } &envPath=${ envPath } `
117
+ opn ( url , { wait : false } )
105
118
}
106
119
}
0 commit comments