UNPKG

@graphprotocol/graph-cli

Version:

CLI for building for and deploying to The Graph

198 lines (171 loc) 3.8 kB
class Param { constructor(name, type) { this.name = name this.type = type } toString() { return `${this.name}: ${this.type.toString()}` } } class Method { constructor(name, params, returnType, body) { this.name = name this.params = params || [] this.returnType = returnType this.body = body || '' } toString() { return ` ${this.name}(${this.params.map(param => param.toString()).join(', ')})${ this.returnType ? `: ${this.returnType.toString()}` : '' } {${this.body} } ` } } class StaticMethod { constructor(name, params, returnType, body) { this.name = name this.params = params || [] this.returnType = returnType || 'void' this.body = body || '' } toString() { return ` static ${this.name}(${this.params.map(param => param.toString()).join(', ')})${ this.returnType ? `: ${this.returnType.toString()}` : '' } {${this.body} } ` } } class Class { constructor(name, options) { this.name = name this.extends = options.extends this.methods = [] this.members = [] this.export = options.export || false } addMember(member) { this.members.push(member) } addMethod(method) { this.methods.push(method) } toString() { return ` ${this.export ? 'export' : ''} class ${this.name}${ this.extends ? ` extends ${this.extends}` : '' } { ${this.members.map(member => member.toString()).join('\n')} ${this.methods.map(method => method.toString()).join('')} } ` } } class ClassMember { constructor(name, type) { this.name = name this.type = type } toString() { return ` ${this.name}: ${this.type.toString()}` } } class NamedType { constructor(name) { this.name = name } toString() { return this.name } capitalize() { this.name = this.name.charAt(0).toUpperCase() + this.name.slice(1) return this } isPrimitive() { let primitives = [ 'boolean', 'u8', 'i8', 'u16', 'i16', 'u32', 'i32', 'u64', 'i64', 'f32', 'f64', 'usize', 'isize', ] return primitives.includes(this.name) } } class ArrayType { constructor(inner) { this.inner = inner this.name = `Array<${inner.toString()}>` } toString() { return this.name } } class NullableType { constructor(inner) { this.inner = inner } toString() { return `${this.inner.toString()} | null` } } class ModuleImports { constructor(nameOrNames, module) { this.nameOrNames = nameOrNames this.module = module } toString() { return `import { ${ typeof this.nameOrNames === 'string' ? this.nameOrNames : this.nameOrNames.join(',') } } from "${this.module}"` } } const namedType = name => new NamedType(name) const arrayType = name => new ArrayType(name) const param = (name, type) => new Param(name, type) const method = (name, params, returnType, body) => new Method(name, params, returnType, body) const staticMethod = (name, params, returnType, body) => new StaticMethod(name, params, returnType, body) const klass = (name, options) => new Class(name, options) const klassMember = (name, type) => new ClassMember(name, type) const nullableType = type => new NullableType(type) const moduleImports = (nameOrNames, module) => new ModuleImports(nameOrNames, module) const GENERATED_FILE_NOTE = ` // THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. ` module.exports = { // Types Param, Method, StaticMethod, Class, ClassMember, NamedType, NullableType, ArrayType, ModuleImports, // Code generators namedType, arrayType, klass, klassMember, method, staticMethod, param, nullableType, moduleImports, // Utilities GENERATED_FILE_NOTE, }