Skip to content

Commit eedff93

Browse files
committed
refactor: Improve data source extraction
1 parent 666e544 commit eedff93

File tree

6 files changed

+31
-39
lines changed

6 files changed

+31
-39
lines changed

src/command-helpers/data-sources.js

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,38 @@
11
const immutable = require('immutable')
22
const { loadManifest } = require('../migrations/util/load-manifest')
33

4-
// From file path
5-
const getDataSourcesAndTemplates = async manifestPath => {
4+
// Loads manifest from file path and returns all:
5+
// - data sources
6+
// - templates
7+
// In a single list.
8+
const fromFilePath = async manifestPath => {
69
const { dataSources = [], templates = [] } = await loadManifest(manifestPath)
710

811
return dataSources.concat(templates)
912
}
1013

11-
// From in memory immutable data structure
12-
const collectDataSources = (manifest, protocolName) =>
14+
const extractDataSourceByType = (manifest, dataSourceType, protocolName) =>
1315
manifest
14-
.get('dataSources')
16+
.get(dataSourceType, immutable.List())
1517
.reduce(
1618
(dataSources, dataSource, dataSourceIndex) =>
1719
dataSource.get('kind') === protocolName
1820
? dataSources.push(
19-
immutable.Map({ path: ['dataSources', dataSourceIndex], dataSource }),
21+
immutable.Map({ path: [dataSourceType, dataSourceIndex], dataSource }),
2022
)
2123
: dataSources,
2224
immutable.List(),
2325
)
2426

25-
const collectDataSourceTemplates = (manifest, protocolName) =>
26-
manifest.get('templates', immutable.List()).reduce(
27-
(templates, template, templateIndex) =>
28-
template.get('kind') === protocolName
29-
? templates.push(
30-
immutable.Map({
31-
path: ['templates', templateIndex],
32-
dataSource: template,
33-
}),
34-
)
35-
: templates,
36-
immutable.List(),
37-
)
27+
// Extracts data sources and templates from a immutable manifest data structure
28+
const fromManifest = (manifest, protocolName) => {
29+
const dataSources = extractDataSourceByType(manifest, 'dataSources', protocolName)
30+
const templates = extractDataSourceByType(manifest, 'templates', protocolName)
31+
32+
return dataSources.concat(templates)
33+
}
3834

3935
module.exports = {
40-
getDataSourcesAndTemplates,
41-
collectDataSources,
42-
collectDataSourceTemplates,
36+
fromFilePath,
37+
fromManifest,
4338
}

src/commands/build.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const chalk = require('chalk')
22

33
const { createCompiler } = require('../command-helpers/compiler')
44
const { fixParameters } = require('../command-helpers/gluegun')
5-
const { getDataSourcesAndTemplates } = require('../command-helpers/data-sources')
5+
const DataSourcesExtractor = require('../command-helpers/data-sources')
66
const Protocol = require('../protocols')
77

88
const HELP = `
@@ -77,7 +77,7 @@ module.exports = {
7777

7878
let protocol
7979
try {
80-
const dataSourcesAndTemplates = await getDataSourcesAndTemplates(manifest)
80+
const dataSourcesAndTemplates = await DataSourcesExtractor.fromFilePath(manifest)
8181

8282
protocol = Protocol.fromDataSources(dataSourcesAndTemplates)
8383
} catch (e) {

src/commands/codegen.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const path = require('path')
44
const TypeGenerator = require('../type-generator')
55
const Protocol = require('../protocols')
66
const { fixParameters } = require('../command-helpers/gluegun')
7-
const { getDataSourcesAndTemplates } = require('../command-helpers/data-sources')
7+
const DataSourcesExtractor = require('../command-helpers/data-sources')
88
const { assertManifestApiVersion, assertGraphTsVersion } = require('../command-helpers/version')
99

1010
const HELP = `
@@ -73,7 +73,7 @@ module.exports = {
7373
await assertManifestApiVersion(manifest, '0.0.5')
7474
await assertGraphTsVersion(path.dirname(manifest), '0.22.0')
7575

76-
const dataSourcesAndTemplates = await getDataSourcesAndTemplates(manifest)
76+
const dataSourcesAndTemplates = await DataSourcesExtractor.fromFilePath(manifest)
7777

7878
protocol = Protocol.fromDataSources(dataSourcesAndTemplates)
7979
} catch (e) {

src/commands/deploy.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const { withSpinner } = require('../command-helpers/spinner')
1111
const { validateSubgraphName } = require('../command-helpers/subgraph')
1212
const { DEFAULT_IPFS_URL } = require('../command-helpers/ipfs')
1313
const { assertManifestApiVersion, assertGraphTsVersion } = require('../command-helpers/version')
14-
const { getDataSourcesAndTemplates } = require('../command-helpers/data-sources')
14+
const DataSourcesExtractor = require('../command-helpers/data-sources')
1515
const { validateStudioNetwork } = require('../command-helpers/studio')
1616
const Protocol = require('../protocols')
1717

@@ -129,7 +129,7 @@ module.exports = {
129129
: filesystem.resolve('subgraph.yaml')
130130

131131
try {
132-
const dataSourcesAndTemplates = await getDataSourcesAndTemplates(manifest)
132+
const dataSourcesAndTemplates = await DataSourcesExtractor.fromFilePath(manifest)
133133

134134
for (const { network } of dataSourcesAndTemplates) {
135135
validateStudioNetwork({ studio, product, network })
@@ -200,7 +200,7 @@ module.exports = {
200200
await assertManifestApiVersion(manifest, '0.0.5')
201201
await assertGraphTsVersion(path.dirname(manifest), '0.22.0')
202202

203-
const dataSourcesAndTemplates = await getDataSourcesAndTemplates(manifest)
203+
const dataSourcesAndTemplates = await DataSourcesExtractor.fromFilePath(manifest)
204204

205205
protocol = Protocol.fromDataSources(dataSourcesAndTemplates)
206206
} catch (e) {

src/commands/init.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const {
99
getSubgraphBasename,
1010
validateSubgraphName,
1111
} = require('../command-helpers/subgraph')
12-
const { getDataSourcesAndTemplates } = require('../command-helpers/data-sources')
12+
const DataSourcesExtractor = require('../command-helpers/data-sources')
1313
const { validateStudioNetwork } = require('../command-helpers/studio')
1414
const { withSpinner, step } = require('../command-helpers/spinner')
1515
const { fixParameters } = require('../command-helpers/gluegun')
@@ -614,7 +614,7 @@ const initSubgraphFromExample = async (
614614
try {
615615
// It doesn't matter if we changed the URL we clone the YAML,
616616
// we'll check it's network anyway. If it's a studio subgraph we're dealing with.
617-
const dataSourcesAndTemplates = await getDataSourcesAndTemplates(path.join(directory, 'subgraph.yaml'))
617+
const dataSourcesAndTemplates = await DataSourcesExtractor.fromFilePath(path.join(directory, 'subgraph.yaml'))
618618

619619
for (const { network } of dataSourcesAndTemplates) {
620620
validateStudioNetwork({ studio, product, network })

src/protocols/ethereum/subgraph.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const immutable = require('immutable')
22
const ABI = require('./abi')
3-
const { collectDataSources, collectDataSourceTemplates } = require('../../command-helpers/data-sources')
3+
const DataSourceExtractor = require('../../command-helpers/data-sources')
44
const { validateContractAddresses } = require('../../validation')
55

66
module.exports = class EthereumSubgraph {
@@ -17,10 +17,9 @@ module.exports = class EthereumSubgraph {
1717
}
1818

1919
validateAbis() {
20-
let dataSources = collectDataSources(this.manifest, 'ethereum/contract')
21-
let dataSourceTemplates = collectDataSourceTemplates(this.manifest, 'ethereum/contract')
20+
const dataSourcesAndTemplates = DataSourceExtractor.fromManifest(this.manifest, 'ethereum/contract')
2221

23-
return dataSources.concat(dataSourceTemplates).reduce(
22+
return dataSourcesAndTemplates.reduce(
2423
(errors, dataSourceOrTemplate) =>
2524
errors.concat(
2625
this.validateDataSourceAbis(
@@ -84,11 +83,9 @@ ${abiNames
8483
}
8584

8685
validateEvents() {
87-
let dataSources = collectDataSources(this.manifest, 'ethereum/contract')
88-
let dataSourceTemplates = collectDataSourceTemplates(this.manifest, 'ethereum/contract')
86+
const dataSourcesAndTemplates = DataSourceExtractor.fromManifest(this.manifest, 'ethereum/contract')
8987

90-
return dataSources
91-
.concat(dataSourceTemplates)
88+
return dataSourcesAndTemplates
9289
.reduce((errors, dataSourceOrTemplate) => {
9390
return errors.concat(
9491
this.validateDataSourceEvents(

0 commit comments

Comments
 (0)