Skip to content

Commit d5c6413

Browse files
author
Akos Kitta
committed
fix: rename function gets filename with extension
Closes sindresorhus#109 Signed-off-by: Akos Kitta <[email protected]>
1 parent 15f9557 commit d5c6413

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,14 @@ const preprocessDestinationPath = ({entry, destination, options}) => {
118118
@param {string|Function} rename
119119
*/
120120
const renameFile = (source, rename) => {
121-
const filename = path.basename(source, path.extname(source));
122-
const fileExtension = path.extname(source);
123121
const directory = path.dirname(source);
124122
if (typeof rename === 'string') {
125123
return path.join(directory, rename);
126124
}
127125

128126
if (typeof rename === 'function') {
129-
return path.join(directory, `${rename(filename)}${fileExtension}`);
127+
const filename = path.basename(source);
128+
return path.join(directory, rename(filename));
130129
}
131130

132131
return source;

test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,25 @@ test('rename filenames using a function', async t => {
215215
);
216216
});
217217

218+
test('rename function receives the basename argument with the file extension', async t => {
219+
fs.mkdirSync(t.context.tmp);
220+
fs.writeFileSync(path.join(t.context.tmp, 'foo.js'), '');
221+
fs.writeFileSync(path.join(t.context.tmp, 'foo.ts'), '');
222+
223+
const visited = [];
224+
await cpy(['foo.js', 'foo.ts'], 'destination/subdir', {
225+
cwd: t.context.tmp,
226+
rename(basename) {
227+
visited.push(basename);
228+
return basename;
229+
},
230+
});
231+
232+
t.is(visited.length, 2);
233+
t.true(visited.includes('foo.js'));
234+
t.true(visited.includes('foo.ts'));
235+
});
236+
218237
test('flatten directory tree', async t => {
219238
fs.mkdirSync(t.context.tmp);
220239
fs.mkdirSync(path.join(t.context.tmp, 'source'));

0 commit comments

Comments
 (0)