Skip to content

Commit

Permalink
Fix: Make rename function get filename with extension (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
kittaakos authored May 2, 2023
1 parent 15f9557 commit cdb7ed4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
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

0 comments on commit cdb7ed4

Please sign in to comment.