Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions lib/classes/plugin-manager.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const path = require('path');
const config = require('@serverless/utils/config');
const _ = require('lodash');
const ServerlessError = require('../serverless-error');
const resolveCliInput = require('../cli/resolve-input');
Expand Down Expand Up @@ -134,7 +133,7 @@ class PluginManager {
.filter(Boolean)
.forEach((Plugin) => this.addPlugin(Plugin));
isRegisteringExternalPlugins = false;
if (EnterprisePlugin) this.dashboardPlugin = this.addPlugin(EnterprisePlugin);
this.dashboardPlugin = this.addPlugin(EnterprisePlugin);
isRegisteringExternalPlugins = true;
return this.asyncPluginInit();
}
Expand Down Expand Up @@ -255,7 +254,6 @@ class PluginManager {
}

resolveEnterprisePlugin() {
if (config.getConfig().enterpriseDisabled) return null;
this.pluginIndependentCommands.add('login').add('logout').add('dashboard');
return require('@serverless/dashboard-plugin');
}
Expand Down
7 changes: 3 additions & 4 deletions lib/cli/conditionally-load-dotenv.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ const _ = require('lodash');

module.exports = async (options, configuration) => {
const stage = options.stage || _.get(configuration, 'provider.stage', 'dev');
if (configuration.useDotenv) {
require('./load-dotenv')(stage);
return;
}
if (!configuration.useDotenv) return false;
require('./load-dotenv')(stage);
return true;
};
164 changes: 87 additions & 77 deletions scripts/serverless.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,44 +362,36 @@ processSpanPromise = (async () => {
}

// Load eventual environment variables from .env files
await require('../lib/cli/conditionally-load-dotenv')(options, configuration);

if (envVarNamesNeededForDotenvResolution) {
for (const envVarName of envVarNamesNeededForDotenvResolution) {
if (process.env[envVarName]) {
throw new ServerlessError(
'Cannot reliably resolve "env" variables due to resolution conflict.\n' +
`Environment variable "${envVarName}" which influences resolution of ` +
'".env" file were found to be defined in resolved ".env" file.' +
'DOTENV_ENV_VAR_RESOLUTION_CONFLICT'
);
if (await require('../lib/cli/conditionally-load-dotenv')(options, configuration)) {
if (envVarNamesNeededForDotenvResolution) {
for (const envVarName of envVarNamesNeededForDotenvResolution) {
if (process.env[envVarName]) {
throw new ServerlessError(
'Cannot reliably resolve "env" variables due to resolution conflict.\n' +
`Environment variable "${envVarName}" which influences resolution of ` +
'".env" file were found to be defined in resolved ".env" file.' +
'DOTENV_ENV_VAR_RESOLUTION_CONFLICT'
);
}
}
}
if (!isPropertyResolved(variablesMeta, 'provider\0name')) {
await resolveVariables(resolverConfiguration);
if (
eventuallyReportVariableResolutionErrors(
configurationPath,
configuration,
variablesMeta
)
) {
variablesMeta = null;
return;
}
}
}

if (!variablesMeta.size) return; // No properties configured with variables

if (isHelpRequest || commands[0] === 'plugin') {
// We do not need full config resolved, we just need to know what
// provider is service setup with, and with what eventual plugins Framework is extended
// as that influences what CLI commands and options could be used,
resolverConfiguration.propertyPathsToResolve.add('plugins');
} else {
delete resolverConfiguration.propertyPathsToResolve;
}

await resolveVariables(resolverConfiguration);
if (
eventuallyReportVariableResolutionErrors(
configurationPath,
configuration,
variablesMeta
)
) {
variablesMeta = null;
return;
}

if (!providerName) {
if (!ensureResolvedProperty('provider\0name')) return;
providerName = resolveProviderName(configuration);
Expand All @@ -417,21 +409,31 @@ processSpanPromise = (async () => {
commandSchema,
providerName,
});
await resolveVariables(resolverConfiguration);
if (
eventuallyReportVariableResolutionErrors(
configurationPath,
configuration,
variablesMeta
)
) {
variablesMeta = null;
return;
}
}
}
}

if (isHelpRequest || commands[0] === 'plugin') {
// We do not need full config resolved, we just need to know what
// provider is service setup with, and with what eventual plugins Framework is extended
// as that influences what CLI commands and options could be used,
resolverConfiguration.propertyPathsToResolve.add('plugins');
} else {
delete resolverConfiguration.propertyPathsToResolve;
}

await resolveVariables(resolverConfiguration);
if (
eventuallyReportVariableResolutionErrors(
configurationPath,
configuration,
variablesMeta
)
) {
variablesMeta = null;
return;
}

if (!variablesMeta.size) return; // All properties successuflly resolved

if (!ensureResolvedProperty('plugins')) return;
Expand Down Expand Up @@ -563,19 +565,6 @@ processSpanPromise = (async () => {
if (isHelpRequest) return;
if (!_.get(variablesMeta, 'size')) return;

// Resolve remaininig service configuration variables
if (providerName === 'aws') {
// Ensure properties which are crucial to some variable source resolvers
// are actually resolved.
if (
!ensureResolvedProperty('provider\0credentials') ||
!ensureResolvedProperty('provider\0deploymentBucket\0serverSideEncryption') ||
!ensureResolvedProperty('provider\0profile') ||
!ensureResolvedProperty('provider\0region')
) {
return;
}
}
if (commandSchema) {
resolverConfiguration.options = filterSupportedOptions(options, {
commandSchema,
Expand All @@ -584,12 +573,51 @@ processSpanPromise = (async () => {
}
resolverConfiguration.fulfilledSources.add('opt');

// Register serverless instance and AWS provider specific variable sources
// Register serverless instance specific variable sources
resolverConfiguration.sources.sls =
require('../lib/configuration/variables/sources/instance-dependent/get-sls')(serverless);
resolverConfiguration.fulfilledSources.add('sls');

resolverConfiguration.sources.param =
serverless.pluginManager.dashboardPlugin.configurationVariablesSources.param;
resolverConfiguration.fulfilledSources.add('param');

// Register dashboard specific variable source resolvers
if (configuration.org) {
for (const [sourceName, sourceConfig] of Object.entries(
serverless.pluginManager.dashboardPlugin.configurationVariablesSources
)) {
if (sourceName === 'param') continue;
resolverConfiguration.sources[sourceName] = sourceConfig;
resolverConfiguration.fulfilledSources.add(sourceName);
}
}

// Register AWS provider specific variable sources
if (providerName === 'aws') {
// Pre-resolve to eventually pick not yet resolved AWS auth related properties
await resolveVariables(resolverConfiguration);
if (!variablesMeta.size) return;
if (
eventuallyReportVariableResolutionErrors(
configurationPath,
configuration,
variablesMeta
)
) {
return;
}

// Ensure properties which are crucial to some variable source resolvers
// are actually resolved.
if (
!ensureResolvedProperty('provider\0credentials') ||
!ensureResolvedProperty('provider\0deploymentBucket\0serverSideEncryption') ||
!ensureResolvedProperty('provider\0profile') ||
!ensureResolvedProperty('provider\0region')
) {
return;
}
Object.assign(resolverConfiguration.sources, {
cf: require('../lib/configuration/variables/sources/instance-dependent/get-cf')(
serverless
Expand All @@ -604,23 +632,7 @@ processSpanPromise = (async () => {
serverless
),
});
resolverConfiguration.fulfilledSources.add('cf').add('s3').add('ssm');
}

// Register dashboard specific variable source resolvers
if (serverless.pluginManager.dashboardPlugin) {
if (configuration.org) {
for (const [sourceName, sourceConfig] of Object.entries(
serverless.pluginManager.dashboardPlugin.configurationVariablesSources
)) {
resolverConfiguration.sources[sourceName] = sourceConfig;
resolverConfiguration.fulfilledSources.add(sourceName);
}
} else {
resolverConfiguration.sources.param =
serverless.pluginManager.dashboardPlugin.configurationVariablesSources.param;
resolverConfiguration.fulfilledSources.add('param');
}
resolverConfiguration.fulfilledSources.add('cf').add('s3').add('ssm').add('aws');
}

// Register variable source resolvers provided by external plugins
Expand Down Expand Up @@ -682,9 +694,7 @@ processSpanPromise = (async () => {
}
} catch (error) {
// If Dashboard Plugin, capture error
const dashboardPlugin =
serverless.pluginManager.dashboardPlugin ||
serverless.pluginManager.plugins.find((p) => p.enterprise);
const dashboardPlugin = serverless.pluginManager.dashboardPlugin;
const dashboardErrorHandler = _.get(dashboardPlugin, 'enterprise.errorHandler');
if (!dashboardErrorHandler) throw error;
try {
Expand Down