-
Notifications
You must be signed in to change notification settings - Fork 12
/
login.ts
105 lines (83 loc) · 2.83 KB
/
login.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import fs from 'fs-extra';
import chalk from 'chalk';
import { Flags } from '@oclif/core';
import Command from '../base.js';
import AccountAdd from './account/add.js';
import AccountUse from './account/use.js';
import { createDebugLogger } from '../utils/output.js';
import { GLOBAL_CONF_PATH, GLOBAL_CONF_VERSION } from '../constants.js';
export default class Login extends Command {
static description = 'login to your account';
static flags = {
...Command.flags,
email: Flags.string({ char: 'e', description: 'your email' }),
password: Flags.string({ char: 'p', description: 'your password' }),
interactive: Flags.boolean({
char: 'i',
description: 'login with username/password',
}),
browser: Flags.string({
description: 'browser to open',
options: ['chrome', 'firefox', 'edge'],
}),
};
async run() {
const { flags } = await this.parse(Login);
const debug = createDebugLogger(flags.debug);
const sendFlag = [
'--api-token',
flags['api-token'] || '',
'--email',
flags.email || '',
'--password',
flags.password || '',
'--from-login',
];
if (flags.region) {
sendFlag.push('--region', flags.region);
this.log(`You're logging into "${flags.region}" region:`);
}
if (!flags.interactive) {
try {
const accounts = await this.browser(flags.browser);
this.spinner.start('Logging in.');
const currentAccounts = (await this.readGlobalConfig()).accounts;
let currentAccount;
for (const account of accounts) {
const name = `${account.email.split('@')[0]}_${account.region}`;
if (account.current) {
currentAccount = name;
}
currentAccounts[name] = {
email: account.email,
region: account.region,
avatar: account.avatar,
api_token: account.token,
fullname: account.fullname,
current: false,
};
}
fs.writeFileSync(
GLOBAL_CONF_PATH,
JSON.stringify({
accounts: currentAccounts,
version: GLOBAL_CONF_VERSION,
})
);
this.spinner.succeed('You have logged in successfully.');
currentAccount && (await AccountUse.run(['--account', currentAccount]));
const { accountName } = await this.getCurrentAccount();
this.log(`> Auth credentials saved in ${chalk.bold(GLOBAL_CONF_PATH)}`);
accountName && this.log(`> Current account is: ${accountName}`);
return;
} catch (error) {
debug(`${error.message}\n`);
this.spinner.fail(
'Cannot open browser. Browser unavailable or lacks permissions.'
);
}
}
await AccountAdd.run(sendFlag);
this.log(chalk.green('You have logged in successfully.'));
}
}