2022-10-31 16:07:52 +01:00

87 lines
2.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
const inquirer = require('@serverless/utils/inquirer');
const { StepHistory } = require('@serverless/utils/telemetry');
const log = require('@serverless/utils/log').log.get('onboarding');
const { resolveInitialContext } = require('./utils');
const { awsRequest } = require('./utils');
const steps = {
service: require('./service'),
consoleLogin: require('./console-login'),
dashboardLogin: require('./dashboard-login'),
consoleResolveOrg: require('./console-resolve-org'),
consoleSetupIamRole: require('./console-setup-iam-role'),
dashboardSetOrg: require('./dashboard-set-org'),
awsCredentials: require('./aws-credentials'),
deploy: require('./deploy'),
};
const resolveAwsAccountId = async (context) => {
try {
return (await awsRequest(context, 'STS', 'getCallerIdentity')).Account;
} catch {
return null;
}
};
module.exports = async (context) => {
const stepsDetails = new Map(
Object.entries(steps).map(([stepName, step]) => {
return [stepName, { configuredQuestions: step.configuredQuestions }];
})
);
const { commandUsage, options } = context;
const history = new Map();
context = { ...context, inquirer, history };
commandUsage.stepsHistory = history;
commandUsage.stepsHistory.toJSON = () => {
return Array.from(stepsDetails.entries()).map(([step, stepDetails]) => {
const stepHistory = history.get(step);
return {
name: step,
...stepDetails,
history: stepHistory ? stepHistory.toJSON() : [],
};
});
};
const initialContext = resolveInitialContext(context);
commandUsage.initialContext = initialContext;
context.initial = initialContext;
context.awsAccountId = await resolveAwsAccountId(context);
if (options.console) {
if (!context.awsAccountId) {
log.error(
'Were unable to connect Console via the CLI - No local AWS credentials found\n' +
'Visit https://console.serverless.com/ to set up Console from the web'
);
} else {
context.isConsole = true;
}
}
for (const [stepName, step] of Object.entries(steps)) {
delete context.stepHistory;
delete context.inapplicabilityReasonCode;
const stepData = await step.isApplicable(context);
if (stepData) log.debug('%s: applicable: %o', stepName, stepData);
else log.debug('%s: not applicable: %s', stepName, context.inapplicabilityReasonCode);
Object.assign(stepsDetails.get(stepName), {
isApplicable: Boolean(stepData),
inapplicabilityReasonCode: context.inapplicabilityReasonCode,
timestamp: Date.now(),
});
if (stepData) {
log.notice();
context.stepHistory = new StepHistory();
context.history.set(stepName, context.stepHistory);
await step.run(context, stepData);
}
}
return context;
};