Skip to content

Commit

Permalink
Add overwrite option
Browse files Browse the repository at this point in the history
  • Loading branch information
r0qs committed Sep 29, 2022
1 parent ea371fd commit 440c739
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
30 changes: 19 additions & 11 deletions solc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>', 'Output directory for the contracts.')
.option('-p, --pretty-json', 'Pretty-print all JSON output.', false)
.option('-v, --verbose', 'More detailed console output.', false);
Expand Down Expand Up @@ -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;
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions test/cli.ts
Original file line number Diff line number Diff line change
@@ -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 '../';

Expand Down Expand Up @@ -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();
});
});

0 comments on commit 440c739

Please sign in to comment.