jake
Advanced tools
Comparing version 0.2.21 to 0.2.22
@@ -8,2 +8,3 @@ var fs = require('fs') | ||
, to = path.normalize(toPath) | ||
, options = opts || {} | ||
, fromStat | ||
@@ -14,38 +15,30 @@ , toStat | ||
, content | ||
, createDir = opts.createDir | ||
, recurse = opts.recurse; | ||
, filename | ||
, dirContents | ||
, targetDir; | ||
if (fromPath == toPath) { | ||
throw new Error('Cannot copy ' + from + ' to itself.'); | ||
} | ||
fromStat = fs.statSync(from); | ||
try { | ||
//console.dir(to + ' destExists'); | ||
toStat = fs.statSync(to); | ||
destExists = true; | ||
//console.dir(to + ' destExists'); | ||
} | ||
catch(e) { | ||
//console.dir(to + ' does not exist'); | ||
destDoesNotExistErr = e; | ||
destExists = false; | ||
//console.dir(to + ' does not exist'); | ||
} | ||
// Destination dir or file exists, copy in or overwrite | ||
if (destExists || createDir) { | ||
if (createDir) { | ||
//console.log('creating dir ' + to); | ||
try { | ||
fs.mkdirSync(to); | ||
} | ||
catch(e) { | ||
if (e.code != 'EEXIST') { | ||
throw e; | ||
} | ||
} | ||
} | ||
// Destination dir or file exists, copy into (directory) | ||
// or overwrite (file) | ||
if (destExists) { | ||
// If there's a rename-via-copy file/dir name passed, use it. | ||
// Otherwise use the actual file/dir name | ||
filename = options.rename || path.basename(from); | ||
// Copying a directory | ||
if (fromStat.isDirectory()) { | ||
var dirContents = fs.readdirSync(from) | ||
, targetDir = path.join(to, path.basename(from)); | ||
dirContents = fs.readdirSync(from); | ||
targetDir = path.join(to, filename); | ||
// We don't care if the target dir already exists | ||
@@ -62,4 +55,3 @@ try { | ||
//console.log(dirContents[i]); | ||
_copyFile(path.join(from, dirContents[i]), targetDir, | ||
{createDir: true}); | ||
_copyFile(path.join(from, dirContents[i]), targetDir); | ||
} | ||
@@ -73,7 +65,7 @@ } | ||
//console.log('copy into dir ' + to); | ||
fs.writeFileSync(path.join(to, path.basename(from)), content); | ||
fs.writeFileSync(path.join(to, filename), content); | ||
} | ||
// Overwrite file | ||
else { | ||
console.log('overwriting ' + to); | ||
//console.log('overwriting ' + to); | ||
fs.writeFileSync(to, content); | ||
@@ -100,3 +92,3 @@ } | ||
var curr = path.join(dir, p); | ||
console.log(curr); | ||
//console.log(curr); | ||
var stat = fs.statSync(curr); | ||
@@ -111,4 +103,48 @@ ret.push(curr); | ||
this.cpR = function (from, to) { | ||
_copyFile.apply(this, [from, to, {recurse: true}]); | ||
this.cpR = function (fromPath, toPath) { | ||
var from = path.normalize(fromPath) | ||
, to = path.normalize(toPath) | ||
, toStat | ||
, doesNotExistErr | ||
, paths | ||
, filename | ||
, opts = {}; | ||
if (from == to) { | ||
throw new Error('Cannot copy ' + from + ' to itself.'); | ||
} | ||
// Handle rename-via-copy | ||
try { | ||
toStat = fs.statSync(to); | ||
} | ||
catch(e) { | ||
doesNotExistErr = e; | ||
// Get abs path so it's possible to check parent dir | ||
if (to.indexOf('/') != 0 || /^[A-Za-z]+:/.test(to)) { | ||
to = path.join(process.cwd() , to); | ||
} | ||
// Save the file/dir name | ||
filename = path.basename(to); | ||
// See if a parent dir exists, so there's a place to put the | ||
/// renamed file/dir (resets the destination for the copy) | ||
to = path.dirname(to); | ||
try { | ||
toStat = fs.statSync(to); | ||
} | ||
catch(e) {} | ||
if (toStat && toStat.isDirectory()) { | ||
// Set the rename opt to pass to the copy func, will be used | ||
// as the new file/dir name | ||
opts.rename = filename; | ||
//console.log('filename ' + filename); | ||
} | ||
else { | ||
throw doesNotExistErr; | ||
} | ||
} | ||
_copyFile(from, to, opts); | ||
}; | ||
@@ -122,3 +158,3 @@ | ||
if (paths[0] == '' || /[A-Za-z]+:/.test(paths[0])) { | ||
if (paths[0] == '' || /^[A-Za-z]+:/.test(paths[0])) { | ||
currPath = paths.shift() || '/'; | ||
@@ -125,0 +161,0 @@ currPath = path.join(currPath, paths.shift()); |
@@ -10,3 +10,3 @@ { | ||
], | ||
"version": "0.2.21", | ||
"version": "0.2.22", | ||
"author": "Matthew Eernisse <mde@fleegix.org> (http://fleegix.org)", | ||
@@ -13,0 +13,0 @@ "bin": { |
102412
2348