@@ -11,6 +11,9 @@ const Subgraph = require('../subgraph')
11
11
const Watcher = require ( '../watcher' )
12
12
const { applyMigrations } = require ( '../migrations' )
13
13
const asc = require ( './asc' )
14
+ const SubstreamsSubgraph = require ( '../protocols/substreams/subgraph' )
15
+
16
+ let compilerDebug = require ( '../debug' ) ( 'graph-cli:compiler' )
14
17
15
18
class Compiler {
16
19
constructor ( options ) {
@@ -20,36 +23,38 @@ class Compiler {
20
23
this . blockIpfsMethods = options . blockIpfsMethods
21
24
this . libsDirs = [ ]
22
25
23
- for (
24
- let dir = path . resolve ( this . sourceDir ) ;
25
- // Terminate after the root dir or when we have found node_modules
26
- dir !== undefined ;
27
- // Continue with the parent directory, terminate after the root dir
28
- dir = path . dirname ( dir ) === dir ? undefined : path . dirname ( dir )
29
- ) {
30
- if ( fs . existsSync ( path . join ( dir , 'node_modules' ) ) ) {
31
- this . libsDirs . push ( path . join ( dir , 'node_modules' ) )
26
+ if ( options . protocol . name !== 'substreams' ) {
27
+ for (
28
+ let dir = path . resolve ( this . sourceDir ) ;
29
+ // Terminate after the root dir or when we have found node_modules
30
+ dir !== undefined ;
31
+ // Continue with the parent directory, terminate after the root dir
32
+ dir = path . dirname ( dir ) === dir ? undefined : path . dirname ( dir )
33
+ ) {
34
+ if ( fs . existsSync ( path . join ( dir , 'node_modules' ) ) ) {
35
+ this . libsDirs . push ( path . join ( dir , 'node_modules' ) )
36
+ }
32
37
}
33
- }
34
38
35
- if ( this . libsDirs . length === 0 ) {
36
- throw Error (
37
- `could not locate \`node_modules\` in parent directories of subgraph manifest` ,
38
- )
39
- }
39
+ if ( this . libsDirs . length === 0 ) {
40
+ throw Error (
41
+ `could not locate \`node_modules\` in parent directories of subgraph manifest` ,
42
+ )
43
+ }
40
44
41
- const globalsFile = path . join ( '@graphprotocol' , 'graph-ts' , 'global' , 'global.ts' )
42
- const globalsLib = this . libsDirs . find ( item => {
43
- return fs . existsSync ( path . join ( item , globalsFile ) )
44
- } )
45
+ const globalsFile = path . join ( '@graphprotocol' , 'graph-ts' , 'global' , 'global.ts' )
46
+ const globalsLib = this . libsDirs . find ( item => {
47
+ return fs . existsSync ( path . join ( item , globalsFile ) )
48
+ } )
45
49
46
- if ( ! globalsLib ) {
47
- throw Error (
48
- 'Could not locate `@graphprotocol/graph-ts` package in parent directories of subgraph manifest.' ,
49
- )
50
- }
50
+ if ( ! globalsLib ) {
51
+ throw Error (
52
+ 'Could not locate `@graphprotocol/graph-ts` package in parent directories of subgraph manifest.' ,
53
+ )
54
+ }
51
55
52
- this . globalsFile = path . join ( globalsLib , globalsFile )
56
+ this . globalsFile = path . join ( globalsLib , globalsFile )
57
+ }
53
58
54
59
this . protocol = this . options . protocol
55
60
this . ABI = this . protocol . getABI ( )
@@ -83,7 +88,10 @@ class Compiler {
83
88
}
84
89
let subgraph = await this . loadSubgraph ( )
85
90
let compiledSubgraph = await this . compileSubgraph ( subgraph )
86
- let localSubgraph = await this . writeSubgraphToOutputDirectory ( compiledSubgraph )
91
+ let localSubgraph = await this . writeSubgraphToOutputDirectory (
92
+ this . options . protocol ,
93
+ compiledSubgraph ,
94
+ )
87
95
88
96
if ( this . ipfs !== undefined ) {
89
97
let ipfsHash = await this . uploadSubgraphToIPFS ( localSubgraph )
@@ -214,6 +222,7 @@ class Compiler {
214
222
dataSources . map ( dataSource =>
215
223
dataSource . updateIn ( [ 'mapping' , 'file' ] , mappingPath =>
216
224
this . _compileDataSourceMapping (
225
+ this . protocol ,
217
226
dataSource ,
218
227
mappingPath ,
219
228
compiledFiles ,
@@ -243,7 +252,11 @@ class Compiler {
243
252
)
244
253
}
245
254
246
- _compileDataSourceMapping ( dataSource , mappingPath , compiledFiles , spinner ) {
255
+ _compileDataSourceMapping ( protocol , dataSource , mappingPath , compiledFiles , spinner ) {
256
+ if ( protocol . name == 'substreams' ) {
257
+ return
258
+ }
259
+
247
260
try {
248
261
let dataSourceName = dataSource . getIn ( [ 'name' ] )
249
262
@@ -378,7 +391,7 @@ class Compiler {
378
391
_validateMappingContent ( filePath ) {
379
392
const data = fs . readFileSync ( filePath )
380
393
if (
381
- this . blockIpfsMethods &&
394
+ this . blockIpfsMethods &&
382
395
( data . includes ( 'ipfs.cat' ) || data . includes ( 'ipfs.map' ) )
383
396
) {
384
397
throw Error ( `
@@ -389,7 +402,7 @@ class Compiler {
389
402
}
390
403
}
391
404
392
- async writeSubgraphToOutputDirectory ( subgraph ) {
405
+ async writeSubgraphToOutputDirectory ( protocol , subgraph ) {
393
406
const displayDir = `${ this . displayPath ( this . options . outputDir ) } ${
394
407
toolbox . filesystem . separator
395
408
} `
@@ -437,6 +450,30 @@ class Compiler {
437
450
)
438
451
}
439
452
453
+ if ( protocol . name == 'substreams' ) {
454
+ updatedDataSource = updatedDataSource
455
+ // Write data source ABIs to the output directory
456
+ . updateIn ( [ 'source' , 'package' ] , substreamsPackage =>
457
+ substreamsPackage . update ( 'file' , packageFile => {
458
+ packageFile = path . resolve ( this . sourceDir , packageFile )
459
+ let packageContent = fs . readFileSync ( packageFile )
460
+
461
+ return path . relative (
462
+ this . options . outputDir ,
463
+ this . _writeSubgraphFile (
464
+ packageFile ,
465
+ packageContent ,
466
+ this . sourceDir ,
467
+ this . subgraphDir ( this . options . outputDir , dataSource ) ,
468
+ spinner ,
469
+ ) ,
470
+ )
471
+ } ) ,
472
+ )
473
+
474
+ return updatedDataSource
475
+ }
476
+
440
477
// The mapping file is already being written to the output
441
478
// directory by the AssemblyScript compiler
442
479
return updatedDataSource . updateIn ( [ 'mapping' , 'file' ] , mappingFile =>
@@ -445,48 +482,48 @@ class Compiler {
445
482
path . resolve ( this . sourceDir , mappingFile ) ,
446
483
) ,
447
484
)
448
- } )
485
+ } ) ,
449
486
)
450
487
451
488
// Copy template files and update their paths
452
489
subgraph = subgraph . update ( 'templates' , templates =>
453
490
templates === undefined
454
491
? templates
455
492
: templates . map ( template => {
456
- let updatedTemplate = template
457
-
458
- if ( this . protocol . hasABIs ( ) ) {
459
- updatedTemplate = updatedTemplate
460
- // Write template ABIs to the output directory
461
- . updateIn ( [ 'mapping' , 'abis' ] , abis =>
462
- abis . map ( abi =>
463
- abi . update ( 'file' , abiFile => {
464
- abiFile = path . resolve ( this . sourceDir , abiFile )
465
- let abiData = this . ABI . load ( abi . get ( 'name' ) , abiFile )
466
- return path . relative (
467
- this . options . outputDir ,
468
- this . _writeSubgraphFile (
469
- abiFile ,
470
- JSON . stringify ( abiData . data . toJS ( ) , null , 2 ) ,
471
- this . sourceDir ,
472
- this . subgraphDir ( this . options . outputDir , template ) ,
473
- spinner ,
474
- ) ,
475
- )
476
- } ) ,
477
- ) ,
478
- )
479
- }
480
-
481
- // The mapping file is already being written to the output
482
- // directory by the AssemblyScript compiler
483
- return updatedTemplate . updateIn ( [ 'mapping' , 'file' ] , mappingFile =>
484
- path . relative (
485
- this . options . outputDir ,
486
- path . resolve ( this . sourceDir , mappingFile ) ,
487
- ) ,
488
- )
489
- } )
493
+ let updatedTemplate = template
494
+
495
+ if ( this . protocol . hasABIs ( ) ) {
496
+ updatedTemplate = updatedTemplate
497
+ // Write template ABIs to the output directory
498
+ . updateIn ( [ 'mapping' , 'abis' ] , abis =>
499
+ abis . map ( abi =>
500
+ abi . update ( 'file' , abiFile => {
501
+ abiFile = path . resolve ( this . sourceDir , abiFile )
502
+ let abiData = this . ABI . load ( abi . get ( 'name' ) , abiFile )
503
+ return path . relative (
504
+ this . options . outputDir ,
505
+ this . _writeSubgraphFile (
506
+ abiFile ,
507
+ JSON . stringify ( abiData . data . toJS ( ) , null , 2 ) ,
508
+ this . sourceDir ,
509
+ this . subgraphDir ( this . options . outputDir , template ) ,
510
+ spinner ,
511
+ ) ,
512
+ )
513
+ } ) ,
514
+ ) ,
515
+ )
516
+ }
517
+
518
+ // The mapping file is already being written to the output
519
+ // directory by the AssemblyScript compiler
520
+ return updatedTemplate . updateIn ( [ 'mapping' , 'file' ] , mappingFile =>
521
+ path . relative (
522
+ this . options . outputDir ,
523
+ path . resolve ( this . sourceDir , mappingFile ) ,
524
+ ) ,
525
+ )
526
+ } ) ,
490
527
)
491
528
492
529
// Write the subgraph manifest itself
@@ -537,15 +574,28 @@ class Compiler {
537
574
}
538
575
539
576
// Upload all mappings
540
- for ( let [ i , dataSource ] of subgraph . get ( 'dataSources' ) . entries ( ) ) {
541
- updates . push ( {
542
- keyPath : [ 'dataSources' , i , 'mapping' , 'file' ] ,
543
- value : await this . _uploadFileToIPFS (
544
- dataSource . getIn ( [ 'mapping' , 'file' ] ) ,
545
- uploadedFiles ,
546
- spinner ,
547
- ) ,
548
- } )
577
+ if ( this . protocol . name !== 'substreams' ) {
578
+ for ( let [ i , dataSource ] of subgraph . get ( 'dataSources' ) . entries ( ) ) {
579
+ updates . push ( {
580
+ keyPath : [ 'dataSources' , i , 'mapping' , 'file' ] ,
581
+ value : await this . _uploadFileToIPFS (
582
+ dataSource . getIn ( [ 'mapping' , 'file' ] ) ,
583
+ uploadedFiles ,
584
+ spinner ,
585
+ ) ,
586
+ } )
587
+ }
588
+ } else {
589
+ for ( let [ i , dataSource ] of subgraph . get ( 'dataSources' ) . entries ( ) ) {
590
+ updates . push ( {
591
+ keyPath : [ 'dataSources' , i , 'source' , 'package' , 'file' ] ,
592
+ value : await this . _uploadFileToIPFS (
593
+ dataSource . getIn ( [ 'source' , 'package' , 'file' ] ) ,
594
+ uploadedFiles ,
595
+ spinner ,
596
+ ) ,
597
+ } )
598
+ }
549
599
}
550
600
551
601
for ( let [ i , template ] of subgraph . get ( 'templates' , immutable . List ( ) ) . entries ( ) ) {
@@ -584,6 +634,11 @@ class Compiler {
584
634
}
585
635
586
636
async _uploadFileToIPFS ( maybeRelativeFile , uploadedFiles , spinner ) {
637
+ compilerDebug (
638
+ 'Resolving IPFS file "%s" from output dir "%s"' ,
639
+ maybeRelativeFile ,
640
+ this . options . outputDir ,
641
+ )
587
642
let absoluteFile = path . resolve ( this . options . outputDir , maybeRelativeFile )
588
643
step ( spinner , 'Add file to IPFS' , this . displayPath ( absoluteFile ) )
589
644
0 commit comments