Skip to content

Commit 29f33da

Browse files
committed
refactor: use opener module
1 parent 2260140 commit 29f33da

3 files changed

Lines changed: 11 additions & 13 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
[![npm](https://img.shields.io/npm/v/shelljs-plugin-open.svg?style=flat-square)](https://www.npmjs.com/package/shelljs-plugin-open)
66

77
A [ShellJS](https://github.com/shelljs/shelljs) plugin for the `open()` command
8-
to open a file (or URL) with its default application.
8+
to open a file (or URL) with its default application. This is largely based on
9+
[opener](https://github.com/domenic/opener).
910

1011
This is designed to imitate the `open` command on Mac OS X. Here's the
1112
equivalent commands for other systems:

index.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
var plugin = require('shelljs/plugin');
33

44
// Require whatever modules you need for your project
5-
var child = require('child_process');
5+
var opener = require('opener');
66
var fs = require('fs');
77

88
// Implement your command in a function, which accepts `options` as the
@@ -14,23 +14,17 @@ function open(options, fileName) {
1414
plugin.error('Unable to locate file: ' + fileName);
1515
}
1616

17-
var cmd;
18-
if (process.platform.match(/^win/))
19-
cmd = 'start';
20-
else if (process.platform.match(/^linux/))
21-
cmd = 'xdg-open';
22-
else // assume it's Mac OS X, which uses `open`
23-
cmd = 'open';
24-
25-
child.exec(cmd + ' ' + JSON.stringify(fileName));
17+
var proc = opener(fileName);
18+
proc.unref();
19+
proc.stdin.unref();
20+
proc.stdout.unref();
21+
proc.stderr.unref();
2622
return '';
2723
}
2824

2925
// Register the new plugin as a ShellJS command
3026
plugin.register('open', open, {
31-
globStart: 1, // Start globbing at the first non-option argument
3227
cmdOptions: {}, // There are no supported options for this command
33-
wrapOutput: true, // Wrap the output in a ShellString
3428
});
3529

3630
// Optionally, you can export the implementation of the command like so:

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,8 @@
3838
},
3939
"peerDependencies": {
4040
"shelljs": "^0.7.3"
41+
},
42+
"dependencies": {
43+
"opener": "^1.4.1"
4144
}
4245
}

0 commit comments

Comments
 (0)