@graphprotocol/graph-cli
Version:
CLI for building for and deploying to The Graph
172 lines (171 loc) • 7.43 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const gluegun_1 = require("gluegun");
const prettier_1 = __importDefault(require("prettier"));
const subgraph_1 = require("../command-helpers/subgraph");
const version_1 = require("../version");
const get_docker_file_1 = require("./get-docker-file");
const get_git_ignore_1 = require("./get-git-ignore");
const mapping_1 = require("./mapping");
const schema_1 = require("./schema");
const tests_1 = require("./tests");
const GRAPH_CLI_VERSION = process.env.GRAPH_CLI_TESTS
? // JSON.stringify should remove this key, we will install the local
// graph-cli for the tests using `npm link` instead of fetching from npm.
undefined
: // For scaffolding real subgraphs
version_1.version;
class Scaffold {
constructor(options) {
this.protocol = options.protocol;
this.abi = options.abi;
this.indexEvents = options.indexEvents;
this.contract = options.contract;
this.network = options.network;
this.contractName = options.contractName;
this.subgraphName = options.subgraphName;
this.startBlock = options.startBlock;
this.node = options.node;
this.spkgPath = options.spkgPath;
}
async generatePackageJson() {
return await prettier_1.default.format(JSON.stringify({
name: (0, subgraph_1.getSubgraphBasename)(String(this.subgraphName)),
license: 'UNLICENSED',
scripts: {
codegen: 'graph codegen',
build: 'graph build',
deploy: `graph deploy ` + `--node ${this.node} ` + this.subgraphName,
'create-local': `graph create --node http://localhost:8020/ ${this.subgraphName}`,
'remove-local': `graph remove --node http://localhost:8020/ ${this.subgraphName}`,
'deploy-local': `graph deploy ` +
`--node http://localhost:8020/ ` +
`--ipfs http://localhost:5001 ` +
this.subgraphName,
test: 'graph test',
},
dependencies: {
'@graphprotocol/graph-cli': GRAPH_CLI_VERSION,
'@graphprotocol/graph-ts': `0.32.0`,
},
devDependencies: this.protocol.hasEvents() ? { 'matchstick-as': `0.5.0` } : undefined,
}), { parser: 'json' });
}
async generatePackageJsonForSubstreams() {
return await prettier_1.default.format(JSON.stringify({
name: (0, subgraph_1.getSubgraphBasename)(String(this.subgraphName)),
license: 'UNLICENSED',
scripts: {
build: 'graph build',
deploy: `graph deploy ` + `--node ${this.node} ` + this.subgraphName,
'create-local': `graph create --node http://localhost:8020/ ${this.subgraphName}`,
'remove-local': `graph remove --node http://localhost:8020/ ${this.subgraphName}`,
'deploy-local': `graph deploy ` +
`--node http://localhost:8020/ ` +
`--ipfs http://localhost:5001 ` +
this.subgraphName,
test: 'graph test',
},
dependencies: {
'@graphprotocol/graph-cli': GRAPH_CLI_VERSION,
},
}), { parser: 'json' });
}
async generateManifest() {
const protocolManifest = this.protocol.getManifestScaffold();
return await prettier_1.default.format(`
specVersion: 1.0.0
indexerHints:
prune: auto
schema:
file: ./schema.graphql
dataSources:
- kind: ${this.protocol.name}
name: ${this.contractName}
network: ${this.network}
source: ${protocolManifest.source(this)}
mapping: ${protocolManifest.mapping(this)}
`, { parser: 'yaml' });
}
async generateSchema() {
const hasEvents = this.protocol.hasEvents();
const events = hasEvents ? (0, schema_1.abiEvents)(this.abi).toJS() : [];
return await prettier_1.default.format(hasEvents && this.indexEvents
? events
.map((event) => (0, schema_1.generateEventType)(event, this.protocol.name, this.contractName))
.join('\n\n')
: (0, schema_1.generateExampleEntityType)(this.protocol, events), {
parser: 'graphql',
trailingComma: 'none',
});
}
async generateTsConfig() {
return await prettier_1.default.format(JSON.stringify({
extends: '@graphprotocol/graph-ts/types/tsconfig.base.json',
include: ['src', 'tests'],
}), { parser: 'json' });
}
async generateDockerFileConfig() {
return await prettier_1.default.format((0, get_docker_file_1.getDockerFile)(), { parser: 'yaml' });
}
generateGitIgnoreFile() {
return (0, get_git_ignore_1.getGitIgnore)();
}
async generateMappings() {
return this.protocol.getMappingScaffold()
? { [`${gluegun_1.strings.kebabCase(this.contractName)}.ts`]: await this.generateMapping() }
: undefined;
}
async generateMapping() {
const hasEvents = this.protocol.hasEvents();
const events = hasEvents ? (0, schema_1.abiEvents)(this.abi).toJS() : [];
const protocolMapping = this.protocol.getMappingScaffold();
return await prettier_1.default.format(hasEvents && this.indexEvents
? (0, mapping_1.generateEventIndexingHandlers)(events, this.contractName)
: protocolMapping.generatePlaceholderHandlers({
...this,
events,
}), { parser: 'typescript', semi: false, trailingComma: 'none' });
}
async generateABIs() {
return this.protocol.hasABIs()
? {
[`${this.contractName}.json`]: await prettier_1.default.format(JSON.stringify(this.abi?.data), {
parser: 'json',
}),
}
: undefined;
}
async generateTests() {
const hasEvents = this.protocol.hasEvents();
const events = hasEvents ? (0, schema_1.abiEvents)(this.abi).toJS() : [];
return events.length > 0
? await (0, tests_1.generateTestsFiles)(this.contractName, events, this.indexEvents)
: undefined;
}
async generate() {
if (this.protocol.name === 'substreams') {
return {
'subgraph.yaml': await this.generateManifest(),
'schema.graphql': await this.generateSchema(),
'package.json': await this.generatePackageJsonForSubstreams(),
'.gitignore': await this.generateGitIgnoreFile(),
};
}
return {
'package.json': await this.generatePackageJson(),
'subgraph.yaml': await this.generateManifest(),
'schema.graphql': await this.generateSchema(),
'tsconfig.json': await this.generateTsConfig(),
'docker-compose.yml': await this.generateDockerFileConfig(),
'.gitignore': await this.generateGitIgnoreFile(),
src: await this.generateMappings(),
abis: await this.generateABIs(),
tests: await this.generateTests(),
};
}
}
exports.default = Scaffold;