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

More parameters for option rename function #48

Open
ddhp opened this issue Jul 9, 2018 · 4 comments
Open

More parameters for option rename function #48

ddhp opened this issue Jul 9, 2018 · 4 comments

Comments

@ddhp
Copy link

ddhp commented Jul 9, 2018

Hi, first of all, thanks for this amazing library.

I am wondering if it's possible to pass srcPath and dest to rename function here:
https://github.com/sindresorhus/cpy/blob/master/index.js#L18

What I want to do is to rename the file with the hash value, so I need srcPath to do that.

Thanks!

@ddhp ddhp changed the title More parameter's for option rename function More parameters for option rename function Jul 9, 2018
@sindresorhus

This comment has been minimized.

@sindresorhus
Copy link
Owner

sindresorhus commented Jun 23, 2019

Hmm, another idea. We would pass the callback two arguments, source and destination. Both are a custom file object:

{
	path: '/Users/sindresorhus/foo.txt',
	name: 'foo.txt',
	nameWithoutExtension: 'foo',
	extension: 'txt'
}

source would be immutable (Object.freeze()), while destination would be mutable, so to change something, you just set one of the properties. You can set any of the properties and it will be handled correctly. We could use Proxy for this.

I think this would make it really nice to use:

const cpy = require('cpy');

(async () => {
	await cpy('foo.js', 'destination', {
		rename: (source, destination) => {
			if (source.nameWithoutExtension === 'foo') {
				destination.nameWithoutExtension = 'bar';
			}
		}
	});
})();

If you just want to change the extension, you could do:

const cpy = require('cpy');

(async () => {
	await cpy('foo.js', 'destination', {
		rename: (source, destination) => {
			if (source.nameWithoutExtension === 'foo') {
				destination.extension = 'ts';
			}

			console.log(destination.name);
			//=> 'foo.ts'
		}
	});
})();

I think I prefer this approach.

@adamduncan
Copy link

@sindresorhus This does sound like a nice approach indeed. Was there desire to move ahead with this?

@sindresorhus
Copy link
Owner

My above proposal is how it should be implemented. It's not something I plan to work on anytime soon though. However, good PRs are always welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants