From 5c8040b3fd491b403b68d6287d99c40ce2d8ffe6 Mon Sep 17 00:00:00 2001 From: Marcus Stade Date: Mon, 24 Dec 2012 02:46:28 +0100 Subject: [PATCH] Fixes arturadib/shelljs#44 --- shell.js | 16 +++++++++------- test/cp.js | 27 ++++++++++++++++++++++++++- test/resources/issue44/main.js | 1 + test/rm.js | 4 ++-- 4 files changed, 38 insertions(+), 10 deletions(-) create mode 100644 test/resources/issue44/main.js diff --git a/shell.js b/shell.js index 6293f19..9571456 100644 --- a/shell.js +++ b/shell.js @@ -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 } diff --git a/test/cp.js b/test/cp.js index 7af052a..5e43bbe 100644 --- a/test/cp.js +++ b/test/cp.js @@ -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); diff --git a/test/resources/issue44/main.js b/test/resources/issue44/main.js new file mode 100644 index 0000000..d800886 --- /dev/null +++ b/test/resources/issue44/main.js @@ -0,0 +1 @@ +123 \ No newline at end of file diff --git a/test/rm.js b/test/rm.js index e48c477..61182a1 100644 --- a/test/rm.js +++ b/test/rm.js @@ -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);