From 8b5a5e75b830f989e20692c3986b469a8b2bee8b Mon Sep 17 00:00:00 2001 From: "Rodrigo Q. Saramago" Date: Thu, 29 Sep 2022 14:27:25 +0200 Subject: [PATCH] Add overwrite option --- solc.ts | 30 +++++++++++++++++++----------- test/cli.ts | 23 +++++++++++++++++++++++ 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/solc.ts b/solc.ts index 10dc8ed3..e54f9e4a 100755 --- a/solc.ts +++ b/solc.ts @@ -44,6 +44,7 @@ program 'When using a package manager to install libraries, use this option to specify directories where packages are installed. ' + 'Can be used multiple times to provide multiple locations.' ) + .option('--overwrite', 'If artifacts already exist on disk, overwrite them.', false) .option('-o, --output-dir ', 'Output directory for the contracts.') .option('-p, --pretty-json', 'Pretty-print all JSON output.', false) .option('-v, --verbose', 'More detailed console output.', false); @@ -224,26 +225,33 @@ if (!output) { fs.mkdirSync(destination, { recursive: true }); -function writeFile (file, content) { - file = path.join(destination, file); +function writeFile (file, extension, content) { + file = path.join(destination, `${file}.${extension}`); + + if (fs.existsSync(file) && !options.overwrite) { + throw new Error(`Refusing to overwrite existing file ${file} (use --overwrite to force).`); + } + fs.writeFile(file, content, function (err) { if (err) { - console.error('Failed to write ' + file + ': ' + err); + throw new Error(`Failed to write ${file}: ${err}`); } }); } for (const fileName in output.contracts) { for (const contractName in output.contracts[fileName]) { - let contractFileName = fileName + ':' + contractName; - contractFileName = contractFileName.replace(/[:./\\]/g, '_'); - - if (options.bin) { - writeFile(contractFileName + '.bin', output.contracts[fileName][contractName].evm.bytecode.object); - } + try { + if (options.bin) { + writeFile(contractName, 'bin', output.contracts[fileName][contractName].evm.bytecode.object); + } - if (options.abi) { - writeFile(contractFileName + '.abi', toFormattedJson(output.contracts[fileName][contractName].abi)); + if (options.abi) { + writeFile(contractName, 'abi', toFormattedJson(output.contracts[fileName][contractName].abi)); + } + } catch (err) { + console.error(err.message); + hasError = true; } } } diff --git a/test/cli.ts b/test/cli.ts index 42844565..685b59d1 100644 --- a/test/cli.ts +++ b/test/cli.ts @@ -1,6 +1,8 @@ import tape from 'tape'; import spawn from 'tape-spawn'; import rimraf from 'rimraf'; +import tmp from 'tmp'; +import fs from 'fs'; import * as path from 'path'; import solc from '../'; @@ -278,4 +280,25 @@ tape('CLI', function (t) { spt.end(); }); }); + + t.test('attempt to overwrite without --overwrite flag', function (st) { + const cwd = tmp.dirSync({ unsafeCleanup: true }).name; + // create a fake C.bin to cause name collision + fs.openSync(`${cwd}/C.bin`, 'w'); + + const spt = spawn(st, `node ${solcjs} --bin ${dist}/test/resources/fixtureSmoke.sol`, { cwd }); + spt.stderr.match(/^Refusing to overwrite existing file C\.bin \(use --overwrite to force\)\./); + spt.end(); + }); + + t.test('--overwrite', function (st) { + const cwd = tmp.dirSync({ unsafeCleanup: true }).name; + // create a fake C.bin to cause name collision + fs.openSync(`${cwd}/C.bin`, 'w'); + + const spt = spawn(st, `node ${solcjs} --bin ${dist}/test/resources/fixtureSmoke.sol --overwrite`, { cwd }); + spt.stderr.empty(); + spt.succeeds(); + spt.end(); + }); });