diff --git a/.changeset/soft-scissors-help.md b/.changeset/soft-scissors-help.md new file mode 100644 index 000000000..481426569 --- /dev/null +++ b/.changeset/soft-scissors-help.md @@ -0,0 +1,5 @@ +--- +'@graphprotocol/graph-cli': patch +--- + +Improve ABI file path validation diff --git a/packages/cli/src/commands/init.ts b/packages/cli/src/commands/init.ts index a739b9972..200445a4c 100644 --- a/packages/cli/src/commands/init.ts +++ b/packages/cli/src/commands/init.ts @@ -623,47 +623,34 @@ async function processInitForm( type: 'input', name: 'abi', message: 'ABI file (path)', - initial: initAbi, + initial: initAbiPath, skip: () => !protocolInstance.hasABIs() || initFromExample !== undefined || abiFromEtherscan !== undefined || - isSubstreams || - !!initAbiPath, + isSubstreams, validate: async (value: string) => { if (initFromExample || abiFromEtherscan || !protocolInstance.hasABIs()) { return true; } const ABI = protocolInstance.getABI(); - if (initAbiPath) { - try { - loadAbiFromFile(ABI, initAbiPath); - return true; - } catch (e) { - this.error(e.message); - } - } + if (initAbiPath) value = initAbiPath; try { loadAbiFromFile(ABI, value); return true; } catch (e) { - this.error(e.message); + return e.message; } }, result: async (value: string) => { if (initFromExample || abiFromEtherscan || !protocolInstance.hasABIs()) { return null; } + const ABI = protocolInstance.getABI(); - if (initAbiPath) { - try { - return loadAbiFromFile(ABI, initAbiPath); - } catch (e) { - return e.message; - } - } + if (initAbiPath) value = initAbiPath; try { return loadAbiFromFile(ABI, value); diff --git a/packages/cli/src/protocols/ethereum/abi.ts b/packages/cli/src/protocols/ethereum/abi.ts index 6208f29ee..50d736dc5 100644 --- a/packages/cli/src/protocols/ethereum/abi.ts +++ b/packages/cli/src/protocols/ethereum/abi.ts @@ -141,7 +141,13 @@ export default class ABI { } static load(name: string, file: string) { - const data = JSON.parse(fs.readFileSync(file).toString()); + let data; + try { + data = JSON.parse(fs.readFileSync(file).toString()); + } catch (e) { + throw Error(`Could not parse ABI: ${e}`); + } + const abi = ABI.normalized(data); if (abi === null || abi === undefined) {