diff --git a/.changeset/cyan-jobs-report.md b/.changeset/cyan-jobs-report.md new file mode 100644 index 000000000..2b698200a --- /dev/null +++ b/.changeset/cyan-jobs-report.md @@ -0,0 +1,5 @@ +--- +'@graphprotocol/graph-cli': patch +--- + +Add prompt for existing directory in `init` flow diff --git a/packages/cli/src/commands/init.ts b/packages/cli/src/commands/init.ts index f7ad931b1..a739b9972 100644 --- a/packages/cli/src/commands/init.ts +++ b/packages/cli/src/commands/init.ts @@ -387,10 +387,6 @@ async function processFromExampleInitForm( name: 'directory', message: 'Directory to create the subgraph in', initial: () => initDirectory || getSubgraphBasename(subgraphName), - validate: value => - filesystem.exists(value || initDirectory || getSubgraphBasename(subgraphName)) - ? 'Directory already exists' - : true, }, ]); @@ -506,10 +502,6 @@ async function processInitForm( name: 'directory', message: 'Directory to create the subgraph in', initial: () => initDirectory || getSubgraphBasename(subgraphName), - validate: value => - filesystem.exists(value || initDirectory || getSubgraphBasename(subgraphName)) - ? 'Directory already exists' - : true, }, ]); @@ -889,9 +881,17 @@ async function initSubgraphFromExample( }; }, ) { - // Fail if the output directory already exists + let overwrite = false; if (filesystem.exists(directory)) { - this.error(`Directory or file "${directory}" already exists`, { exit: 1 }); + overwrite = await prompt.confirm( + 'Directory already exists, do you want to initialize the subgraph here (files will be overwritten) ?', + false, + ); + + if (!overwrite) { + this.exit(1); + return; + } } // Clone the example subgraph repository @@ -921,7 +921,7 @@ async function initSubgraphFromExample( return { result: false, error: `Example not found: ${fromExample}` }; } - filesystem.copy(exampleSubgraphPath, directory); + filesystem.copy(exampleSubgraphPath, directory, { overwrite }); return true; } finally { filesystem.remove(tmpDir); @@ -1050,9 +1050,15 @@ async function initSubgraphFromContract( ) { const isSubstreams = protocolInstance.name === 'substreams'; - // Fail if the output directory already exists - if (filesystem.exists(directory)) { - this.error(`Directory or file "${directory}" already exists`, { exit: 1 }); + if ( + filesystem.exists(directory) && + !(await prompt.confirm( + 'Directory already exists, do you want to initialize the subgraph here (files will be overwritten) ?', + false, + )) + ) { + this.exit(1); + return; } if (