Change sh.exec to take a list vs. object for passed args

This commit is contained in:
David Humphrey (:humph) david.humphrey@senecacollege.ca 2014-02-19 17:36:26 -05:00
parent eadd4ec03c
commit a7a1f4b82e
3 changed files with 19 additions and 19 deletions

View File

@ -997,7 +997,7 @@ var sh = fs.Shell();
* [sh.cd(path, callback)](#cd) * [sh.cd(path, callback)](#cd)
* [sh.pwd()](#pwd) * [sh.pwd()](#pwd)
* [sh.ls(dir, [options], callback)](#ls) * [sh.ls(dir, [options], callback)](#ls)
* [sh.exec(path, [options], callback)](#exec) * [sh.exec(path, [args], callback)](#exec)
* [sh.touch(path, [options], callback)](#touch) * [sh.touch(path, [options], callback)](#touch)
* [sh.cat(files, callback)](#cat) * [sh.cat(files, callback)](#cat)
* [sh.rm(path, [options], callback)](#rm) * [sh.rm(path, [options], callback)](#rm)
@ -1069,22 +1069,22 @@ sh.ls('/dir', { recursive: true }, function(err, entries) {
}); });
``` ```
#### sh.exec(path, [options], callback)<a name="exec"></a> #### sh.exec(path, [args], callback)<a name="exec"></a>
Attempts to execute the .js command located at `path`. The `sh.exec` method Attempts to execute the .js command located at `path`. The `sh.exec` method
enables apps to install larger programs into the file system and run them enables apps to install larger programs into the file system and run them
later without having to re-download. Such commands should be written so as later without having to re-download. Such commands should be written so as
to assume the existence of 3 global variables, which will be defined at runtime: to assume the existence of 3 global variables, which will be defined at runtime:
* `options` - <Object> an object containing any arguments, data, etc. * `fs` - [FileSystem] the `FileSystem` object bound to this shell.
* `fs` - <FileSystem> the `FileSystem` object bound to this shell. * `args` - [Array] a list of any arguments for the command, or the empty list
* `callback` - <Function> a callback function of the form `function callback(error, result)` * `callback` - [Function] a callback function of the form `function callback(error, result)`
to call when done. to call when done.
The .js command's contents should be the body of a function that The .js command's contents should be the body of a function that
looks like this: looks like this:
```javascript ```javascript
function(fs, options, callback) { function(fs, args, callback) {
//-------------------------commmand code here--------- //-------------------------commmand code here---------
// ... // ...
//---------------------------------------------------- //----------------------------------------------------
@ -1095,14 +1095,14 @@ Example:
```javascript ```javascript
// Simple command to delete a file. // Simple command to delete a file.
var cmd = "fs.unlink(options.path, callback);" var cmd = "fs.unlink(args[0], callback);"
// Write the file to the filesystem // Write the file to the filesystem
fs.writeFile('/cmd.js', cmd, callback(err) { fs.writeFile('/cmd.js', cmd, callback(err) {
if(err) throw err; if(err) throw err;
// Execute the command // Execute the command
sh.exec('/cmd.js', { path: '/file' }, function(err, result) { sh.exec('/cmd.js', [ '/file' ], function(err, result) {
if(err) throw err; if(err) throw err;
}); });
}); });

View File

@ -66,23 +66,23 @@ define(function(require) {
* defined at runtime: * defined at runtime:
* *
* * fs - the current shell's bound filesystem object * * fs - the current shell's bound filesystem object
* * options - an object containing any arguments, data, etc. * * args - a list of arguments for the command, or an empty list if none
* * callback - a callback function(error, result) to call when done. * * callback - a callback function(error, result) to call when done.
* *
* The .js command's contents should be the body of a function * The .js command's contents should be the body of a function
* that looks like this: * that looks like this:
* *
* function(fs, options, callback) { * function(fs, args, callback) {
* // .js code here * // .js code here
* } * }
*/ */
Shell.prototype.exec = function(path, options, callback) { Shell.prototype.exec = function(path, args, callback) {
var fs = this.fs; var fs = this.fs;
if(typeof options === 'function') { if(typeof args === 'function') {
callback = options; callback = args;
options = {}; args = [];
} }
options = options || {}; args = args || [];
callback = callback || function(){}; callback = callback || function(){};
path = Path.resolve(this.cwd, path); path = Path.resolve(this.cwd, path);
@ -92,8 +92,8 @@ define(function(require) {
return; return;
} }
try { try {
var cmd = new Function('fs', 'options', 'callback', data); var cmd = new Function('fs', 'args', 'callback', data);
cmd(fs, options, callback); cmd(fs, args, callback);
} catch(e) { } catch(e) {
callback(e); callback(e);
} }

View File

@ -12,12 +12,12 @@ define(["Filer", "util"], function(Filer, util) {
it('should be able to execute a command .js file from the filesystem', function(done) { it('should be able to execute a command .js file from the filesystem', function(done) {
var fs = util.fs(); var fs = util.fs();
var shell = fs.Shell(); var shell = fs.Shell();
var cmdString = "fs.writeFile(options.path, options.data, callback);"; var cmdString = "fs.writeFile(args[0], args[1], callback);";
fs.writeFile('/cmd.js', cmdString, function(error) { fs.writeFile('/cmd.js', cmdString, function(error) {
if(error) throw error; if(error) throw error;
shell.exec('/cmd.js', {path: '/test', data: 'hello world'}, function(error, result) { shell.exec('/cmd.js', ['/test', 'hello world'], function(error, result) {
if(error) throw error; if(error) throw error;
fs.readFile('/test', 'utf8', function(error, data) { fs.readFile('/test', 'utf8', function(error, data) {