From a7a1f4b82e3dfd5feb9ab28880de6f83f4fedc58 Mon Sep 17 00:00:00 2001 From: "David Humphrey (:humph) david.humphrey@senecacollege.ca" Date: Wed, 19 Feb 2014 17:36:26 -0500 Subject: [PATCH] Change sh.exec to take a list vs. object for passed args --- README.md | 16 ++++++++-------- src/shell.js | 18 +++++++++--------- tests/spec/shell/exec.spec.js | 4 ++-- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 8a7fdd4..1b3b4a4 100644 --- a/README.md +++ b/README.md @@ -997,7 +997,7 @@ var sh = fs.Shell(); * [sh.cd(path, callback)](#cd) * [sh.pwd()](#pwd) * [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.cat(files, callback)](#cat) * [sh.rm(path, [options], callback)](#rm) @@ -1069,22 +1069,22 @@ sh.ls('/dir', { recursive: true }, function(err, entries) { }); ``` -#### sh.exec(path, [options], callback) +#### sh.exec(path, [args], callback) 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 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: -* `options` - an object containing any arguments, data, etc. -* `fs` - the `FileSystem` object bound to this shell. -* `callback` - a callback function of the form `function callback(error, result)` +* `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)` to call when done. The .js command's contents should be the body of a function that looks like this: ```javascript -function(fs, options, callback) { +function(fs, args, callback) { //-------------------------commmand code here--------- // ... //---------------------------------------------------- @@ -1095,14 +1095,14 @@ Example: ```javascript // 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 fs.writeFile('/cmd.js', cmd, callback(err) { if(err) throw err; // Execute the command - sh.exec('/cmd.js', { path: '/file' }, function(err, result) { + sh.exec('/cmd.js', [ '/file' ], function(err, result) { if(err) throw err; }); }); diff --git a/src/shell.js b/src/shell.js index d936a28..a691196 100644 --- a/src/shell.js +++ b/src/shell.js @@ -66,23 +66,23 @@ define(function(require) { * defined at runtime: * * * 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. * * The .js command's contents should be the body of a function * that looks like this: * - * function(fs, options, callback) { + * function(fs, args, callback) { * // .js code here * } */ - Shell.prototype.exec = function(path, options, callback) { + Shell.prototype.exec = function(path, args, callback) { var fs = this.fs; - if(typeof options === 'function') { - callback = options; - options = {}; + if(typeof args === 'function') { + callback = args; + args = []; } - options = options || {}; + args = args || []; callback = callback || function(){}; path = Path.resolve(this.cwd, path); @@ -92,8 +92,8 @@ define(function(require) { return; } try { - var cmd = new Function('fs', 'options', 'callback', data); - cmd(fs, options, callback); + var cmd = new Function('fs', 'args', 'callback', data); + cmd(fs, args, callback); } catch(e) { callback(e); } diff --git a/tests/spec/shell/exec.spec.js b/tests/spec/shell/exec.spec.js index 6f325a0..fbbecb9 100644 --- a/tests/spec/shell/exec.spec.js +++ b/tests/spec/shell/exec.spec.js @@ -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) { var fs = util.fs(); 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) { 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; fs.readFile('/test', 'utf8', function(error, data) {