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

improved version #58

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ lib-cov
*.pid
*.gz
*.sw*
*.bak

pids
logs
Expand Down
4 changes: 2 additions & 2 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
test
*.sw*
test
*.sw*
26 changes: 26 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
language: node_js

branches:
only:
- master
- /^greenkeeper-.*$/
- /^greenkeeper/.*$/

addons:
firefox: "latest"

node_js:
- 12
- 13
- 14
- 15
- lts/*
- node

os:
- windows
- linux
- osx

env:
- NODE_ENV=testing
44 changes: 22 additions & 22 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
Copyright (c) 2012 Jay Jordan
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
Copyright (c) 2012 Jay Jordan

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
# open

[![Inline docs](http://inch-ci.org/github/HansHammel/node-open.svg?branch=master)](http://inch-ci.org/github/HansHammel/node-open)
[![star this repo](http://githubbadges.com/star.svg?user=HansHammel&repo=node-open&style=flat&color=fff&background=007ec6)](https://github.com/HansHammel/node-open)
[![fork this repo](http://githubbadges.com/fork.svg?user=HansHammel&repo=node-open&style=flat&color=fff&background=007ec6)](https://github.com/HansHammel/node-open/fork)
[![david dependency](https://img.shields.io/david/HansHammel/node-open.svg)](https://david-dm.org/HansHammel/node-open)
[![david devDependency](https://img.shields.io/david/dev/HansHammel/node-open.svg)](https://david-dm.org/HansHammel/node-open)
[![david optionalDependency](https://img.shields.io/david/optional/HansHammel/node-open.svg)](https://david-dm.org/HansHammel/node-open)
[![david peerDependency](https://img.shields.io/david/peer/HansHammel/node-open.svg)](https://david-dm.org/HansHammel/node-open)
[![Known Vulnerabilities](https://snyk.io/test/github/HansHammel/node-open/badge.svg)](https://snyk.io/test/github/HansHammel/node-open)

Open a file or url in the user's preferred application.

# Usage
# Node usage

```javascript
var open = require("open");
Expand All @@ -16,6 +25,33 @@ file or URL.
open("http://www.google.com", "firefox");
```

# NPM script usage

```javascript
{ ...
"scripts: {
...
"browser": "node-open http://www.google.com",
"firefox": "node-open http://www.google.com firefox",
...
},
...
}

With development profile

```javascript
open("http://www.google.com", "firefox", "-P development");
```

Getting Error from program (null if success)

```javascript
open("http://www.google.com", "firefox", "-P development", function(Error){
console.log(Error);
});
```

# Installation

npm install open
Expand Down
5 changes: 5 additions & 0 deletions bin/open
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#! /usr/bin/env node
var open = require('../lib/open');
var url = process.argv[2];
var browser = process.argv[3];
open(url, browser);
154 changes: 91 additions & 63 deletions lib/open.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,91 @@
var exec = require('child_process').exec
, path = require('path')
;


/**
* open a file or uri using the default application for the file type.
*
* @return {ChildProcess} - the child process object.
* @param {string} target - the file/uri to open.
* @param {string} appName - (optional) the application to be used to open the
* file (for example, "chrome", "firefox")
* @param {function(Error)} callback - called with null on success, or
* an error object that contains a property 'code' with the exit
* code of the process.
*/

module.exports = open;

function open(target, appName, callback) {
var opener;

if (typeof(appName) === 'function') {
callback = appName;
appName = null;
}

switch (process.platform) {
case 'darwin':
if (appName) {
opener = 'open -a "' + escape(appName) + '"';
} else {
opener = 'open';
}
break;
case 'win32':
// if the first parameter to start is quoted, it uses that as the title
// so we pass a blank title so we can quote the file we are opening
if (appName) {
opener = 'start "" "' + escape(appName) + '"';
} else {
opener = 'start ""';
}
break;
default:
if (appName) {
opener = escape(appName);
} else {
// use Portlands xdg-open everywhere else
opener = path.join(__dirname, '../vendor/xdg-open');
}
break;
}

if (process.env.SUDO_USER) {
opener = 'sudo -u ' + process.env.SUDO_USER + ' ' + opener;
}
return exec(opener + ' "' + escape(target) + '"', callback);
}

function escape(s) {
return s.replace(/"/g, '\\\"');
}
var exec = require('child_process').exec
, path = require('path')
whereis = function(filename){
var pathSep = process.platform === 'win32' ? ';' : ':';

var directories = process.env.PATH.split(pathSep);
for (var i = 0; i < directories.length; i++) {
var path = directories[i] + '/' + filename;
if (fs.existsSync(path)) {
return path;
}
}
return null;
};


/**
* open a file or uri using the default application for the file type.
*
* @return {ChildProcess} - the child process object.
* @param {string} target - the file/uri to open.
* @param {string} appName - (optional) the application to be used to open the
* file (for example, "chrome", "firefox")
* @param {function(error, stdout, stderr)} callback - called with null on success, or
* an error object that contains a property 'code' with the exit
* code of the process.
*/

module.exports = open;

function open(target, appName, args, callback) {
var opener;

if (typeof(appName) === 'function') {
callback = appName;
appName = null;
}

if (typeof(args) === 'function') {
callback = args;
args = null;
}

switch (process.platform) {
case 'darwin':
if (appName) {
opener = 'open -a "' + escape(appName) + '"';
if (args) {
opener += ' --args ';
}
} else {
opener = 'open';
}
break;
case 'win32':
// if the first parameter to start is quoted, it uses that as the title
// so we pass a blank title so we can quote the file we are opening
if (appName) {
opener = 'start "" "' + escape(appName) + '"';
} else {
opener = 'start ""';
}
break;
default:
if (appName) {
opener = escape(appName);
} else if (whereis('xdg-open')) {
// use default xdg-open if exist
opener = 'xdg-open';
} else {
// use Portlands xdg-open everywhere else
opener = path.join(__dirname, '../vendor/xdg-open');
}
break;
}

if (process.env.SUDO_USER) {
opener = 'sudo -u ' + process.env.SUDO_USER + ' ' + opener;
}

if (args) {
opener = opener + ' ' + args;
}

return exec(opener + ' "' + escape(target) + '"', { "shell": true},callback);

}

function escape(s) {
return s.replace(/"/g, '\\\"');
}
18 changes: 14 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
{
"name": "open",
"version": "0.0.5",
"version": "0.1.1",
"description": "open a file or url in the user's preferred application",
"keywords": ["start", "open", "browser", "editor", "default"],
"keywords": [
"start",
"open",
"browser",
"editor",
"default"
],
"homepage": "https://github.com/jjrdn/node-open",
"author": "J Jordan <[email protected]>",
"license": "MIT",
Expand All @@ -19,13 +25,17 @@
"engines": {
"node": ">= 0.6.0"
},
"dependencies": {},
"dependencies": {
},
"devDependencies": {
"mocha": "*"
},
"optionalDependencies": {},
"main": "lib/open.js",
"bin": {
"node-open": "bin/open"
},
"scripts": {
"test": "node_modules/mocha/bin/mocha"
"test": "mocha"
}
}
Loading