1 | const { flags } = require('@oclif/command')
|
2 | const Command = require('../utils/command')
|
3 | const chalk = require('chalk')
|
4 |
|
5 | class LoginCommand extends Command {
|
6 | async run() {
|
7 | const { flags } = this.parse(LoginCommand)
|
8 | const [accessToken, location] = this.getConfigToken()
|
9 | if (accessToken && !flags.new) {
|
10 | this.log(`Already logged in ${msg(location)}`)
|
11 | this.log()
|
12 | this.log(`Run ${chalk.cyanBright('netlify status')} for account details`)
|
13 | this.log()
|
14 | this.log(`To see all available commands run: ${chalk.cyanBright('netlify help')}`)
|
15 | this.log()
|
16 | return this.exit()
|
17 | }
|
18 |
|
19 | await this.config.runHook('analytics', {
|
20 | eventName: 'command',
|
21 | payload: {
|
22 | command: 'login',
|
23 | new: flags.new
|
24 | }
|
25 | })
|
26 |
|
27 | await this.expensivelyAuthenticate()
|
28 |
|
29 | return this.exit()
|
30 | }
|
31 | }
|
32 |
|
33 | function msg(location) {
|
34 | switch (location) {
|
35 | case 'env':
|
36 | return 'via process.env.NETLIFY_AUTH_TOKEN set in your terminal session'
|
37 | case 'flag':
|
38 | return 'via CLI --auth flag'
|
39 | case 'config':
|
40 | return 'via netlify config on your machine'
|
41 | default:
|
42 | return ''
|
43 | }
|
44 | }
|
45 |
|
46 | LoginCommand.flags = {
|
47 | new: flags.boolean({
|
48 | description: 'Login to new Netlify account'
|
49 | })
|
50 | }
|
51 | LoginCommand.description = `Login to your Netlify account
|
52 |
|
53 | Opens a web browser to acquire an OAuth token.
|
54 | `
|
55 |
|
56 | module.exports = LoginCommand
|