-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathgraph-deploy.js
99 lines (90 loc) · 2.86 KB
/
graph-deploy.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
const path = require('path')
const { runGraphCli, subgraphLocation: defaultSubgraphLocation} = require('./graph-cli.js')
const { graphNode, ipfsNode, subgraphName: defaultSubgraphName } = require('./settings')
const cwd = path.resolve(`${__dirname}/..`)
function extractSubgraphId(output) {
return output
.trim()
.split('\n')
.reverse()
.find(line => line.match(/(Build completed|Subgraph): Qm/))
.split(':')
.slice(-1)[0]
.trim()
}
async function deploy (opts = {}) {
if (!opts.subgraphLocation) {
opts.subgraphLocation = defaultSubgraphLocation
}
if (!opts.subgraphName) {
opts.subgraphName = defaultSubgraphName
}
let result
let msg
/* create the subgraph */
if (graphNode !== 'https://api.thegraph.com/deploy/' && graphNode !== 'https://api.staging.thegraph.com/deploy/') {
result = await runGraphCli([
'create',
'--access-token ""',
'--node ' + graphNode,
opts.subgraphName
], cwd)
msg = result[1] + result[2]
if (msg.toLowerCase().indexOf('error') > 0) {
if (msg.match(/subgraph already exists/)) {
// the subgraph was already created before -we're ok
console.log('subgraph already exists - deploying a new version')
} else {
console.error(`Create failed! ${msg}`)
}
}
}
/* upload subgraph files to the shared IPFS node */
let builtSubgraphId
if (graphNode.match(/thegraph\.com/)) {
let sharedIpfsNode = graphNode.match(/staging/)
? 'https://api.staging.thegraph.com/ipfs/'
: 'https://api.thegraph.com/ipfs/'
result = await runGraphCli(['build', '--ipfs', sharedIpfsNode, opts.subgraphLocation])
msg = result[1] + result[2]
if (result[0] === 1 || result[0] === 127) {
throw Error(`Failed to upload subgraph files to ${sharedIpfsNode}: ${msg}`)
}
builtSubgraphId = extractSubgraphId(result[1])
console.log(`Uploaded subgraph: ${builtSubgraphId}`)
}
result = await runGraphCli(
[
'deploy',
'--access-token "${access_token-""}"',
'--ipfs ' + ipfsNode,
'--node ' + graphNode,
opts.subgraphName,
opts.subgraphLocation,
],
cwd
)
msg = result[1] + result[2]
if (result[0] === 1 || result[0] === 127) {
throw Error(`Deployment failed! ${msg}`)
}
if (msg.toLowerCase().indexOf('error') > 0) {
throw Error(`Deployment failed! ${msg}`)
}
if (graphNode.match(/thegraph\.com/)) {
let deployedSubgraphId = extractSubgraphId(result[1])
console.log(`Deployed subgraph: ${deployedSubgraphId}`)
if (builtSubgraphId !== deployedSubgraphId) {
console.error(
`Subgraph ID mismatch between 'build' and 'deploy':`,
`${builtSubgraphId} !== ${deployedSubgraphId}`
)
}
}
return result
}
if (require.main === module) {
deploy().catch((err) => { console.log(err); process.exit(1) })
} else {
module.exports = deploy
}