filer/src/shell.js

126 lines
3.1 KiB
JavaScript
Raw Normal View History

/* jshint evil:true */
define(function(require) {
2014-02-15 15:54:54 +00:00
var Path = require('src/path');
2014-02-17 15:47:47 +00:00
var Error = require('src/error');
2014-02-15 15:54:54 +00:00
function Shell(fs, options) {
options = options || {};
2014-02-17 15:47:47 +00:00
var cwd = '/';
Object.defineProperty(this, 'fs', {
get: function() { return fs; },
enumerable: true
});
Object.defineProperty(this, 'cwd', {
get: function() { return cwd; },
enumerable: true
});
// We include `cd` on the this vs. proto so that
// we can access cwd without exposing it externally.
2014-02-17 15:47:47 +00:00
this.cd = function(path, callback) {
path = Path.resolve(this.cwd, path);
// Make sure the path actually exists, and is a dir
fs.stat(path, function(err, stats) {
if(err) {
callback(new Error.ENotDirectory());
return;
}
if(stats.type === 'DIRECTORY') {
cwd = path;
callback();
} else {
callback(new Error.ENotDirectory());
}
});
};
}
2014-02-15 20:28:00 +00:00
/**
* Execute the .js command located at `path`. Such commands
* should assume the existence of 3 arguments, which will be
* defined at runtime:
*
* * options - an object containing any arguments, data, etc.
* * callback - a callback function(error, result) to call when done.
*
* The .js command's contents should be the body of a function
* that looks like this:
*
* function(fs, options, callback) {
* // .js code here
* }
*/
Shell.prototype.exec = function(path, options, callback) {
var fs = this.fs;
if(typeof options === 'function') {
callback = options;
options = {};
}
options = options || {};
callback = callback || function(){};
path = Path.resolve(this.cwd, path);
2014-02-15 20:28:00 +00:00
fs.readFile(path, "utf8", function(error, data) {
if(error) {
callback(error);
return;
}
try {
var cmd = new Function('fs', 'options', 'callback', data);
cmd(fs, options, callback);
} catch(e) {
callback(e);
}
});
};
2014-02-15 15:54:54 +00:00
/**
* Create a file if it does not exist, or update access and
* modified times if it does. Valid options include:
*
2014-02-15 20:28:00 +00:00
* * updateOnly - whether to create the file if missing (defaults to false)
2014-02-15 15:54:54 +00:00
* * date - use the provided Date value instead of current date/time
*/
Shell.prototype.touch = function(path, options, callback) {
var fs = this.fs;
2014-02-15 15:54:54 +00:00
if(typeof options === 'function') {
callback = options;
options = {};
}
2014-02-15 20:28:00 +00:00
options = options || {};
callback = callback || function(){};
path = Path.resolve(this.cwd, path);
function createFile(path) {
2014-02-17 15:47:47 +00:00
fs.writeFile(path, '', callback);
}
function updateTimes(path) {
var now = Date.now();
2014-02-15 15:54:54 +00:00
var atime = options.date || now;
var mtime = options.date || now;
2014-02-17 15:47:47 +00:00
fs.utimes(path, atime, mtime, callback);
}
fs.stat(path, function(error, stats) {
if(error) {
2014-02-15 20:28:00 +00:00
if(options.updateOnly === true) {
2014-02-15 15:54:54 +00:00
callback();
} else {
createFile(path);
}
} else {
updateTimes(path);
}
});
};
return Shell;
});