Skip to content

Commit f09eac7

Browse files
committedFeb 21, 2018
feat: server-only will overrule all other playground command args
1 parent f62adbb commit f09eac7

File tree

1 file changed

+36
-23
lines changed

1 file changed

+36
-23
lines changed
 

Diff for: ‎src/cmds/playground.ts

+36-23
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ export const builder = {
2626
describe: 'Open web version (even if desktop app available)',
2727
type: 'boolean',
2828
},
29-
serverOnly: {
29+
'server-only': {
3030
describe: 'Run only server',
3131
type: 'boolean',
32-
'default': 'false'
32+
'default': false
3333
}
3434
}
3535

@@ -42,36 +42,27 @@ function randomString(len = 32) {
4242
.replace(/\//g, '0')
4343
}
4444

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) => {
5647
const app = express()
57-
5848
const config = await context.getConfig()
5949
const projects = config.getProjects()
6050

6151
if (projects === undefined) {
6252
const projectConfig = await context.getProjectConfig()
53+
6354
if (!projectConfig.endpointsExtension) {
6455
throw noEndpointError
6556
}
66-
const endpoint = projectConfig.endpointsExtension.getEndpoint(
67-
argv.endpoint,
57+
const { url, headers } = projectConfig.endpointsExtension.getEndpoint(
58+
endpoint,
6859
)
6960

7061
app.use(
7162
'/graphql',
7263
requestProxy({
73-
url: endpoint.url,
74-
headers: endpoint.headers,
64+
url,
65+
headers,
7566
}),
7667
)
7768

@@ -89,18 +80,40 @@ export async function handler(
8980
)
9081
}
9182

92-
const port = argv.port || 3000
93-
9483
const listener = app.listen(port, () => {
9584
let host = listener.address().address
9685
if (host === '::') {
9786
host = 'localhost'
9887
}
9988
const link = `http://${host}:${port}/playground`
10089
console.log('Serving playground at %s', chalk.blue(link))
101-
if (!argv.serverOnly) {
102-
opn(link)
103-
}
90+
91+
resolve(link)
10492
})
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 })
105118
}
106119
}

0 commit comments

Comments
 (0)
Please sign in to comment.