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

when rename is a function, basename does not contain the file extension from 9.x #109

Closed
kittaakos opened this issue Apr 24, 2023 · 1 comment · Fixed by #110
Closed

Comments

@kittaakos
Copy link
Contributor

kittaakos commented Apr 24, 2023

Hi there,

Thank you for the great lib.

After bumping from 8.1.2 to 9.0.1, I noticed that when rename is a function, the basename argument no longer contains the file extension. Is it by design or a bug? The Breaking section of the release notes does not mention it.

8.1.2:

cpy/index.js

Lines 41 to 47 in 94fff3a

let basename = path.basename(source);
if (typeof options.rename === 'string') {
basename = options.rename;
} else if (typeof options.rename === 'function') {
basename = options.rename(basename);
}

9.0.1:

cpy/index.js

Lines 121 to 130 in 0a3dcbe

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}`);
}

index.js:

const { deepEqual } = require('assert');
const { promises: fs } = require('fs');
const { tmpdir } = require('os');
const { join } = require('path');

(async () => {
    const cpy = await import('cpy');
    const sourceDir = await fs.mkdtemp(join(tmpdir(), 'cpy-source'));
    const targetDir = await fs.mkdtemp(join(tmpdir(), 'cpy-target'));
    fs.writeFile(join(sourceDir, 'a.js'), '// js', { encoding: 'utf8' });
    fs.writeFile(join(sourceDir, 'a.ts'), '// ts', { encoding: 'utf8' });

    await cpy.default(sourceDir, targetDir, {
        rename: (basename) => {
            console.log('cpy#filter, basename:', basename);
            return basename; // It does not matter
        }
    });
    const files = await fs.readdir(targetDir);
    deepEqual(files.length, 2);
    deepEqual(files.includes('a.js'), true);
    deepEqual(files.includes('a.ts'), true);
})();

[email protected]:

% node index.js
cpy#filter, basename: a.js
cpy#filter, basename: a.ts

[email protected]:

% node index.js 
cpy#filter, basename: a
cpy#filter, basename: a

Thank you!

@kittaakos kittaakos changed the title basename does not contain the file extension from 9.x when rename is a function when rename is a function, basename does not contain the file extension from 9.x Apr 24, 2023
@sindresorhus
Copy link
Owner

This is a bug. It should include the extension.

kittaakos pushed a commit to kittaakos/cpy that referenced this issue Apr 26, 2023
kittaakos pushed a commit to kittaakos/cpy that referenced this issue Apr 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants