Skip to content

Commit 5c525b5

Browse files
committed
Require Node.js 8
1 parent 15caf1c commit 5c525b5

File tree

5 files changed

+73
-69
lines changed

5 files changed

+73
-69
lines changed

.travis.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
language: node_js
22
node_js:
3-
- '4'
3+
- '10'
4+
- '8'

index.js

+44-43
Original file line numberDiff line numberDiff line change
@@ -3,86 +3,87 @@ const path = require('path');
33
const childProcess = require('child_process');
44
const isWsl = require('is-wsl');
55

6-
module.exports = (target, opts) => {
6+
module.exports = async (target, options) => {
77
if (typeof target !== 'string') {
8-
return Promise.reject(new Error('Expected a `target`'));
8+
throw new TypeError('Expected a `target`');
99
}
1010

11-
opts = Object.assign({wait: true}, opts);
11+
options = {
12+
wait: true,
13+
...options
14+
};
1215

13-
let cmd;
14-
let appArgs = [];
15-
let args = [];
16-
const cpOpts = {};
16+
let command;
17+
let appArguments = [];
18+
const cliArguments = [];
19+
const childProcessOptions = {};
1720

18-
if (Array.isArray(opts.app)) {
19-
appArgs = opts.app.slice(1);
20-
opts.app = opts.app[0];
21+
if (Array.isArray(options.app)) {
22+
appArguments = options.app.slice(1);
23+
options.app = options.app[0];
2124
}
2225

2326
if (process.platform === 'darwin') {
24-
cmd = 'open';
27+
command = 'open';
2528

26-
if (opts.wait) {
27-
args.push('-W');
29+
if (options.wait) {
30+
cliArguments.push('-W');
2831
}
2932

30-
if (opts.app) {
31-
args.push('-a', opts.app);
33+
if (options.app) {
34+
cliArguments.push('-a', options.app);
3235
}
3336
} else if (process.platform === 'win32' || isWsl) {
34-
cmd = 'cmd' + (isWsl ? '.exe' : '');
35-
args.push('/c', 'start', '""', '/b');
37+
command = 'cmd' + (isWsl ? '.exe' : '');
38+
cliArguments.push('/c', 'start', '""', '/b');
3639
target = target.replace(/&/g, '^&');
3740

38-
if (opts.wait) {
39-
args.push('/wait');
41+
if (options.wait) {
42+
cliArguments.push('/wait');
4043
}
4144

42-
if (opts.app) {
43-
args.push(opts.app);
45+
if (options.app) {
46+
cliArguments.push(options.app);
4447
}
4548

46-
if (appArgs.length > 0) {
47-
args = args.concat(appArgs);
49+
if (appArguments.length > 0) {
50+
cliArguments.push(...appArguments);
4851
}
4952
} else {
50-
if (opts.app) {
51-
cmd = opts.app;
53+
if (options.app) {
54+
command = options.app;
5255
} else {
5356
const useSystemXdgOpen = process.versions.electron || process.platform === 'android';
54-
cmd = useSystemXdgOpen ? 'xdg-open' : path.join(__dirname, 'xdg-open');
57+
command = useSystemXdgOpen ? 'xdg-open' : path.join(__dirname, 'xdg-open');
5558
}
5659

57-
if (appArgs.length > 0) {
58-
args = args.concat(appArgs);
60+
if (appArguments.length > 0) {
61+
cliArguments.push(...appArguments);
5962
}
6063

61-
if (!opts.wait) {
62-
// `xdg-open` will block the process unless
63-
// stdio is ignored and it's detached from the parent
64-
// even if it's unref'd
65-
cpOpts.stdio = 'ignore';
66-
cpOpts.detached = true;
64+
if (!options.wait) {
65+
// `xdg-open` will block the process unless stdio is ignored
66+
// and it's detached from the parent even if it's unref'd.
67+
childProcessOptions.stdio = 'ignore';
68+
childProcessOptions.detached = true;
6769
}
6870
}
6971

70-
args.push(target);
72+
cliArguments.push(target);
7173

72-
if (process.platform === 'darwin' && appArgs.length > 0) {
73-
args.push('--args');
74-
args = args.concat(appArgs);
74+
if (process.platform === 'darwin' && appArguments.length > 0) {
75+
cliArguments.push('--args', ...appArguments);
7576
}
7677

77-
const cp = childProcess.spawn(cmd, args, cpOpts);
78+
const cp = childProcess.spawn(command, cliArguments, childProcessOptions);
7879

79-
if (opts.wait) {
80+
if (options.wait) {
8081
return new Promise((resolve, reject) => {
8182
cp.once('error', reject);
8283

83-
cp.once('close', code => {
84-
if (code > 0) {
85-
reject(new Error('Exited with code ' + code));
84+
cp.once('close', exitCode => {
85+
if (exitCode > 0) {
86+
reject(new Error(`Exited with code ${exitCode}`));
8687
return;
8788
}
8889

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"url": "sindresorhus.com"
1111
},
1212
"engines": {
13-
"node": ">=4"
13+
"node": ">=8"
1414
},
1515
"scripts": {
1616
"test": "xo"
@@ -50,7 +50,7 @@
5050
"is-wsl": "^1.1.0"
5151
},
5252
"devDependencies": {
53-
"ava": "^0.25.0",
54-
"xo": "^0.20.0"
53+
"ava": "^1.3.1",
54+
"xo": "^0.24.0"
5555
}
5656
}

readme.md

+15-13
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ If need this for Electron, use [`shell.openItem()`](https://electronjs.org/docs/
77

88
#### Why?
99

10-
- Actively maintained
11-
- Supports app arguments
12-
- Safer as it uses `spawn` instead of `exec`
13-
- Fixes most of the open `node-open` issues
14-
- Includes the latest [`xdg-open` script](http://cgit.freedesktop.org/xdg/xdg-utils/commit/?id=c55122295c2a480fa721a9614f0e2d42b2949c18) for Linux
10+
- Actively maintained.
11+
- Supports app arguments.
12+
- Safer as it uses `spawn` instead of `exec`.
13+
- Fixes most of the open `node-open` issues.
14+
- Includes the latest [`xdg-open` script](http://cgit.freedesktop.org/xdg/xdg-utils/commit/?id=c55122295c2a480fa721a9614f0e2d42b2949c18) for Linux.
1515

1616

1717
## Install
@@ -24,21 +24,23 @@ $ npm install opn
2424
## Usage
2525

2626
```js
27-
const opn = require('opn');
27+
const open = require('opn');
2828

2929
// Opens the image in the default image viewer
30-
opn('unicorn.png').then(() => {
31-
// image viewer closed
32-
});
30+
(async () => {
31+
await open('unicorn.png');
32+
33+
console.log('The image viewer clsoed');
34+
})();
3335

3436
// Opens the url in the default browser
35-
opn('http://sindresorhus.com');
37+
open('http://sindresorhus.com');
3638

3739
// Specify the app to open in
38-
opn('http://sindresorhus.com', {app: 'firefox'});
40+
open('http://sindresorhus.com', {app: 'firefox'});
3941

4042
// Specify app arguments
41-
opn('http://sindresorhus.com', {app: ['google chrome', '--incognito']});
43+
open('http://sindresorhus.com', {app: ['google chrome', '--incognito']});
4244
```
4345

4446

@@ -73,7 +75,7 @@ On Windows you have to explicitly specify an app for it to be able to wait.
7375

7476
##### app
7577

76-
Type: `string` `Array`
78+
Type: `string | string[]`
7779

7880
Specify the app to open the `target` with, or an array with the app and app arguments.
7981

test.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import test from 'ava';
22
import isWsl from 'is-wsl';
3-
import m from '.';
3+
import open from '.';
44

55
let chromeName;
66
let firefoxName;
@@ -17,31 +17,31 @@ if (process.platform === 'darwin') {
1717
}
1818

1919
// Tests only checks that opening doesn't return an error
20-
// it has no way make sure that it actually opened anything
20+
// it has no way make sure that it actually opened anything.
2121

22-
// these have to be manually verified
22+
// These have to be manually verified.
2323

2424
test('open file in default app', async () => {
25-
await m('index.js');
25+
await open('index.js');
2626
});
2727

2828
test('not wait for the app to close if wait: false', async () => {
29-
await m('http://sindresorhus.com', {wait: false});
29+
await open('http://sindresorhus.com', {wait: false});
3030
});
3131

3232
test('open url in default app', async () => {
33-
await m('http://sindresorhus.com');
33+
await open('http://sindresorhus.com');
3434
});
3535

3636
test('open url in specified app', async () => {
37-
await m('http://sindresorhus.com', {app: firefoxName});
37+
await open('http://sindresorhus.com', {app: firefoxName});
3838
});
3939

4040
test('open url in specified app with arguments', async () => {
41-
await m('http://sindresorhus.com', {app: [chromeName, '--incognito']});
41+
await open('http://sindresorhus.com', {app: [chromeName, '--incognito']});
4242
});
4343

4444
test('return the child process when called', async t => {
45-
const cp = await m('index.js');
45+
const cp = await open('index.js');
4646
t.true('stdout' in cp);
4747
});

0 commit comments

Comments
 (0)