shelljs
Advanced tools
Comparing version 0.7.7 to 0.7.8
{ | ||
"name": "shelljs", | ||
"version": "0.7.7", | ||
"version": "0.7.8", | ||
"description": "Portable Unix shell commands for Node.js", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -735,3 +735,4 @@ # ShellJS - Unix shell commands for Node.js | ||
cd('dir/'); | ||
ls('subdir/'); | ||
rm('-rf', 'foo.txt', 'bar.txt'); | ||
exec('echo hello'); | ||
``` | ||
@@ -743,3 +744,4 @@ | ||
cd dir/ | ||
ls subdir/ | ||
rm -rf foo.txt bar.txt | ||
exec echo hello | ||
``` | ||
@@ -746,0 +748,0 @@ |
@@ -106,3 +106,4 @@ // | ||
//@ cd('dir/'); | ||
//@ ls('subdir/'); | ||
//@ rm('-rf', 'foo.txt', 'bar.txt'); | ||
//@ exec('echo hello'); | ||
//@ ``` | ||
@@ -114,3 +115,4 @@ //@ | ||
//@ cd dir/ | ||
//@ ls subdir/ | ||
//@ rm -rf foo.txt bar.txt | ||
//@ exec echo hello | ||
//@ ``` | ||
@@ -117,0 +119,0 @@ |
@@ -33,2 +33,4 @@ var common = require('./common'); | ||
common.error('no such file or directory: ' + file); | ||
} else if (fs.statSync(file).isDirectory()) { | ||
common.error(file + ': Is a directory'); | ||
} | ||
@@ -35,0 +37,0 @@ |
@@ -41,2 +41,3 @@ // Ignore warning about 'new String()' | ||
execPath: null, | ||
bufLength: 64 * 1024, // 64KB | ||
}; | ||
@@ -70,5 +71,2 @@ | ||
var platform = os.type().match(/^Win/) ? 'win' : 'unix'; | ||
exports.platform = platform; | ||
// This is populated by calls to commonl.wrap() | ||
@@ -265,5 +263,12 @@ var pipeMethods = []; | ||
} else { | ||
var ret = glob.sync(listEl, config.globOptions); | ||
// if glob fails, interpret the string literally | ||
expanded = expanded.concat(ret.length > 0 ? ret : [listEl]); | ||
var ret; | ||
try { | ||
ret = glob.sync(listEl, config.globOptions); | ||
// if nothing matched, interpret the string literally | ||
ret = ret.length > 0 ? ret : [listEl]; | ||
} catch (e) { | ||
// if glob fails, interpret the string literally | ||
ret = [listEl]; | ||
} | ||
expanded = expanded.concat(ret); | ||
} | ||
@@ -275,2 +280,13 @@ }); | ||
// Normalizes Buffer creation, using Buffer.alloc if possible. | ||
// Also provides a good default buffer length for most use cases. | ||
var buffer = typeof Buffer.alloc === 'function' ? | ||
function (len) { | ||
return Buffer.alloc(len || config.bufLength); | ||
} : | ||
function (len) { | ||
return new Buffer(len || config.bufLength); | ||
}; | ||
exports.buffer = buffer; | ||
// Normalizes _unlinkSync() across platforms to match Unix behavior, i.e. | ||
@@ -277,0 +293,0 @@ // file can be unlinked even if it's read-only, see https://github.com/joyent/node/issues/3006 |
var fs = require('fs'); | ||
var path = require('path'); | ||
var common = require('./common'); | ||
var os = require('os'); | ||
@@ -27,2 +26,4 @@ common.register('cp', _cp, { | ||
var isWindows = process.platform === 'win32'; | ||
// Check the mtimes of the files if the '-u' flag is provided | ||
@@ -46,7 +47,7 @@ try { | ||
var symlinkFull = fs.readlinkSync(srcFile); | ||
fs.symlinkSync(symlinkFull, destFile, os.platform() === 'win32' ? 'junction' : null); | ||
fs.symlinkSync(symlinkFull, destFile, isWindows ? 'junction' : null); | ||
} else { | ||
var BUF_LENGTH = 64 * 1024; | ||
var buf = new Buffer(BUF_LENGTH); | ||
var bytesRead = BUF_LENGTH; | ||
var buf = common.buffer(); | ||
var bufLength = buf.length; | ||
var bytesRead = bufLength; | ||
var pos = 0; | ||
@@ -70,4 +71,4 @@ var fdr = null; | ||
while (bytesRead === BUF_LENGTH) { | ||
bytesRead = fs.readSync(fdr, buf, 0, BUF_LENGTH, pos); | ||
while (bytesRead === bufLength) { | ||
bytesRead = fs.readSync(fdr, buf, 0, bufLength, pos); | ||
fs.writeSync(fdw, buf, 0, bytesRead); | ||
@@ -99,2 +100,4 @@ pos += bytesRead; | ||
var isWindows = process.platform === 'win32'; | ||
// Create the directory where all our junk is moving to; read the mode of the | ||
@@ -123,3 +126,3 @@ // source directory and mirror it | ||
symlinkFull = fs.readlinkSync(srcFile); | ||
fs.symlinkSync(symlinkFull, destFile, os.platform() === 'win32' ? 'junction' : null); | ||
fs.symlinkSync(symlinkFull, destFile, isWindows ? 'junction' : null); | ||
continue; | ||
@@ -139,3 +142,3 @@ } | ||
} | ||
fs.symlinkSync(symlinkFull, destFile, os.platform() === 'win32' ? 'junction' : null); | ||
fs.symlinkSync(symlinkFull, destFile, isWindows ? 'junction' : null); | ||
} else if (srcFileStat.isSymbolicLink() && opts.followsymlink) { | ||
@@ -159,2 +162,10 @@ srcFileStat = fs.statSync(srcFile); | ||
// Checks if cureent file was created recently | ||
function checkRecentCreated(sources, index) { | ||
var lookedSource = sources[index]; | ||
return sources.slice(0, index).some(function (src) { | ||
return path.basename(src) === path.basename(lookedSource); | ||
}); | ||
} | ||
function cpcheckcycle(sourceDir, srcFile) { | ||
@@ -234,4 +245,5 @@ var srcFileStat = fs.lstatSync(srcFile); | ||
sources.forEach(function (src) { | ||
sources.forEach(function (src, srcIndex) { | ||
if (!fs.existsSync(src)) { | ||
if (src === '') src = "''"; // if src was empty string, display empty string | ||
common.error('no such file or directory: ' + src, { continue: true }); | ||
@@ -270,3 +282,12 @@ return; // skip file | ||
if (fs.existsSync(thisDest) && options.no_force) { | ||
var thisDestExists = fs.existsSync(thisDest); | ||
if (thisDestExists && checkRecentCreated(sources, srcIndex)) { | ||
// cannot overwrite file created recently in current execution, but we want to continue copying other files | ||
if (!options.no_force) { | ||
common.error("will not overwrite just-created '" + thisDest + "' with '" + src + "'", { continue: true }); | ||
} | ||
return; | ||
} | ||
if (thisDestExists && options.no_force) { | ||
return; // skip file | ||
@@ -273,0 +294,0 @@ } |
@@ -33,3 +33,3 @@ var fs = require('fs'); | ||
function pushFile(file) { | ||
if (common.platform === 'win') { | ||
if (process.platform === 'win32') { | ||
file = file.replace(/\\/g, '/'); | ||
@@ -36,0 +36,0 @@ } |
@@ -13,5 +13,5 @@ var common = require('./common'); | ||
function readSomeLines(file, numLines) { | ||
var BUF_LENGTH = 64 * 1024; | ||
var buf = new Buffer(BUF_LENGTH); | ||
var bytesRead = BUF_LENGTH; | ||
var buf = common.buffer(); | ||
var bufLength = buf.length; | ||
var bytesRead = bufLength; | ||
var pos = 0; | ||
@@ -28,4 +28,4 @@ var fdr = null; | ||
var ret = ''; | ||
while (bytesRead === BUF_LENGTH && numLinesRead < numLines) { | ||
bytesRead = fs.readSync(fdr, buf, 0, BUF_LENGTH, pos); | ||
while (bytesRead === bufLength && numLinesRead < numLines) { | ||
bytesRead = fs.readSync(fdr, buf, 0, bufLength, pos); | ||
var bufStr = buf.toString('utf8', 0, bytesRead); | ||
@@ -77,5 +77,12 @@ numLinesRead += bufStr.split('\n').length - 1; | ||
files.forEach(function (file) { | ||
if (!fs.existsSync(file) && file !== '-') { | ||
common.error('no such file or directory: ' + file, { continue: true }); | ||
return; | ||
if (file !== '-') { | ||
if (!fs.existsSync(file)) { | ||
common.error('no such file or directory: ' + file, { continue: true }); | ||
return; | ||
} else if (fs.statSync(file).isDirectory()) { | ||
common.error("error reading '" + file + "': Is a directory", { | ||
continue: true, | ||
}); | ||
return; | ||
} | ||
} | ||
@@ -82,0 +89,0 @@ |
@@ -46,3 +46,3 @@ var fs = require('fs'); | ||
if (options.symlink) { | ||
var isWindows = common.platform === 'win'; | ||
var isWindows = process.platform === 'win32'; | ||
var linkType = isWindows ? 'file' : null; | ||
@@ -49,0 +49,0 @@ var resolvedSourcePath = isAbsolute ? sourcePath : path.resolve(process.cwd(), path.dirname(dest), source); |
@@ -60,5 +60,7 @@ var common = require('./common'); | ||
try { | ||
fs.lstatSync(dir); | ||
var stat = fs.lstatSync(dir); | ||
if (!options.fullpath) { | ||
common.error('path already exists: ' + dir, { continue: true }); | ||
} else if (stat.isFile()) { | ||
common.error('cannot create directory ' + dir + ': File exists', { continue: true }); | ||
} | ||
@@ -84,4 +86,7 @@ return; // skip dir | ||
} catch (e) { | ||
var reason; | ||
if (e.code === 'EACCES') { | ||
common.error('cannot create directory ' + dir + ': Permission denied'); | ||
reason = 'Permission denied'; | ||
} else if (e.code === 'ENOTDIR' || e.code === 'ENOENT') { | ||
reason = 'Not a directory'; | ||
} else { | ||
@@ -91,2 +96,3 @@ /* istanbul ignore next */ | ||
} | ||
common.error('cannot create directory ' + dir + ': ' + reason, { continue: true }); | ||
} | ||
@@ -93,0 +99,0 @@ }); |
@@ -14,2 +14,10 @@ var fs = require('fs'); | ||
// Checks if cureent file was created recently | ||
function checkRecentCreated(sources, index) { | ||
var lookedSource = sources[index]; | ||
return sources.slice(0, index).some(function (src) { | ||
return path.basename(src) === path.basename(lookedSource); | ||
}); | ||
} | ||
//@ | ||
@@ -59,3 +67,3 @@ //@ ### mv([options ,] source [, source ...], dest') | ||
sources.forEach(function (src) { | ||
sources.forEach(function (src, srcIndex) { | ||
if (!fs.existsSync(src)) { | ||
@@ -75,2 +83,12 @@ common.error('no such file or directory: ' + src, { continue: true }); | ||
var thisDestExists = fs.existsSync(thisDest); | ||
if (thisDestExists && checkRecentCreated(sources, srcIndex)) { | ||
// cannot overwrite file created recently in current execution, but we want to continue copying other files | ||
if (!options.no_force) { | ||
common.error("will not overwrite just-created '" + thisDest + "' with '" + src + "'", { continue: true }); | ||
} | ||
return; | ||
} | ||
if (fs.existsSync(thisDest) && options.no_force) { | ||
@@ -77,0 +95,0 @@ common.error('dest file already exists: ' + thisDest, { continue: true }); |
@@ -20,3 +20,3 @@ var common = require('./common'); | ||
// http://www.opensource.org/licenses/mit-license.php | ||
function rmdirSyncRecursive(dir, force) { | ||
function rmdirSyncRecursive(dir, force, fromSymlink) { | ||
var files; | ||
@@ -47,2 +47,6 @@ | ||
// if was directory was referenced through a symbolic link, | ||
// the contents should be removed, but not the directory itself | ||
if (fromSymlink) return; | ||
// Now that we know everything in the sub-tree has been deleted, we can delete the main directory. | ||
@@ -96,2 +100,53 @@ // Huzzah for the shopkeep. | ||
function handleFile(file, options) { | ||
if (options.force || isWriteable(file)) { | ||
// -f was passed, or file is writable, so it can be removed | ||
common.unlinkSync(file); | ||
} else { | ||
common.error('permission denied: ' + file, { continue: true }); | ||
} | ||
} | ||
function handleDirectory(file, options) { | ||
if (options.recursive) { | ||
// -r was passed, so directory can be removed | ||
rmdirSyncRecursive(file, options.force); | ||
} else { | ||
common.error('path is a directory', { continue: true }); | ||
} | ||
} | ||
function handleSymbolicLink(file, options) { | ||
var stats; | ||
try { | ||
stats = fs.statSync(file); | ||
} catch (e) { | ||
// symlink is broken, so remove the symlink itself | ||
common.unlinkSync(file); | ||
return; | ||
} | ||
if (stats.isFile()) { | ||
common.unlinkSync(file); | ||
} else if (stats.isDirectory()) { | ||
if (file[file.length - 1] === '/') { | ||
// trailing separator, so remove the contents, not the link | ||
if (options.recursive) { | ||
// -r was passed, so directory can be removed | ||
var fromSymlink = true; | ||
rmdirSyncRecursive(file, options.force, fromSymlink); | ||
} else { | ||
common.error('path is a directory', { continue: true }); | ||
} | ||
} else { | ||
// no trailing separator, so remove the link | ||
common.unlinkSync(file); | ||
} | ||
} | ||
} | ||
function handleFIFO(file) { | ||
common.unlinkSync(file); | ||
} | ||
//@ | ||
@@ -121,5 +176,8 @@ //@ ### rm([options,] file [, file ...]) | ||
files.forEach(function (file) { | ||
var stats; | ||
var lstats; | ||
try { | ||
stats = fs.lstatSync(file); // test for existence | ||
var filepath = (file[file.length - 1] === '/') | ||
? file.slice(0, -1) // remove the '/' so lstatSync can detect symlinks | ||
: file; | ||
lstats = fs.lstatSync(filepath); // test for existence | ||
} catch (e) { | ||
@@ -134,18 +192,10 @@ // Path does not exist, no force flag given | ||
// If here, path exists | ||
if (stats.isFile()) { | ||
if (options.force || isWriteable(file)) { | ||
// -f was passed, or file is writable, so it can be removed | ||
common.unlinkSync(file); | ||
} else { | ||
common.error('permission denied: ' + file, { continue: true }); | ||
} | ||
} else if (stats.isDirectory()) { | ||
if (options.recursive) { | ||
// -r was passed, so directory can be removed | ||
rmdirSyncRecursive(file, options.force); | ||
} else { | ||
common.error('path is a directory', { continue: true }); | ||
} | ||
} else if (stats.isSymbolicLink() || stats.isFIFO()) { | ||
common.unlinkSync(file); | ||
if (lstats.isFile()) { | ||
handleFile(file, options); | ||
} else if (lstats.isDirectory()) { | ||
handleDirectory(file, options); | ||
} else if (lstats.isSymbolicLink()) { | ||
handleSymbolicLink(file, options); | ||
} else if (lstats.isFIFO()) { | ||
handleFIFO(file); | ||
} | ||
@@ -152,0 +202,0 @@ }); // forEach(file) |
@@ -72,5 +72,12 @@ var common = require('./common'); | ||
files.forEach(function (file) { | ||
if (!fs.existsSync(file) && file !== '-') { | ||
// exit upon any sort of error | ||
common.error('no such file or directory: ' + file); | ||
if (file !== '-') { | ||
if (!fs.existsSync(file)) { | ||
common.error('no such file or directory: ' + file, { continue: true }); | ||
return; | ||
} else if (fs.statSync(file).isDirectory()) { | ||
common.error('read failed: ' + file + ': Is a directory', { | ||
continue: true, | ||
}); | ||
return; | ||
} | ||
} | ||
@@ -77,0 +84,0 @@ |
@@ -49,5 +49,12 @@ var common = require('./common'); | ||
files.forEach(function (file) { | ||
if (!fs.existsSync(file) && file !== '-') { | ||
common.error('no such file or directory: ' + file, { continue: true }); | ||
return; | ||
if (file !== '-') { | ||
if (!fs.existsSync(file)) { | ||
common.error('no such file or directory: ' + file, { continue: true }); | ||
return; | ||
} else if (fs.statSync(file).isDirectory()) { | ||
common.error("error reading '" + file + "': Is a directory", { | ||
continue: true, | ||
}); | ||
return; | ||
} | ||
} | ||
@@ -54,0 +61,0 @@ |
@@ -43,4 +43,15 @@ var common = require('./common'); | ||
if (!input && !pipe) common.error('no input given'); | ||
if (!pipe) { | ||
if (!input) common.error('no input given'); | ||
if (!fs.existsSync(input)) { | ||
common.error(input + ': No such file or directory'); | ||
} else if (fs.statSync(input).isDirectory()) { | ||
common.error("error reading '" + input + "'"); | ||
} | ||
} | ||
if (output && fs.existsSync(output) && fs.statSync(output).isDirectory()) { | ||
common.error(output + ': Is a directory'); | ||
} | ||
var lines = (input ? fs.readFileSync(input, 'utf8') : pipe). | ||
@@ -47,0 +58,0 @@ trimRight(). |
@@ -40,2 +40,3 @@ var common = require('./common'); | ||
var isWindows = process.platform === 'win32'; | ||
var pathEnv = process.env.path || process.env.Path || process.env.PATH; | ||
@@ -51,3 +52,3 @@ var pathArray = splitPath(pathEnv); | ||
var pathExtArray = ['']; | ||
if (common.platform === 'win') { | ||
if (isWindows) { | ||
// In case the PATHEXT variable is somehow not set (e.g. | ||
@@ -66,3 +67,3 @@ // child_process.spawn with an empty environment), use the XP default. | ||
if (common.platform === 'win') { | ||
if (isWindows) { | ||
attempt = attempt.toUpperCase(); | ||
@@ -69,0 +70,0 @@ } |
Sorry, the diff of this file is too big to display
3142
788
195854
41