Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve debug logger #1431

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/cli/src/codegen/schema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable unicorn/no-array-for-each */
import debug from 'debug';
import debug from '../debug';
import Schema from '../schema';
import * as typesCodegen from './types';
import * as tsCodegen from './typescript';
Expand Down Expand Up @@ -287,7 +287,7 @@ export default class SchemaCodeGenerator {

_generateEntityFieldGetter(_entityDef: ObjectTypeDefinitionNode, fieldDef: FieldDefinitionNode) {
const isDerivedField = this._isDerivedField(fieldDef);
const codegenDebug = debug('codegen');
const codegenDebug = debug.extend('codegen');
if (isDerivedField) {
codegenDebug(`Generating derived field getter for ${fieldDef.name.value}`);
return this._generateDerivedFieldGetter(_entityDef, fieldDef);
Expand Down
10 changes: 10 additions & 0 deletions packages/cli/src/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import { withSpinner } from '../command-helpers/spinner';
import Protocol from '../protocols';
import EthereumABI from '../protocols/ethereum/abi';
import Subgraph from '../subgraph';
import debug from '../debug';

const debugLog = debug.extend('add');

export default class AddCommand extends Command {
static description = 'Adds a new datasource to a subgraph.';
Expand Down Expand Up @@ -82,6 +85,7 @@ export default class AddCommand extends Command {
const entities = getEntities(manifest);
const contractNames = getContractNames(manifest);
if (contractNames.includes(contractName)) {
debugLog(`contractNames: %L`, contractNames, 'contractName: ', contractName);
this.error(
`Datasource or template with name ${contractName} already exists, please choose a different name.`,
{ exit: 1 },
Expand All @@ -102,6 +106,7 @@ export default class AddCommand extends Command {
} catch (error) {
// we cannot ask user to do prompt in test environment
if (process.env.NODE_ENV !== 'test') {
debugLog(`error: %O`, error);
// If we can't get the start block, we'll just leave it out of the manifest
const { startBlock: userInputStartBlock } = await prompt.ask<{ startBlock: string }>([
{
Expand All @@ -119,6 +124,7 @@ export default class AddCommand extends Command {
}
}

debugLog('writing abi to file');
await writeABI(ethabi, contractName);

const { collisionEntities, onlyCollisions, abiData } = updateEventNamesOnCollision(
Expand All @@ -130,17 +136,21 @@ export default class AddCommand extends Command {

ethabi.data = abiData;

debugLog('writing schema to file');
await writeSchema(
ethabi,
protocol,
result.getIn(['schema', 'file']) as any,
collisionEntities,
contractName,
);
debugLog('writing mapping to file');
await writeMapping(ethabi, protocol, contractName, collisionEntities);
debugLog('writing tests to file');
await writeTestsFiles(ethabi, protocol, contractName);

const dataSources = result.get('dataSources');
debugLog('dataSources: %L', dataSources);
const dataSource = await generateDataSource(
protocol,
contractName,
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { updateSubgraphNetwork } from '../command-helpers/network';
import debug from '../debug';
import Protocol from '../protocols';

const buildDebug = debug('graph-cli:build');
const buildDebug = debug.extend('build');

export default class BuildCommand extends Command {
static description = 'Builds a subgraph and (optionally) uploads it to IPFS.';
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import debug from '../debug';
import Protocol from '../protocols';
import TypeGenerator from '../type-generator';

const codegenDebug = debug('graph-cli:codegen');
const codegenDebug = debug.extend('codegen');

export default class CodegenCommand extends Command {
static description = 'Generates AssemblyScript types for a subgraph.';
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Subgraph from '../subgraph';
import Watcher from '../watcher';
import * as asc from './asc';

const compilerDebug = debug('graph-cli:compiler');
const compilerDebug = debug.extend('compiler');

interface CompilerOptions {
ipfs: any;
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ debugFactory.formatters.M = immutableMap => {
return immutableMap;
};

export default debugFactory;
export default debugFactory('graph-cli');
2 changes: 1 addition & 1 deletion packages/cli/src/protocols/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { SubgraphOptions } from './subgraph';
import * as SubstreamsManifestScaffold from './substreams/scaffold/manifest';
import SubstreamsSubgraph from './substreams/subgraph';

const protocolDebug = debug('graph-cli:protocol');
const protocolDebug = debug.extend('protocol');

export default class Protocol {
static fromDataSources(dataSourcesAndTemplates: any) {
Expand Down
10 changes: 7 additions & 3 deletions packages/cli/src/subgraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import debug from './debug';
import { Subgraph as ISubgraph } from './protocols/subgraph';
import * as validation from './validation';

const subgraphDebug = debug('graph-cli:subgraph');
const subgraphDebug = debug.extend('subgraph');

const throwCombinedError = (filename: string, errors: immutable.List<any>) => {
throw new Error(
Expand Down Expand Up @@ -64,7 +64,9 @@ export default class Subgraph {
});

// Validate the subgraph manifest using this schema
return validation.validateManifest(data, rootType, schema, protocol, { resolveFile });
return validation.validateManifest(data, rootType, schema, protocol, {
resolveFile,
});
}

static validateSchema(manifest: any, { resolveFile }: { resolveFile: ResolveFile }) {
Expand Down Expand Up @@ -253,7 +255,9 @@ More than one template named '${name}', template names must be unique.`,

// TODO: Validation for file data sources
if (!has_file_data_sources) {
const manifestErrors = await Subgraph.validate(data, protocol, { resolveFile });
const manifestErrors = await Subgraph.validate(data, protocol, {
resolveFile,
});
if (manifestErrors.size > 0) {
throwCombinedError(filename, manifestErrors);
}
Expand Down