Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: rename function gets filename with extension #110

Merged
merged 1 commit into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,14 @@ const preprocessDestinationPath = ({entry, destination, options}) => {
@param {string|Function} rename
*/
const renameFile = (source, rename) => {
const filename = path.basename(source, path.extname(source));
const fileExtension = path.extname(source);
const directory = path.dirname(source);
if (typeof rename === 'string') {
return path.join(directory, rename);
}

if (typeof rename === 'function') {
return path.join(directory, `${rename(filename)}${fileExtension}`);
const filename = path.basename(source);
return path.join(directory, rename(filename));
}

return source;
Expand Down
19 changes: 19 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,25 @@ test('rename filenames using a function', async t => {
);
});

test('rename function receives the basename argument with the file extension', async t => {
fs.mkdirSync(t.context.tmp);
fs.writeFileSync(path.join(t.context.tmp, 'foo.js'), '');
fs.writeFileSync(path.join(t.context.tmp, 'foo.ts'), '');

const visited = [];
await cpy(['foo.js', 'foo.ts'], 'destination/subdir', {
cwd: t.context.tmp,
rename(basename) {
visited.push(basename);
return basename;
},
});

t.is(visited.length, 2);
t.true(visited.includes('foo.js'));
t.true(visited.includes('foo.ts'));
});

test('flatten directory tree', async t => {
fs.mkdirSync(t.context.tmp);
fs.mkdirSync(path.join(t.context.tmp, 'source'));
Expand Down