Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
address windows, mac, linux behavior w/ callback and auto-open Wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedotexe committed May 8, 2020
1 parent 45050f0 commit 6a40ac6
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,34 +77,42 @@ exports.login = async function (options) {

// attempt to capture accountId automatically via browser callback
let tempUrl;
const isWin = process.platform === 'win32';
// See: https://github.com/near/near-shell/issues/358
// Mac: set up a callback open site automatically
// Windows: don't set up callback open site automatically
// Linux: set up callback don't open site automatically
const isMac = process.platform === 'darwin';
const isLinux = process.platform === 'linux';

// find a callback URL on the local machine
try {
if (!isWin) { // capture callback is currently not working on windows. This is a workaround to not use it
if (isMac || isLinux) { // capture callback is currently not working on windows. This is a workaround to not use it
tempUrl = await capture.callback(5000);
// if we found a suitable URL, attempt to use it
if (isMac) {
newUrl.searchParams.set('success_url', `http://${tempUrl.hostname}:${tempUrl.port}`);
await openUrl(newUrl);
} else {
// is linux
if (process.env.GITPOD_WORKSPACE_URL) {
const workspaceUrl = new URL(process.env.GITPOD_WORKSPACE_URL);
newUrl.searchParams.set('success_url', `https://${tempUrl.port}-${workspaceUrl.hostname}`);
// Browser not opened, as will open automatically for opened port
} else {
// is linux but not Gitpod
newUrl.searchParams.set('success_url', `http://${tempUrl.hostname}:${tempUrl.port}`);
}
}
} else {
// redirect automatically, but do not use the browser callback
await openUrl(newUrl);
}
} catch (error) {
// console.error("Failed to find suitable port.", error.message)
// TODO: Is it? Try triggering error
// silent error is better here
}

// if we found a suitable URL, attempt to use it
if (tempUrl) {
if (process.env.GITPOD_WORKSPACE_URL) {
const workspaceUrl = new URL(process.env.GITPOD_WORKSPACE_URL);
newUrl.searchParams.set('success_url', `https://${tempUrl.port}-${workspaceUrl.hostname}`);
// Browser not opened, as will open automatically for opened port
} else {
newUrl.searchParams.set('success_url', `http://${tempUrl.hostname}:${tempUrl.port}`);
openUrl(newUrl);
}
} else if (isWin) {
// redirect automatically on windows, but do not use the browser callback
openUrl(newUrl);
}

console.log(chalk`\n{dim If your browser doesn't automatically open, please visit this URL\n${newUrl.toString()}}`);

const getAccountFromWebpage = async () => {
Expand Down Expand Up @@ -136,8 +144,10 @@ exports.login = async function (options) {
accountId = await new Promise((resolve, reject) => {
let resolved = false;
const resolveOnce = (result) => { if (!resolved) resolve(result); resolved = true; };
getAccountFromWebpage()
.then(resolveOnce); // NOTE: error ignored on purpose
if (!isLinux) {
getAccountFromWebpage()
.then(resolveOnce); // NOTE: error ignored on purpose
}
getAccountFromConsole()
.then(resolveOnce)
.catch(reject);
Expand Down

0 comments on commit 6a40ac6

Please sign in to comment.