Change sh.exec to take a list vs. object for passed args
This commit is contained in:
parent
eadd4ec03c
commit
a7a1f4b82e
16
README.md
16
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)<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
|
||||
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` - <Object> an object containing any arguments, data, etc.
|
||||
* `fs` - <FileSystem> the `FileSystem` object bound to this shell.
|
||||
* `callback` - <Function> 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;
|
||||
});
|
||||
});
|
||||
|
|
18
src/shell.js
18
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);
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue