Skip to content

Commit

Permalink
Fix "Error: no error?" message (#1889)
Browse files Browse the repository at this point in the history
* properly handle ESC and Ctrl-C on prompts

* changeset

* remove unneeded catch
  • Loading branch information
YaroShkvorets authored Jan 8, 2025
1 parent b4564a7 commit e085e39
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 95 deletions.
5 changes: 5 additions & 0 deletions .changeset/ninety-laws-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphprotocol/graph-cli': patch
---

fix confusing "no error" message when pressing Escape on prompts
78 changes: 42 additions & 36 deletions packages/cli/src/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,23 @@ export default class AddCommand extends Command {
} catch (error) {
// we cannot ask user to do prompt in test environment
if (process.env.NODE_ENV !== 'test') {
const { abi: abiFile } = await prompt.ask<{ abi: string }>([
{
type: 'input',
name: 'abi',
message: 'ABI file (path)',
validate: async (value: string) => {
try {
EthereumABI.load(contractName, value);
return true;
} catch (e) {
return `Failed to load ABI from ${value}: ${e.message}`;
}
const { abi: abiFile } = await prompt
.ask<{ abi: string }>([
{
type: 'input',
name: 'abi',
message: 'ABI file (path)',
validate: async (value: string) => {
try {
EthereumABI.load(contractName, value);
return true;
} catch (e) {
return `Failed to load ABI from ${value}: ${e.message}`;
}
},
},
},
]);
])
.catch(() => this.exit(1)); // properly handle ESC
ethabi = EthereumABI.load(contractName, abiFile);
}
}
Expand All @@ -130,18 +132,20 @@ export default class AddCommand extends Command {
// we cannot ask user to do prompt in test environment
if (process.env.NODE_ENV !== 'test') {
// If we can't get the start block, we'll just leave it out of the manifest
const { startBlock: userInputStartBlock } = await prompt.ask<{ startBlock: string }>([
{
type: 'input',
name: 'startBlock',
message: 'Start Block',
initial: '0',
validate: value => parseInt(value) >= 0,
result(value) {
return value;
const { startBlock: userInputStartBlock } = await prompt
.ask<{ startBlock: string }>([
{
type: 'input',
name: 'startBlock',
message: 'Start Block',
initial: '0',
validate: value => parseInt(value) >= 0,
result(value) {
return value;
},
},
},
]);
])
.catch(() => this.exit(1));
startBlock = userInputStartBlock;
}
}
Expand All @@ -155,18 +159,20 @@ export default class AddCommand extends Command {
} catch (error) {
// not asking user to do prompt in test environment
if (process.env.NODE_ENV !== 'test') {
const { contractName: userInputContractName } = await prompt.ask<{ contractName: string }>([
{
type: 'input',
name: 'contractName',
message: 'Contract Name',
initial: 'Contract',
validate: value => value && value.length > 0,
result(value) {
return value;
const { contractName: userInputContractName } = await prompt
.ask<{ contractName: string }>([
{
type: 'input',
name: 'contractName',
message: 'Contract Name',
initial: 'Contract',
validate: value => value && value.length > 0,
result(value) {
return value;
},
},
},
]);
])
.catch(() => this.exit(1));
contractName = userInputContractName;
}
}
Expand Down
26 changes: 14 additions & 12 deletions packages/cli/src/commands/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,20 @@ export default class AuthCommand extends Command {

const { node } = chooseNodeUrl({});

const { deployKey } = await prompt.ask<{ deployKey: string }>([
{
type: 'input',
name: 'deployKey',
message: () => 'What is your Subgraph Studio deploy key?',
required: true,
initial: initialDeployKey,
skip: this.validateStudioDeployKey(initialDeployKey),
validate: value =>
this.validateStudioDeployKey(value) || `Invalid Subgraph Studio deploy key: ${value}`,
},
]);
const { deployKey } = await prompt
.ask<{ deployKey: string }>([
{
type: 'input',
name: 'deployKey',
message: () => 'What is your Subgraph Studio deploy key?',
required: true,
initial: initialDeployKey,
skip: this.validateStudioDeployKey(initialDeployKey),
validate: value =>
this.validateStudioDeployKey(value) || `Invalid Subgraph Studio deploy key: ${value}`,
},
])
.catch(() => this.exit(1));

try {
await saveDeployKey(node!, deployKey);
Expand Down
44 changes: 24 additions & 20 deletions packages/cli/src/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,18 @@ export default class DeployCommand extends Command {
},
} = await this.parse(DeployCommand);

const { subgraphName } = await prompt.ask<{ subgraphName: string }>([
{
type: 'input',
name: 'subgraphName',
message: () => 'What is the subgraph name?',
skip: () => !!subgraphNameArg,
initial: subgraphNameArg,
required: true,
},
]);
const { subgraphName } = await prompt
.ask<{ subgraphName: string }>([
{
type: 'input',
name: 'subgraphName',
message: () => 'What is the subgraph name?',
skip: () => !!subgraphNameArg,
initial: subgraphNameArg,
required: true,
},
])
.catch(() => this.exit(1));

const { node } = chooseNodeUrl({
node: nodeFlag,
Expand Down Expand Up @@ -153,16 +155,18 @@ export default class DeployCommand extends Command {
}

// Ask for label if not on hosted service
const { versionLabel } = await prompt.ask<{ versionLabel: string }>([
{
type: 'input',
name: 'versionLabel',
message: () => 'Which version label to use? (e.g. "v0.0.1")',
initial: versionLabelFlag,
skip: () => !!versionLabelFlag,
required: true,
},
]);
const { versionLabel } = await prompt
.ask<{ versionLabel: string }>([
{
type: 'input',
name: 'versionLabel',
message: () => 'Which version label to use? (e.g. "v0.0.1")',
initial: versionLabelFlag,
skip: () => !!versionLabelFlag,
required: true,
},
])
.catch(() => this.exit(1));

const deploySubgraph = async (ipfsHash: string) => {
const spinner = print.spin(`Deploying to Graph node ${requestUrl}`);
Expand Down
42 changes: 24 additions & 18 deletions packages/cli/src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -966,12 +966,13 @@ async function initSubgraphFromExample(
};
},
) {
let overwrite = false;
if (filesystem.exists(directory)) {
overwrite = await prompt.confirm(
'Directory already exists, do you want to initialize the subgraph here (files will be overwritten) ?',
false,
);
const overwrite = await prompt
.confirm(
'Directory already exists, do you want to initialize the subgraph here (files will be overwritten) ?',
false,
)
.catch(() => false);

if (!overwrite) {
this.exit(1);
Expand Down Expand Up @@ -1005,7 +1006,7 @@ async function initSubgraphFromExample(
return { result: false, error: `Example not found: ${fromExample}` };
}

filesystem.copy(exampleSubgraphPath, directory, { overwrite });
filesystem.copy(exampleSubgraphPath, directory, { overwrite: true });
return true;
} finally {
filesystem.remove(tmpDir);
Expand Down Expand Up @@ -1129,14 +1130,17 @@ async function initSubgraphFromContract(
},
) {
const isComposedSubgraph = protocolInstance.isComposedSubgraph();
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);
if (filesystem.exists(directory)) {
const overwrite = await prompt
.confirm(
'Directory already exists, do you want to initialize the subgraph here (files will be overwritten) ?',
false,
)
.catch(() => false);

if (!overwrite) {
this.exit(1);
}
}

let entities: string[] | undefined;
Expand Down Expand Up @@ -1232,10 +1236,12 @@ async function initSubgraphFromContract(
}

while (addContract) {
addContract = await addAnotherContract.bind(this)({
protocolInstance,
directory,
});
addContract = await addAnotherContract
.bind(this)({
protocolInstance,
directory,
})
.catch(() => false);
}
}

Expand Down
20 changes: 11 additions & 9 deletions packages/cli/src/commands/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,17 @@ export default class PublishCommand extends Command {
protocolNetwork: string | undefined;
apiKey: string | undefined;
}) {
const { openBrowser } = await prompt.ask<{ openBrowser: boolean }>([
{
type: 'confirm',
name: 'openBrowser',
message: () => `Open up the browser to continue publishing ?`,
initial: true,
required: true,
},
]);
const { openBrowser } = await prompt
.ask<{ openBrowser: boolean }>([
{
type: 'confirm',
name: 'openBrowser',
message: () => `Open up the browser to continue publishing ?`,
initial: true,
required: true,
},
])
.catch(() => this.exit(1));

if (!openBrowser) {
this.exit(0);
Expand Down

0 comments on commit e085e39

Please sign in to comment.