UNPKG

@graphprotocol/graph-cli

Version:

CLI for building for and deploying to The Graph

44 lines (43 loc) 1.59 kB
import immutable from 'immutable'; import FileTemplateCodeGen from '../protocols/file_template.js'; import * as tsCodegen from './typescript.js'; export default class DataSourceTemplateCodeGenerator { template; protocolTemplateCodeGen; constructor(template, protocol) { this.template = template; this.template = template; const kind = template.get('kind'); if (kind.split('/')[0] == protocol.name) { this.protocolTemplateCodeGen = protocol.getTemplateCodeGen(template); } else if (kind == 'file/ipfs' || kind == 'file/arweave') { this.protocolTemplateCodeGen = new FileTemplateCodeGen(template); } else { throw new Error(`DataSourceTemplate kind not supported: ${kind}`); } } generateModuleImports() { return [ tsCodegen.moduleImports([ ...this.protocolTemplateCodeGen.generateModuleImports(), 'DataSourceTemplate', 'DataSourceContext', ], '@graphprotocol/graph-ts'), ]; } generateTypes() { return immutable.List([this._generateTemplateType()]); } _generateTemplateType() { const name = this.template.get('name'); const klass = tsCodegen.klass(name, { export: true, extends: 'DataSourceTemplate', }); klass.addMethod(this.protocolTemplateCodeGen.generateCreateMethod()); klass.addMethod(this.protocolTemplateCodeGen.generateCreateWithContextMethod()); return klass; } }