Skip to content
Open
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/early-init-directory-validation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphprotocol/graph-cli": patch
---

Check for existing paths at the directory prompt during interactive `graph init`, allowing users to choose another location before contract information is fetched.
41 changes: 39 additions & 2 deletions packages/cli/src/command-helpers/subgraph.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { describe, expect, it } from 'vitest';
import { formatContractName, formatSubgraphName, getSubgraphBasename } from './subgraph';
import os from 'node:os';
import path from 'node:path';
import fs from 'fs-extra';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import {
formatContractName,
formatSubgraphName,
getSubgraphBasename,
validateSubgraphDirectory,
} from './subgraph';

describe('getSubgraphBasename', () => {
it('returns the last segment of a subgraph name', () => {
Expand Down Expand Up @@ -48,3 +56,32 @@ describe('formatContractName', () => {
expect(formatContractName('MyContractName')).toBe('MyContractName');
});
});

describe('validateSubgraphDirectory', () => {
let dir: string;
beforeEach(async () => {
dir = await fs.mkdtemp(path.join(os.tmpdir(), 'graph-init-'));
});
afterEach(async () => {
await fs.remove(dir);
});

it('rejects an empty directory', () => {
expect(validateSubgraphDirectory('')).toBe('Directory must not be empty');
});
it('rejects an existing directory at the directory prompt', () => {
expect(validateSubgraphDirectory(dir)).toContain('Directory already exists');
});
it('rejects an existing regular file', async () => {
const file = path.join(dir, 'file');
await fs.writeFile(file, 'keep this content');
expect(validateSubgraphDirectory(file)).toContain('Directory already exists');
expect(await fs.readFile(file, 'utf8')).toBe('keep this content');
});
it('accepts a new directory', () => {
expect(validateSubgraphDirectory(path.join(dir, 'new'))).toBe(true);
});
it('accepts nested directories that have not been created yet', () => {
expect(validateSubgraphDirectory(path.join(dir, 'new', 'nested'))).toBe(true);
});
});
8 changes: 8 additions & 0 deletions packages/cli/src/command-helpers/subgraph.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import fs from 'node:fs';

export const getSubgraphBasename = (name: string) => {
const segments = name.split('/', 2);
return segments[segments.length - 1];
Expand All @@ -13,3 +15,9 @@ export const formatSubgraphName = (slug: string) => {
export const formatContractName = (contractName: string) => {
return contractName.replace(/[\s.]+/g, '_').replace(/[^a-zA-Z0-9_-]/g, '');
};

export const validateSubgraphDirectory = (directory: string): true | string => {
if (directory.length === 0) return 'Directory must not be empty';
if (fs.existsSync(directory)) return 'Directory already exists. Please choose a new directory.';
return true;
};
5 changes: 3 additions & 2 deletions packages/cli/src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
formatContractName,
formatSubgraphName,
getSubgraphBasename,
validateSubgraphDirectory,
} from '../command-helpers/subgraph.js';
import { GRAPH_CLI_SHARED_HEADERS } from '../constants.js';
import debugFactory from '../debug.js';
Expand Down Expand Up @@ -415,7 +416,7 @@ async function processFromExampleInitForm(
name: 'directory',
message: 'Directory to create the subgraph in',
initial: () => initDirectory || getSubgraphBasename(subgraphName!),
validate: value => value.length > 0 || 'Directory must not be empty',
validate: validateSubgraphDirectory,
result: value => {
directory = value;
initDebugger.extend('processFromExampleInitForm')('directory: %O', value);
Expand Down Expand Up @@ -641,7 +642,7 @@ async function processInitForm(
name: 'directory',
message: 'Directory to create the subgraph in',
initial: () => initDirectory || getSubgraphBasename(subgraphName!),
validate: value => value.length > 0 || 'Directory must not be empty',
validate: validateSubgraphDirectory,
result: value => {
directory = value;
initDebugger.extend('processInitForm')('directory: %O', value);
Expand Down