/
dashboard-login.js
86 lines (75 loc) · 2.57 KB
/
dashboard-login.js
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
'use strict';
const _ = require('lodash');
const { ServerlessSDK } = require('@serverless/platform-client');
const configUtils = require('@serverless/utils/config');
const promptWithHistory = require('@serverless/utils/inquirer/prompt-with-history');
const login = require('../../commands/login/dashboard');
const { showOnboardingWelcome } = require('./utils');
const loginOrRegisterQuestion = async ({ stepHistory }) =>
promptWithHistory({
message: 'Do you want to login/register to Serverless Dashboard?',
type: 'confirm',
name: 'shouldLoginOrRegister',
stepHistory,
});
const steps = {
loginOrRegister: async (context) => {
const shouldLoginOrRegister =
context.options.org || context.configuration.org || (await loginOrRegisterQuestion(context));
if (shouldLoginOrRegister) await login(context.options);
},
};
module.exports = {
async isApplicable(context) {
const { configuration, options, serviceDir } = context;
if (options.console) {
context.inapplicabilityReasonCode = 'CONSOLE_CONTEXT';
return false;
}
if (!serviceDir) {
context.inapplicabilityReasonCode = 'NOT_IN_SERVICE_DIRECTORY';
return false;
}
if (
_.get(configuration, 'provider') !== 'aws' &&
_.get(configuration, 'provider.name') !== 'aws'
) {
context.inapplicabilityReasonCode = 'NON_AWS_PROVIDER';
return false;
}
if (process.env.SERVERLESS_ACCESS_KEY) {
context.inapplicabilityReasonCode = 'SERVERLESS_ACCESS_KEY_PROVIDED';
return false;
}
const sdk = new ServerlessSDK();
const { supportedRegions, supportedRuntimes } = await sdk.metadata.get();
if (!supportedRuntimes.includes(_.get(configuration.provider, 'runtime') || 'nodejs12.x')) {
context.inapplicabilityReasonCode = 'UNSUPPORTED_RUNTIME';
return false;
}
if (
!supportedRegions.includes(options.region || configuration.provider.region || 'us-east-1')
) {
context.inapplicabilityReasonCode = 'UNSUPPORTED_REGION';
return false;
}
const isLoggedIn = Boolean(configUtils.getLoggedInUser());
if (isLoggedIn) {
context.inapplicabilityReasonCode = 'ALREADY_LOGGED_IN';
}
return !isLoggedIn;
},
async run(context) {
const isOrgProvided = context.options.org || context.configuration.org;
if (
context.initial.isInServiceContext &&
!context.initial.isDashboardEnabled &&
!isOrgProvided
) {
showOnboardingWelcome(context);
}
return steps.loginOrRegister(context);
},
steps,
configuredQuestions: ['shouldLoginOrRegister'],
};