Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stdin support #7

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 23 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@
'use strict';
const meow = require('meow');
const opn = require('opn');
const getStdin = require('get-stdin');
const tempWrite = require('temp-write');
const fileType = require('file-type');

const cli = meow(`
Usage
$ opn <file|url> [--wait] [-- <app> [args]]
$ stdout | opn [--wait] [--ext] [-- <app> [args]]

Options
--wait Wait for the app to exit
--ext File extension for stdin

Examples
$ opn http://sindresorhus.com
$ opn http://sindresorhus.com -- firefox
$ opn http://sindresorhus.com -- 'google chrome' --incognito
$ opn unicorn.png
$ cat ./image.png | opn
$ echo '[]' | opn --ext json
`, {
default: {
wait: false
Expand All @@ -23,4 +30,19 @@ const cli = meow(`

cli.flags.app = cli.input.slice(1);

opn(cli.input[0], cli.flags);
const input = cli.input[0];

if (!input && process.stdin.isTTY) {
console.error('Input required');
process.exit(1);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just do it like this?

const input = cli.input[0];

if (!input && process.stdin.isTTY) {
    console.error('Input required');
    process.exit(1);
}

if (input) {
    opn(input, cli.flags);
} else {
    // stdin stuff here
}

You have to show an error if input is empty and process.stdin.isTTY is true.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh you are showing an error. Would rather see it extracted from the this if-else block because this looks weird.

  1. handle input
  2. show error
  3. handle input

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}

if (input) {
opn(input, cli.flags);
} else {
getStdin.buffer().then(stdin => {
const type = fileType(stdin);
const ext = (cli.flags.ext || type && type.ext || 'txt');
opn(tempWrite.sync(stdin, `opn.${ext}`), cli.flags);
});
}
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@
"file"
],
"dependencies": {
"file-type": "^3.6.0",
"get-stdin": "^5.0.1",
"meow": "^3.7.0",
"opn": "^4.0.0"
"opn": "^4.0.0",
"temp-write": "^2.1.0"
},
"devDependencies": {
"ava": "*",
Expand Down
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@ $ opn --help

Usage
$ opn <file|url> [--wait] [-- <app> [args]]
$ stdout | opn [--wait] [--ext] [-- <app> [args]]

Options
--wait Wait for the app to exit
--ext File extension for stdin

Examples
$ opn http://sindresorhus.com
$ opn http://sindresorhus.com -- firefox
$ opn http://sindresorhus.com -- 'google chrome' --incognito
$ opn unicorn.png
$ cat ./image.png | opn
$ echo '[]' | opn --ext json
```


Expand Down