Skip to content

Commit 8b5a5e7

Browse files
committed
Add overwrite option
1 parent 19775fc commit 8b5a5e7

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

solc.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ program
4444
'When using a package manager to install libraries, use this option to specify directories where packages are installed. ' +
4545
'Can be used multiple times to provide multiple locations.'
4646
)
47+
.option('--overwrite', 'If artifacts already exist on disk, overwrite them.', false)
4748
.option('-o, --output-dir <output-directory>', 'Output directory for the contracts.')
4849
.option('-p, --pretty-json', 'Pretty-print all JSON output.', false)
4950
.option('-v, --verbose', 'More detailed console output.', false);
@@ -224,26 +225,33 @@ if (!output) {
224225

225226
fs.mkdirSync(destination, { recursive: true });
226227

227-
function writeFile (file, content) {
228-
file = path.join(destination, file);
228+
function writeFile (file, extension, content) {
229+
file = path.join(destination, `${file}.${extension}`);
230+
231+
if (fs.existsSync(file) && !options.overwrite) {
232+
throw new Error(`Refusing to overwrite existing file ${file} (use --overwrite to force).`);
233+
}
234+
229235
fs.writeFile(file, content, function (err) {
230236
if (err) {
231-
console.error('Failed to write ' + file + ': ' + err);
237+
throw new Error(`Failed to write ${file}: ${err}`);
232238
}
233239
});
234240
}
235241

236242
for (const fileName in output.contracts) {
237243
for (const contractName in output.contracts[fileName]) {
238-
let contractFileName = fileName + ':' + contractName;
239-
contractFileName = contractFileName.replace(/[:./\\]/g, '_');
240-
241-
if (options.bin) {
242-
writeFile(contractFileName + '.bin', output.contracts[fileName][contractName].evm.bytecode.object);
243-
}
244+
try {
245+
if (options.bin) {
246+
writeFile(contractName, 'bin', output.contracts[fileName][contractName].evm.bytecode.object);
247+
}
244248

245-
if (options.abi) {
246-
writeFile(contractFileName + '.abi', toFormattedJson(output.contracts[fileName][contractName].abi));
249+
if (options.abi) {
250+
writeFile(contractName, 'abi', toFormattedJson(output.contracts[fileName][contractName].abi));
251+
}
252+
} catch (err) {
253+
console.error(err.message);
254+
hasError = true;
247255
}
248256
}
249257
}

test/cli.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import tape from 'tape';
22
import spawn from 'tape-spawn';
33
import rimraf from 'rimraf';
4+
import tmp from 'tmp';
5+
import fs from 'fs';
46
import * as path from 'path';
57
import solc from '../';
68

@@ -278,4 +280,25 @@ tape('CLI', function (t) {
278280
spt.end();
279281
});
280282
});
283+
284+
t.test('attempt to overwrite without --overwrite flag', function (st) {
285+
const cwd = tmp.dirSync({ unsafeCleanup: true }).name;
286+
// create a fake C.bin to cause name collision
287+
fs.openSync(`${cwd}/C.bin`, 'w');
288+
289+
const spt = spawn(st, `node ${solcjs} --bin ${dist}/test/resources/fixtureSmoke.sol`, { cwd });
290+
spt.stderr.match(/^Refusing to overwrite existing file C\.bin \(use --overwrite to force\)\./);
291+
spt.end();
292+
});
293+
294+
t.test('--overwrite', function (st) {
295+
const cwd = tmp.dirSync({ unsafeCleanup: true }).name;
296+
// create a fake C.bin to cause name collision
297+
fs.openSync(`${cwd}/C.bin`, 'w');
298+
299+
const spt = spawn(st, `node ${solcjs} --bin ${dist}/test/resources/fixtureSmoke.sol --overwrite`, { cwd });
300+
spt.stderr.empty();
301+
spt.succeeds();
302+
spt.end();
303+
});
281304
});

0 commit comments

Comments
 (0)