Skip to content

Commit

Permalink
Fix flat option when copying a single file (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
dpilafian committed Mar 7, 2022
1 parent 84fe6ed commit 2014a4f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ const preprocessDestinationPath = ({entry, destination, options}) => {
return path.join(options.cwd, destination, path.basename(entry.pattern.originalPath), path.relative(entry.pattern.originalPath, entry.path));
}

if (!entry.pattern.isDirectory && options.flat) {
return path.join(options.cwd, destination, path.basename(entry.pattern.originalPath));
}

return path.join(options.cwd, destination, path.relative(options.cwd, entry.path));
};

Expand Down
19 changes: 19 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,25 @@ test('flatten directory tree', async t => {
);
});

test('flatten single file', async t => {
fs.mkdirSync(t.context.tmp);
fs.mkdirSync(path.join(t.context.tmp, 'source'));
fs.writeFileSync(
path.join(t.context.tmp, 'source/bar.js'),
'console.log("bar");',
);

await cpy('source/bar.js', 'destination', {
cwd: t.context.tmp,
flat: true,
});

t.is(
read(t.context.tmp, 'source/bar.js'),
read(t.context.tmp, 'destination/bar.js'),
);
});

// TODO: Enable again when ESM supports mocking.
// eslint-disable-next-line ava/no-skip-test
test.skip('cp-file errors are CpyErrors', async t => {
Expand Down

0 comments on commit 2014a4f

Please sign in to comment.