Skip to content

Commit 3b13e9c

Browse files
committed
docs: update docs for using a plugin
1 parent 9afe239 commit 3b13e9c

3 files changed

Lines changed: 29 additions & 12 deletions

File tree

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,10 @@ shell.open('file.txt'); // the plugin is now available!
5858
- Windows
5959

6060
This is only supported for Node v4+
61+
62+
## Writing plugins
63+
64+
If you're interested in taking a look at the current state of the plugin API,
65+
take a look at [index.js](index.js). This has helpful comments explaining the
66+
necessary boilerplate for writing a plugin. For an example usage of the plugin,
67+
take a look at [test/test.js](test/test.js).

index.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1+
// This exposes the plugin utilities
12
var plugin = require('shelljs/src/common');
3+
4+
// Require whatever modules you need for your project
25
var child = require('child_process');
36
var shell = require('shelljs');
47
var fs = require('fs');
58

6-
//@
7-
//@ ### open([options,] file [, file ...])
8-
//@ ### open([options,] file_array)
9-
//@ Examples:
10-
//@
11-
//@ ```javascript
12-
//@ open('file.jpg');
13-
//@ ```
14-
//@
15-
//@ Opens files with their default application
9+
// Implement your command in a function, which takes an options string as the
10+
// first parameter
1611
function open(options, fileName) {
12+
// Use the plugin utils to parse the options to only those options that are
13+
// available (in this case, there are no available options
1714
options = plugin.parseOptions(options, {
1815
});
1916

@@ -36,6 +33,8 @@ function open(options, fileName) {
3633
return new shell.ShellString('', '', 0);
3734
}
3835

36+
// Register the new plugin as a ShellJS command
3937
plugin.register('open', open, { globStart: 1 });
4038

39+
// Optionally, you can export the implementation of the command like so:
4140
exports.open = open;

test/test.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1-
/* globals describe, it, before, afterEach */
1+
/* globals describe, it */
2+
3+
// Steps for using a plugin:
4+
// First, you must load in ShellJS:
25
var shell = require('shelljs');
3-
var pluginOpen = require('..'); // load plugin
6+
// After that, you can load the plugin:
7+
var pluginOpen = require('..');
8+
/*
9+
* You can load a plugin with either of the following syntaxes:
10+
* `require('shelljs-plugin-open');` or
11+
* `var pluginOpen = require('shelljs-plugin-open');`
12+
*/
13+
14+
// Now, require whichever other modules you want to require:
415
require('should');
516

617
// override console.error() to cover up common.error() calls

0 commit comments

Comments
 (0)