-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
46 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const open = require('opn'); | ||
const tcpPortUsed = require('tcp-port-used'); | ||
const url = require('url'); | ||
|
||
const openUrls = { | ||
docs: 'http://localhost:8000', | ||
storybook: 'http://localhost:9001', | ||
}; | ||
|
||
module.exports = ({tasks, prompt, addPrompt}) => ({ | ||
start: Object.assign({}, tasks.start, { | ||
requiredArgs: ['open'], | ||
|
||
choice: addPrompt( | ||
tasks.start.choice, | ||
() => prompt([{ | ||
type: 'list', | ||
name: 'open', | ||
message: 'What do you want to open in the browser?', | ||
choices: [ | ||
{value: 'docs', short: 'Docs', name: 'The documentation website'}, | ||
{value: 'storybook', short: 'Storybook', name: 'The component explorer'}, | ||
] | ||
}]) | ||
), | ||
|
||
run(options) { | ||
const openUrl = openUrls[options.open]; | ||
const port = parseInt(url.parse(openUrl).port, 10); | ||
|
||
if(port) { | ||
// wait for whatever (storybook/gatsby) to be listening on the port | ||
// try every 500ms and give up after 30s | ||
tcpPortUsed.waitUntilUsed(port, 500, 30000) | ||
.then(() => open(openUrl)) | ||
.catch(e => console.error(e.stack)); | ||
} | ||
|
||
return tasks.start.run(options); | ||
}, | ||
}), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters