UNPKG

@graphprotocol/graph-cli

Version:

CLI for building for and deploying to The Graph

94 lines (93 loc) 4.92 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.updateNetworksFile = exports.initNetworksConfig = exports.updateSubgraphNetwork = void 0; const path_1 = __importDefault(require("path")); const gluegun_1 = require("gluegun"); const yaml_1 = __importDefault(require("yaml")); const spinner_1 = require("./spinner"); const updateSubgraphNetwork = async (manifest, network, networksFile, identifierName) => await (0, spinner_1.withSpinner)(`Update sources network`, `Failed to update sources network`, `Warnings while updating sources network`, async (spinner) => { (0, spinner_1.step)(spinner, `Reading networks config`); const allNetworks = await gluegun_1.filesystem.read(networksFile, 'json'); const networkConfig = allNetworks[network]; // Exit if the network passed with --network does not exits in networks.json if (!networkConfig) { throw new Error(`Network '${network}' was not found in '${networksFile}'`); } await gluegun_1.patching.update(manifest, content => { const subgraph = yaml_1.default.parse(content); const networkSources = Object.keys(networkConfig); const subgraphSources = subgraph.dataSources.map((value) => value.name); // Update the dataSources network config subgraph.dataSources = subgraph.dataSources.map((source) => { if (!networkSources.includes(source.name)) { throw new Error(`'${source.name}' was not found in the '${network}' configuration, please update!`); } if (hasChanges(identifierName, network, networkConfig[source.name], source)) { (0, spinner_1.step)(spinner, `Update '${source.name}' network configuration`); source.network = network; source.source = source.source.abi ? { abi: source.source.abi } : {}; Object.assign(source.source, networkConfig[source.name]); } else { (0, spinner_1.step)(spinner, `Skip '${source.name}': No changes to network configuration`); } return source; }); // All data sources shoud be on the same network, // so we have to update the network of all templates too. // eslint-disable-next-line -- prettier has problems with &&= subgraph.templates && (subgraph.templates = subgraph.templates.map((template) => ({ ...template, network, }))); const unusedSources = networkSources.filter(x => !subgraphSources.includes(x)); for (const source of unusedSources) { (0, spinner_1.step)(spinner, `dataSource '${source}' from '${networksFile}' not found in ${manifest}`); } const yaml_doc = new yaml_1.default.Document(); yaml_doc.contents = subgraph; return yaml_doc.toString(); }); }); exports.updateSubgraphNetwork = updateSubgraphNetwork; const initNetworksConfig = async (directory, identifierName) => await (0, spinner_1.withSpinner)(`Initialize networks config`, `Failed to initialize networks config`, `Warnings while initializing networks config`, async () => { const subgraphStr = gluegun_1.filesystem.read(path_1.default.join(directory, 'subgraph.yaml')); const subgraph = yaml_1.default.parse(subgraphStr); const networks = subgraph.dataSources.reduce((acc, source) => Object.assign(acc, { [source.network]: { [source.name]: { [identifierName]: source.source.address, startBlock: source.source.startBlock, }, }, }), {}); gluegun_1.filesystem.write(`${directory}/networks.json`, networks); return true; }); exports.initNetworksConfig = initNetworksConfig; // Checks if any network attribute has been changed function hasChanges(identifierName, network, networkConfig, dataSource) { const networkChanged = dataSource.network !== network; // Return directly if the network is different if (networkChanged) return networkChanged; const addressChanged = networkConfig[identifierName] !== dataSource.source[identifierName]; const startBlockChanged = networkConfig.startBlock !== dataSource.source.startBlock; return networkChanged || addressChanged || startBlockChanged; } async function updateNetworksFile(network, dataSource, address, networksFile) { await gluegun_1.patching.update(networksFile, config => { if (Object.keys(config).includes(network)) { Object.assign(config[network], { [dataSource]: { address } }); } else { Object.assign(config, { [network]: { [dataSource]: { address } } }); } return config; }); } exports.updateNetworksFile = updateNetworksFile;