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

The --rename flag supports string templates. #40

Merged
merged 4 commits into from Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion cli.js
Expand Up @@ -11,7 +11,7 @@ const cli = meow(`
Options
--no-overwrite Don't overwrite the destination
--cwd=<dir> Working directory for files
--rename=<filename> Rename all <source> filenames to <filename>
--rename=<filename> Rename all <source> filenames to <filename>, supports string templates.
--dot Allow patterns to match entries that begin with a period (.)
--flat Flatten directory structure. All copied files will be put in the same directory.
--concurrency Number of files being copied concurrently
Expand All @@ -24,6 +24,9 @@ const cli = meow(`

Copy all files inside src folder into dist and preserve path structure
$ cpy . '../dist/' --cwd=src

Copy all .png files in the src folder to dist and prefix the image filenames.
$ cpy 'src/*.png' dist --cwd=src --rename=hi-{{basename}}
`, {
importMeta: import.meta,
flags: {
Expand Down Expand Up @@ -55,6 +58,12 @@ const cli = meow(`

(async () => {
try {
const {rename} = cli.flags;
const stringTemplate = '{{basename}}';
if (rename && rename.includes(stringTemplate)) {
cli.flags.rename = basename => rename.replace(stringTemplate, basename);
}

await cpy(cli.input, cli.input.pop(), {
cwd: cli.flags.cwd,
rename: cli.flags.rename,
Expand Down
5 changes: 4 additions & 1 deletion readme.md
Expand Up @@ -26,7 +26,7 @@ $ cpy --help
Options
--no-overwrite Don't overwrite the destination
--cwd=<dir> Working directory for files
--rename=<filename> Rename all <source> filenames to <filename>
--rename=<filename> Rename all <source> filenames to <filename>, supports string templates.
--dot Allow patterns to match entries that begin with a period (.)
--flat Flatten directory structure. All copied files will be put in the same directory.
--concurrency Number of files being copied concurrently
Expand All @@ -39,6 +39,9 @@ $ cpy --help

Copy all files inside src folder into dist and preserve path structure
$ cpy . '../dist/' --cwd=src

Copy all .png files in the src folder to dist and prefix the image filenames.
$ cpy 'src/*.png' dist --cwd=src --rename=hi-{{basename}}
```

## Related
Expand Down
3 changes: 3 additions & 0 deletions test.js
Expand Up @@ -51,6 +51,9 @@ test('rename filenames but not filepaths', async t => {
await execa('./cli.js', [path.join(t.context.tmp, 'hello.js'), path.join(t.context.tmp, 'dest'), '--rename=hi.js']);

t.is(read(t.context.tmp, 'hello.js'), read(t.context.tmp, 'dest/hi.js'));

await execa('./cli.js', [path.join(t.context.tmp, 'hello.js'), path.join(t.context.tmp, 'dest'), '--rename=hi-{{basename}}-1']);
t.is(read(t.context.tmp, 'hello.js'), read(t.context.tmp, 'dest/hi-hello-1.js'));
});

test('overwrite files by default', async t => {
Expand Down