Skip to content

Commit 93dcd65

Browse files
committed
feat(cli): delegate commands to gnd by default
`graph <cmd>` now execs the matching `gnd <cmd>` (the Rust `@graphprotocol/gnd` binary). Two exceptions stay on the oclif/TS path: `local`, which has no gnd equivalent, and `dev`, which is a thin oclif shim that spawns `gnd dev` so the command is discoverable from `graph --help`. Set `GRAPH_CLI_IGNORE_GND=1` to force the TS implementation everywhere; the test suite does this automatically.
1 parent 1714b50 commit 93dcd65

5 files changed

Lines changed: 66 additions & 2 deletions

File tree

packages/cli/CLAUDE.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,23 @@ CLI for building and deploying subgraphs to The Graph Network.
66

77
Built on [oclif](https://oclif.io/) command framework. Entry point: `bin/run.js` -> `dist/commands/`.
88

9+
## gnd dispatch
10+
11+
By default, `graph <cmd>` execs `gnd <cmd>` (the Rust `@graphprotocol/gnd`
12+
binary). Exceptions:
13+
14+
- `graph local` always runs the TypeScript implementation — gnd has no
15+
equivalent command.
16+
- `graph dev` is an oclif shim that spawns `gnd dev`, so the command is
17+
discoverable from `graph --help` even when running in TS mode.
18+
19+
Set `GRAPH_CLI_IGNORE_GND=1` to force every command through the oclif/TS
20+
implementation. The test suite sets this automatically via
21+
`tests/cli/globalSetup.ts`.
22+
23+
The dispatch lives in `bin/run.js` and shares the spawn helper in
24+
`src/command-helpers/gnd.ts` with `src/commands/dev.ts`.
25+
926
## Key Directories
1027

1128
```
@@ -34,7 +51,8 @@ src/
3451
| `add` | Add data source to manifest |
3552
| `remove` | Remove data source from manifest |
3653
| `auth` | Set deploy key |
37-
| `local` | Manage local Graph Node |
54+
| `local` | Manage local Graph Node (always TS — gnd has no equivalent) |
55+
| `dev` | Run graph-node in dev mode (delegates to `gnd dev`) |
3856
| `clean` | Remove build artifacts |
3957

4058
## Protocol System

packages/cli/bin/run.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import process from 'node:process';
33
import semver from 'semver';
44
import { execute } from '@oclif/core';
55
import { nodeVersion } from '../dist/version.js';
6+
import { runGnd } from '../dist/command-helpers/gnd.js';
67

78
if (!semver.satisfies(process.version, nodeVersion)) {
89
process.stderr.write(
@@ -11,4 +12,11 @@ if (!semver.satisfies(process.version, nodeVersion)) {
1112
process.exit(1);
1213
}
1314

14-
await execute({ dir: import.meta.url });
15+
const args = process.argv.slice(2);
16+
const useGnd = !process.env.GRAPH_CLI_IGNORE_GND && args[0] !== 'local';
17+
18+
if (useGnd) {
19+
runGnd(args);
20+
} else {
21+
await execute({ dir: import.meta.url });
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { spawn } from 'node:child_process';
2+
import { createRequire } from 'node:module';
3+
import process from 'node:process';
4+
5+
const require = createRequire(import.meta.url);
6+
7+
export function runGnd(args: string[]): never {
8+
const gndEntry = require.resolve('@graphprotocol/gnd/bin/gnd.js');
9+
const child = spawn(process.execPath, [gndEntry, ...args], { stdio: 'inherit' });
10+
child.on('exit', (code, signal) => {
11+
if (signal) {
12+
process.kill(process.pid, signal);
13+
} else {
14+
process.exit(code ?? 1);
15+
}
16+
});
17+
child.on('error', err => {
18+
process.stderr.write(`Failed to launch gnd: ${err.message}\n`);
19+
process.exit(1);
20+
});
21+
return new Promise<never>(() => {}) as never;
22+
}

packages/cli/src/commands/dev.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Command } from '@oclif/core';
2+
import { runGnd } from '../command-helpers/gnd.js';
3+
4+
export default class DevCommand extends Command {
5+
static description =
6+
'Run graph-node in dev mode. Delegates to `gnd dev`; run `graph dev --help` (without GRAPH_CLI_IGNORE_GND set) for the full list of supported flags.';
7+
8+
static strict = false;
9+
10+
static args = {};
11+
12+
async run() {
13+
runGnd(['dev', ...this.argv]);
14+
}
15+
}

packages/cli/tests/cli/globalSetup.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ import { linkCli } from './link';
44

55
export default async () => {
66
process.env.GRAPH_CLI_TESTS = '1';
7+
process.env.GRAPH_CLI_IGNORE_GND = '1';
78
await linkCli();
89
};

0 commit comments

Comments
 (0)