Skip to content

Commit

Permalink
fix: only ask for subgraph name and directory for init examples (#1382)
Browse files Browse the repository at this point in the history
* attempt

* fix

* run codegen

* local codegen command

* fix substream eg

* prettier
  • Loading branch information
saihaj committed Jun 21, 2023
1 parent 16252be commit d2a1aa6
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 64 deletions.
5 changes: 5 additions & 0 deletions .changeset/witty-seals-battle.md
@@ -0,0 +1,5 @@
---
'@graphprotocol/graph-cli': patch
---

Only ask directory and subgraph name when using `graph init --from-example ...`
3 changes: 1 addition & 2 deletions examples/substreams-powered-subgraph/.gitignore
Expand Up @@ -2,5 +2,4 @@
target/
.idea
src/pb/
node_modules/
yarn.lock
node_modules/
3 changes: 2 additions & 1 deletion examples/substreams-powered-subgraph/package.json
Expand Up @@ -8,8 +8,9 @@
},
"private": true,
"scripts": {
"codegen": "graph codegen",
"deploy": "graph deploy",
"subgraph:build": "graph build",
"subgraph:deploy": "graph deploy",
"substreams:build": "cargo build --target wasm32-unknown-unknown --release",
"substreams:clean": "rm -rf ./target && rm -rf ./src/pb",
"substreams:package": "substreams pack ./substreams.yaml",
Expand Down
166 changes: 105 additions & 61 deletions packages/cli/src/commands/init.ts
Expand Up @@ -8,7 +8,6 @@ import {
loadAbiFromEtherscan,
loadStartBlockForContract,
} from '../command-helpers/abi';
import * as DataSourcesExtractor from '../command-helpers/data-sources';
import { initNetworksConfig } from '../command-helpers/network';
import { chooseNodeUrl } from '../command-helpers/node';
import { generateScaffold, writeScaffold } from '../command-helpers/scaffold';
Expand Down Expand Up @@ -167,16 +166,13 @@ export default class InitCommand extends Command {

// If all parameters are provided from the command-line,
// go straight to creating the subgraph from the example
if (fromExample && subgraphName && directory && protocol) {
if (fromExample && subgraphName && directory) {
await initSubgraphFromExample.bind(this)(
{
fromExample,
allowSimpleName,
directory,
subgraphName,
studio,
product,
protocolInstance: new Protocol(protocol as ProtocolName),
},
{ commands },
);
Expand Down Expand Up @@ -246,43 +242,51 @@ export default class InitCommand extends Command {
return this.exit(0);
}

// Otherwise, take the user through the interactive form
const answers = await processInitForm.bind(this)({
protocol: protocol as ProtocolName | undefined,
product,
studio,
node,
abi,
abiPath,
allowSimpleName,
directory,
contract: fromContract,
indexEvents,
fromExample,
network,
subgraphName,
contractName,
startBlock,
spkgPath,
});
if (!answers) {
this.exit(1);
return;
}

if (fromExample) {
const answers = await processFromExampleInitForm.bind(this)({
allowSimpleName,
subgraphName,
directory,
});

if (!answers) {
this.exit(1);
return;
}

await initSubgraphFromExample.bind(this)(
{
fromExample,
subgraphName: answers.subgraphName,
directory: answers.directory,
studio: answers.studio,
product: answers.product,
protocolInstance: answers.protocolInstance,
},
{ commands },
);
} else {
// Otherwise, take the user through the interactive form
const answers = await processInitForm.bind(this)({
protocol: protocol as ProtocolName | undefined,
product,
studio,
node,
abi,
abiPath,
allowSimpleName,
directory,
contract: fromContract,
indexEvents,
fromExample,
network,
subgraphName,
contractName,
startBlock,
spkgPath,
});
if (!answers) {
this.exit(1);
return;
}

({ node, allowSimpleName } = chooseNodeUrl({
product: answers.product,
studio: answers.studio,
Expand Down Expand Up @@ -314,6 +318,70 @@ export default class InitCommand extends Command {
}
}

async function processFromExampleInitForm(
this: InitCommand,
{
directory: initDirectory,
subgraphName: initSubgraphName,
allowSimpleName: initAllowSimpleName,
}: {
directory?: string;
subgraphName?: string;
allowSimpleName: boolean | undefined;
},
): Promise<
| {
subgraphName: string;
directory: string;
}
| undefined
> {
try {
const { subgraphName } = await prompt.ask<{ subgraphName: string }>([
{
type: 'input',
name: 'subgraphName',
// TODO: is defaulting to studio ok?
message: () => 'Subgraph slug',
initial: initSubgraphName,
validate: name => {
try {
validateSubgraphName(name, { allowSimpleName: initAllowSimpleName });
return true;
} catch (e) {
return `${e.message}
Examples:
$ graph init ${os.userInfo().username}/${name}
$ graph init ${name} --allow-simple-name`;
}
},
},
]);

const { directory } = await prompt.ask<{ directory: string }>([
{
type: 'input',
name: 'directory',
message: 'Directory to create the subgraph in',
initial: () => initDirectory || getSubgraphBasename(subgraphName),
validate: value =>
filesystem.exists(value || initDirectory || getSubgraphBasename(subgraphName))
? 'Directory already exists'
: true,
},
]);

return {
subgraphName,
directory,
};
} catch (e) {
this.error(e, { exit: 1 });
}
}

async function processInitForm(
this: InitCommand,
{
Expand Down Expand Up @@ -773,21 +841,15 @@ Make sure to visit the documentation on https://thegraph.com/docs/ for further i
async function initSubgraphFromExample(
this: InitCommand,
{
protocolInstance,
fromExample,
allowSimpleName,
subgraphName,
directory,
studio,
product,
}: {
protocolInstance: Protocol;
fromExample: string | boolean;
allowSimpleName?: boolean;
subgraphName: string;
directory: string;
studio: boolean;
product?: string;
},
{
commands,
Expand All @@ -800,8 +862,6 @@ async function initSubgraphFromExample(
};
},
) {
const isSubstreams = protocolInstance.name === 'substreams';

// Fail if the subgraph name is invalid
if (!revalidateSubgraphName.bind(this)(subgraphName, { allowSimpleName })) {
process.exitCode = 1;
Expand Down Expand Up @@ -852,20 +912,6 @@ async function initSubgraphFromExample(
return;
}

try {
// It doesn't matter if we changed the URL we clone the YAML,
// we'll check it's network anyway. If it's a studio subgraph we're dealing with.
const dataSourcesAndTemplates = await DataSourcesExtractor.fromFilePath(
path.join(directory, 'subgraph.yaml'),
);

for (const { network } of dataSourcesAndTemplates) {
validateStudioNetwork({ studio, product, network });
}
} catch (e) {
this.error(e.message, { exit: 1 });
}

const networkConf = await initNetworksConfig(directory, 'address');
if (networkConf !== true) {
this.exit(1);
Expand Down Expand Up @@ -923,13 +969,11 @@ async function initSubgraphFromExample(
return;
}

if (!isSubstreams) {
// Run code-generation
const codegen = await runCodegen(directory, commands.codegen);
if (codegen !== true) {
this.exit(1);
return;
}
// Run code-generation
const codegen = await runCodegen(directory, commands.codegen);
if (codegen !== true) {
this.exit(1);
return;
}

printNextSteps.bind(this)({ subgraphName, directory }, { commands });
Expand Down

0 comments on commit d2a1aa6

Please sign in to comment.