@graphprotocol/graph-cli 0.37.3-alpha-20230111134832-100975c → 0.37.3-alpha-20230111150012-96ec4db
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/dist/command-helpers/compiler.js +3 -3
- package/dist/compiler/index.js +33 -21
- package/package.json +1 -1
- package/src/command-helpers/{compiler.js → compiler.ts} +24 -7
- package/src/command-helpers/ipfs-http-client.d.ts +1 -0
- package/src/compiler/{index.js → index.ts} +110 -61
package/CHANGELOG.md
CHANGED
|
@@ -29,10 +29,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
29
29
|
exports.createCompiler = void 0;
|
|
30
30
|
const URL = require('url').URL;
|
|
31
31
|
const ipfs_http_client_1 = __importDefault(require("ipfs-http-client"));
|
|
32
|
-
const toolbox = __importStar(require("gluegun
|
|
32
|
+
const toolbox = __importStar(require("gluegun"));
|
|
33
33
|
const compiler_1 = __importDefault(require("../compiler"));
|
|
34
34
|
// Helper function to construct a subgraph compiler
|
|
35
|
-
|
|
35
|
+
function createCompiler(manifest, { ipfs, headers, outputDir, outputFormat, skipMigrations, blockIpfsMethods, protocol, }) {
|
|
36
36
|
// Parse the IPFS URL
|
|
37
37
|
let url;
|
|
38
38
|
try {
|
|
@@ -62,5 +62,5 @@ The IPFS URL must be of the following format: http(s)://host[:port]/[path]`);
|
|
|
62
62
|
blockIpfsMethods,
|
|
63
63
|
protocol,
|
|
64
64
|
});
|
|
65
|
-
}
|
|
65
|
+
}
|
|
66
66
|
exports.createCompiler = createCompiler;
|
package/dist/compiler/index.js
CHANGED
|
@@ -32,17 +32,25 @@ const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
|
32
32
|
const immutable_1 = __importDefault(require("immutable"));
|
|
33
33
|
const path_1 = __importDefault(require("path"));
|
|
34
34
|
const js_yaml_1 = __importDefault(require("js-yaml"));
|
|
35
|
-
const toolbox = __importStar(require("gluegun
|
|
35
|
+
const toolbox = __importStar(require("gluegun"));
|
|
36
36
|
const spinner_1 = require("../command-helpers/spinner");
|
|
37
37
|
const subgraph_1 = __importDefault(require("../subgraph"));
|
|
38
38
|
const watcher_1 = __importDefault(require("../watcher"));
|
|
39
39
|
const migrations_1 = require("../migrations");
|
|
40
40
|
const asc = __importStar(require("./asc"));
|
|
41
|
-
const subgraph_2 = __importDefault(require("../protocols/substreams/subgraph"));
|
|
42
41
|
const debug_1 = __importDefault(require("../debug"));
|
|
43
42
|
let compilerDebug = (0, debug_1.default)('graph-cli:compiler');
|
|
44
43
|
class Compiler {
|
|
44
|
+
options;
|
|
45
|
+
ipfs;
|
|
46
|
+
sourceDir;
|
|
47
|
+
blockIpfsMethods;
|
|
48
|
+
libsDirs;
|
|
49
|
+
globalsFile;
|
|
50
|
+
protocol;
|
|
51
|
+
ABI;
|
|
45
52
|
constructor(options) {
|
|
53
|
+
this.options = options;
|
|
46
54
|
this.options = options;
|
|
47
55
|
this.ipfs = options.ipfs;
|
|
48
56
|
this.sourceDir = path_1.default.dirname(options.subgraphManifest);
|
|
@@ -70,6 +78,10 @@ class Compiler {
|
|
|
70
78
|
}
|
|
71
79
|
this.globalsFile = path_1.default.join(globalsLib, globalsFile);
|
|
72
80
|
}
|
|
81
|
+
// @ts-expect-error assigned or not, we dont care
|
|
82
|
+
if (!this.globalsFile) {
|
|
83
|
+
throw new Error('Globals file is missing.');
|
|
84
|
+
}
|
|
73
85
|
this.protocol = this.options.protocol;
|
|
74
86
|
this.ABI = this.protocol.getABI();
|
|
75
87
|
process.on('uncaughtException', function (e) {
|
|
@@ -121,11 +133,12 @@ class Compiler {
|
|
|
121
133
|
async loadSubgraph({ quiet } = { quiet: false }) {
|
|
122
134
|
const subgraphLoadOptions = { protocol: this.protocol, skipValidation: false };
|
|
123
135
|
if (quiet) {
|
|
124
|
-
return subgraph_1.default.load(this.options.subgraphManifest, subgraphLoadOptions)
|
|
136
|
+
return (await subgraph_1.default.load(this.options.subgraphManifest, subgraphLoadOptions))
|
|
137
|
+
.result;
|
|
125
138
|
}
|
|
126
139
|
else {
|
|
127
140
|
const manifestPath = this.displayPath(this.options.subgraphManifest);
|
|
128
|
-
return await (0, spinner_1.withSpinner)(`Load subgraph from ${manifestPath}`, `Failed to load subgraph from ${manifestPath}`, `Warnings loading subgraph from ${manifestPath}`, async (
|
|
141
|
+
return await (0, spinner_1.withSpinner)(`Load subgraph from ${manifestPath}`, `Failed to load subgraph from ${manifestPath}`, `Warnings loading subgraph from ${manifestPath}`, async () => {
|
|
129
142
|
return subgraph_1.default.load(this.options.subgraphManifest, subgraphLoadOptions);
|
|
130
143
|
});
|
|
131
144
|
}
|
|
@@ -138,11 +151,11 @@ class Compiler {
|
|
|
138
151
|
files.push(this.options.subgraphManifest);
|
|
139
152
|
// Add all file paths specified in manifest
|
|
140
153
|
files.push(path_1.default.resolve(subgraph.getIn(['schema', 'file'])));
|
|
141
|
-
subgraph.get('dataSources').map(dataSource => {
|
|
154
|
+
subgraph.get('dataSources').map((dataSource) => {
|
|
142
155
|
files.push(dataSource.getIn(['mapping', 'file']));
|
|
143
156
|
// Only watch ABI related files if the target protocol has support/need for them.
|
|
144
157
|
if (this.protocol.hasABIs()) {
|
|
145
|
-
dataSource.getIn(['mapping', 'abis']).map(abi => {
|
|
158
|
+
dataSource.getIn(['mapping', 'abis']).map((abi) => {
|
|
146
159
|
files.push(abi.get('file'));
|
|
147
160
|
});
|
|
148
161
|
}
|
|
@@ -154,7 +167,7 @@ class Compiler {
|
|
|
154
167
|
throw Error(`Failed to load subgraph: ${e.message}`);
|
|
155
168
|
}
|
|
156
169
|
}
|
|
157
|
-
async watchAndCompile(onCompiled
|
|
170
|
+
async watchAndCompile(onCompiled) {
|
|
158
171
|
let compiler = this;
|
|
159
172
|
let spinner;
|
|
160
173
|
// Create watcher and recompile once and then on every change to a watched file
|
|
@@ -165,9 +178,7 @@ class Compiler {
|
|
|
165
178
|
spinner.info(`File change detected: ${this.displayPath(changedFile)}\n`);
|
|
166
179
|
}
|
|
167
180
|
let ipfsHash = await compiler.compile();
|
|
168
|
-
|
|
169
|
-
onCompiled(ipfsHash);
|
|
170
|
-
}
|
|
181
|
+
onCompiled?.(ipfsHash);
|
|
171
182
|
spinner.start();
|
|
172
183
|
},
|
|
173
184
|
onCollectFiles: async () => await compiler.getFilesToWatch(),
|
|
@@ -203,10 +214,10 @@ class Compiler {
|
|
|
203
214
|
// Cache compiled files so identical input files are only compiled once
|
|
204
215
|
let compiledFiles = new Map();
|
|
205
216
|
await asc.ready();
|
|
206
|
-
subgraph = subgraph.update('dataSources', dataSources => dataSources.map(dataSource => dataSource.updateIn(['mapping', 'file'], mappingPath => this._compileDataSourceMapping(this.protocol, dataSource, mappingPath, compiledFiles, spinner))));
|
|
207
|
-
subgraph = subgraph.update('templates', templates => templates === undefined
|
|
217
|
+
subgraph = subgraph.update('dataSources', (dataSources) => dataSources.map((dataSource) => dataSource.updateIn(['mapping', 'file'], (mappingPath) => this._compileDataSourceMapping(this.protocol, dataSource, mappingPath, compiledFiles, spinner))));
|
|
218
|
+
subgraph = subgraph.update('templates', (templates) => templates === undefined
|
|
208
219
|
? templates
|
|
209
|
-
: templates.map(template => template.updateIn(['mapping', 'file'], mappingPath => this._compileTemplateMapping(template, mappingPath, compiledFiles, spinner))));
|
|
220
|
+
: templates.map((template) => template.updateIn(['mapping', 'file'], (mappingPath) => this._compileTemplateMapping(template, mappingPath, compiledFiles, spinner))));
|
|
210
221
|
return subgraph;
|
|
211
222
|
});
|
|
212
223
|
}
|
|
@@ -327,12 +338,12 @@ class Compiler {
|
|
|
327
338
|
return path_1.default.relative(this.options.outputDir, targetFile);
|
|
328
339
|
});
|
|
329
340
|
// Copy data source files and update their paths
|
|
330
|
-
subgraph = subgraph.update('dataSources', dataSources => dataSources.map(dataSource => {
|
|
341
|
+
subgraph = subgraph.update('dataSources', (dataSources) => dataSources.map(dataSource => {
|
|
331
342
|
let updatedDataSource = dataSource;
|
|
332
343
|
if (this.protocol.hasABIs()) {
|
|
333
344
|
updatedDataSource = updatedDataSource
|
|
334
345
|
// Write data source ABIs to the output directory
|
|
335
|
-
.updateIn(['mapping', 'abis'], abis => abis.map(abi => abi.update('file', abiFile => {
|
|
346
|
+
.updateIn(['mapping', 'abis'], (abis) => abis.map((abi) => abi.update('file', (abiFile) => {
|
|
336
347
|
abiFile = path_1.default.resolve(this.sourceDir, abiFile);
|
|
337
348
|
let abiData = this.ABI.load(abi.get('name'), abiFile);
|
|
338
349
|
return path_1.default.relative(this.options.outputDir, this._writeSubgraphFile(abiFile, JSON.stringify(abiData.data.toJS(), null, 2), this.sourceDir, this.subgraphDir(this.options.outputDir, dataSource), spinner));
|
|
@@ -341,7 +352,7 @@ class Compiler {
|
|
|
341
352
|
if (protocol.name == 'substreams') {
|
|
342
353
|
updatedDataSource = updatedDataSource
|
|
343
354
|
// Write data source ABIs to the output directory
|
|
344
|
-
.updateIn(['source', 'package'], substreamsPackage => substreamsPackage.update('file', packageFile => {
|
|
355
|
+
.updateIn(['source', 'package'], (substreamsPackage) => substreamsPackage.update('file', (packageFile) => {
|
|
345
356
|
packageFile = path_1.default.resolve(this.sourceDir, packageFile);
|
|
346
357
|
let packageContent = fs_extra_1.default.readFileSync(packageFile);
|
|
347
358
|
return path_1.default.relative(this.options.outputDir, this._writeSubgraphFile(packageFile, packageContent, this.sourceDir, this.subgraphDir(this.options.outputDir, dataSource), spinner));
|
|
@@ -350,17 +361,17 @@ class Compiler {
|
|
|
350
361
|
}
|
|
351
362
|
// The mapping file is already being written to the output
|
|
352
363
|
// directory by the AssemblyScript compiler
|
|
353
|
-
return updatedDataSource.updateIn(['mapping', 'file'], mappingFile => path_1.default.relative(this.options.outputDir, path_1.default.resolve(this.sourceDir, mappingFile)));
|
|
364
|
+
return updatedDataSource.updateIn(['mapping', 'file'], (mappingFile) => path_1.default.relative(this.options.outputDir, path_1.default.resolve(this.sourceDir, mappingFile)));
|
|
354
365
|
}));
|
|
355
366
|
// Copy template files and update their paths
|
|
356
367
|
subgraph = subgraph.update('templates', templates => templates === undefined
|
|
357
368
|
? templates
|
|
358
|
-
: templates.map(template => {
|
|
369
|
+
: templates.map((template) => {
|
|
359
370
|
let updatedTemplate = template;
|
|
360
371
|
if (this.protocol.hasABIs()) {
|
|
361
372
|
updatedTemplate = updatedTemplate
|
|
362
373
|
// Write template ABIs to the output directory
|
|
363
|
-
.updateIn(['mapping', 'abis'], abis => abis.map(abi => abi.update('file', abiFile => {
|
|
374
|
+
.updateIn(['mapping', 'abis'], (abis) => abis.map(abi => abi.update('file', (abiFile) => {
|
|
364
375
|
abiFile = path_1.default.resolve(this.sourceDir, abiFile);
|
|
365
376
|
let abiData = this.ABI.load(abi.get('name'), abiFile);
|
|
366
377
|
return path_1.default.relative(this.options.outputDir, this._writeSubgraphFile(abiFile, JSON.stringify(abiData.data.toJS(), null, 2), this.sourceDir, this.subgraphDir(this.options.outputDir, template), spinner));
|
|
@@ -368,7 +379,7 @@ class Compiler {
|
|
|
368
379
|
}
|
|
369
380
|
// The mapping file is already being written to the output
|
|
370
381
|
// directory by the AssemblyScript compiler
|
|
371
|
-
return updatedTemplate.updateIn(['mapping', 'file'], mappingFile => path_1.default.relative(this.options.outputDir, path_1.default.resolve(this.sourceDir, mappingFile)));
|
|
382
|
+
return updatedTemplate.updateIn(['mapping', 'file'], (mappingFile) => path_1.default.relative(this.options.outputDir, path_1.default.resolve(this.sourceDir, mappingFile)));
|
|
372
383
|
}));
|
|
373
384
|
// Write the subgraph manifest itself
|
|
374
385
|
let outputFilename = path_1.default.join(this.options.outputDir, 'subgraph.yaml');
|
|
@@ -434,7 +445,7 @@ class Compiler {
|
|
|
434
445
|
subgraph = subgraph.setIn(update.keyPath, update.value);
|
|
435
446
|
}
|
|
436
447
|
// Upload the subgraph itself
|
|
437
|
-
return await this._uploadSubgraphDefinitionToIPFS(subgraph
|
|
448
|
+
return await this._uploadSubgraphDefinitionToIPFS(subgraph);
|
|
438
449
|
});
|
|
439
450
|
}
|
|
440
451
|
async _uploadFileToIPFS(maybeRelativeFile, uploadedFiles, spinner) {
|
|
@@ -444,6 +455,7 @@ class Compiler {
|
|
|
444
455
|
let uploadCacheKey = this.cacheKeyForFile(absoluteFile);
|
|
445
456
|
let alreadyUploaded = uploadedFiles.has(uploadCacheKey);
|
|
446
457
|
if (!alreadyUploaded) {
|
|
458
|
+
// @ts-expect-error Buffer.from with Buffer data can indeed accept utf-8
|
|
447
459
|
let content = Buffer.from(await fs_extra_1.default.readFile(absoluteFile), 'utf-8');
|
|
448
460
|
let hash = await this._uploadToIPFS({
|
|
449
461
|
path: path_1.default.relative(this.options.outputDir, absoluteFile),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphprotocol/graph-cli",
|
|
3
|
-
"version": "0.37.3-alpha-
|
|
3
|
+
"version": "0.37.3-alpha-20230111150012-96ec4db",
|
|
4
4
|
"license": "(Apache-2.0 OR MIT)",
|
|
5
5
|
"description": "CLI for building for and deploying to The Graph",
|
|
6
6
|
"main": "dist/cli.js",
|
|
@@ -1,13 +1,32 @@
|
|
|
1
1
|
const URL = require('url').URL
|
|
2
2
|
import ipfsHttpClient from 'ipfs-http-client'
|
|
3
|
-
import * as toolbox from 'gluegun
|
|
3
|
+
import * as toolbox from 'gluegun'
|
|
4
4
|
import Compiler from '../compiler'
|
|
5
|
+
import Protocol from '../protocols'
|
|
6
|
+
|
|
7
|
+
interface CreateCompilerOptions {
|
|
8
|
+
ipfs: any
|
|
9
|
+
headers: any
|
|
10
|
+
outputDir: string
|
|
11
|
+
outputFormat: string
|
|
12
|
+
skipMigrations: boolean
|
|
13
|
+
blockIpfsMethods: boolean
|
|
14
|
+
protocol: Protocol
|
|
15
|
+
}
|
|
5
16
|
|
|
6
17
|
// Helper function to construct a subgraph compiler
|
|
7
|
-
|
|
8
|
-
manifest,
|
|
9
|
-
{
|
|
10
|
-
|
|
18
|
+
export function createCompiler(
|
|
19
|
+
manifest: string,
|
|
20
|
+
{
|
|
21
|
+
ipfs,
|
|
22
|
+
headers,
|
|
23
|
+
outputDir,
|
|
24
|
+
outputFormat,
|
|
25
|
+
skipMigrations,
|
|
26
|
+
blockIpfsMethods,
|
|
27
|
+
protocol,
|
|
28
|
+
}: CreateCompilerOptions,
|
|
29
|
+
) {
|
|
11
30
|
// Parse the IPFS URL
|
|
12
31
|
let url
|
|
13
32
|
try {
|
|
@@ -39,5 +58,3 @@ The IPFS URL must be of the following format: http(s)://host[:port]/[path]`)
|
|
|
39
58
|
protocol,
|
|
40
59
|
})
|
|
41
60
|
}
|
|
42
|
-
|
|
43
|
-
export { createCompiler }
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
declare module 'ipfs-http-client'
|
|
@@ -4,19 +4,37 @@ import fs from 'fs-extra'
|
|
|
4
4
|
import immutable from 'immutable'
|
|
5
5
|
import path from 'path'
|
|
6
6
|
import yaml from 'js-yaml'
|
|
7
|
-
import * as toolbox from 'gluegun
|
|
8
|
-
import { step, withSpinner } from '../command-helpers/spinner'
|
|
7
|
+
import * as toolbox from 'gluegun'
|
|
8
|
+
import { Spinner, step, withSpinner } from '../command-helpers/spinner'
|
|
9
9
|
import Subgraph from '../subgraph'
|
|
10
10
|
import Watcher from '../watcher'
|
|
11
11
|
import { applyMigrations } from '../migrations'
|
|
12
12
|
import * as asc from './asc'
|
|
13
|
-
import SubstreamsSubgraph from '../protocols/substreams/subgraph'
|
|
14
13
|
import debug from '../debug'
|
|
14
|
+
import Protocol from '../protocols'
|
|
15
15
|
|
|
16
16
|
let compilerDebug = debug('graph-cli:compiler')
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
interface CompilerOptions {
|
|
19
|
+
ipfs: any
|
|
20
|
+
subgraphManifest: string
|
|
21
|
+
outputDir: string
|
|
22
|
+
outputFormat: string
|
|
23
|
+
skipMigrations: boolean
|
|
24
|
+
blockIpfsMethods: boolean
|
|
25
|
+
protocol: Protocol
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export default class Compiler {
|
|
29
|
+
private ipfs: any
|
|
30
|
+
private sourceDir: string
|
|
31
|
+
private blockIpfsMethods: boolean
|
|
32
|
+
private libsDirs: string[]
|
|
33
|
+
private globalsFile: string
|
|
34
|
+
private protocol: Protocol
|
|
35
|
+
private ABI: any
|
|
36
|
+
|
|
37
|
+
constructor(private options: CompilerOptions) {
|
|
20
38
|
this.options = options
|
|
21
39
|
this.ipfs = options.ipfs
|
|
22
40
|
this.sourceDir = path.dirname(options.subgraphManifest)
|
|
@@ -25,7 +43,7 @@ class Compiler {
|
|
|
25
43
|
|
|
26
44
|
if (options.protocol.name !== 'substreams') {
|
|
27
45
|
for (
|
|
28
|
-
let dir = path.resolve(this.sourceDir);
|
|
46
|
+
let dir: string | undefined = path.resolve(this.sourceDir);
|
|
29
47
|
// Terminate after the root dir or when we have found node_modules
|
|
30
48
|
dir !== undefined;
|
|
31
49
|
// Continue with the parent directory, terminate after the root dir
|
|
@@ -56,23 +74,28 @@ class Compiler {
|
|
|
56
74
|
this.globalsFile = path.join(globalsLib, globalsFile)
|
|
57
75
|
}
|
|
58
76
|
|
|
77
|
+
// @ts-expect-error assigned or not, we dont care
|
|
78
|
+
if (!this.globalsFile) {
|
|
79
|
+
throw new Error('Globals file is missing.')
|
|
80
|
+
}
|
|
81
|
+
|
|
59
82
|
this.protocol = this.options.protocol
|
|
60
83
|
this.ABI = this.protocol.getABI()
|
|
61
84
|
|
|
62
|
-
process.on('uncaughtException', function
|
|
85
|
+
process.on('uncaughtException', function(e) {
|
|
63
86
|
toolbox.print.error(`UNCAUGHT EXCEPTION: ${e}`)
|
|
64
87
|
})
|
|
65
88
|
}
|
|
66
89
|
|
|
67
|
-
subgraphDir(parent, subgraph) {
|
|
90
|
+
subgraphDir(parent: string, subgraph: immutable.Map<any, any>) {
|
|
68
91
|
return path.join(parent, subgraph.get('name'))
|
|
69
92
|
}
|
|
70
93
|
|
|
71
|
-
displayPath(p) {
|
|
94
|
+
displayPath(p: string) {
|
|
72
95
|
return path.relative(process.cwd(), p)
|
|
73
96
|
}
|
|
74
97
|
|
|
75
|
-
cacheKeyForFile(filename) {
|
|
98
|
+
cacheKeyForFile(filename: string) {
|
|
76
99
|
let hash = crypto.createHash('sha1')
|
|
77
100
|
hash.update(fs.readFileSync(filename))
|
|
78
101
|
return hash.digest('hex')
|
|
@@ -107,7 +130,7 @@ class Compiler {
|
|
|
107
130
|
}
|
|
108
131
|
}
|
|
109
132
|
|
|
110
|
-
completed(ipfsHashOrPath) {
|
|
133
|
+
completed(ipfsHashOrPath: string) {
|
|
111
134
|
toolbox.print.info('')
|
|
112
135
|
toolbox.print.success(`Build completed: ${chalk.blue(ipfsHashOrPath)}`)
|
|
113
136
|
toolbox.print.info('')
|
|
@@ -117,7 +140,8 @@ class Compiler {
|
|
|
117
140
|
const subgraphLoadOptions = { protocol: this.protocol, skipValidation: false }
|
|
118
141
|
|
|
119
142
|
if (quiet) {
|
|
120
|
-
return Subgraph.load(this.options.subgraphManifest, subgraphLoadOptions)
|
|
143
|
+
return (await Subgraph.load(this.options.subgraphManifest, subgraphLoadOptions))
|
|
144
|
+
.result
|
|
121
145
|
} else {
|
|
122
146
|
const manifestPath = this.displayPath(this.options.subgraphManifest)
|
|
123
147
|
|
|
@@ -125,7 +149,7 @@ class Compiler {
|
|
|
125
149
|
`Load subgraph from ${manifestPath}`,
|
|
126
150
|
`Failed to load subgraph from ${manifestPath}`,
|
|
127
151
|
`Warnings loading subgraph from ${manifestPath}`,
|
|
128
|
-
async
|
|
152
|
+
async () => {
|
|
129
153
|
return Subgraph.load(this.options.subgraphManifest, subgraphLoadOptions)
|
|
130
154
|
},
|
|
131
155
|
)
|
|
@@ -142,11 +166,11 @@ class Compiler {
|
|
|
142
166
|
|
|
143
167
|
// Add all file paths specified in manifest
|
|
144
168
|
files.push(path.resolve(subgraph.getIn(['schema', 'file'])))
|
|
145
|
-
subgraph.get('dataSources').map(dataSource => {
|
|
169
|
+
subgraph.get('dataSources').map((dataSource: any) => {
|
|
146
170
|
files.push(dataSource.getIn(['mapping', 'file']))
|
|
147
171
|
// Only watch ABI related files if the target protocol has support/need for them.
|
|
148
172
|
if (this.protocol.hasABIs()) {
|
|
149
|
-
dataSource.getIn(['mapping', 'abis']).map(abi => {
|
|
173
|
+
dataSource.getIn(['mapping', 'abis']).map((abi: any) => {
|
|
150
174
|
files.push(abi.get('file'))
|
|
151
175
|
})
|
|
152
176
|
}
|
|
@@ -159,9 +183,9 @@ class Compiler {
|
|
|
159
183
|
}
|
|
160
184
|
}
|
|
161
185
|
|
|
162
|
-
async watchAndCompile(onCompiled
|
|
186
|
+
async watchAndCompile(onCompiled?: (ipfsHash: string) => void) {
|
|
163
187
|
let compiler = this
|
|
164
|
-
let spinner
|
|
188
|
+
let spinner: Spinner
|
|
165
189
|
|
|
166
190
|
// Create watcher and recompile once and then on every change to a watched file
|
|
167
191
|
let watcher = new Watcher({
|
|
@@ -171,9 +195,7 @@ class Compiler {
|
|
|
171
195
|
spinner.info(`File change detected: ${this.displayPath(changedFile)}\n`)
|
|
172
196
|
}
|
|
173
197
|
let ipfsHash = await compiler.compile()
|
|
174
|
-
|
|
175
|
-
onCompiled(ipfsHash)
|
|
176
|
-
}
|
|
198
|
+
onCompiled?.(ipfsHash)
|
|
177
199
|
spinner.start()
|
|
178
200
|
},
|
|
179
201
|
onCollectFiles: async () => await compiler.getFilesToWatch(),
|
|
@@ -197,7 +219,13 @@ class Compiler {
|
|
|
197
219
|
}
|
|
198
220
|
}
|
|
199
221
|
|
|
200
|
-
_writeSubgraphFile(
|
|
222
|
+
_writeSubgraphFile(
|
|
223
|
+
maybeRelativeFile: string,
|
|
224
|
+
data: string | Buffer,
|
|
225
|
+
sourceDir: string,
|
|
226
|
+
targetDir: string,
|
|
227
|
+
spinner: Spinner,
|
|
228
|
+
) {
|
|
201
229
|
let absoluteSourceFile = path.resolve(sourceDir, maybeRelativeFile)
|
|
202
230
|
let relativeSourceFile = path.relative(sourceDir, absoluteSourceFile)
|
|
203
231
|
let targetFile = path.join(targetDir, relativeSourceFile)
|
|
@@ -207,7 +235,7 @@ class Compiler {
|
|
|
207
235
|
return targetFile
|
|
208
236
|
}
|
|
209
237
|
|
|
210
|
-
async compileSubgraph(subgraph) {
|
|
238
|
+
async compileSubgraph(subgraph: any) {
|
|
211
239
|
return await withSpinner(
|
|
212
240
|
`Compile subgraph`,
|
|
213
241
|
`Failed to compile subgraph`,
|
|
@@ -218,9 +246,9 @@ class Compiler {
|
|
|
218
246
|
|
|
219
247
|
await asc.ready()
|
|
220
248
|
|
|
221
|
-
subgraph = subgraph.update('dataSources', dataSources =>
|
|
222
|
-
dataSources.map(dataSource =>
|
|
223
|
-
dataSource.updateIn(['mapping', 'file'], mappingPath =>
|
|
249
|
+
subgraph = subgraph.update('dataSources', (dataSources: any[]) =>
|
|
250
|
+
dataSources.map((dataSource: any) =>
|
|
251
|
+
dataSource.updateIn(['mapping', 'file'], (mappingPath: string) =>
|
|
224
252
|
this._compileDataSourceMapping(
|
|
225
253
|
this.protocol,
|
|
226
254
|
dataSource,
|
|
@@ -232,11 +260,11 @@ class Compiler {
|
|
|
232
260
|
),
|
|
233
261
|
)
|
|
234
262
|
|
|
235
|
-
subgraph = subgraph.update('templates', templates =>
|
|
263
|
+
subgraph = subgraph.update('templates', (templates: any) =>
|
|
236
264
|
templates === undefined
|
|
237
265
|
? templates
|
|
238
|
-
: templates.map(template =>
|
|
239
|
-
template.updateIn(['mapping', 'file'], mappingPath =>
|
|
266
|
+
: templates.map((template: any) =>
|
|
267
|
+
template.updateIn(['mapping', 'file'], (mappingPath: string) =>
|
|
240
268
|
this._compileTemplateMapping(
|
|
241
269
|
template,
|
|
242
270
|
mappingPath,
|
|
@@ -252,7 +280,13 @@ class Compiler {
|
|
|
252
280
|
)
|
|
253
281
|
}
|
|
254
282
|
|
|
255
|
-
_compileDataSourceMapping(
|
|
283
|
+
_compileDataSourceMapping(
|
|
284
|
+
protocol: Protocol,
|
|
285
|
+
dataSource: immutable.Map<any, any>,
|
|
286
|
+
mappingPath: string,
|
|
287
|
+
compiledFiles: Map<any, any>,
|
|
288
|
+
spinner: Spinner,
|
|
289
|
+
) {
|
|
256
290
|
if (protocol.name == 'substreams') {
|
|
257
291
|
return
|
|
258
292
|
}
|
|
@@ -321,7 +355,12 @@ class Compiler {
|
|
|
321
355
|
}
|
|
322
356
|
}
|
|
323
357
|
|
|
324
|
-
_compileTemplateMapping(
|
|
358
|
+
_compileTemplateMapping(
|
|
359
|
+
template: immutable.Collection<any, any>,
|
|
360
|
+
mappingPath: string,
|
|
361
|
+
compiledFiles: Map<any, any>,
|
|
362
|
+
spinner: Spinner,
|
|
363
|
+
) {
|
|
325
364
|
try {
|
|
326
365
|
let templateName = template.get('name')
|
|
327
366
|
|
|
@@ -388,7 +427,7 @@ class Compiler {
|
|
|
388
427
|
}
|
|
389
428
|
}
|
|
390
429
|
|
|
391
|
-
_validateMappingContent(filePath) {
|
|
430
|
+
_validateMappingContent(filePath: string) {
|
|
392
431
|
const data = fs.readFileSync(filePath)
|
|
393
432
|
if (
|
|
394
433
|
this.blockIpfsMethods &&
|
|
@@ -402,7 +441,10 @@ class Compiler {
|
|
|
402
441
|
}
|
|
403
442
|
}
|
|
404
443
|
|
|
405
|
-
async writeSubgraphToOutputDirectory(
|
|
444
|
+
async writeSubgraphToOutputDirectory(
|
|
445
|
+
protocol: Protocol,
|
|
446
|
+
subgraph: immutable.Map<any, any>,
|
|
447
|
+
) {
|
|
406
448
|
const displayDir = `${this.displayPath(this.options.outputDir)}${
|
|
407
449
|
toolbox.filesystem.separator
|
|
408
450
|
}`
|
|
@@ -414,8 +456,8 @@ class Compiler {
|
|
|
414
456
|
async spinner => {
|
|
415
457
|
// Copy schema and update its path
|
|
416
458
|
subgraph = subgraph.updateIn(['schema', 'file'], schemaFile => {
|
|
417
|
-
const schemaFilePath = path.resolve(this.sourceDir, schemaFile)
|
|
418
|
-
const schemaFileName = path.basename(schemaFile)
|
|
459
|
+
const schemaFilePath = path.resolve(this.sourceDir, schemaFile as string)
|
|
460
|
+
const schemaFileName = path.basename(schemaFile as string)
|
|
419
461
|
const targetFile = path.resolve(this.options.outputDir, schemaFileName)
|
|
420
462
|
step(spinner, 'Copy schema file', this.displayPath(targetFile))
|
|
421
463
|
fs.copyFileSync(schemaFilePath, targetFile)
|
|
@@ -423,16 +465,16 @@ class Compiler {
|
|
|
423
465
|
})
|
|
424
466
|
|
|
425
467
|
// Copy data source files and update their paths
|
|
426
|
-
subgraph = subgraph.update('dataSources', dataSources =>
|
|
468
|
+
subgraph = subgraph.update('dataSources', (dataSources: any[]) =>
|
|
427
469
|
dataSources.map(dataSource => {
|
|
428
470
|
let updatedDataSource = dataSource
|
|
429
471
|
|
|
430
472
|
if (this.protocol.hasABIs()) {
|
|
431
473
|
updatedDataSource = updatedDataSource
|
|
432
474
|
// Write data source ABIs to the output directory
|
|
433
|
-
.updateIn(['mapping', 'abis'], abis =>
|
|
434
|
-
abis.map(abi =>
|
|
435
|
-
abi.update('file', abiFile => {
|
|
475
|
+
.updateIn(['mapping', 'abis'], (abis: any[]) =>
|
|
476
|
+
abis.map((abi: any) =>
|
|
477
|
+
abi.update('file', (abiFile: string) => {
|
|
436
478
|
abiFile = path.resolve(this.sourceDir, abiFile)
|
|
437
479
|
let abiData = this.ABI.load(abi.get('name'), abiFile)
|
|
438
480
|
return path.relative(
|
|
@@ -453,8 +495,8 @@ class Compiler {
|
|
|
453
495
|
if (protocol.name == 'substreams') {
|
|
454
496
|
updatedDataSource = updatedDataSource
|
|
455
497
|
// Write data source ABIs to the output directory
|
|
456
|
-
.updateIn(['source', 'package'], substreamsPackage =>
|
|
457
|
-
substreamsPackage.update('file', packageFile => {
|
|
498
|
+
.updateIn(['source', 'package'], (substreamsPackage: any) =>
|
|
499
|
+
substreamsPackage.update('file', (packageFile: string) => {
|
|
458
500
|
packageFile = path.resolve(this.sourceDir, packageFile)
|
|
459
501
|
let packageContent = fs.readFileSync(packageFile)
|
|
460
502
|
|
|
@@ -476,11 +518,13 @@ class Compiler {
|
|
|
476
518
|
|
|
477
519
|
// The mapping file is already being written to the output
|
|
478
520
|
// directory by the AssemblyScript compiler
|
|
479
|
-
return updatedDataSource.updateIn(
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
path.
|
|
483
|
-
|
|
521
|
+
return updatedDataSource.updateIn(
|
|
522
|
+
['mapping', 'file'],
|
|
523
|
+
(mappingFile: string) =>
|
|
524
|
+
path.relative(
|
|
525
|
+
this.options.outputDir,
|
|
526
|
+
path.resolve(this.sourceDir, mappingFile),
|
|
527
|
+
),
|
|
484
528
|
)
|
|
485
529
|
}),
|
|
486
530
|
)
|
|
@@ -489,15 +533,15 @@ class Compiler {
|
|
|
489
533
|
subgraph = subgraph.update('templates', templates =>
|
|
490
534
|
templates === undefined
|
|
491
535
|
? templates
|
|
492
|
-
: templates.map(template => {
|
|
536
|
+
: templates.map((template: any) => {
|
|
493
537
|
let updatedTemplate = template
|
|
494
538
|
|
|
495
539
|
if (this.protocol.hasABIs()) {
|
|
496
540
|
updatedTemplate = updatedTemplate
|
|
497
541
|
// Write template ABIs to the output directory
|
|
498
|
-
.updateIn(['mapping', 'abis'], abis =>
|
|
542
|
+
.updateIn(['mapping', 'abis'], (abis: any[]) =>
|
|
499
543
|
abis.map(abi =>
|
|
500
|
-
abi.update('file', abiFile => {
|
|
544
|
+
abi.update('file', (abiFile: string) => {
|
|
501
545
|
abiFile = path.resolve(this.sourceDir, abiFile)
|
|
502
546
|
let abiData = this.ABI.load(abi.get('name'), abiFile)
|
|
503
547
|
return path.relative(
|
|
@@ -517,11 +561,13 @@ class Compiler {
|
|
|
517
561
|
|
|
518
562
|
// The mapping file is already being written to the output
|
|
519
563
|
// directory by the AssemblyScript compiler
|
|
520
|
-
return updatedTemplate.updateIn(
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
path.
|
|
524
|
-
|
|
564
|
+
return updatedTemplate.updateIn(
|
|
565
|
+
['mapping', 'file'],
|
|
566
|
+
(mappingFile: string) =>
|
|
567
|
+
path.relative(
|
|
568
|
+
this.options.outputDir,
|
|
569
|
+
path.resolve(this.sourceDir, mappingFile),
|
|
570
|
+
),
|
|
525
571
|
)
|
|
526
572
|
}),
|
|
527
573
|
)
|
|
@@ -536,7 +582,7 @@ class Compiler {
|
|
|
536
582
|
)
|
|
537
583
|
}
|
|
538
584
|
|
|
539
|
-
async uploadSubgraphToIPFS(subgraph) {
|
|
585
|
+
async uploadSubgraphToIPFS(subgraph: immutable.Map<any, any>) {
|
|
540
586
|
return withSpinner(
|
|
541
587
|
`Upload subgraph to IPFS`,
|
|
542
588
|
`Failed to upload subgraph to IPFS`,
|
|
@@ -552,7 +598,7 @@ class Compiler {
|
|
|
552
598
|
updates.push({
|
|
553
599
|
keyPath: ['schema', 'file'],
|
|
554
600
|
value: await this._uploadFileToIPFS(
|
|
555
|
-
subgraph.getIn(['schema', 'file']),
|
|
601
|
+
subgraph.getIn(['schema', 'file']) as string,
|
|
556
602
|
uploadedFiles,
|
|
557
603
|
spinner,
|
|
558
604
|
),
|
|
@@ -628,12 +674,16 @@ class Compiler {
|
|
|
628
674
|
}
|
|
629
675
|
|
|
630
676
|
// Upload the subgraph itself
|
|
631
|
-
return await this._uploadSubgraphDefinitionToIPFS(subgraph
|
|
677
|
+
return await this._uploadSubgraphDefinitionToIPFS(subgraph)
|
|
632
678
|
},
|
|
633
679
|
)
|
|
634
680
|
}
|
|
635
681
|
|
|
636
|
-
async _uploadFileToIPFS(
|
|
682
|
+
async _uploadFileToIPFS(
|
|
683
|
+
maybeRelativeFile: string,
|
|
684
|
+
uploadedFiles: Map<any, any>,
|
|
685
|
+
spinner: Spinner,
|
|
686
|
+
) {
|
|
637
687
|
compilerDebug(
|
|
638
688
|
'Resolving IPFS file "%s" from output dir "%s"',
|
|
639
689
|
maybeRelativeFile,
|
|
@@ -646,6 +696,7 @@ class Compiler {
|
|
|
646
696
|
let alreadyUploaded = uploadedFiles.has(uploadCacheKey)
|
|
647
697
|
|
|
648
698
|
if (!alreadyUploaded) {
|
|
699
|
+
// @ts-expect-error Buffer.from with Buffer data can indeed accept utf-8
|
|
649
700
|
let content = Buffer.from(await fs.readFile(absoluteFile), 'utf-8')
|
|
650
701
|
let hash = await this._uploadToIPFS({
|
|
651
702
|
path: path.relative(this.options.outputDir, absoluteFile),
|
|
@@ -664,13 +715,13 @@ class Compiler {
|
|
|
664
715
|
return immutable.fromJS({ '/': `/ipfs/${hash}` })
|
|
665
716
|
}
|
|
666
717
|
|
|
667
|
-
async _uploadSubgraphDefinitionToIPFS(subgraph) {
|
|
718
|
+
async _uploadSubgraphDefinitionToIPFS(subgraph: immutable.Map<any, any>) {
|
|
668
719
|
let str = yaml.safeDump(subgraph.toJS(), { noRefs: true, sortKeys: true })
|
|
669
720
|
let file = { path: 'subgraph.yaml', content: Buffer.from(str, 'utf-8') }
|
|
670
721
|
return await this._uploadToIPFS(file)
|
|
671
722
|
}
|
|
672
723
|
|
|
673
|
-
async _uploadToIPFS(file) {
|
|
724
|
+
async _uploadToIPFS(file: { path: string; content: Buffer }) {
|
|
674
725
|
try {
|
|
675
726
|
let hash = (await this.ipfs.add([file]))[0].hash
|
|
676
727
|
await this.ipfs.pin.add(hash)
|
|
@@ -680,5 +731,3 @@ class Compiler {
|
|
|
680
731
|
}
|
|
681
732
|
}
|
|
682
733
|
}
|
|
683
|
-
|
|
684
|
-
export default Compiler
|