Skip to content

Commit 7f22631

Browse files
graph init: format subgraph name to match graph-node rules (#1962)
* format subgraph slug * changeset * fix test * remove slug formatter to keep default formatting
1 parent 0c36a02 commit 7f22631

File tree

4 files changed

+64
-32
lines changed

4 files changed

+64
-32
lines changed

.changeset/gold-birds-dream.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphprotocol/graph-cli': patch
3+
---
4+
5+
`graph init`: format and validate subgraph slug to match graph-node rules

packages/cli/src/command-helpers/subgraph.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,10 @@ export const getSubgraphBasename = (name: string) => {
22
const segments = name.split('/', 2);
33
return segments[segments.length - 1];
44
};
5+
6+
export const formatSubgraphName = (slug: string) => {
7+
return slug
8+
.toLowerCase()
9+
.replace(/\s+/g, '-')
10+
.replace(/[^a-z0-9_-]/g, '');
11+
};

packages/cli/src/commands/init.ts

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { retryWithPrompt } from '../command-helpers/retry.js';
1616
import { generateScaffold, writeScaffold } from '../command-helpers/scaffold.js';
1717
import { sortWithPriority } from '../command-helpers/sort.js';
1818
import { withSpinner } from '../command-helpers/spinner.js';
19-
import { getSubgraphBasename } from '../command-helpers/subgraph.js';
19+
import { formatSubgraphName, getSubgraphBasename } from '../command-helpers/subgraph.js';
2020
import { GRAPH_CLI_SHARED_HEADERS } from '../constants.js';
2121
import debugFactory from '../debug.js';
2222
import EthereumABI from '../protocols/ethereum/abi.js';
@@ -38,8 +38,8 @@ export default class InitCommand extends Command {
3838
static description = 'Creates a new subgraph with basic scaffolding.';
3939

4040
static args = {
41-
subgraphName: Args.string(),
42-
directory: Args.string(),
41+
argSubgraphName: Args.string(),
42+
argDirectory: Args.string(),
4343
};
4444

4545
static flags = {
@@ -116,10 +116,13 @@ export default class InitCommand extends Command {
116116

117117
async run() {
118118
const {
119-
args: { subgraphName, directory },
119+
args: { argSubgraphName, argDirectory },
120120
flags,
121121
} = await this.parse(InitCommand);
122122

123+
const subgraphName = formatSubgraphName(argSubgraphName ?? '');
124+
const directory = argDirectory ?? '';
125+
123126
const {
124127
protocol,
125128
node: nodeFlag,
@@ -343,27 +346,43 @@ async function processFromExampleInitForm(
343346
| undefined
344347
> {
345348
try {
346-
const { subgraphName } = await prompt.ask<{ subgraphName: string }>([
347-
{
348-
type: 'input',
349-
name: 'subgraphName',
350-
message: 'Subgraph slug',
351-
initial: initSubgraphName,
349+
const promptManager = new PromptManager();
350+
351+
let subgraphName = initSubgraphName;
352+
let directory = initDirectory;
353+
354+
promptManager.addStep({
355+
type: 'input',
356+
name: 'subgraphName',
357+
message: 'Subgraph slug',
358+
initial: initSubgraphName,
359+
validate: value => formatSubgraphName(value).length > 0 || 'Subgraph slug must not be empty',
360+
result: value => {
361+
value = formatSubgraphName(value);
362+
initDebugger.extend('processFromExampleInitForm')('subgraphName: %O', value);
363+
subgraphName = value;
364+
return value;
352365
},
353-
]);
354-
355-
const { directory } = await prompt.ask<{ directory: string }>([
356-
{
357-
type: 'input',
358-
name: 'directory',
359-
message: 'Directory to create the subgraph in',
360-
initial: () => initDirectory || getSubgraphBasename(subgraphName),
366+
});
367+
368+
promptManager.addStep({
369+
type: 'input',
370+
name: 'directory',
371+
message: 'Directory to create the subgraph in',
372+
initial: () => initDirectory || getSubgraphBasename(subgraphName!),
373+
validate: value => value.length > 0 || 'Directory must not be empty',
374+
result: value => {
375+
directory = value;
376+
initDebugger.extend('processFromExampleInitForm')('directory: %O', value);
377+
return value;
361378
},
362-
]);
379+
});
380+
381+
await promptManager.executeInteractive();
363382

364383
return {
365-
subgraphName,
366-
directory,
384+
subgraphName: subgraphName!,
385+
directory: directory!,
367386
};
368387
} catch (e) {
369388
this.error(e, { exit: 1 });
@@ -563,8 +582,9 @@ async function processInitForm(
563582
name: 'subgraphName',
564583
message: 'Subgraph slug',
565584
initial: initSubgraphName,
566-
validate: value => value.length > 0 || 'Subgraph slug must not be empty',
585+
validate: value => formatSubgraphName(value).length > 0 || 'Subgraph slug must not be empty',
567586
result: value => {
587+
value = formatSubgraphName(value);
568588
initDebugger.extend('processInitForm')('subgraphName: %O', value);
569589
subgraphName = value;
570590
return value;
@@ -575,7 +595,7 @@ async function processInitForm(
575595
type: 'input',
576596
name: 'directory',
577597
message: 'Directory to create the subgraph in',
578-
initial: () => initDirectory || getSubgraphBasename(subgraphName),
598+
initial: () => initDirectory || getSubgraphBasename(subgraphName!),
579599
validate: value => value.length > 0 || 'Directory must not be empty',
580600
result: value => {
581601
directory = value;

packages/cli/tests/cli/__snapshots__/init.test.ts.snap

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ exports[`Init > Ethereum > From contract 2`] = `0`;
2222

2323
exports[`Init > Ethereum > From contract 3`] = `
2424
"
25-
Subgraph user/subgraph-from-contract created in from-contract
25+
Subgraph usersubgraph-from-contract created in from-contract
2626
2727
Next steps:
2828
@@ -58,7 +58,7 @@ exports[`Init > Ethereum > From contract with abi 2`] = `0`;
5858

5959
exports[`Init > Ethereum > From contract with abi 3`] = `
6060
"
61-
Subgraph user/subgraph-from-contract-with-abi created in from-contract-with-abi
61+
Subgraph usersubgraph-from-contract-with-abi created in from-contract-with-abi
6262
6363
Next steps:
6464
@@ -94,7 +94,7 @@ exports[`Init > Ethereum > From contract with abi and structs 2`] = `0`;
9494

9595
exports[`Init > Ethereum > From contract with abi and structs 3`] = `
9696
"
97-
Subgraph user/subgraph-from-contract-with-abi-and-structs created in from-contract-with-abi-and-structs
97+
Subgraph usersubgraph-from-contract-with-abi-and-structs created in from-contract-with-abi-and-structs
9898
9999
Next steps:
100100
@@ -130,7 +130,7 @@ exports[`Init > Ethereum > From contract with index events and abi with ID in ev
130130

131131
exports[`Init > Ethereum > From contract with index events and abi with ID in events 3`] = `
132132
"
133-
Subgraph user/subgraph-from-contract-with-index-events-and-abi-with-id created in duplicate-ids
133+
Subgraph usersubgraph-from-contract-with-index-events-and-abi-with-id created in duplicate-ids
134134
135135
Next steps:
136136
@@ -166,7 +166,7 @@ exports[`Init > Ethereum > From contract with list items in abi 2`] = `0`;
166166

167167
exports[`Init > Ethereum > From contract with list items in abi 3`] = `
168168
"
169-
Subgraph user/subgraph-from-contract-with-lists-in-abi created in from-contract-with-lists-in-abi
169+
Subgraph usersubgraph-from-contract-with-lists-in-abi created in from-contract-with-lists-in-abi
170170
171171
Next steps:
172172
@@ -202,7 +202,7 @@ exports[`Init > Ethereum > From contract with overloaded elements 2`] = `0`;
202202

203203
exports[`Init > Ethereum > From contract with overloaded elements 3`] = `
204204
"
205-
Subgraph user/subgraph-from-contract-with-overloaded-elements created in from-contract-with-overloaded-elements
205+
Subgraph usersubgraph-from-contract-with-overloaded-elements created in from-contract-with-overloaded-elements
206206
207207
Next steps:
208208
@@ -236,7 +236,7 @@ exports[`Init > Ethereum > From example 2`] = `0`;
236236

237237
exports[`Init > Ethereum > From example 3`] = `
238238
"
239-
Subgraph user/example-subgraph created in from-example
239+
Subgraph userexample-subgraph created in from-example
240240
241241
Next steps:
242242
@@ -272,7 +272,7 @@ exports[`Init > NEAR > From contract 2`] = `0`;
272272

273273
exports[`Init > NEAR > From contract 3`] = `
274274
"
275-
Subgraph user/near-from-contract created in from-contract
275+
Subgraph usernear-from-contract created in from-contract
276276
277277
Next steps:
278278
@@ -304,7 +304,7 @@ exports[`Init > Substreams > From package 2`] = `0`;
304304

305305
exports[`Init > Substreams > From package 3`] = `
306306
"
307-
Subgraph user/subgraph-from-substreams created in from-package
307+
Subgraph usersubgraph-from-substreams created in from-package
308308
309309
Next steps:
310310

0 commit comments

Comments
 (0)