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

graph init: format subgraph name to match graph-node rules #1962

Merged
merged 4 commits into from
Feb 11, 2025
Merged
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
5 changes: 5 additions & 0 deletions .changeset/gold-birds-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphprotocol/graph-cli': patch
---

`graph init`: format and validate subgraph slug to match graph-node rules
7 changes: 7 additions & 0 deletions packages/cli/src/command-helpers/subgraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,10 @@ export const getSubgraphBasename = (name: string) => {
const segments = name.split('/', 2);
return segments[segments.length - 1];
};

export const formatSubgraphName = (slug: string) => {
return slug
.toLowerCase()
.replace(/\s+/g, '-')
.replace(/[^a-z0-9_-]/g, '');
};
66 changes: 43 additions & 23 deletions packages/cli/src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { retryWithPrompt } from '../command-helpers/retry.js';
import { generateScaffold, writeScaffold } from '../command-helpers/scaffold.js';
import { sortWithPriority } from '../command-helpers/sort.js';
import { withSpinner } from '../command-helpers/spinner.js';
import { getSubgraphBasename } from '../command-helpers/subgraph.js';
import { formatSubgraphName, getSubgraphBasename } from '../command-helpers/subgraph.js';
import { GRAPH_CLI_SHARED_HEADERS } from '../constants.js';
import debugFactory from '../debug.js';
import EthereumABI from '../protocols/ethereum/abi.js';
Expand All @@ -38,8 +38,8 @@ export default class InitCommand extends Command {
static description = 'Creates a new subgraph with basic scaffolding.';

static args = {
subgraphName: Args.string(),
directory: Args.string(),
argSubgraphName: Args.string(),
argDirectory: Args.string(),
};

static flags = {
Expand Down Expand Up @@ -116,10 +116,13 @@ export default class InitCommand extends Command {

async run() {
const {
args: { subgraphName, directory },
args: { argSubgraphName, argDirectory },
flags,
} = await this.parse(InitCommand);

const subgraphName = formatSubgraphName(argSubgraphName ?? '');
const directory = argDirectory ?? '';

const {
protocol,
node: nodeFlag,
Expand Down Expand Up @@ -343,27 +346,43 @@ async function processFromExampleInitForm(
| undefined
> {
try {
const { subgraphName } = await prompt.ask<{ subgraphName: string }>([
{
type: 'input',
name: 'subgraphName',
message: 'Subgraph slug',
initial: initSubgraphName,
const promptManager = new PromptManager();

let subgraphName = initSubgraphName;
let directory = initDirectory;

promptManager.addStep({
type: 'input',
name: 'subgraphName',
message: 'Subgraph slug',
initial: initSubgraphName,
validate: value => formatSubgraphName(value).length > 0 || 'Subgraph slug must not be empty',
result: value => {
value = formatSubgraphName(value);
initDebugger.extend('processFromExampleInitForm')('subgraphName: %O', value);
subgraphName = value;
return value;
},
]);

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

promptManager.addStep({
type: 'input',
name: 'directory',
message: 'Directory to create the subgraph in',
initial: () => initDirectory || getSubgraphBasename(subgraphName!),
validate: value => value.length > 0 || 'Directory must not be empty',
result: value => {
directory = value;
initDebugger.extend('processFromExampleInitForm')('directory: %O', value);
return value;
},
]);
});

await promptManager.executeInteractive();

return {
subgraphName,
directory,
subgraphName: subgraphName!,
directory: directory!,
};
} catch (e) {
this.error(e, { exit: 1 });
Expand Down Expand Up @@ -563,8 +582,9 @@ async function processInitForm(
name: 'subgraphName',
message: 'Subgraph slug',
initial: initSubgraphName,
validate: value => value.length > 0 || 'Subgraph slug must not be empty',
validate: value => formatSubgraphName(value).length > 0 || 'Subgraph slug must not be empty',
result: value => {
value = formatSubgraphName(value);
initDebugger.extend('processInitForm')('subgraphName: %O', value);
subgraphName = value;
return value;
Expand All @@ -575,7 +595,7 @@ async function processInitForm(
type: 'input',
name: 'directory',
message: 'Directory to create the subgraph in',
initial: () => initDirectory || getSubgraphBasename(subgraphName),
initial: () => initDirectory || getSubgraphBasename(subgraphName!),
validate: value => value.length > 0 || 'Directory must not be empty',
result: value => {
directory = value;
Expand Down
18 changes: 9 additions & 9 deletions packages/cli/tests/cli/__snapshots__/init.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ exports[`Init > Ethereum > From contract 2`] = `0`;

exports[`Init > Ethereum > From contract 3`] = `
"
Subgraph user/subgraph-from-contract created in from-contract
Subgraph usersubgraph-from-contract created in from-contract

Next steps:

Expand Down Expand Up @@ -58,7 +58,7 @@ exports[`Init > Ethereum > From contract with abi 2`] = `0`;

exports[`Init > Ethereum > From contract with abi 3`] = `
"
Subgraph user/subgraph-from-contract-with-abi created in from-contract-with-abi
Subgraph usersubgraph-from-contract-with-abi created in from-contract-with-abi

Next steps:

Expand Down Expand Up @@ -94,7 +94,7 @@ exports[`Init > Ethereum > From contract with abi and structs 2`] = `0`;

exports[`Init > Ethereum > From contract with abi and structs 3`] = `
"
Subgraph user/subgraph-from-contract-with-abi-and-structs created in from-contract-with-abi-and-structs
Subgraph usersubgraph-from-contract-with-abi-and-structs created in from-contract-with-abi-and-structs

Next steps:

Expand Down Expand Up @@ -130,7 +130,7 @@ exports[`Init > Ethereum > From contract with index events and abi with ID in ev

exports[`Init > Ethereum > From contract with index events and abi with ID in events 3`] = `
"
Subgraph user/subgraph-from-contract-with-index-events-and-abi-with-id created in duplicate-ids
Subgraph usersubgraph-from-contract-with-index-events-and-abi-with-id created in duplicate-ids

Next steps:

Expand Down Expand Up @@ -166,7 +166,7 @@ exports[`Init > Ethereum > From contract with list items in abi 2`] = `0`;

exports[`Init > Ethereum > From contract with list items in abi 3`] = `
"
Subgraph user/subgraph-from-contract-with-lists-in-abi created in from-contract-with-lists-in-abi
Subgraph usersubgraph-from-contract-with-lists-in-abi created in from-contract-with-lists-in-abi

Next steps:

Expand Down Expand Up @@ -202,7 +202,7 @@ exports[`Init > Ethereum > From contract with overloaded elements 2`] = `0`;

exports[`Init > Ethereum > From contract with overloaded elements 3`] = `
"
Subgraph user/subgraph-from-contract-with-overloaded-elements created in from-contract-with-overloaded-elements
Subgraph usersubgraph-from-contract-with-overloaded-elements created in from-contract-with-overloaded-elements

Next steps:

Expand Down Expand Up @@ -236,7 +236,7 @@ exports[`Init > Ethereum > From example 2`] = `0`;

exports[`Init > Ethereum > From example 3`] = `
"
Subgraph user/example-subgraph created in from-example
Subgraph userexample-subgraph created in from-example

Next steps:

Expand Down Expand Up @@ -272,7 +272,7 @@ exports[`Init > NEAR > From contract 2`] = `0`;

exports[`Init > NEAR > From contract 3`] = `
"
Subgraph user/near-from-contract created in from-contract
Subgraph usernear-from-contract created in from-contract

Next steps:

Expand Down Expand Up @@ -304,7 +304,7 @@ exports[`Init > Substreams > From package 2`] = `0`;

exports[`Init > Substreams > From package 3`] = `
"
Subgraph user/subgraph-from-substreams created in from-package
Subgraph usersubgraph-from-substreams created in from-package

Next steps:

Expand Down