Skip to content

Commit 8f6b69d

Browse files
committed
type-generator: Remove circular dependency
1 parent b3d3bec commit 8f6b69d

File tree

4 files changed

+36
-30
lines changed

4 files changed

+36
-30
lines changed

src/codegen/typescript.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ const klassMember = (name, type) => new ClassMember(name, type)
179179
const nullableType = type => new NullableType(type)
180180
const moduleImports = (nameOrNames, module) => new ModuleImports(nameOrNames, module)
181181

182+
const GENERATED_FILE_NOTE = `
183+
// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
184+
`
185+
182186
module.exports = {
183187
// Types
184188
NullableType,
@@ -194,4 +198,7 @@ module.exports = {
194198
param,
195199
nullableType,
196200
moduleImports,
201+
202+
// Utilities
203+
GENERATED_FILE_NOTE,
197204
}

src/command-helpers/fs.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const path = require('path')
2+
3+
const displayPath = p =>
4+
path.relative(process.cwd(), p)
5+
6+
module.exports = {
7+
displayPath,
8+
}

src/protocols/ethereum/type-generator.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ const fs = require('fs-extra')
22
const path = require('path')
33
const immutable = require('immutable')
44
const prettier = require('prettier')
5-
const TypeGenerator = require('../../type-generator')
65
const ABI = require('./abi')
76
const { step, withSpinner } = require('../../command-helpers/spinner')
7+
const { GENERATED_FILE_NOTE } = require('../../codegen/typescript')
8+
const { displayPath } = require('../../command-helpers/fs')
89

910
module.exports = class EthereumTypeGenerator {
1011
constructor(options = {}) {
@@ -50,7 +51,7 @@ module.exports = class EthereumTypeGenerator {
5051
try {
5152
if (this.sourceDir) {
5253
let absolutePath = path.resolve(this.sourceDir, maybeRelativePath)
53-
step(spinner, `Load contract ABI from`, TypeGenerator.displayPath(absolutePath))
54+
step(spinner, `Load contract ABI from`, displayPath(absolutePath))
5455
return { dataSource: dataSource, abi: ABI.load(name, absolutePath) }
5556
} else {
5657
return { dataSource: dataSource, abi: ABI.load(name, maybeRelativePath) }
@@ -91,7 +92,7 @@ module.exports = class EthereumTypeGenerator {
9192
step(
9293
spinner,
9394
`Load data source template ABI from`,
94-
TypeGenerator.displayPath(absolutePath),
95+
displayPath(absolutePath),
9596
)
9697
return { template, abi: ABI.load(name, absolutePath) }
9798
} else {
@@ -120,13 +121,13 @@ module.exports = class EthereumTypeGenerator {
120121
step(
121122
spinner,
122123
`Generate types for contract ABI:`,
123-
`${abi.abi.name} (${TypeGenerator.displayPath(abi.abi.file)})`,
124+
`${abi.abi.name} (${displayPath(abi.abi.file)})`,
124125
)
125126

126127
let codeGenerator = abi.abi.codeGenerator()
127128
let code = prettier.format(
128129
[
129-
TypeGenerator.getGeneratedFileNote(),
130+
GENERATED_FILE_NOTE,
130131
...codeGenerator.generateModuleImports(),
131132
...codeGenerator.generateTypes(),
132133
].join('\n'),
@@ -140,7 +141,7 @@ module.exports = class EthereumTypeGenerator {
140141
abi.dataSource.get('name'),
141142
`${abi.abi.name}.ts`,
142143
)
143-
step(spinner, `Write types to`, TypeGenerator.displayPath(outputFile))
144+
step(spinner, `Write types to`, displayPath(outputFile))
144145
await fs.mkdirs(path.dirname(outputFile))
145146
await fs.writeFile(outputFile, code)
146147
} catch (e) {
@@ -169,15 +170,15 @@ module.exports = class EthereumTypeGenerator {
169170
step(
170171
spinner,
171172
`Generate types for data source template ABI:`,
172-
`${abi.template.get('name')} > ${abi.abi.name} (${TypeGenerator.displayPath(
173+
`${abi.template.get('name')} > ${abi.abi.name} (${displayPath(
173174
abi.abi.file,
174175
)})`,
175176
)
176177

177178
let codeGenerator = abi.abi.codeGenerator()
178179
let code = prettier.format(
179180
[
180-
TypeGenerator.getGeneratedFileNote(),
181+
GENERATED_FILE_NOTE,
181182
...codeGenerator.generateModuleImports(),
182183
...codeGenerator.generateTypes(),
183184
].join('\n'),
@@ -192,7 +193,7 @@ module.exports = class EthereumTypeGenerator {
192193
abi.template.get('name'),
193194
`${abi.abi.name}.ts`,
194195
)
195-
step(spinner, `Write types to`, TypeGenerator.displayPath(outputFile))
196+
step(spinner, `Write types to`, displayPath(outputFile))
196197
await fs.mkdirs(path.dirname(outputFile))
197198
await fs.writeFile(outputFile, code)
198199
} catch (e) {

src/type-generator.js

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ const DataSourceTemplateCodeGenerator = require('./codegen/template')
1212
const Watcher = require('./watcher')
1313
const { step, withSpinner } = require('./command-helpers/spinner')
1414
const { applyMigrations } = require('./migrations')
15-
16-
const GENERATED_FILE_NOTE = `
17-
// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
18-
`
15+
const { GENERATED_FILE_NOTE } = require('./codegen/typescript')
16+
const { displayPath } = require('./command-helpers/fs')
1917

2018
module.exports = class TypeGenerator {
2119
constructor(options) {
@@ -35,14 +33,6 @@ module.exports = class TypeGenerator {
3533
})
3634
}
3735

38-
static displayPath(p) {
39-
return path.relative(process.cwd(), p)
40-
}
41-
42-
static getGeneratedFileNote() {
43-
return GENERATED_FILE_NOTE
44-
}
45-
4636
async generateTypes() {
4737
try {
4838
if (!this.options.skipMigrations && this.options.subgraphManifest) {
@@ -85,7 +75,7 @@ module.exports = class TypeGenerator {
8575
? this.options.subgraph
8676
: Subgraph.load(this.options.subgraphManifest, subgraphLoadOptions).result
8777
} else {
88-
const manifestPath = TypeGenerator.displayPath(this.options.subgraphManifest)
78+
const manifestPath = displayPath(this.options.subgraphManifest)
8979

9080
return await withSpinner(
9181
`Load subgraph from ${manifestPath}`,
@@ -104,9 +94,9 @@ module.exports = class TypeGenerator {
10494
let maybeRelativePath = subgraph.getIn(['schema', 'file'])
10595
let absolutePath = path.resolve(this.sourceDir, maybeRelativePath)
10696
return await withSpinner(
107-
`Load GraphQL schema from ${TypeGenerator.displayPath(absolutePath)}`,
108-
`Failed to load GraphQL schema from ${TypeGenerator.displayPath(absolutePath)}`,
109-
`Warnings while loading GraphQL schema from ${TypeGenerator.displayPath(absolutePath)}`,
97+
`Load GraphQL schema from ${displayPath(absolutePath)}`,
98+
`Failed to load GraphQL schema from ${displayPath(absolutePath)}`,
99+
`Warnings while loading GraphQL schema from ${displayPath(absolutePath)}`,
110100
async spinner => {
111101
let maybeRelativePath = subgraph.getIn(['schema', 'file'])
112102
let absolutePath = path.resolve(this.sourceDir, maybeRelativePath)
@@ -125,7 +115,7 @@ module.exports = class TypeGenerator {
125115
let codeGenerator = schema.codeGenerator()
126116
let code = prettier.format(
127117
[
128-
TypeGenerator.getGeneratedFileNote(),
118+
GENERATED_FILE_NOTE,
129119
...codeGenerator.generateModuleImports(),
130120
...codeGenerator.generateTypes(),
131121
].join('\n'),
@@ -135,7 +125,7 @@ module.exports = class TypeGenerator {
135125
)
136126

137127
let outputFile = path.join(this.options.outputDir, 'schema.ts')
138-
step(spinner, 'Write types to', TypeGenerator.displayPath(outputFile))
128+
step(spinner, 'Write types to', displayPath(outputFile))
139129
await fs.mkdirs(path.dirname(outputFile))
140130
await fs.writeFile(outputFile, code)
141131
},
@@ -170,12 +160,12 @@ module.exports = class TypeGenerator {
170160
}, immutable.List())
171161

172162
if (!codeSegments.isEmpty()) {
173-
let code = prettier.format([TypeGenerator.getGeneratedFileNote(), ...codeSegments].join('\n'), {
163+
let code = prettier.format([GENERATED_FILE_NOTE, ...codeSegments].join('\n'), {
174164
parser: 'typescript',
175165
})
176166

177167
let outputFile = path.join(this.options.outputDir, 'templates.ts')
178-
step(spinner, `Write types for templates to`, TypeGenerator.displayPath(outputFile))
168+
step(spinner, `Write types for templates to`, displayPath(outputFile))
179169
await fs.mkdirs(path.dirname(outputFile))
180170
await fs.writeFile(outputFile, code)
181171
}
@@ -217,7 +207,7 @@ module.exports = class TypeGenerator {
217207
onReady: () => (spinner = toolbox.print.spin('Watching subgraph files')),
218208
onTrigger: async changedFile => {
219209
if (changedFile !== undefined) {
220-
spinner.info(`File change detected: ${TypeGenerator.displayPath(changedFile)}\n`)
210+
spinner.info(`File change detected: ${displayPath(changedFile)}\n`)
221211
}
222212
await generator.generateTypes()
223213
spinner.start()

0 commit comments

Comments
 (0)