diff --git a/.changeset/early-init-directory-validation.md b/.changeset/early-init-directory-validation.md new file mode 100644 index 000000000..8c47893a7 --- /dev/null +++ b/.changeset/early-init-directory-validation.md @@ -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. diff --git a/packages/cli/src/command-helpers/subgraph.test.ts b/packages/cli/src/command-helpers/subgraph.test.ts index b3ac994e7..74a0162b5 100644 --- a/packages/cli/src/command-helpers/subgraph.test.ts +++ b/packages/cli/src/command-helpers/subgraph.test.ts @@ -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', () => { @@ -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); + }); +}); diff --git a/packages/cli/src/command-helpers/subgraph.ts b/packages/cli/src/command-helpers/subgraph.ts index 2a72b58b3..69fd70398 100644 --- a/packages/cli/src/command-helpers/subgraph.ts +++ b/packages/cli/src/command-helpers/subgraph.ts @@ -1,3 +1,5 @@ +import fs from 'node:fs'; + export const getSubgraphBasename = (name: string) => { const segments = name.split('/', 2); return segments[segments.length - 1]; @@ -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; +}; diff --git a/packages/cli/src/commands/init.ts b/packages/cli/src/commands/init.ts index 8a5e23ab0..01cc0322c 100644 --- a/packages/cli/src/commands/init.ts +++ b/packages/cli/src/commands/init.ts @@ -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'; @@ -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); @@ -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);