forked from hellocoop/packages-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
87 lines (73 loc) · 2.53 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import open from 'open'
import getPort from 'get-port'
import readline from 'readline'
import * as http from 'http'
import { URLSearchParams, parse } from 'url';
import page from './page.js'
export const validQuickstartParams = [
'suffix',
'name',
'tos_uri',
'pp_uri',
'image_uri',
'dark_image_uri',
'redirect_uri',
'integration',
'wildcard_domain',
'provider_hint',
]
const quickstart = async function (params) {
return new Promise(async (resolve) => {
if (!process.stdout.isTTY) {
const error = new Error('Not running on interactive terminal. Exiting Quickstart CLI')
console.error(error)
return error
}
const paramKeys = Object.keys(params || {})
if (paramKeys) {
paramKeys.forEach( param => {
if (!validQuickstartParams.includes(param))
throw(new Error(`Invalid param:${param}`))
})
}
const port = await getPort()
const host = 'localhost'
const server = http.createServer((req, res) => {
const u = parse(req.url,true)
if (u.pathname != '/') {
res.writeHead(200);
return res.end('ok')
}
res.writeHead(200);
// TBD - check for error response from quickstart.hello.coop
const clientHTML = page(u.query.client_id)
res.end(clientHTML, () => {
server.closeAllConnections()
server.close(() => {
resolve(u.query.client_id)
})
})
})
const response_uri = `http://${host}:${port}/`
const queryParams = {
... params,
response_uri,
}
const queryString = new URLSearchParams(queryParams).toString();
const hellooDomain = process.env.HELLO_DOMAIN || 'hello.coop'
const quickstartURL = `https://quickstart.${hellooDomain}/?${queryString}`
server.listen(port, host, () => {
console.log('Obtaining a client_id from Hellō Quickstart using:')
console.log(quickstartURL)
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Press ENTER to open in the browser...', (answer) => {
open(quickstartURL)
rl.close();
});
})
})
}
export default quickstart;