-
Notifications
You must be signed in to change notification settings - Fork 1
/
templatify.ts
55 lines (46 loc) · 1.49 KB
/
templatify.ts
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
import yaml = require('js-yaml');
import Handlebars = require('handlebars');
import fs = require('fs-extra');
import path = require('path');
Handlebars.registerHelper('lowercase', function (str) {
if (str && typeof str === 'string') {
return str.toLowerCase();
}
return '';
});
function getNetworkNameForSubgraph(): string | null {
switch (process.env.SUBGRAPH) {
case 'ORGANISATION/SUBGRAPH':
return 'mainnet';
case 'ORGANISATION/SUBGRAPH-GOERLI':
return 'goerli';
default:
return null;
}
}
(async (): Promise<void> => {
if (fs.existsSync('subgraph.yaml')) fs.rmSync(`subgraph.yaml`);
const networksFilePath = path.join(__dirname, 'networks.yaml');
const networks = yaml.load(
await fs.readFile(networksFilePath, { encoding: 'utf-8' }),
);
const networkName = process.env.NETWORK_NAME || getNetworkNameForSubgraph();
const network = { ...networks[networkName || ''], networkName };
if (!networkName) {
process.exitCode = 1;
throw new Error(
'Please set either a "NETWORK_NAME" or a "SUBGRAPH" environment variable',
);
}
const templateFiles: [string, string][] = [['subgraph', 'yaml']];
templateFiles.forEach((templateFile) => {
const template = fs
.readFileSync(`${templateFile[0]}.template.${templateFile[1]}`)
.toString();
fs.writeFileSync(
`${templateFile[0]}.${templateFile[1]}`,
Handlebars.compile(template)(network),
);
});
console.log('🎉 subgraph successfully generated\n');
})();