sindresorhus / cpy-cli

Copy files
MIT License
344 stars 33 forks source link

npx cpy-cli --nooverwrite produces an error when file exists. Expected skip #45

Open jafin opened 1 year ago

jafin commented 1 year ago

Platform

windows 11 22H2
cpy-cli@5.0.0

When I execute

npx cpy-cli .\test.json foo\ --no-overwrite

it returns the error

Cannot copy from `test.json` to `d:\temp\foo\test.json`: 
Cannot write to `d:\temp\foo\test.json`: EEXIST: file already exists, open 'd:\temp\foo\test.json'

Issuing the command without --no-overwrite works fine (but the file is overwritten)

Expected

when setting the --no-overwrite parameter it should continue without error even if the file exists.

My Misunderstanding of purpose?

I found the test for cpy, it appears it is expected that overwrite:false will throw an exception? If so is there any way to 'skip/ignore' existing files similar to how the non async approach appears to work?

test('do not overwrite', async t => {
    fs.mkdirSync(t.context.tmp);
    fs.writeFileSync(path.join(t.context.tmp, 'license'), '');

    await t.throwsAsync(cpy(['license'], t.context.tmp, {overwrite: false}));

    t.is(read(t.context.tmp, 'license'), '');
});

EDIT: reading cp-file code, if I understand async copy overwrite false will throw an exception, but sync copy overwrite will not. If that is correct, then this was just a misunderstanding, please close the issue.

sindresorhus commented 1 year ago

It's working as intended, but we could maybe add another flag to not throw when the file already exists. For example, inspired by rsync:

--ignore-existing skip updating files that already exist on receiver

staff0rd commented 6 months ago

I am interested in adding such functionality, but i'm uncertain where it should go after seeing the dependency chain: cpy-cli > cpy > copy-file.