-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
170 lines (139 loc) · 3.92 KB
/
Copy pathindex.js
File metadata and controls
170 lines (139 loc) · 3.92 KB
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
const inquirer = require('inquirer');
const mustache = require('mustache');
const fs = require('fs');
const path = require('path');
const colors = require('colors/safe');
const cliSpinners = require('cli-spinners');
const ora = require('ora');
const {promisify} = require('util');
const exec = promisify(require('child_process').exec);
// available network on the graph
const availableNetworks = [
'mainnet',
'kovan',
'rinkeby',
'ropsten',
'goerli',
'poa-core',
'poa-sokol',
'xdai',
'matic',
'mumbai',
'fantom',
'bsc',
'chapel',
'clover',
'avalanche',
'fuji',
'celo',
'celo-alfajores',
'fuse',
'mbase',
'arbitrum-one',
'arbitrum-rinkeby',
'optimism',
'optimism-kovan'
]
const DEFAULT_NETWORKS = ['mainnet', 'bsc', 'matic'];
const TEMPLATE_PATH = path.join(process.cwd(), './templates/subgraph.template.yaml');
const NETWORK_PATH = path.join(process.cwd(), './networks');
const render = (name, block = null, network = null) => {
let view = '';
let info = {};
if (name) {
const tpl = TEMPLATE_PATH;
const data = `${NETWORK_PATH}/${name}.json`;
view = fs.readFileSync(tpl);
info = require(data);
} else {
view = fs.readFileSync(TEMPLATE_PATH);
}
if (block) {
info = {...info, ...{StartBlock: block}}
}
if (network) {
info = {...info, ...{network: network}}
}
return mustache.render(view.toString(), info);
};
const MAINNET = 'mainnet';
const PREPARE_CMD = 'prepare'
const DIALOG_SETTINGS = []
const deploy = async (settings = {}) => {
const availableDestination = [...availableNetworks];
const {
destination = [],
showOnlyNetworks = []
} = settings;
if (destination && destination.length) {
availableDestination.push(...destination)
}
const choices = [...new Set(availableDestination)]
.map(destination => ({name: destination}))
const dialogStructure = {
type: 'list',
message: 'Destination: ',
name: 'destination',
choices: [
{
name: 'prepare',
},
...(showOnlyNetworks.length ? choices.filter(d => showOnlyNetworks.includes(d.name)) : choices),
{
name: 'cancel'
}
],
}
DIALOG_SETTINGS.push(dialogStructure);
const answers = await inquirer.prompt(DIALOG_SETTINGS);
let content = '';
let prepareOnly = false;
const currentChoice = answers.destination;
if (availableDestination.includes(currentChoice)) {
content = render(answers.destination);
} else if (currentChoice === PREPARE_CMD) {
prepareOnly = true;
content = render(MAINNET);
} else {
console.log('Good bay...')
return;
}
console.log(colors.green(`\nRendering config for ${answers.destination}.`));
fs.writeFileSync('subgraph.yaml', content);
console.log(colors.green(`Completed...`));
if (prepareOnly) {
console.log(colors.green(`Selected prepareOnly = ${prepareOnly} options`));
return;
}
console.log(colors.green(`Start deploy to graph.\n`));
const spinner = ora({text: 'Deploying...', spinner: cliSpinners.aesthetic}).start();
let msg = '';
try {
const res = await exec(`graph codegen`);
console.log(res.stdout);
} catch (e) {
msg = colors.red(`Error ${e.message}`);
spinner.stopAndPersist({
text: msg
});
return
}
try {
const res2 = await exec(`npm run deploy:${currentChoice}`);
console.log(res2.stdout);
} catch (e) {
msg = colors.red(`Error ${e.message}`);
spinner.stopAndPersist({
text: msg
});
return
}
msg = colors.green(`Deployed successfully.`);
spinner.stopAndPersist({
text: msg
});
}
module.exports = {
deploy,
DEFAULT_NETWORKS
}