Skip to content

Commit

Permalink
Update cpy to v9 (#34)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
andy2mrqz and sindresorhus committed Mar 1, 2022
1 parent b271612 commit 693722f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 16 deletions.
16 changes: 8 additions & 8 deletions cli.js
Expand Up @@ -9,30 +9,26 @@ const cli = meow(`
Options
--no-overwrite Don't overwrite the destination
--parents Preserve path structure
--cwd=<dir> Working directory for files
--rename=<filename> Rename all <source> filenames to <filename>
--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.
<source> can contain globs if quoted
Examples
Copy all .png files in src folder into dist except src/goat.png
$ cpy 'src/*.png' '!src/goat.png' dist
Copy all .html files inside src folder into dist and preserve path structure
$ cpy '**/*.html' '../dist/' --cwd=src --parents
Copy all files inside src folder into dist and preserve path structure
$ cpy . '../dist/' --cwd=src
`, {
importMeta: import.meta,
flags: {
overwrite: {
type: 'boolean',
default: true,
},
parents: {
type: 'boolean',
default: false,
},
cwd: {
type: 'string',
default: process.cwd(),
Expand All @@ -44,6 +40,10 @@ const cli = meow(`
type: 'boolean',
default: false,
},
flat: {
type: 'boolean',
default: false,

This comment has been minimized.

Copy link
@antongolub

antongolub Mar 1, 2022

Contributor

@sindresorhus

Shouldn't it be set to true by default to make it more compliant to *nix cp util?

This comment has been minimized.

Copy link
@sindresorhus

sindresorhus Mar 1, 2022

Author Owner

cp compatibility is a non-goal. Most users expect the directory structure to be preserved.

This comment has been minimized.

Copy link
@antongolub

antongolub Mar 1, 2022

Contributor

Hmm... We use cpy-cli as drop-in cp repl for windows-based builds.

This comment has been minimized.

Copy link
@sindresorhus

sindresorhus Mar 1, 2022

Author Owner

Then just pass the --flat flag.

},
},
});

Expand All @@ -52,9 +52,9 @@ const cli = meow(`
await cpy(cli.input, cli.input.pop(), {
cwd: cli.flags.cwd,
rename: cli.flags.rename,
parents: cli.flags.parents,
overwrite: cli.flags.overwrite,
dot: cli.flags.dot,
flat: cli.flags.flat,
});
} catch (error) {
if (error.name === 'CpyError') {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -51,7 +51,7 @@
"contents"
],
"dependencies": {
"cpy": "^8.1.2",
"cpy": "^9.0.0",
"meow": "^10.1.2"
},
"devDependencies": {
Expand Down
6 changes: 3 additions & 3 deletions readme.md
Expand Up @@ -25,19 +25,19 @@ $ cpy --help
Options
--no-overwrite Don't overwrite the destination
--parents Preserve path structure
--cwd=<dir> Working directory for files
--rename=<filename> Rename all <source> filenames to <filename>
--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.
<source> can contain globs if quoted
Examples
Copy all .png files in src folder into dist except src/goat.png
$ cpy 'src/*.png' '!src/goat.png' dist
Copy all .html files inside src folder into dist and preserve path structure
$ cpy '**/*.html' '../dist/' --cwd=src --parents
Copy all files inside src folder into dist and preserve path structure
$ cpy . '../dist/' --cwd=src
```

## Related
Expand Down
35 changes: 31 additions & 4 deletions test.js
Expand Up @@ -29,14 +29,18 @@ test('cwd', async t => {
t.is(read(t.context.tmp, 'cwd/hello.js'), read(t.context.tmp, 'cwd/dest/hello.js'));
});

test('keep path structure with flag `--parents`', async t => {
test('path structure', async t => {
fs.mkdirSync(t.context.tmp);
fs.mkdirSync(path.join(t.context.tmp, 'cwd'));
fs.mkdirSync(path.join(t.context.tmp, 'out'));
fs.writeFileSync(path.join(t.context.tmp, 'cwd/hello.js'), 'console.log("hello");');

await execa('./cli.js', [path.join(t.context.tmp, 'cwd/hello.js'), t.context.tmp, '--parents']);
await execa('./cli.js', [path.join(t.context.tmp, '**'), path.join(t.context.tmp, 'out')]);

t.is(read(t.context.tmp, 'cwd/hello.js'), read(t.context.tmp, t.context.tmp, 'cwd/hello.js'));
t.is(
read(t.context.tmp, 'cwd/hello.js'),
read(t.context.tmp, 'out/cwd/hello.js'),
);
});

test('rename filenames but not filepaths', async t => {
Expand Down Expand Up @@ -68,9 +72,32 @@ test('do not copy files in the negated glob patterns', async t => {
fs.writeFileSync(path.join(t.context.tmp, 'src/hello.jsx'), 'console.log("world");');
fs.writeFileSync(path.join(t.context.tmp, 'src/hello.es2015'), 'console.log("world");');

await execa('./cli.js', ['src/*.*', '!src/*.jsx', '!src/*.es2015', 'dest', '--cwd', t.context.tmp]);
await execa('./cli.js', ['src/*.*', '!src/*.jsx', '!src/*.es2015', path.join(t.context.tmp, 'dest'), '--cwd', t.context.tmp]);

t.is(read(t.context.tmp, 'dest/hello.js'), 'console.log("hello");');
t.false(pathExistsSync(path.join(t.context.tmp, 'dest/hello.jsx')));
t.false(pathExistsSync(path.join(t.context.tmp, 'dest/hello.es2015')));
});

test('flatten directory tree', async t => {
fs.mkdirSync(t.context.tmp);
fs.mkdirSync(path.join(t.context.tmp, 'source'));
fs.mkdirSync(path.join(t.context.tmp, 'source', 'nested'));
fs.writeFileSync(path.join(t.context.tmp, 'foo.js'), 'console.log("foo");');
fs.writeFileSync(path.join(t.context.tmp, 'source/bar.js'), 'console.log("bar");');
fs.writeFileSync(path.join(t.context.tmp, 'source/nested/baz.ts'), 'console.log("baz");');

await execa('./cli.js', ['**/*.js', 'destination/subdir', '--cwd', t.context.tmp, '--flat']);

t.is(
read(t.context.tmp, 'foo.js'),
read(t.context.tmp, 'destination/subdir/foo.js'),
);
t.is(
read(t.context.tmp, 'source/bar.js'),
read(t.context.tmp, 'destination/subdir/bar.js'),
);
t.falsy(
fs.existsSync(path.join(t.context.tmp, 'destination/subdir/baz.ts')),
);
});

0 comments on commit 693722f

Please sign in to comment.