Skip to content

Commit

Permalink
new --rename option
Browse files Browse the repository at this point in the history
Closes #6
  • Loading branch information
schnittstabil committed Jun 3, 2015
1 parent 58437de commit 7067fb3
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 7 deletions.
8 changes: 5 additions & 3 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ var cpy = require('./');
var cli = meow({
help: [
'Usage',
' $ cpy <source> <destination> [--no-overwrite] [--parents] [--cwd <dir>]',
' $ cpy <source> <destination> [--no-overwrite] [--parents] [--cwd <dir>] [--rename=<filename>]',
'',
'Example',
' $ cpy \'src/*.png\' dist',
'',
'Options',
' --parents Preseve path structure',
' --cwd <dir> Working directory for source files',
' --parents Preseve path structure',
' --cwd <dir> Working directory for source files',
' --rename=<filename> Rename all <source> filenames to <filename>',
'',
'<source> can contain globs if quoted',
].join('\n')
Expand All @@ -35,6 +36,7 @@ function errorHandler(err) {
try {
cpy([cli.input[0]], cli.input[1], {
cwd: cli.flags.cwd || process.cwd(),
rename: cli.flags.rename,
parents: cli.flags.parents,
overwrite: cli.flags.overwrite
}, errorHandler);
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function preprocessSrcPath(srcPath, opts) {
}

function preprocessDestPath(srcPath, dest, opts) {
var basename = path.basename(srcPath);
var basename = opts.rename || path.basename(srcPath);
var dirname = path.dirname(srcPath);

if (opts.cwd) {
Expand Down
12 changes: 9 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ Default: `false`

Keep the path structure when copying files.

##### rename

Type: `string`

The filename which is used to rename every file in `files`.

#### callback(err)

Expand All @@ -80,14 +85,15 @@ $ npm install --global cpy
$ cpy --help
Usage
$ cpy <source> <destination> [--no-overwrite] [--parents] [--cwd <dir>]
$ cpy <source> <destination> [--no-overwrite] [--parents] [--cwd <dir>] [--rename=<filename>]
Example
$ cpy 'src/*.png' dist
Options
--parents Preseve path structure
--cwd <dir> Working directory for source files
--parents Preseve path structure
--cwd <dir> Working directory for source files
--rename=<filename> Rename all <source> filenames to <filename>
<source> can contain globs if quoted
```
Expand Down
42 changes: 42 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,32 @@ describe('api', function () {
cb();
});
});

it('should rename filenames (but not filepaths)', function (cb) {
fs.mkdirSync('tmp');
fs.mkdirSync('tmp/src');

fs.writeFileSync('tmp/hello.js', 'console.log("hello");');
fs.writeFileSync('tmp/src/hello.js', 'console.log("src/hello");');

var opts = {cwd: 'tmp', parents: true, rename: 'hi.js'};

cpy(['hello.js', 'src/hello.js'], 'dest/subdir', opts, function (err) {
assert(!err, err);

assert.strictEqual(
fs.readFileSync('tmp/dest/subdir/hi.js', 'utf8'),
fs.readFileSync('tmp/hello.js', 'utf8')
);

assert.strictEqual(
fs.readFileSync('tmp/dest/subdir/src/hi.js', 'utf8'),
fs.readFileSync('tmp/src/hello.js', 'utf8')
);

cb();
});
});
});

describe('cli', function () {
Expand Down Expand Up @@ -177,4 +203,20 @@ describe('cli', function () {
done();
});
});

it('should rename files', function (done) {
fs.mkdirSync('tmp');
fs.mkdirSync('tmp/dest');
fs.writeFileSync('tmp/hello.js', 'console.log("hello");');

var sut = spawn('./cli.js', ['tmp/hello.js', 'tmp/dest', '--rename=hi.js'])
sut.on('close', function (status) {
assert.ok(status === 0, 'unexpected exit status: ' + status);
assert.strictEqual(
fs.readFileSync('tmp/hello.js', 'utf8'),
fs.readFileSync('tmp/dest/hi.js', 'utf8')
);
done();
});
});
});

0 comments on commit 7067fb3

Please sign in to comment.