Skip to content

Commit

Permalink
Fix issue with yargs and nodemon integration
Browse files Browse the repository at this point in the history
  • Loading branch information
darcyclarke committed Feb 16, 2018
1 parent 3094b84 commit fa98fcb
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 75 deletions.
14 changes: 4 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,6 @@ or utilizing [`npx`](https://www.npmjs.com/package/npx)
$ npx rawkit example.js
```

### Don't want to install the Chrome Extension?

**Homebrew install [chrome-cli](https://github.com/prasmussen/chrome-cli)**

```bash
$ brew install chrome-cli
```

**rawkit** will automatically detect `chrome-cli` and use it over prompting to use the Chrome Extension. Unfortunately, Windows users will still have to go through that first option.

<img src="https://user-images.githubusercontent.com/459713/29084045-d01c953a-7c38-11e7-9711-a2875d81f1c1.gif" width="100%" alt="tutorial">

## Options
Expand Down Expand Up @@ -74,4 +64,8 @@ Open the devtools in canary.
### Should this be in node core?
*Maybe*. A flag like `--launch` would be a nice supplement to `--inspect`. That said...

### Why do I need to install a Chrome Extension?

**rawkit** will automatically launch a splash page asking you to install a corresponding Chrome Extension the first time you run it. Unfortunately, this is a requirement to be able to open the debugging window/tab. Read more below:

*Opening internal Chrome links, externally, is not possible at the moment (ie. `chrome://` or `chrome-devtools://` in this case). This is most likely a security feature. That said, you can use a Chrome Extension as a proxy to make this work. Check out the [RESEARCH.md](https://github.com/darcyclarke/rawkit/blob/master/RESEARCH.md) for more information. You can also use something like this [lighthouse/chrome-launcher](https://github.com/GoogleChrome/lighthouse/tree/master/chrome-launcher).*
26 changes: 26 additions & 0 deletions RESEARCH.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,29 @@ Great article by Eric Bidelman here: https://developers.google.com/web/updates/2
#### Keys to the Chrome Extension approach:
- Use `chrome.tabs.create({ url: '[insert chrome-specific link here]' })`
- Put the above **inside** a user/browser action/listener (ex. `chrome.tabs.onCreated.addListener()` or `chrome.browserAction.onClicked.addListener()`)

### Chrome CLI
An old approach to checking if someone had installed [chrome-cli](https://github.com/prasmussen/chrome-cli) which no longer supports dynamically opening the devtools/debugger window (unfortunately).

```js
const execSync = require('child_process').execSync
const spawn = require('child_process').spawn

if (exists('chrome-cli')) {
let chrome = spawn('chrome-cli', [ 'open', ref ])
execSync(`open -a "${this.chrome}"`)
chrome.stdout.on('data', _ => {})
chrome.stderr.on('data', _ => {})
chrome.on('close', _ => {})
}

function exists (cmd) {
try {
let stdout = execSync(`command -v ${cmd} 2>/dev/null && { echo >&1 \'${cmd} found\'; exit 0; }`
)
return !!stdout
} catch (error) {
return false
}
}
```
50 changes: 27 additions & 23 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ const opn = require('opn')
const path = require('path')
const exec = require('child_process').exec
const execSync = require('child_process').execSync
const spawn = require('child_process').spawn
const yargs = require('yargs')
const compare = require('semver-compare')
const getos = require('getos')
const semver = require('semver')
const version = require('../package').version

class CLI {
constructor (args) {
Expand All @@ -18,7 +19,6 @@ class CLI {
this.image = { path: '../extension/icon.png', type: 'image/png' }
this.index = { path: '../extension/index.html', type: 'text/html' }
this.caught = false
this.browser = `${this.args.executable} ${this.args.canary ? 'canary' : ''}`.trim()
this.errors = {
process: 'Error: You must define a path to a node process directly or within your package.json under "main"',
nodemon: 'Error: nodemon is not installed'
Expand All @@ -27,18 +27,17 @@ class CLI {

get props () {
return {
args: this.args,
browser: this.browser
args: this.args
}
}

parseArguments (args) {
let ret = yargs
return yargs
.version()
.demandCommand(1)
.usage('rawkit [options] <file ...>')
.alias('v', 'version')
.version(() => require('../package').version)
.version(version)
.describe('v', 'show version information')
.alias('h', 'help')
.help('help')
Expand Down Expand Up @@ -75,7 +74,6 @@ class CLI {
boolean: true
})
.parse(args)
return ret
}

parseURL (str) {
Expand All @@ -97,7 +95,7 @@ class CLI {
}

path (path) {
path = path | this.pkg(process.cwd())
path = path || this.pkg(process.cwd())
path = (path && fs.lstatSync(path).isDirectory()) ? this.pkg(path) : path
if (path && fs.lstatSync(path).isFile()) {
return path
Expand Down Expand Up @@ -125,7 +123,7 @@ class CLI {
console.error(this.errors.nodemon)
process.exit()
}
if (fs.existsSync(path)) {
if (fs.existsSync(file)) {
let config = JSON.parse(fs.readFileSync(file, 'utf8'))
return (config && config.execMap) ? config.execMap.js : cmd
}
Expand All @@ -135,7 +133,7 @@ class CLI {
exec () {
let path = this.path(this.args._[2])
let o = process.argv
let legacy = (compare(process.version, '8.0.0') === -1)
let legacy = semver.lt(semver.coerce(process.version), '8.0.0')
let brk = (legacy) ? 'debug-brk' : 'inspect-brk'
let cmd = (this.args.brk) ? brk : 'inspect'
let args = o.splice(o.indexOf(path), o.length).join(' ').replace(path, '')
Expand All @@ -155,25 +153,31 @@ class CLI {
let ref = this.parseURL(data)
if (!this.caught && ref && !this.args['no-prompt']) {
this.caught = true
if (this.exists('chrome-cli')) {
let chrome = spawn('chrome-cli', [ 'open', ref ])
execSync(`open -a "${this.chrome}"`)
chrome.stdout.on('data', _ => {})
chrome.stderr.on('data', _ => {})
chrome.on('close', _ => {})
} else {
getos((e, data) => {
let link = `https://darcyclarke.github.io/rawkit/?rawkit=${encodeURIComponent(ref)}`
if (!e) {
if (data.os === 'win32') {
this.args.executable = 'chrome'
}
if (data.os === 'linux') {
this.args.executable = 'google-chrome'
}
if (data.os === 'darwin' && this.args.canary) {
this.args.executable = 'google chrome canary'
}
}
let opts = {
app: [this.browser],
app: this.args.executable,
wait: false
}
console.log(link, opts)
opn(link, opts)
.then(() => {})
.catch((e) => {})
}
if (!this.args.silent) {
console.log('\x1b[33m%s\x1b[0m', 'Devtools URL:', ref)
}
if (!this.args.silent) {
console.log('\x1b[33m%s\x1b[0m', 'Devtools URL:', ref)
}
})
} else if (!this.args.silent) {
process.stdout.write(data)
}
Expand Down
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
"casual": "^1.5.14",
"chai": "4.1.2",
"cz-conventional-changelog": "^2.0.0",
"eslint": "4.11.0",
"eslint": "4.17.0",
"eslint-config-standard": "11.0.0-beta.0",
"eslint-plugin-import": "2.7.0",
"eslint-plugin-import": "2.8.0",
"eslint-plugin-node": "6.0.0",
"eslint-plugin-promise": "3.5.0",
"eslint-plugin-promise": "3.6.0",
"eslint-plugin-standard": "3.0.1",
"mocha": "4.1.0",
"mocha": "5.0.1",
"pre-git": "3.17.0",
"standard-version": "4.3.0"
},
Expand Down Expand Up @@ -73,9 +73,10 @@
}
},
"dependencies": {
"getos": "^3.1.0",
"opn": "5.2.0",
"semver-compare": "1.0.0",
"yargs": "10.1.1"
"semver": "^5.5.0",
"yargs": "11.0.0"
},
"release": {
"analyzeCommits": "simple-commit-message"
Expand Down
1 change: 1 addition & 0 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const port = 8888

server.on('request', (req, res) => {
console.log('request', req)
console.log('hello')
res.end('server is running...')
}).listen(port)

Expand Down
Loading

0 comments on commit fa98fcb

Please sign in to comment.