From d5c64135992af21e86ae2d94bbd9fec3029fc917 Mon Sep 17 00:00:00 2001 From: Akos Kitta Date: Wed, 26 Apr 2023 17:07:59 +0200 Subject: [PATCH] fix: rename function gets filename with extension Closes sindresorhus/cpy#109 Signed-off-by: Akos Kitta --- index.js | 5 ++--- test.js | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 4abb713..8efd4df 100644 --- a/index.js +++ b/index.js @@ -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; diff --git a/test.js b/test.js index 4c0c01a..bd10338 100644 --- a/test.js +++ b/test.js @@ -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'));