Skip to content

Commit 809b593

Browse files
committed
Update for new Plugin changes
1 parent 3b13e9c commit 809b593

3 files changed

Lines changed: 69 additions & 20 deletions

File tree

index.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,11 @@ var plugin = require('shelljs/src/common');
33

44
// Require whatever modules you need for your project
55
var child = require('child_process');
6-
var shell = require('shelljs');
76
var fs = require('fs');
87

98
// Implement your command in a function, which takes an options string as the
109
// first parameter
1110
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
14-
options = plugin.parseOptions(options, {
15-
});
16-
1711
var URL_REGEX = /^https?:\/\/.*/;
1812

1913
if (!fs.existsSync(fileName) && !fileName.match(URL_REGEX)) {
@@ -30,11 +24,15 @@ function open(options, fileName) {
3024
// assume it's Mac OS X, which uses `open()`
3125
child.exec('open ' + JSON.stringify(fileName));
3226
}
33-
return new shell.ShellString('', '', 0);
27+
return '';
3428
}
3529

3630
// Register the new plugin as a ShellJS command
37-
plugin.register('open', open, { globStart: 1 });
31+
plugin.register('open', open, {
32+
globStart: 1, // Start globbing at the first non-option argument
33+
cmdOptions: {}, // No supported options
34+
wrapOutput: true, // Wrap the output in a ShellString
35+
});
3836

3937
// Optionally, you can export the implementation of the command like so:
4038
exports.open = open;

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@
2828
"eslint-config-airbnb-base": "^4.0.2",
2929
"eslint-plugin-import": "^1.11.1",
3030
"mocha": "^2.5.3",
31-
"shelljs": "0.7.1",
31+
"shelljs": "0.7.2",
3232
"shelljs-changelog": "^0.2.2",
3333
"shelljs-release": "^0.2.0",
3434
"should": "^10.0.0"
3535
},
3636
"peerDependencies": {
37-
"shelljs": "0.7.1"
37+
"shelljs": "0.7.2"
3838
}
3939
}

test/test.js

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,75 @@
11
/* globals describe, it */
22

3-
// Steps for using a plugin:
4-
// First, you must load in ShellJS:
5-
var shell = require('shelljs');
6-
// After that, you can load the plugin:
7-
var pluginOpen = require('..');
83
/*
4+
* Using ShellJS Plugins:
5+
*
6+
* To learn how to use a ShellJS plugin, look for the comments following the
7+
* slash-star/star-slash syntax.
8+
*/
9+
10+
/*
11+
* Load all your plugins first:
912
* You can load a plugin with either of the following syntaxes:
1013
* `require('shelljs-plugin-open');` or
1114
* `var pluginOpen = require('shelljs-plugin-open');`
12-
*/
15+
*
16+
* Here, we've opted to save it in a variable, allowing us the option to use the
17+
* bare function in addition to shell.open() (which includes helpful ShellJS
18+
* features)
19+
*/
20+
var pluginOpen = require('..');
21+
// If we were using additional plugins, we could load them here
22+
23+
/*
24+
* After that, you must load in ShellJS itself, either locally (recommended) or
25+
* globally (which still supported). For the purposes of testing this plugin,
26+
* we're actually testing both ways of importing ShellJS (but you would never
27+
* want to use ShellJS like this normally):
28+
*/
29+
var shell = require('shelljs'); // recommended
30+
require('shelljs/global'); // not recommended
1331

14-
// Now, require whichever other modules you want to require:
32+
/*
33+
* Now, require whichever other modules you want to require:
34+
*/
1535
require('should');
36+
var assert = require('assert');
1637

1738
// override console.error() to cover up common.error() calls
1839
console.error = function () { };
1940

2041
describe('plugin-open', function () {
2142
it('gets added to the shelljs instance', function () {
43+
/*
44+
* Plugins automatically add new commands to the ShellJS instance, such as
45+
* shell.open()
46+
*/
2247
(typeof shell.open).should.equal('function');
2348
});
2449

50+
it('gets added to the global namespace for shelljs/global', function () {
51+
/*
52+
* Plugins are also compatible with using require('shelljs/global');
53+
*/
54+
(typeof global.open).should.equal('function');
55+
global.open.should.equal(shell.open);
56+
});
57+
58+
it('does not override other commands or methods', function () {
59+
/*
60+
* Plugins shouldn't interfere with existing commands
61+
*/
62+
(typeof shell.cp).should.equal('function');
63+
(typeof shell.mv).should.equal('function');
64+
shell.ls().should.have.property('toEnd');
65+
shell.ls().should.have.property('grep');
66+
shell.ls().should.have.property('sed');
67+
});
68+
2569
it('exports the plugin implementation', function () {
70+
/*
71+
* A plugin author can also export the implementation of their commands
72+
*/
2673
(typeof pluginOpen).should.equal('object');
2774
pluginOpen.should.have.property('open');
2875
(typeof pluginOpen.open).should.equal('function');
@@ -38,7 +85,7 @@ describe('plugin-open', function () {
3885
it('opens URLs', function () {
3986
var ret = shell.open('https://www.google.com');
4087
ret.code.should.equal(0);
41-
ret.stderr.should.equal('');
88+
assert.ok(!ret.stderr);
4289
});
4390

4491
it('opens files asynchronously', function () {
@@ -47,13 +94,17 @@ describe('plugin-open', function () {
4794
// if this gets to this point, it must have been asynchronous
4895
ret.code.should.equal(0);
4996
ret.stdout.should.equal('');
50-
ret.stderr.should.equal('');
97+
assert.ok(!ret.stderr);
5198
});
5299

53100
it('handles glob characters', function () {
101+
/*
102+
* Plugins can easily take advantage of ShellJS's built-in glob expansion.
103+
* This is indicated by the globStart option
104+
*/
54105
var ret = shell.open('te?t/*st.js');
55106
ret.code.should.equal(0);
56107
ret.stdout.should.equal('');
57-
ret.stderr.should.equal('');
108+
assert.ok(!ret.stderr);
58109
});
59110
});

0 commit comments

Comments
 (0)