Skip to content

Commit

Permalink
refactor(CLI): Replace warnings with modern counterparts
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrzesik committed Oct 15, 2021
1 parent 7d19ca8 commit 4da0899
Show file tree
Hide file tree
Showing 18 changed files with 167 additions and 288 deletions.
48 changes: 36 additions & 12 deletions lib/classes/ConfigSchemaHandler/index.js
Expand Up @@ -6,6 +6,7 @@ const ensurePlainObject = require('type/plain-object/ensure');
const schema = require('../../configSchema');
const ServerlessError = require('../../serverless-error');
const normalizeAjvErrors = require('./normalizeAjvErrors');
const { legacy, log, style } = require('@serverless/utils/log');

const FUNCTION_NAME_PATTERN = '^[a-zA-Z0-9-_]+$';
const ERROR_PREFIX = 'Configuration error';
Expand Down Expand Up @@ -79,30 +80,41 @@ class ConfigSchemaHandler {
if (!this.schema.properties.provider.properties.name) {
configurationValidationResults.set(this.serverless.configurationInput, false);
if (this.serverless.service.configValidationMode !== 'off') {
this.serverless.cli.log(
legacy.log(
`${WARNING_PREFIX}: Unrecognized provider '${this.serverless.service.provider.name}'`,
'Serverless',
{ color: 'orange' }
);
this.serverless.cli.log(' ');
this.serverless.cli.log(
legacy.log(' ');
legacy.log(
"You're relying on provider plugin which doesn't " +
'provide a validation schema for its config.',
'Serverless',
{ color: 'orange' }
);
this.serverless.cli.log(
legacy.log(
'Please report the issue at its bug tracker linking: ' +
'https://www.serverless.com/framework/docs/providers/aws/guide/plugins#extending-validation-schema',
'Serverless',
{ color: 'orange' }
);
this.serverless.cli.log(
legacy.log(
'You may turn off this message with "configValidationMode: off" setting',
'Serverless',
{ color: 'orange' }
);
this.serverless.cli.log(' ');
legacy.log(' ');

log.warning(
[
`You're relying on provider "${this.serverless.service.provider.name}" defined by a plugin which doesn't provide a validation schema for its config.`,
`Please report the issue at its bug tracker linking: ${style.link(
'https://www.serverless.com/framework/docs/providers/aws/guide/plugins#extending-validation-schema'
)}`,
'You may turn off this message with "configValidationMode: off" setting',
'',
].join('\n')
);
}

this.relaxProviderSchema();
Expand Down Expand Up @@ -139,24 +151,36 @@ class ConfigSchemaHandler {
);
} else {
if (messages.length === 1) {
this.serverless.cli.log(`${WARNING_PREFIX} ${messages[0]}`, 'Serverless', {
legacy.log(`${WARNING_PREFIX} ${messages[0]}`, 'Serverless', {
color: 'orange',
});
} else {
this.serverless.cli.log(`${WARNING_PREFIX}:`, 'Serverless', {
legacy.log(`${WARNING_PREFIX}:`, 'Serverless', {
color: 'orange',
});
for (const message of messages) {
this.serverless.cli.log(` ${message}`, 'Serverless', { color: 'orange' });
legacy.log(` ${message}`, 'Serverless', { color: 'orange' });
}
}
this.serverless.cli.log(' ');
this.serverless.cli.log(
legacy.log(' ');
legacy.log(
'Learn more about configuration validation here: http://slss.io/configuration-validation',
'Serverless',
{ color: 'orange' }
);
this.serverless.cli.log(' ');
legacy.log(' ');

log.notice();
log.warning(
[
'Invalid configuration encountered',
...messages.map((message) => ` ${message}`),
'',
`Learn more about configuration validation here: ${style.link(
'http://slss.io/configuration-validation'
)}`,
].join('\n')
);
}
}
}
Expand Down
26 changes: 20 additions & 6 deletions lib/classes/PluginManager.js
Expand Up @@ -133,7 +133,10 @@ class PluginManager {

// don't load plugins twice
if (this.plugins.some((plugin) => plugin instanceof Plugin)) {
this.serverless.cli.log(`WARNING: duplicate plugin ${Plugin.name} was not loaded\n`);
legacy.log(`WARNING: duplicate plugin ${Plugin.name} was not loaded\n`);
log.warning(
`Duplicate plugin definition found in your configuration. Plugin "${Plugin.name}" will not be loaded more than once.`
);
return null;
}

Expand Down Expand Up @@ -300,8 +303,9 @@ class PluginManager {
// Handle command aliases
(details.aliases || []).forEach((alias) => {
if (process.env.SLS_DEBUG) {
this.serverless.cli.log(` -> @${alias}`);
legacy.log(` -> @${alias}`);
}
log.get('lifecycle:command:register').debug(` -> @${alias}`);
this.createCommandAlias(alias, key);
});
return Object.assign({}, details, { key, pluginName, commands });
Expand Down Expand Up @@ -342,9 +346,12 @@ class PluginManager {
if (this.deprecatedEvents[baseEvent]) {
const redirectedEvent = this.deprecatedEvents[baseEvent];
if (process.env.SLS_DEBUG) {
this.serverless.cli.log(`WARNING: Plugin ${pluginName} uses deprecated hook ${event},
legacy.log(`WARNING: Plugin ${pluginName} uses deprecated hook ${event},
use ${redirectedEvent} hook instead`);
}
log.info(
`Plugin "${pluginName}" uses deprecated hook "${event}". Use "${redirectedEvent}" hook instead.`
);
if (redirectedEvent) {
target = event.replace(baseEvent, redirectedEvent);
}
Expand All @@ -365,9 +372,10 @@ class PluginManager {
)) {
let options = resolverOrOptions;
if (!options) {
this.serverless.cli.log(
legacy.log(
`Warning! Ignoring falsy variableResolver for ${variablePrefix} in ${pluginName}.`
);
log.warning(`Ignoring falsy variableResolver for ${variablePrefix} in ${pluginName}.`);
continue;
}
if (typeof resolverOrOptions === 'function') {
Expand Down Expand Up @@ -606,8 +614,9 @@ class PluginManager {
} catch (error) {
if (error instanceof TerminateHookChain) {
if (process.env.SLS_DEBUG) {
this.serverless.cli.log(`Terminate ${commandsArray.join(':')}`);
legacy.log(`Terminate ${commandsArray.join(':')}`);
}
log.debug(`Terminate ${commandsArray.join(':')}`);
return;
}
throw error;
Expand Down Expand Up @@ -666,11 +675,16 @@ class PluginManager {
for (const { hook } of this.hooks.error || []) await hook(commandException);
} catch (errorHookException) {
const errorHookExceptionMeta = tokenizeException(errorHookException);
this.serverless.cli.log(
legacy.log(
`Warning: "error" hook crashed with: ${
errorHookExceptionMeta.stack || errorHookExceptionMeta.message
}`
);
log.warning(
`The "error" hook crashed with:\n${
errorHookExceptionMeta.stack || errorHookExceptionMeta.message
}`
);
} finally {
await deferredBackendNotificationRequest;
throw commandException; // eslint-disable-line no-unsafe-finally
Expand Down
2 changes: 1 addition & 1 deletion lib/cli/commands-schema/resolve-final.js
Expand Up @@ -131,7 +131,7 @@ module.exports = (loadedPlugins, { providerName }) => {
([plugin, optionNames]) =>
`${plugin.constructor.name} for "${Array.from(optionNames).join('", "')}"`
).join('\n - ')}\n\n` +
'Please report this issue in issue tracker of the corresponding plugin.'
'Please report this issue in issue tracker of the corresponding plugin.\n'
);
}

Expand Down
12 changes: 6 additions & 6 deletions lib/plugins/aws/deploy/lib/checkForChanges.js
Expand Up @@ -8,7 +8,7 @@ const BbPromise = require('bluebird');
const _ = require('lodash');
const normalizeFiles = require('../../lib/normalizeFiles');
const ServerlessError = require('../../../../serverless-error');
const { legacy } = require('@serverless/utils/log');
const { legacy, log } = require('@serverless/utils/log');

module.exports = {
async checkForChanges() {
Expand Down Expand Up @@ -108,14 +108,14 @@ module.exports = {

return BbPromise.all(getFunctionResults).then((results) => {
if (couldNotAccessFunction) {
this.serverless.cli.log(
[
'WARNING: Not authorized to perform: lambda:GetFunction for at least one of the lambda functions.',
' Deployment will not be skipped even if service files did not change. ',
].join(''),
legacy.log(
'WARNING: Not authorized to perform: lambda:GetFunction for at least one of the lambda functions. Deployment will not be skipped even if service files did not change.',
'Serverless',
{ color: 'orange' }
);
log.warning(
'Not authorized to perform: lambda:GetFunction for at least one of the lambda functions. Deployment will not be skipped even if service files did not change.'
);
}

return results.reduce((currentMin, date) => {
Expand Down
23 changes: 15 additions & 8 deletions lib/plugins/aws/deploy/lib/extendedValidate.js
Expand Up @@ -4,7 +4,7 @@ const path = require('path');
const _ = require('lodash');
const findReferences = require('../../utils/findReferences');
const ServerlessError = require('../../../../serverless-error');
const { legacy } = require('@serverless/utils/log');
const { legacy, log } = require('@serverless/utils/log');

module.exports = {
extendedValidate() {
Expand Down Expand Up @@ -46,13 +46,20 @@ module.exports = {
if (functionObject.timeout > 30 && functionObject.events) {
functionObject.events.forEach((event) => {
if (Object.keys(event)[0] === 'http' && !event.http.async) {
const warnMessage = [
`WARNING: Function ${functionName} has timeout of ${functionObject.timeout} `,
"seconds, however, it's attached to API Gateway so it's automatically ",
'limited to 30 seconds.',
].join('');

legacy.log(warnMessage);
legacy.log(
[
`WARNING: Function ${functionName} has timeout of ${functionObject.timeout} `,
"seconds, however, it's attached to API Gateway so it's automatically ",
'limited to 30 seconds.',
].join('')
);
log.warning(
[
`Function ${functionName} has timeout of ${functionObject.timeout} `,
"seconds, however, it's attached to API Gateway so it's automatically ",
'limited to 30 seconds.',
].join('')
);
}
});
}
Expand Down
5 changes: 4 additions & 1 deletion lib/plugins/aws/deployFunction.js
Expand Up @@ -179,7 +179,10 @@ class AwsDeployFunction {
'DEPLOY_FUNCTION_CONFIGURATION_UPDATE_TIMED_OUT'
);
}
this.serverless.cli.log(
legacy.log(
`Retrying configuration update for function: ${this.options.function}. Reason: ${err.message}`
);
log.info(
`Retrying configuration update for function: ${this.options.function}. Reason: ${err.message}`
);
await wait(1000);
Expand Down
11 changes: 5 additions & 6 deletions lib/plugins/aws/invokeLocal/index.js
Expand Up @@ -643,12 +643,11 @@ class AwsInvokeLocal {
{ shell: true }
);

// TODO: CONSIDER IT AS A WARNING
this.serverless.cli.log(
[
'In order to get human-readable output,',
' please implement "toString()" method of your "ApiGatewayResponse" object.',
].join('')
legacy.log(
'In order to get human-readable output, please implement "toString()" method of your "ApiGatewayResponse" object.'
);
log.warning(
'In order to get human-readable output, please implement "toString()" method of your "ApiGatewayResponse" object.'
);

java.stdout.on('data', (buf) => {
Expand Down
9 changes: 7 additions & 2 deletions lib/plugins/aws/lib/checkIfEcrRepositoryExists.js
@@ -1,5 +1,7 @@
'use strict';

const { legacy, log } = require('@serverless/utils/log');

module.exports = {
async checkIfEcrRepositoryExists() {
const registryId = await this.provider.getAccountId();
Expand All @@ -16,11 +18,14 @@ module.exports = {
}
if (err.providerError && err.providerError.code === 'AccessDeniedException') {
if (this.serverless.service.provider.ecr && this.serverless.service.provider.ecr.images) {
this.serverless.cli.log(
'WARNING: Could not access ECR repository due to denied access, but there are images defined in `provider.ecr`. ECR repository removal will be skipped.',
legacy.log(
'WARNING: Could not access ECR repository due to denied access, but there are images defined in "provider.ecr". ECR repository removal will be skipped.',
'Serverless',
{ color: 'orange' }
);
log.warning(
'Could not access ECR repository due to denied access, but there are images defined in "provider.ecr". ECR repository removal will be skipped.'
);
}
// Check if user has images defined and issue warning that we could not
return false;
Expand Down
26 changes: 18 additions & 8 deletions lib/plugins/aws/package/compile/events/apiGateway/lib/validate.js
Expand Up @@ -4,6 +4,7 @@ const _ = require('lodash');
const awsArnRegExs = require('../../../../../utils/arnRegularExpressions');
const resolveLambdaTarget = require('../../../../../utils/resolveLambdaTarget');
const ServerlessError = require('../../../../../../../serverless-error');
const { legacy, log } = require('@serverless/utils/log');

const NOT_FOUND = -1;
const DEFAULT_STATUS_CODES = {
Expand Down Expand Up @@ -116,14 +117,15 @@ module.exports = {

if (_.difference(keys, allowedKeys).length) {
const requestWarningMessage = [
`Warning! You're using the ${http.integration} in combination with a request`,
`You're using the ${http.integration} in combination with a request`,
` configuration in your function "${functionName}". Only the `,
allowedKeys.map((value) => `request.${value}`).join(', '),
` configs are available in conjunction with ${http.integration}.`,
' Serverless will remove this configuration automatically',
' before deployment.',
].join('');
this.serverless.cli.log(requestWarningMessage);
legacy.log(`Warning! ${requestWarningMessage}`);
log.warning(requestWarningMessage);
for (const key of keys) {
if (!allowedKeys.includes(key)) {
delete http.request[key];
Expand All @@ -138,12 +140,20 @@ module.exports = {
}
}
if (http.response) {
const warningMessage = [
`Warning! You're using the ${http.integration} in combination with response`,
` configuration in your function "${functionName}".`,
' Serverless will remove this configuration automatically before deployment.',
].join('');
this.serverless.cli.log(warningMessage);
legacy.log(
[
`Warning! You're using the ${http.integration} in combination with response`,
` configuration in your function "${functionName}".`,
' Serverless will remove this configuration automatically before deployment.',
].join('')
);
log.warning(
[
`You're using the ${http.integration} in combination with response`,
` configuration in your function "${functionName}".`,
' Serverless will remove this configuration automatically before deployment.',
].join('')
);

delete http.response;
}
Expand Down

0 comments on commit 4da0899

Please sign in to comment.