Skip to content
This repository has been archived by the owner on Feb 1, 2022. It is now read-only.

Commit

Permalink
feat: added ux.open()
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed May 23, 2018
1 parent 8703593 commit 5bb9f2c
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ await cli.prompt('sometext', 'https://google.com')

![url demo](assets/url.gif)

# cli.open

Open a url in the browser

```typescript
await cli.open('https://oclif.io')
```

# cli.action

Shows a spinner
Expand Down
3 changes: 3 additions & 0 deletions examples/open.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import ux from '../src'

ux.open('https://oclif.io')
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"fs-extra": "^6.0.1",
"hyperlinker": "^1.0.0",
"indent-string": "^3.2.0",
"is-wsl": "^1.1.0",
"lodash": "^4.17.10",
"password-prompt": "^1.0.6",
"semver": "^5.5.0",
Expand Down
2 changes: 2 additions & 0 deletions src/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import screen = require('@oclif/screen')
import ansiStyles = require('ansi-styles')
import stripAnsi = require('strip-ansi')

import open = require('./open')
import prompt = require('./prompt')
import styledHeader = require('./styled/header')
import styledJSON = require('./styled/json')
Expand All @@ -15,6 +16,7 @@ export const deps = {
get passwordPrompt(): any { return fetch('password-prompt') },
get screen(): typeof screen { return fetch('@oclif/screen') },

get open(): typeof open.default { return fetch('./open').default },
get prompt(): typeof prompt.default { return fetch('./prompt').default },
get styledObject(): typeof styledObject.default { return fetch('./styled/object').default },
get styledHeader(): typeof styledHeader.default { return fetch('./styled/header').default },
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const ux = {
get styledHeader() { return deps.styledHeader },
get styledJSON() { return deps.styledJSON },
get table() { return deps.table },
get open() { return deps.open },

async done() {
config.action.stop()
Expand Down
93 changes: 93 additions & 0 deletions src/open.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// this code is largely taken from opn
import * as childProcess from 'child_process'
import * as path from 'path'
const isWsl = require('is-wsl')

export namespace open {
export interface Options {
// wait: boolean
app?: string | string[]
}
}

export default function open(target: string, opts: open.Options = {}) {
// opts = {wait: true, ...opts}

let cmd
let appArgs: string[] = []
let args: string[] = []
const cpOpts: childProcess.SpawnOptions = {}

if (Array.isArray(opts.app)) {
appArgs = opts.app.slice(1)
opts.app = opts.app[0]
}

if (process.platform === 'darwin') {
cmd = 'open'

// if (opts.wait) {
// args.push('-W')
// }

if (opts.app) {
args.push('-a', opts.app)
}
} else if (process.platform === 'win32' || isWsl) {
cmd = 'cmd' + (isWsl ? '.exe' : '')
args.push('/c', 'start', '""', '/b')
target = target.replace(/&/g, '^&')

// if (opts.wait) {
// args.push('/wait')
// }

if (opts.app) {
args.push(opts.app)
}

if (appArgs.length > 0) {
args = args.concat(appArgs)
}
} else {
if (opts.app) {
cmd = opts.app
} else {
cmd = process.platform === 'android' ? 'xdg-open' : path.join(__dirname, 'xdg-open')
}

if (appArgs.length > 0) {
args = args.concat(appArgs)
}

// if (!opts.wait) {
// `xdg-open` will block the process unless
// stdio is ignored and it's detached from the parent
// even if it's unref'd
cpOpts.stdio = 'ignore'
cpOpts.detached = true
// }
}

args.push(target)

if (process.platform === 'darwin' && appArgs.length > 0) {
args.push('--args')
args = args.concat(appArgs)
}

const cp = childProcess.spawn(cmd, args, cpOpts)

return new Promise((resolve, reject) => {
cp.once('error', reject)

cp.once('close', code => {
if (code > 0) {
reject(new Error('Exited with code ' + code))
return
}

resolve(cp)
})
})
}
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,10 @@ is-resolvable@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/is-resolvable/-/is-resolvable-1.1.0.tgz#fb18f87ce1feb925169c9a407c19318a3206ed88"

is-wsl@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/is-wsl/-/is-wsl-1.1.0.tgz#1f16e4aa22b04d1336b66188a66af3c600c3a66d"

[email protected]:
version "0.0.1"
resolved "https://registry.yarnpkg.com/isarray/-/isarray-0.0.1.tgz#8a18acfca9a8f4177e09abfc6038939b05d1eedf"
Expand Down

0 comments on commit 5bb9f2c

Please sign in to comment.