-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: run input.start around help and openining urls
This also refactors open url and open url prompt to the same file.
- Loading branch information
1 parent
3ec86a0
commit f66808b
Showing
13 changed files
with
344 additions
and
359 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
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
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
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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,51 +1,100 @@ | ||
const promiseSpawn = require('@npmcli/promise-spawn') | ||
const { output } = require('proc-log') | ||
|
||
const { output, input } = require('proc-log') | ||
const { URL } = require('url') | ||
const readline = require('readline') | ||
|
||
const assertValidUrl = (url) => { | ||
try { | ||
if (!/^https?:$/.test(new URL(url).protocol)) { | ||
throw new Error() | ||
} | ||
} catch { | ||
throw new Error('Invalid URL: ' + url) | ||
} | ||
} | ||
|
||
const outputMsg = (json, title, url) => { | ||
const msg = json ? JSON.stringify({ title, url }) : `${title}:\n${url}\n` | ||
output.standard(msg) | ||
} | ||
|
||
// attempt to open URL in web-browser, print address otherwise: | ||
const open = async (npm, url, errMsg, isFile) => { | ||
const openUrl = async (npm, url, title, isFile) => { | ||
url = encodeURI(url) | ||
const browser = npm.config.get('browser') | ||
|
||
function printAlternateMsg () { | ||
const json = npm.config.get('json') | ||
const alternateMsg = json | ||
? JSON.stringify({ | ||
title: errMsg, | ||
url, | ||
}, null, 2) | ||
: `${errMsg}:\n ${url}\n` | ||
|
||
output.standard(alternateMsg) | ||
} | ||
const json = npm.config.get('json') | ||
|
||
if (browser === false) { | ||
printAlternateMsg() | ||
outputMsg(json, title, url) | ||
return | ||
} | ||
|
||
// We pass this in as true from the help command so we know we don't have to | ||
// check the protocol | ||
if (!isFile) { | ||
try { | ||
if (!/^https?:$/.test(new URL(url).protocol)) { | ||
throw new Error() | ||
} | ||
} catch { | ||
throw new Error('Invalid URL: ' + url) | ||
assertValidUrl(url) | ||
} | ||
|
||
try { | ||
await input.start(() => promiseSpawn.open(url, { | ||
command: browser === true ? null : browser, | ||
})) | ||
} catch (err) { | ||
if (err.code !== 127) { | ||
throw err | ||
} | ||
outputMsg(json, title, url) | ||
} | ||
} | ||
|
||
// Prompt to open URL in browser if possible | ||
const openUrlPrompt = async (npm, url, title, prompt, emitter) => { | ||
const browser = npm.config.get('browser') | ||
const json = npm.config.get('json') | ||
const isInteractive = process.stdin.isTTY === true && process.stdout.isTTY === true | ||
|
||
assertValidUrl(url) | ||
outputMsg(json, title, url) | ||
|
||
if (browser === false || !isInteractive) { | ||
return | ||
} | ||
|
||
const command = browser === true ? null : browser | ||
await promiseSpawn.open(url, { command }) | ||
.catch((err) => { | ||
if (err.code !== 127) { | ||
throw err | ||
} | ||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout, | ||
}) | ||
|
||
printAlternateMsg() | ||
const tryOpen = await input.read(() => new Promise(resolve => { | ||
rl.on('SIGINT', () => { | ||
rl.close() | ||
resolve('SIGINT') | ||
}) | ||
|
||
rl.question(prompt, () => { | ||
resolve(true) | ||
}) | ||
|
||
if (emitter && emitter.addListener) { | ||
emitter.addListener('abort', () => { | ||
rl.close() | ||
resolve(false) | ||
}) | ||
} | ||
})) | ||
|
||
if (tryOpen === 'SIGINT') { | ||
throw new Error('canceled') | ||
} | ||
|
||
if (!tryOpen) { | ||
return | ||
} | ||
|
||
await openUrl(npm, url, 'Browser unavailable. Please open the URL manually') | ||
} | ||
|
||
module.exports = open | ||
module.exports = { | ||
openUrl, | ||
openUrlPrompt, | ||
} |
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
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
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
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
Oops, something went wrong.