This commit is contained in:
Marcus Stade 2012-12-24 02:46:28 +01:00
parent 35211d0783
commit 5c8040b3fd
4 changed files with 38 additions and 10 deletions

View File

@ -279,10 +279,13 @@ function _cp(options, sources, dest) {
// Recursive allows the shortcut syntax "sourcedir/" for "sourcedir/*"
// (see Github issue #15)
// Also make sure that src should end in "/*" in case it's a dir and dest
// doesn't exist yet (see Github issue #44)
if (options.recursive) {
sources.forEach(function(src, i) {
if (src[src.length - 1] === '/')
sources[i] += '*';
if (src.match(/\/\*?$/) || (_test('-d', src) && !_test('-d', dest))) {
sources[i] = src.replace(/\/\*?$/, '') + '/*';
}
});
}
@ -295,7 +298,6 @@ function _cp(options, sources, dest) {
}
// If here, src exists
if (fs.statSync(src).isDirectory()) {
if (!options.recursive) {
// Non-Recursive
@ -303,15 +305,15 @@ function _cp(options, sources, dest) {
} else {
// Recursive
// 'cp /a/source dest' should create 'source' in 'dest'
var newDest = dest+'/'+path.basename(src),
checkDir = fs.statSync(src);
var newDest = path.join(dest, path.basename(src));
try {
fs.mkdirSync(newDest, checkDir.mode);
mkdirSyncRecursive(newDest);
} catch (e) {
//if the directory already exists, that's okay
if (e.code !== 'EEXIST') throw e;
}
cpdirSyncRecursive(src, newDest, {force: options.force});
cpdirSyncRecursive(src, newDest || dest, {force: options.force});
}
return; // done with dir
}

View File

@ -104,7 +104,7 @@ assert.equal(fs.existsSync('tmp/file2'), true);
shell.rm('-rf', 'tmp/*');
shell.cp('-R', 'resources/cp', 'tmp');
assert.equal(shell.error(), null);
assert.equal(JSON.stringify(shell.ls('-R', 'resources/cp')), JSON.stringify(shell.ls('-R', 'tmp/cp')));
assert.equal(shell.ls('-R', 'resources/cp') + '', shell.ls('-R', 'tmp/cp') + '');
//recursive, nothing exists, source ends in '/' (see Github issue #15)
shell.rm('-rf', 'tmp/*');
@ -127,4 +127,29 @@ shell.cp('-Rf', 'resources/cp', 'tmp');
assert.equal(shell.error(), null);
assert.equal(shell.cat('resources/cp/dir_a/z'), shell.cat('tmp/cp/dir_a/z')); // after cp
// arturadib/shelljs#44
shell.rm('-rf', 'tmp/*')
shell.cp('-r', 'resources/issue44/*', 'tmp/dir2/')
assert.equal(shell.error(), null);
assert.equal(shell.ls('-R', 'resources/issue44') + '', shell.ls('-R', 'tmp/dir2') + '')
assert.equal(shell.cat('resources/issue44/main.js'), shell.cat('tmp/dir2/main.js'))
shell.rm('-rf', 'tmp/*')
shell.cp('-r', 'resources/issue44', 'tmp/dir2')
assert.equal(shell.error(), null);
assert.equal(shell.ls('-R', 'resources/issue44') + '', shell.ls('-R', 'tmp/dir2') + '')
assert.equal(shell.cat('resources/issue44/main.js'), shell.cat('tmp/dir2/main.js'))
shell.rm('-rf', 'tmp/*')
shell.cp('-r', 'resources/issue44/', 'tmp/dir2/')
assert.equal(shell.error(), null);
assert.equal(shell.ls('-R', 'resources/issue44') + '', shell.ls('-R', 'tmp/dir2') + '')
assert.equal(shell.cat('resources/issue44/main.js'), shell.cat('tmp/dir2/main.js'))
shell.rm('-rf', 'tmp/*')
shell.cp('-R', 'resources/issue44/', 'tmp/dir2')
assert.equal(shell.error(), null);
assert.equal(shell.ls('-R', 'resources/issue44') + '', shell.ls('-R', 'tmp/dir2') + '')
assert.equal(shell.cat('resources/issue44/main.js'), shell.cat('tmp/dir2/main.js'))
shell.exit(123);

View File

@ -0,0 +1 @@
123

View File

@ -37,8 +37,8 @@ assert.equal(fs.existsSync('resources/file1'), true);
shell.rm('-f', 'asdfasdf');
assert.equal(shell.error(), null);
// simple rm
shell.cp('-f', 'resources/file1', 'tmp/file1');
// simple rm
shell.cp('-f', 'resources/file1', 'tmp/file1');
assert.equal(fs.existsSync('tmp/file1'), true);
shell.rm('tmp/file1');
assert.equal(shell.error(), null);