Deal with all other uses of this.cwd etc and convert to pwd() on current shell instance.

This commit is contained in:
David Humphrey (:humph) david.humphrey@senecacollege.ca 2014-07-28 18:46:50 -04:00
parent 5b785037f0
commit 2114f65f09
1 changed files with 29 additions and 21 deletions

View File

@ -78,14 +78,15 @@ function Shell(fs, options) {
*/
Shell.prototype.exec = function(path, args, callback) {
/* jshint evil:true */
var fs = this.fs;
var sh = this;
var fs = sh.fs;
if(typeof args === 'function') {
callback = args;
args = [];
}
args = args || [];
callback = callback || function(){};
path = Path.resolve(this.cwd, path);
path = Path.resolve(sh.pwd(), path);
fs.readFile(path, "utf8", function(error, data) {
if(error) {
@ -109,14 +110,15 @@ Shell.prototype.exec = function(path, args, callback) {
* * date - use the provided Date value instead of current date/time
*/
Shell.prototype.touch = function(path, options, callback) {
var fs = this.fs;
var sh = this;
var fs = sh.fs;
if(typeof options === 'function') {
callback = options;
options = {};
}
options = options || {};
callback = callback || function(){};
path = Path.resolve(this.cwd, path);
path = Path.resolve(sh.pwd(), path);
function createFile(path) {
fs.writeFile(path, '', callback);
@ -150,7 +152,8 @@ Shell.prototype.touch = function(path, options, callback) {
* (multiple file paths).
*/
Shell.prototype.cat = function(files, callback) {
var fs = this.fs;
var sh = this;
var fs = sh.fs;
var all = '';
callback = callback || function(){};
@ -162,7 +165,7 @@ Shell.prototype.cat = function(files, callback) {
files = typeof files === 'string' ? [ files ] : files;
function append(item, callback) {
var filename = Path.resolve(this.cwd, item);
var filename = Path.resolve(sh.pwd(), item);
fs.readFile(filename, 'utf8', function(error, data) {
if(error) {
callback(error);
@ -200,7 +203,8 @@ Shell.prototype.cat = function(files, callback) {
* the `recursive=true` option.
*/
Shell.prototype.ls = function(dir, options, callback) {
var fs = this.fs;
var sh = this;
var fs = sh.fs;
if(typeof options === 'function') {
callback = options;
options = {};
@ -214,7 +218,7 @@ Shell.prototype.ls = function(dir, options, callback) {
}
function list(path, callback) {
var pathname = Path.resolve(this.cwd, path);
var pathname = Path.resolve(sh.pwd(), path);
var result = [];
fs.readdir(pathname, function(error, entries) {
@ -272,7 +276,8 @@ Shell.prototype.ls = function(dir, options, callback) {
* `recursive=true` option.
*/
Shell.prototype.rm = function(path, options, callback) {
var fs = this.fs;
var sh = this;
var fs = sh.fs;
if(typeof options === 'function') {
callback = options;
options = {};
@ -286,7 +291,7 @@ Shell.prototype.rm = function(path, options, callback) {
}
function remove(pathname, callback) {
pathname = Path.resolve(this.cwd, pathname);
pathname = Path.resolve(sh.pwd(), pathname);
fs.stat(pathname, function(error, stats) {
if(error) {
callback(error);
@ -343,8 +348,9 @@ Shell.prototype.rm = function(path, options, callback) {
* env.TMP. The callback receives (error, tempDirName).
*/
Shell.prototype.tempDir = function(callback) {
var fs = this.fs;
var tmp = this.env.get('TMP');
var sh = this;
var fs = sh.fs;
var tmp = sh.env.get('TMP');
callback = callback || function(){};
// Try and create it, and it will either work or fail
@ -362,7 +368,8 @@ Shell.prototype.tempDir = function(callback) {
* MIT License
*/
Shell.prototype.mkdirp = function(path, callback) {
var fs = this.fs;
var sh = this;
var fs = sh.fs;
callback = callback || function(){};
if(!path) {
@ -428,7 +435,8 @@ Shell.prototype.mkdirp = function(path, callback) {
* filename is used instead. The callback receives (error, path).
*/
Shell.prototype.wget = function(url, options, callback) {
var fs = this.fs;
var sh = this;
var fs = sh.fs;
if(typeof options === 'function') {
callback = options;
options = {};
@ -447,7 +455,7 @@ Shell.prototype.wget = function(url, options, callback) {
// i.e. instead of "/foo?bar/" we would expect "/foo?bar%2F"
var path = options.filename || url.split('/').pop();
path = Path.resolve(fs.cwd, path);
path = Path.resolve(sh.pwd(), path);
function onerror() {
callback(new Error('unable to get resource'));
@ -469,8 +477,8 @@ Shell.prototype.wget = function(url, options, callback) {
};
Shell.prototype.unzip = function(zipfile, options, callback) {
var fs = this.fs;
var sh = this;
var fs = sh.fs;
if(typeof options === 'function') {
callback = options;
options = {};
@ -483,8 +491,8 @@ Shell.prototype.unzip = function(zipfile, options, callback) {
return;
}
var path = Path.resolve(this.cwd, zipfile);
var destination = Path.resolve(options.destination || this.cwd);
var path = Path.resolve(sh.pwd(), zipfile);
var destination = Path.resolve(options.destination || sh.pwd());
fs.readFile(path, function(err, data) {
if(err) return callback(err);
@ -515,8 +523,8 @@ Shell.prototype.unzip = function(zipfile, options, callback) {
};
Shell.prototype.zip = function(zipfile, paths, options, callback) {
var fs = this.fs;
var sh = this;
var fs = sh.fs;
if(typeof options === 'function') {
callback = options;
options = {};
@ -535,7 +543,7 @@ Shell.prototype.zip = function(zipfile, paths, options, callback) {
if(typeof paths === 'string') {
paths = [ paths ];
}
zipfile = Path.resolve(this.cwd, zipfile);
zipfile = Path.resolve(sh.pwd(), zipfile);
function toRelPath(path) {
// Make path relative within the zip
@ -568,7 +576,7 @@ Shell.prototype.zip = function(zipfile, paths, options, callback) {
}
function add(path, callback) {
path = Path.resolve(sh.cwd, path);
path = Path.resolve(sh.pwd(), path);
fs.stat(path, function(err, stats) {
if(err) return callback(err);