-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve interactive AWS creds flow #6449
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
'use strict'; | ||
// copied from https://raw.githubusercontent.com/sindresorhus/open/master/index.js | ||
// and adapted for node 6 support. Because open>6 requries node >= 8 but open<6 fails npm audit | ||
// changes: | ||
// * use bluebird.promisify instead of util.promisfy | ||
// * Object.assign instead of spread | ||
// * use Array.prototype.push.apply(a,b) instead of a.push(...b) | ||
// * async/await -> then :| | ||
// * prettified with our config | ||
|
||
const { promisify } = require('bluebird'); | ||
const path = require('path'); | ||
const childProcess = require('child_process'); | ||
const fs = require('fs'); | ||
const isWsl = require('is-wsl'); | ||
|
||
const pAccess = promisify(fs.access); | ||
const pExecFile = promisify(childProcess.execFile); | ||
|
||
// Path to included `xdg-open` | ||
const localXdgOpenPath = path.join(__dirname, 'xdg-open'); | ||
|
||
// Convert a path from WSL format to Windows format: | ||
// `/mnt/c/Program Files/Example/MyApp.exe` → `C:\Program Files\Example\MyApp.exe` | ||
const wslToWindowsPath = filePath => | ||
pExecFile('wslpath', ['-w', filePath]).then(({ stdout }) => stdout.trim()); | ||
|
||
module.exports = (target, options) => { | ||
if (typeof target !== 'string') { | ||
throw new TypeError('Expected a `target`'); | ||
} | ||
|
||
options = Object.assign( | ||
{ | ||
wait: false, | ||
background: false, | ||
}, | ||
options | ||
); | ||
|
||
let command; | ||
let appArguments = []; | ||
const cliArguments = []; | ||
const childProcessOptions = {}; | ||
|
||
if (Array.isArray(options.app)) { | ||
appArguments = options.app.slice(1); | ||
options.app = options.app[0]; | ||
} | ||
|
||
return Promise.resolve() | ||
.then(() => { | ||
if (process.platform === 'darwin') { | ||
command = 'open'; | ||
|
||
if (options.wait) { | ||
cliArguments.push('--wait-apps'); | ||
} | ||
|
||
if (options.background) { | ||
cliArguments.push('--background'); | ||
} | ||
|
||
if (options.app) { | ||
cliArguments.push('-a', options.app); | ||
} | ||
return null; | ||
} else if (process.platform === 'win32' || isWsl) { | ||
command = `cmd${isWsl ? '.exe' : ''}`; | ||
cliArguments.push('/c', 'start', '""', '/b'); | ||
target = target.replace(/&/g, '^&'); | ||
|
||
if (options.wait) { | ||
cliArguments.push('/wait'); | ||
} | ||
|
||
return Promise.resolve() | ||
.then(() => { | ||
if (options.app) { | ||
if (isWsl && options.app.startsWith('/mnt/')) { | ||
return wslToWindowsPath(options.app).then(windowsPath => { | ||
options.app = windowsPath; | ||
cliArguments.push(options.app); | ||
}); | ||
} | ||
|
||
cliArguments.push(options.app); | ||
} | ||
return null; | ||
}) | ||
.then(() => { | ||
if (appArguments.length > 0) { | ||
Array.prototype.push.apply(cliArguments, appArguments); | ||
} | ||
return null; | ||
}); | ||
} | ||
return Promise.resolve() | ||
.then(() => { | ||
if (options.app) { | ||
command = options.app; | ||
} else { | ||
// When bundled by Webpack, there's no actual package file path and no local `xdg-open`. | ||
const isBundled = !__dirname || __dirname === '/'; | ||
|
||
// Check if local `xdg-open` exists and is executable. | ||
return pAccess(localXdgOpenPath, fs.constants.X_OK) | ||
.then(() => true) | ||
.catch(() => false) | ||
.then(exeLocalXdgOpen => { | ||
const useSystemXdgOpen = | ||
process.versions.electron || | ||
process.platform === 'android' || | ||
isBundled || | ||
!exeLocalXdgOpen; | ||
command = useSystemXdgOpen ? 'xdg-open' : localXdgOpenPath; | ||
}); | ||
} | ||
return null; | ||
}) | ||
.then(() => { | ||
if (appArguments.length > 0) { | ||
Array.prototype.push.apply(cliArguments, appArguments); | ||
} | ||
|
||
if (!options.wait) { | ||
// `xdg-open` will block the process unless stdio is ignored | ||
// and it's detached from the parent even if it's unref'd. | ||
childProcessOptions.stdio = 'ignore'; | ||
childProcessOptions.detached = true; | ||
} | ||
}); | ||
}) | ||
.then(() => { | ||
cliArguments.push(target); | ||
|
||
if (process.platform === 'darwin' && appArguments.length > 0) { | ||
cliArguments.push('--args'); | ||
Array.prototype.push.apply(cliArguments, appArguments); | ||
} | ||
|
||
const subprocess = childProcess.spawn(command, cliArguments, childProcessOptions); | ||
|
||
if (options.wait) { | ||
return new Promise((resolve, reject) => { | ||
subprocess.once('error', reject); | ||
|
||
subprocess.once('close', exitCode => { | ||
if (exitCode > 0) { | ||
reject(new Error(`Exited with code ${exitCode}`)); | ||
return; | ||
} | ||
|
||
resolve(subprocess); | ||
}); | ||
}); | ||
} | ||
|
||
subprocess.unref(); | ||
|
||
return subprocess; | ||
}); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict'; | ||
|
||
/* eslint-disable no-console */ | ||
|
||
const opn = require('./open'); | ||
const chalk = require('chalk'); | ||
const isDockerContainer = require('is-docker'); | ||
const BbPromise = require('bluebird'); | ||
|
||
function displayManualOpenMessage(url, err) { | ||
// https://github.com/sindresorhus/log-symbols | ||
console.log('---------------------------'); | ||
const errMsg = err ? `\nError: ${err.message}` : ''; | ||
const msg = `Unable to open browser automatically${errMsg}`; | ||
console.log(`🙈 ${chalk.red(msg)}`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While cute I'm unsure if we want to go the "emotification of all the CLIs" route in an enterprise-facing project. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I copy and pasted this out of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (as to why i copied, it.. didn't want to add sdk as a direct dep, which is the only way to guarantee it's availability with things like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
But SDK Is dependency of a plugin.. so if SDK was updated to not work in v6, then through Plugin it'll also break SLS (?) |
||
console.log(chalk.green('Please open your browser & open the URL below to login:')); | ||
console.log(chalk.yellow(url)); | ||
console.log('---------------------------'); | ||
return false; | ||
} | ||
|
||
module.exports = function openBrowser(url) { | ||
let browser = process.env.BROWSER; | ||
if (browser === 'none' || isDockerContainer()) { | ||
return BbPromise.resolve(displayManualOpenMessage(url)); | ||
} | ||
if (process.platform === 'darwin' && browser === 'open') { | ||
browser = undefined; | ||
} | ||
const options = { wait: false, app: browser }; | ||
return opn(url, options).catch(err => displayManualOpenMessage(url, err)); | ||
}; | ||
|
||
/* eslint-enable no-console */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why we do not rely here on serverless logger? Even if we don't like to use
cli.log
we may rel oncli.consoleLog
.It'll clearly indicate the intention of generating user facing CLI output.
Direct use of
console.log
is more for temporary debug output (which we prevent from committing in via lint rules)