Skip to content

Commit

Permalink
Make the --rename flag support string templates (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove authored and sindresorhus committed Aug 23, 2022
1 parent a8e6cb7 commit b42d028
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
11 changes: 10 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit b42d028

Please sign in to comment.