/
thegraph
181 lines (166 loc) · 4.26 KB
/
thegraph
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env node
/**
* Interact with the graph
*
* Usage: node bin/thegraph --help
*/
const { hideBin } = require('yargs/helpers')
const networks = require('@unlock-protocol/networks')
const util = require('util')
const exec = util.promisify(require('child_process').exec)
const { networkName } = require('./networks')
const executeCommand = async (command) => {
try {
const { stdout, stderr } = await exec(command)
console.log('stdout:', stdout)
if (stderr) {
console.log(`stderr: ${stderr}`)
return stderr
}
} catch (e) {
// should contain code (exit code) and signal (that caused the termination).
console.error(`exec error: ${e}`)
}
}
// use shorthand of git sha commit as version label
const getVersion = () => {
const gitSha = require('child_process')
.execSync('git rev-parse --short HEAD')
.toString()
.trim()
return `${gitSha}`
}
const executeStudioDeploy = async ({
subgraphName,
version,
flags = '--product=subgraph-studio',
}) => {
const cmd = `graph deploy --version-label="${version}" ${flags} ${subgraphName}`
let stderr = await executeCommand(cmd)
if (stderr) {
if (stderr.includes(`Version label already exists`)) {
console.log(
`Studio version '${version}' for ${subgraphName} exists - skipping`
)
}
}
}
const checkStudioAccessToken = () => {
if (!process.env.SUBGRAPH_STUDIO_DEPLOY_KEY) {
throw new Error(
`
Missing access token in SUBGRAPH_STUDIO_DEPLOY_KEY env.
You can get a token from https://thegraph.com/studio/
`
)
}
}
// Generate subgraph manifest
const build = async (network) => {
console.log(`Building subgraph for ${network} (${networkName(network)})`)
await executeCommand(`yarn generate-manifest ${networkName(network)}.json`)
await executeCommand(`graph codegen`)
}
/**
* Deploys subgraph with studio
* @param version
* @param subgraphName
* @returns
*/
const deployStudio = async (subgraphName, version) => {
checkStudioAccessToken()
await executeCommand(
`graph auth --studio ${process.env.SUBGRAPH_STUDIO_DEPLOY_KEY}`
)
if (!version) {
version = getVersion()
}
return executeStudioDeploy({ subgraphName, version })
}
/**
* deploy command
* @param network
* @param studioName
* @param graphLabel
* @returns
*/
const deploy = async (network, studioName, graphLabel) => {
if (!network) {
throw new Error('Missing network name')
}
if (!networks[network]?.subgraph?.endpoint) {
// TODO: create subgraph automatically if necessary
}
if (!studioName) {
;({ studioName } = networks[network].subgraph)
}
if (!studioName) {
throw new Error(
`Missing name for studio graph
(add it as 'studioName' to the '@unlock-protocol/networks' package
or use the '--studioName' flag)`
)
} else {
return deployStudio(studioName, graphLabel)
}
}
const choices = Object.keys(networks).filter(
(d) => !['networks', 'default', 'localhost'].includes(d)
)
require('yargs/yargs')(hideBin(process.argv))
.command(
'build <network>',
'build thegraph manifest and files',
(yargs) =>
yargs.positional('network', {
describe: 'the network to deploy',
choices,
}),
async ({ network }) => {
await build(network)
}
)
.command(
'deploy <network>',
'deploy subgraph to thegraph studio',
(yargs) =>
yargs.positional('network', {
describe: 'the network to deploy',
choices,
}),
async ({ network, studioName, label }) => {
await build(network)
await deploy(network, studioName, label)
}
)
.command(
'deploy-all',
'Deploy subgraph on all networks',
{},
async ({ label }) => {
// check config
console.log(
`Deploying subgraphs for:\n${choices.map((c) => ` - ${c}\n`).join('')}`
)
// build and deploy each network
for (const network of choices) {
await build(network)
await deploy(network, label)
}
console.log('Subgraph deployments done.')
}
)
.demandCommand()
.options({
label: {
type: 'string',
description: 'The version label to use for deployment',
},
})
.options({
studioName: {
type: 'string',
description: 'The name of the studio deployment',
},
})
.parse()