diff options
Diffstat (limited to 'node_modules/shelljs/src')
-rw-r--r-- | node_modules/shelljs/src/cat.js | 2 | ||||
-rw-r--r-- | node_modules/shelljs/src/common.js | 28 | ||||
-rw-r--r-- | node_modules/shelljs/src/cp.js | 43 | ||||
-rw-r--r-- | node_modules/shelljs/src/find.js | 2 | ||||
-rw-r--r-- | node_modules/shelljs/src/head.js | 23 | ||||
-rw-r--r-- | node_modules/shelljs/src/ln.js | 2 | ||||
-rw-r--r-- | node_modules/shelljs/src/mkdir.js | 10 | ||||
-rw-r--r-- | node_modules/shelljs/src/mv.js | 20 | ||||
-rw-r--r-- | node_modules/shelljs/src/rm.js | 88 | ||||
-rw-r--r-- | node_modules/shelljs/src/sort.js | 13 | ||||
-rw-r--r-- | node_modules/shelljs/src/tail.js | 13 | ||||
-rw-r--r-- | node_modules/shelljs/src/uniq.js | 13 | ||||
-rw-r--r-- | node_modules/shelljs/src/which.js | 5 |
13 files changed, 204 insertions, 58 deletions
diff --git a/node_modules/shelljs/src/cat.js b/node_modules/shelljs/src/cat.js index a74a25c84..af1ad1d4d 100644 --- a/node_modules/shelljs/src/cat.js +++ b/node_modules/shelljs/src/cat.js @@ -30,6 +30,8 @@ function _cat(options, files) { files.forEach(function (file) { if (!fs.existsSync(file)) { common.error('no such file or directory: ' + file); + } else if (fs.statSync(file).isDirectory()) { + common.error(file + ': Is a directory'); } cat += fs.readFileSync(file, 'utf8'); diff --git a/node_modules/shelljs/src/common.js b/node_modules/shelljs/src/common.js index f5197d8ec..4a1f43456 100644 --- a/node_modules/shelljs/src/common.js +++ b/node_modules/shelljs/src/common.js @@ -38,6 +38,7 @@ var DEFAULT_CONFIG = { silent: false, verbose: false, execPath: null, + bufLength: 64 * 1024, // 64KB }; var config = { @@ -66,9 +67,6 @@ exports.state = state; delete process.env.OLDPWD; // initially, there's no previous directory -var platform = os.type().match(/^Win/) ? 'win' : 'unix'; -exports.platform = platform; - // This is populated by calls to commonl.wrap() var pipeMethods = []; @@ -260,15 +258,33 @@ function expand(list) { if (typeof listEl !== 'string') { expanded.push(listEl); } 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); } }); return expanded; } exports.expand = expand; +// 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. // file can be unlinked even if it's read-only, see https://github.com/joyent/node/issues/3006 function unlinkSync(file) { diff --git a/node_modules/shelljs/src/cp.js b/node_modules/shelljs/src/cp.js index 04c4e57ef..99c97b2eb 100644 --- a/node_modules/shelljs/src/cp.js +++ b/node_modules/shelljs/src/cp.js @@ -1,7 +1,6 @@ var fs = require('fs'); var path = require('path'); var common = require('./common'); -var os = require('os'); common.register('cp', _cp, { cmdOptions: { @@ -24,6 +23,8 @@ function copyFileSync(srcFile, destFile, options) { common.error('copyFileSync: no such file or directory: ' + srcFile); } + var isWindows = process.platform === 'win32'; + // Check the mtimes of the files if the '-u' flag is provided try { if (options.update && fs.statSync(srcFile).mtime < fs.statSync(destFile).mtime) { @@ -42,11 +43,11 @@ function copyFileSync(srcFile, destFile, options) { } 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; var fdr = null; var fdw = null; @@ -65,8 +66,8 @@ function copyFileSync(srcFile, destFile, options) { common.error('copyFileSync: could not write to dest file (code=' + e.code + '):' + destFile); } - 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); pos += bytesRead; } @@ -93,6 +94,8 @@ function cpdirSyncRecursive(sourceDir, destDir, currentDepth, opts) { if (currentDepth >= common.config.maxdepth) return; currentDepth++; + var isWindows = process.platform === 'win32'; + // Create the directory where all our junk is moving to; read the mode of the // source directory and mirror it try { @@ -116,7 +119,7 @@ function cpdirSyncRecursive(sourceDir, destDir, currentDepth, opts) { // Cycle link found. console.error('Cycle link found.'); symlinkFull = fs.readlinkSync(srcFile); - fs.symlinkSync(symlinkFull, destFile, os.platform() === 'win32' ? 'junction' : null); + fs.symlinkSync(symlinkFull, destFile, isWindows ? 'junction' : null); continue; } } @@ -131,7 +134,7 @@ function cpdirSyncRecursive(sourceDir, destDir, currentDepth, opts) { } catch (e) { // it doesn't exist, so no work needs to be done } - fs.symlinkSync(symlinkFull, destFile, os.platform() === 'win32' ? 'junction' : null); + fs.symlinkSync(symlinkFull, destFile, isWindows ? 'junction' : null); } else if (srcFileStat.isSymbolicLink() && opts.followsymlink) { srcFileStat = fs.statSync(srcFile); if (srcFileStat.isDirectory()) { @@ -150,6 +153,14 @@ function cpdirSyncRecursive(sourceDir, destDir, currentDepth, opts) { } // for files } // cpdirSyncRecursive +// 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) { var srcFileStat = fs.lstatSync(srcFile); if (srcFileStat.isSymbolicLink()) { @@ -224,8 +235,9 @@ function _cp(options, sources, dest) { return new common.ShellString('', '', 0); } - 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 }); return; // skip file } @@ -259,7 +271,16 @@ function _cp(options, sources, dest) { thisDest = path.normalize(dest + '/' + path.basename(src)); } - 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 } diff --git a/node_modules/shelljs/src/find.js b/node_modules/shelljs/src/find.js index 625aa2972..76a16c4ee 100644 --- a/node_modules/shelljs/src/find.js +++ b/node_modules/shelljs/src/find.js @@ -30,7 +30,7 @@ function _find(options, paths) { var list = []; function pushFile(file) { - if (common.platform === 'win') { + if (process.platform === 'win32') { file = file.replace(/\\/g, '/'); } list.push(file); diff --git a/node_modules/shelljs/src/head.js b/node_modules/shelljs/src/head.js index 13d582977..e112e49e1 100644 --- a/node_modules/shelljs/src/head.js +++ b/node_modules/shelljs/src/head.js @@ -10,9 +10,9 @@ common.register('head', _head, { // This reads n or more lines, or the entire file, whichever is less. 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; var fdr = null; @@ -24,8 +24,8 @@ function readSomeLines(file, numLines) { var numLinesRead = 0; 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); numLinesRead += bufStr.split('\n').length - 1; ret += bufStr; @@ -72,9 +72,16 @@ function _head(options, files) { var shouldAppendNewline = false; 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; + } } var contents; diff --git a/node_modules/shelljs/src/ln.js b/node_modules/shelljs/src/ln.js index 7393d9fcd..9b8beb9ec 100644 --- a/node_modules/shelljs/src/ln.js +++ b/node_modules/shelljs/src/ln.js @@ -43,7 +43,7 @@ function _ln(options, source, dest) { } if (options.symlink) { - var isWindows = common.platform === 'win'; + var isWindows = process.platform === 'win32'; var linkType = isWindows ? 'file' : null; var resolvedSourcePath = isAbsolute ? sourcePath : path.resolve(process.cwd(), path.dirname(dest), source); if (!fs.existsSync(resolvedSourcePath)) { diff --git a/node_modules/shelljs/src/mkdir.js b/node_modules/shelljs/src/mkdir.js index 115f75ca4..44b1b2162 100644 --- a/node_modules/shelljs/src/mkdir.js +++ b/node_modules/shelljs/src/mkdir.js @@ -57,9 +57,11 @@ function _mkdir(options, dirs) { dirs.forEach(function (dir) { 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 }); } return; // skip dir } catch (e) { @@ -80,12 +82,16 @@ function _mkdir(options, dirs) { fs.mkdirSync(dir, parseInt('0777', 8)); } } 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 { /* istanbul ignore next */ throw e; } + common.error('cannot create directory ' + dir + ': ' + reason, { continue: true }); } }); return ''; diff --git a/node_modules/shelljs/src/mv.js b/node_modules/shelljs/src/mv.js index 7fc7cf04c..6ebaa8a03 100644 --- a/node_modules/shelljs/src/mv.js +++ b/node_modules/shelljs/src/mv.js @@ -11,6 +11,14 @@ common.register('mv', _mv, { }, }); +// 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); + }); +} + //@ //@ ### mv([options ,] source [, source ...], dest') //@ ### mv([options ,] source_array, dest') @@ -55,7 +63,7 @@ function _mv(options, sources, dest) { common.error('dest file already exists: ' + dest); } - sources.forEach(function (src) { + sources.forEach(function (src, srcIndex) { if (!fs.existsSync(src)) { common.error('no such file or directory: ' + src, { continue: true }); return; // skip file @@ -70,6 +78,16 @@ function _mv(options, sources, dest) { thisDest = path.normalize(dest + '/' + path.basename(src)); } + 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) { common.error('dest file already exists: ' + thisDest, { continue: true }); return; // skip file diff --git a/node_modules/shelljs/src/rm.js b/node_modules/shelljs/src/rm.js index 595368114..2ad6914b4 100644 --- a/node_modules/shelljs/src/rm.js +++ b/node_modules/shelljs/src/rm.js @@ -17,7 +17,7 @@ common.register('rm', _rm, { // // Licensed under the MIT License // http://www.opensource.org/licenses/mit-license.php -function rmdirSyncRecursive(dir, force) { +function rmdirSyncRecursive(dir, force, fromSymlink) { var files; files = fs.readdirSync(dir); @@ -43,6 +43,10 @@ function rmdirSyncRecursive(dir, force) { } } + // 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. // Huzzah for the shopkeep. @@ -91,6 +95,57 @@ function isWriteable(file) { return writePermission; } +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); +} + //@ //@ ### rm([options,] file [, file ...]) //@ ### rm([options,] file_array) @@ -115,9 +170,12 @@ function _rm(options, files) { files = [].slice.call(arguments, 1); 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) { // Path does not exist, no force flag given if (!options.force) { @@ -127,22 +185,14 @@ function _rm(options, files) { } // 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); } }); // forEach(file) return ''; diff --git a/node_modules/shelljs/src/sort.js b/node_modules/shelljs/src/sort.js index 041b03772..2ebccd7f4 100644 --- a/node_modules/shelljs/src/sort.js +++ b/node_modules/shelljs/src/sort.js @@ -69,9 +69,16 @@ function _sort(options, files) { var lines = []; 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; + } } var contents = file === '-' ? pipe : fs.readFileSync(file, 'utf8'); diff --git a/node_modules/shelljs/src/tail.js b/node_modules/shelljs/src/tail.js index 7ece654b1..e5a88055c 100644 --- a/node_modules/shelljs/src/tail.js +++ b/node_modules/shelljs/src/tail.js @@ -46,9 +46,16 @@ function _tail(options, files) { var shouldAppendNewline = false; 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; + } } var contents = file === '-' ? pipe : fs.readFileSync(file, 'utf8'); diff --git a/node_modules/shelljs/src/uniq.js b/node_modules/shelljs/src/uniq.js index 8f5da0028..30121616a 100644 --- a/node_modules/shelljs/src/uniq.js +++ b/node_modules/shelljs/src/uniq.js @@ -40,7 +40,18 @@ function _uniq(options, input, output) { // Check if this is coming from a pipe var pipe = common.readFromPipe(); - 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). trimRight(). diff --git a/node_modules/shelljs/src/which.js b/node_modules/shelljs/src/which.js index 03db57bcd..a5f9e15eb 100644 --- a/node_modules/shelljs/src/which.js +++ b/node_modules/shelljs/src/which.js @@ -37,6 +37,7 @@ function checkPath(pathName) { function _which(options, cmd) { if (!cmd) common.error('must specify command'); + var isWindows = process.platform === 'win32'; var pathEnv = process.env.path || process.env.Path || process.env.PATH; var pathArray = splitPath(pathEnv); @@ -47,7 +48,7 @@ function _which(options, cmd) { // Assume that there are no extensions to append to queries (this is the // case for unix) var pathExtArray = ['']; - if (common.platform === 'win') { + if (isWindows) { // In case the PATHEXT variable is somehow not set (e.g. // child_process.spawn with an empty environment), use the XP default. var pathExtEnv = process.env.PATHEXT || XP_DEFAULT_PATHEXT; @@ -61,7 +62,7 @@ function _which(options, cmd) { var attempt = path.resolve(pathArray[k], cmd); - if (common.platform === 'win') { + if (isWindows) { attempt = attempt.toUpperCase(); } |