-
Notifications
You must be signed in to change notification settings - Fork 340
/
graph-init.js
151 lines (129 loc) · 4.05 KB
/
graph-init.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// @ts-check
const { Buffer } = require('buffer')
const { OneGraphClient } = require('netlify-onegraph-internal')
const { v4: uuidv4 } = require('uuid')
const { OneGraphCliClient, ensureCLISession } = require('../../lib/one-graph/cli-client')
const { getNetlifyGraphConfig } = require('../../lib/one-graph/cli-netlify-graph')
const { NETLIFYDEVERR, chalk, error, exit, getToken, log } = require('../../utils')
const { msg } = require('../login/login')
const { ensureAppForSite, executeCreateApiTokenMutation } = OneGraphCliClient
/**
* Creates the `netlify graph:init` command
* @param {import('commander').OptionValues} options
* @param {import('../base-command').BaseCommand} command
* @returns
*/
const graphInit = async (options, command) => {
const { api, site, state } = command.netlify
const siteId = site.id
if (!siteId) {
error(
`${NETLIFYDEVERR} Warning: no siteId defined, unable to start Netlify Graph. To enable, run ${chalk.yellow(
'netlify init',
)} or ${chalk.yellow('netlify link')}`,
)
}
let [netlifyToken, loginLocation] = await getToken()
if (!netlifyToken) {
netlifyToken = await command.authenticate()
}
let siteData = null
try {
// @ts-ignore: we need better types for our api object
siteData = await api.getSite({ siteId })
} catch (error_) {
if (netlifyToken && error_.status === 401) {
log(`Already logged in ${msg(loginLocation)}`)
log()
log(`Run ${chalk.cyanBright('netlify status')} for account details`)
log()
log(`or run ${chalk.cyanBright('netlify switch')} to switch accounts`)
log()
log(`To see all available commands run: ${chalk.cyanBright('netlify help')}`)
log()
return exit()
}
throw error_
}
if (netlifyToken == null) {
error(
`${NETLIFYDEVERR} Error: Unable to start Netlify Graph without a login. To enable, run ${chalk.yellow(
'netlify login',
)} first`,
)
return exit()
}
await ensureAppForSite(netlifyToken, siteId)
const netlifyGraphConfig = await getNetlifyGraphConfig({ command, options })
await ensureCLISession({
metadata: {},
netlifyToken,
site,
state,
netlifyGraphConfig,
})
let envChanged = false
// Get current environment variables set in the UI
const {
build_settings: { env = {} },
} = siteData
const newEnv = {
...env,
}
if (!env.NETLIFY_GRAPH_WEBHOOK_SECRET) {
envChanged = true
const secret = Buffer.from(uuidv4()).toString('base64')
newEnv.NETLIFY_GRAPH_WEBHOOK_SECRET = secret
}
if (!env.NETLIFY_GRAPH_PERSIST_QUERY_TOKEN) {
const variables = {
input: {
appId: siteId,
scopes: ['MODIFY_SCHEMA', 'PERSIST_QUERY'],
},
}
const { jwt } = await OneGraphClient.getGraphJwtForSite({ siteId, nfToken: netlifyToken })
const result = await executeCreateApiTokenMutation(variables, {
siteId,
accessToken: jwt,
})
const token =
result.data &&
result.data.oneGraph &&
result.data.oneGraph.createApiToken &&
result.data.oneGraph.createApiToken.accessToken &&
result.data.oneGraph.createApiToken.accessToken.token
if (token) {
envChanged = true
newEnv.NETLIFY_GRAPH_PERSIST_QUERY_TOKEN = token
} else {
error(`Unable to create Netlify Graph persist query token: ${JSON.stringify(result.errors, null, 2)}`)
}
}
if (envChanged) {
// Apply environment variable updates
// @ts-ignore
await api.updateSite({
siteId,
body: {
build_settings: {
env: newEnv,
},
},
})
log(`Finished updating Graph-related environment variables for site ${siteData.name}`)
}
}
/**
* Creates the `netlify graph:init` command
* @param {import('../base-command').BaseCommand} program
* @returns
*/
const createGraphInitCommand = (program) =>
program
.command('graph:init')
.description('Initialize all the resources for Netlify Graph')
.action(async (options, command) => {
await graphInit(options, command)
})
module.exports = { createGraphInitCommand }