Skip to content

Commit

Permalink
feat: Error instead of warning when missing "commands" or "options"
Browse files Browse the repository at this point in the history
BREAKING CHANGE: When creating `Serverless` class instance programatically,
both "options" and "commands" have to be passed via "config" to the constructor.
  • Loading branch information
pgrzesik authored and medikoo committed Jan 27, 2022
1 parent 99941f0 commit f86f691
Show file tree
Hide file tree
Showing 94 changed files with 110 additions and 119 deletions.
33 changes: 9 additions & 24 deletions lib/Serverless.js
Expand Up @@ -93,17 +93,12 @@ class Serverless {
this._shouldResolveConfigurationInternally = true;
this._shouldReportMissingServiceDeprecation = true;
}
const commands = ensureArray(configObject.commands, { isOptional: true });
let options = ensurePlainObject(configObject.options, { isOptional: true });
const commands = ensureArray(configObject.commands);
let options = ensurePlainObject(configObject.options);
// This is a temporary workaround to ensure that original `options` are not mutated
// Should be removed after addressing: https://github.com/serverless/serverless/issues/2582
if (options) options = { ...options };
if (!commands || !options) {
this._shouldReportCommandsDeprecation = true;
this.processedInput = resolveCliInput();
} else {
this.processedInput = { commands, options };
}
this.processedInput = { commands, options };
this.hasResolvedCommandsExternally = Boolean(configObject.hasResolvedCommandsExternally);
this.isTelemetryReportedExternally = Boolean(configObject.isTelemetryReportedExternally);

Expand Down Expand Up @@ -157,22 +152,12 @@ class Serverless {
if (this._isInvokedByGlobalInstallation) {
logDeprecation.defaultMode = 'warn';
logDeprecation.flushBuffered();
} else {
if (this._shouldReportMissingServiceDeprecation) {
this._logDeprecation(
'MISSING_SERVICE_CONFIGURATION',
'Serverless constructor expects service configuration details to be provided.\n' +
'Starting from next major Serverless will no longer auto resolve it internally.'
);
}
if (this._shouldReportCommandsDeprecation) {
this._logDeprecation(
'MISSING_COMMANDS_OR_OPTIONS_AT_CONSTRUCTION',
'Serverless constructor expects resolved CLI commands and options to be provided ' +
'via "config.commands" and "config.options".\n' +
'Starting from next major Serverless will no longer auto resolve CLI arguments internally.'
);
}
} else if (this._shouldReportMissingServiceDeprecation) {
this._logDeprecation(
'MISSING_SERVICE_CONFIGURATION',
'Serverless constructor expects service configuration details to be provided.\n' +
'Starting from next major Serverless will no longer auto resolve it internally.'
);
}
if (this._shouldResolveConfigurationInternally) {
const configurationPath = await resolveConfigurationPath();
Expand Down
2 changes: 1 addition & 1 deletion test/unit/lib/Serverless.test.js
Expand Up @@ -33,7 +33,7 @@ describe('Serverless', () => {

describe('#constructor()', () => {
it('should set the correct config if a config object is passed', () => {
const configObj = { some: 'config' };
const configObj = { some: 'config', commands: [], options: {} };
const serverlessWithConfig = new Serverless(configObj);

expect(serverlessWithConfig.config.some).to.equal('config');
Expand Down
2 changes: 1 addition & 1 deletion test/unit/lib/classes/CLI.test.js
Expand Up @@ -15,7 +15,7 @@ describe('CLI', () => {
let serverless;

beforeEach(() => {
serverless = new Serverless({});
serverless = new Serverless({ commands: [], options: {} });
});

describe('#constructor()', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/lib/classes/Config.test.js
Expand Up @@ -4,7 +4,7 @@ const expect = require('chai').expect;
const Config = require('../../../../lib/classes/Config');
const Serverless = require('../../../../lib/Serverless');

const serverless = new Serverless();
const serverless = new Serverless({ commands: [], options: {} });

describe('Config', () => {
describe('#constructor()', () => {
Expand Down
4 changes: 2 additions & 2 deletions test/unit/lib/classes/PluginManager.test.js
Expand Up @@ -422,7 +422,7 @@ describe('PluginManager', () => {

beforeEach(() => {
({ restoreEnv } = overrideEnv({ whitelist: ['APPDATA', 'PATH'] }));
serverless = new Serverless();
serverless = new Serverless({ commands: [], options: {} });
serverless.cli = new CLI();
serverless.processedInput = { commands: ['print'], options: {} };
pluginManager = new PluginManager(serverless);
Expand Down Expand Up @@ -1257,7 +1257,7 @@ describe('PluginManager', () => {
let pluginManagerInstance;

beforeEach(() => {
serverlessInstance = new Serverless();
serverlessInstance = new Serverless({ commands: [], options: {} });
serverlessInstance.configurationInput = null;
serverlessInstance.serviceDir = 'my-service';
pluginManagerInstance = new PluginManager(serverlessInstance);
Expand Down
2 changes: 1 addition & 1 deletion test/unit/lib/classes/Utils.test.js
Expand Up @@ -16,7 +16,7 @@ describe('Utils', () => {
let serverless;

beforeEach(() => {
serverless = new Serverless();
serverless = new Serverless({ commands: [], options: {} });
utils = new Utils(serverless);
});

Expand Down
2 changes: 1 addition & 1 deletion test/unit/lib/classes/YamlParser.test.js
Expand Up @@ -14,7 +14,7 @@ const { getTmpFilePath, getTmpDirPath } = require('../../../utils/fs');
chai.use(require('chai-as-promised'));
const expect = require('chai').expect;

const serverless = new Serverless();
const serverless = new Serverless({ commands: [], options: {} });

describe('YamlParser', () => {
describe('#parse()', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/lib/plugins/aws/common/index.test.js
Expand Up @@ -9,7 +9,7 @@ const sinon = require('sinon');
describe('AwsCommon', () => {
let awsCommon;
beforeEach(() => {
const serverless = new Serverless();
const serverless = new Serverless({ commands: [], options: {} });
const options = {
stage: 'dev',
region: 'us-east-1',
Expand Down
4 changes: 2 additions & 2 deletions test/unit/lib/plugins/aws/common/lib/artifacts.test.js
Expand Up @@ -14,7 +14,7 @@ describe('#moveArtifactsToPackage()', () => {
const moveServerlessPath = path.join(moveBasePath, '.serverless');

beforeEach(() => {
serverless = new Serverless();
serverless = new Serverless({ commands: [], options: {} });
awsCommon = new AWSCommon(serverless, {});

serverless.serviceDir = moveBasePath;
Expand Down Expand Up @@ -104,7 +104,7 @@ describe('#moveArtifactsToTemp()', () => {
const moveTargetPath = path.join(moveBasePath, 'target');

beforeEach(() => {
serverless = new Serverless();
serverless = new Serverless({ commands: [], options: {} });
awsCommon = new AWSCommon(serverless, {});

serverless.serviceDir = moveBasePath;
Expand Down
Expand Up @@ -11,7 +11,7 @@ describe('#cleanupTempDir()', () => {
let packageService;

beforeEach(() => {
serverless = new Serverless();
serverless = new Serverless({ commands: [], options: {} });
packageService = new Package(serverless);

serverless.serviceDir = getTmpDirPath();
Expand Down
2 changes: 1 addition & 1 deletion test/unit/lib/plugins/aws/customResources/index.test.js
Expand Up @@ -36,7 +36,7 @@ describe('#addCustomResourceToService()', () => {
region: 'us-east-1',
};
tmpDirPath = createTmpDir();
serverless = new Serverless();
serverless = new Serverless({ commands: [], options: {} });
serverless.cli = new CLI();
serverless.pluginManager.cliOptions = options;
provider = new AwsProvider(serverless, options);
Expand Down
2 changes: 1 addition & 1 deletion test/unit/lib/plugins/aws/deploy/index.test.js
Expand Up @@ -22,7 +22,7 @@ describe('AwsDeploy', () => {
let options;

beforeEach(() => {
serverless = new Serverless();
serverless = new Serverless({ commands: [], options: {} });
options = {
stage: 'dev',
region: 'us-east-1',
Expand Down
Expand Up @@ -33,7 +33,7 @@ describe('checkForChanges', () => {
stage: 'dev',
region: 'us-east-1',
};
serverless = new Serverless();
serverless = new Serverless({ commands: [], options: {} });
serverless.serviceDir = 'my-service';
provider = new AwsProvider(serverless, options);
serverless.setProvider('aws', provider);
Expand Down
Expand Up @@ -24,7 +24,7 @@ describe('cleanupS3Bucket', () => {
stage: 'dev',
region: 'us-east-1',
};
serverless = new Serverless();
serverless = new Serverless({ commands: [], options: {} });
serverless.serviceDir = 'foo';
provider = new AwsProvider(serverless, options);
serverless.setProvider('aws', provider);
Expand Down
2 changes: 1 addition & 1 deletion test/unit/lib/plugins/aws/deploy/lib/createStack.test.js
Expand Up @@ -30,7 +30,7 @@ describe('createStack', () => {
};

beforeEach(() => {
const serverless = new Serverless();
const serverless = new Serverless({ commands: [], options: {} });
serverless.setProvider('aws', new AwsProvider(serverless, {}));
serverless.utils.writeFileSync(serverlessYmlPath, serverlessYml);
serverless.serviceDir = tmpDirPath;
Expand Down
Expand Up @@ -40,7 +40,7 @@ describe('extendedValidate', () => {
stage: 'dev',
region: 'us-east-1',
};
const serverless = new Serverless();
const serverless = new Serverless({ commands: [], options: {} });
serverless.setProvider('aws', new AwsProvider(serverless, options));
serverless.utils.writeFileSync(serverlessYmlPath, serverlessYml);
serverless.serviceDir = tmpDirPath;
Expand Down
Expand Up @@ -25,7 +25,7 @@ describe('uploadArtifacts', () => {
let cryptoStub;

beforeEach(() => {
serverless = new Serverless();
serverless = new Serverless({ commands: [], options: {} });
serverless.serviceDir = 'foo';
serverless.setProvider('aws', new AwsProvider(serverless, {}));
const options = {
Expand Down
Expand Up @@ -23,7 +23,7 @@ describe('validateTemplate', () => {
stage: 'dev',
region: 'us-east-1',
};
serverless = new Serverless();
serverless = new Serverless({ commands: [], options: {} });
serverless.serviceDir = 'foo';
serverless.setProvider('aws', new AwsProvider(serverless, options));
awsDeploy = new AwsDeploy(serverless, options);
Expand Down
2 changes: 1 addition & 1 deletion test/unit/lib/plugins/aws/deployList.test.js
Expand Up @@ -17,7 +17,7 @@ describe('AwsDeployList', () => {
stage: 'dev',
region: 'us-east-1',
};
serverless = new Serverless();
serverless = new Serverless({ commands: [], options: {} });
provider = new AwsProvider(serverless, options);
serverless.setProvider('aws', provider);
serverless.service.service = 'listDeployments';
Expand Down
2 changes: 1 addition & 1 deletion test/unit/lib/plugins/aws/info/getApiKeyValues.test.js
Expand Up @@ -16,7 +16,7 @@ describe('#getApiKeyValues()', () => {
stage: 'dev',
region: 'us-east-1',
};
serverless = new Serverless();
serverless = new Serverless({ commands: [], options: {} });
serverless.setProvider('aws', new AwsProvider(serverless, options));
serverless.service.service = 'my-service';
awsInfo = new AwsInfo(serverless, options);
Expand Down
2 changes: 1 addition & 1 deletion test/unit/lib/plugins/aws/info/getResourceCount.test.js
Expand Up @@ -18,7 +18,7 @@ describe('#getResourceCount()', () => {
stage: 'dev',
region: 'us-east-1',
};
serverless = new Serverless();
serverless = new Serverless({ commands: [], options: {} });
serverless.setProvider('aws', new AwsProvider(serverless, options));
serverless.service.service = 'my-service';
serverless.service.functions = {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/lib/plugins/aws/info/getStackInfo.test.js
Expand Up @@ -16,7 +16,7 @@ describe('#getStackInfo()', () => {
stage: 'dev',
region: 'us-east-1',
};
serverless = new Serverless();
serverless = new Serverless({ commands: [], options: {} });
serverless.setProvider('aws', new AwsProvider(serverless, options));
serverless.service.service = 'my-service';
serverless.service.functions = {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/lib/plugins/aws/info/index.test.js
Expand Up @@ -25,7 +25,7 @@ describe('AwsInfo', () => {
stage: 'dev',
region: 'us-east-1',
};
serverless = new Serverless();
serverless = new Serverless({ commands: [], options: {} });
serverless.setProvider('aws', new AwsProvider(serverless, options));
serverless.cli = {
log: sinon.stub().returns(),
Expand Down
2 changes: 1 addition & 1 deletion test/unit/lib/plugins/aws/invokeLocal/index.test.js
Expand Up @@ -64,7 +64,7 @@ describe('AwsInvokeLocal', () => {
'get-stdin': stdinStub,
'child-process-ext/spawn': spawnExtStub,
});
serverless = new Serverless();
serverless = new Serverless({ commands: [], options: {} });
serverless.serviceDir = 'servicePath';
serverless.cli = new CLI(serverless);
serverless.processedInput = { commands: ['invoke'] };
Expand Down
2 changes: 1 addition & 1 deletion test/unit/lib/plugins/aws/lib/getServiceState.test.js
Expand Up @@ -17,7 +17,7 @@ describe('#getServiceState()', () => {
const awsPlugin = {};

beforeEach(() => {
serverless = new Serverless();
serverless = new Serverless({ commands: [], options: {} });
serverless.serviceDir = 'my-service';
awsPlugin.serverless = serverless;
awsPlugin.provider = new AwsProvider(serverless, options);
Expand Down
2 changes: 1 addition & 1 deletion test/unit/lib/plugins/aws/lib/monitorStack.test.js
Expand Up @@ -12,7 +12,7 @@ chai.use(require('chai-as-promised'));
const { expect } = chai;

describe('monitorStack', () => {
const serverless = new Serverless();
const serverless = new Serverless({ commands: [], options: {} });
const awsPlugin = {};

beforeEach(() => {
Expand Down
2 changes: 2 additions & 0 deletions test/unit/lib/plugins/aws/lib/naming.test.js
Expand Up @@ -15,6 +15,8 @@ describe('#naming()', () => {
options = {
stage: 'dev',
region: 'us-east-1',
commands: [],
options: {},
};
serverless = new Serverless(options);
sdk = new SDK(serverless, options);
Expand Down
2 changes: 1 addition & 1 deletion test/unit/lib/plugins/aws/lib/setBucketName.test.js
Expand Up @@ -12,7 +12,7 @@ describe('#setBucketName()', () => {
let getServerlessDeploymentBucketNameStub;

beforeEach(() => {
serverless = new Serverless();
serverless = new Serverless({ commands: [], options: {} });
serverless.serviceDir = 'foo';
const options = {
stage: 'dev',
Expand Down
2 changes: 1 addition & 1 deletion test/unit/lib/plugins/aws/lib/updateStack.test.js
Expand Up @@ -17,7 +17,7 @@ describe('updateStack', () => {
stage: 'dev',
region: 'us-east-1',
};
serverless = new Serverless();
serverless = new Serverless({ commands: [], options: {} });
serverless.serviceDir = 'foo';
serverless.setProvider('aws', new AwsProvider(serverless, options));
awsDeploy = new AwsDeploy(serverless, options);
Expand Down
2 changes: 1 addition & 1 deletion test/unit/lib/plugins/aws/lib/validate.test.js
Expand Up @@ -11,7 +11,7 @@ chai.use(require('chai-as-promised'));
const expect = chai.expect;

describe('#validate', () => {
const serverless = new Serverless();
const serverless = new Serverless({ commands: [], options: {} });
let provider;
const awsPlugin = {};

Expand Down
4 changes: 2 additions & 2 deletions test/unit/lib/plugins/aws/logs.test.js
Expand Up @@ -21,7 +21,7 @@ describe('AwsLogs', () => {
region: 'us-east-1',
function: 'first',
};
serverless = new Serverless();
serverless = new Serverless({ commands: [], options: {} });
const provider = new AwsProvider(serverless, options);
provider.cachedCredentials = {
credentials: { accessKeyId: 'foo', secretAccessKey: 'bar' },
Expand Down Expand Up @@ -335,7 +335,7 @@ describe('AwsLogs', () => {
region: 'us-east-1',
function: 'first',
};
serverless = new Serverless();
serverless = new Serverless({ commands: [], options: {} });
const provider = new AwsProvider(serverless, options);
provider.cachedCredentials = {
credentials: { accessKeyId: 'foo', secretAccessKey: 'bar' },
Expand Down
2 changes: 1 addition & 1 deletion test/unit/lib/plugins/aws/metrics.test.js
Expand Up @@ -17,7 +17,7 @@ describe('AwsMetrics', () => {
let serverless;

beforeEach(() => {
serverless = new Serverless();
serverless = new Serverless({ commands: [], options: {} });
serverless.cli = new CLI(serverless);
const options = {
stage: 'dev',
Expand Down
Expand Up @@ -9,7 +9,7 @@ describe('#compileListenerRules()', () => {
let awsCompileAlbEvents;

beforeEach(() => {
const serverless = new Serverless();
const serverless = new Serverless({ commands: [], options: {} });
serverless.setProvider('aws', new AwsProvider(serverless));
serverless.service.service = 'some-service';
serverless.service.provider.compiledCloudFormationTemplate = { Resources: {} };
Expand Down
Expand Up @@ -9,7 +9,7 @@ describe('#compilePermissions()', () => {
let awsCompileAlbEvents;

beforeEach(() => {
const serverless = new Serverless();
const serverless = new Serverless({ commands: [], options: {} });
serverless.setProvider('aws', new AwsProvider(serverless));
serverless.service.service = 'some-service';
serverless.service.provider.compiledCloudFormationTemplate = { Resources: {} };
Expand Down
Expand Up @@ -9,7 +9,7 @@ describe('#compileTargetGroups()', () => {
let awsCompileAlbEvents;

beforeEach(() => {
const serverless = new Serverless();
const serverless = new Serverless({ commands: [], options: {} });
serverless.setProvider('aws', new AwsProvider(serverless));
serverless.service.service = 'some-service';
serverless.service.provider.compiledCloudFormationTemplate = { Resources: {} };
Expand Down
Expand Up @@ -9,7 +9,7 @@ describe('#validate()', () => {
let awsCompileAlbEvents;

beforeEach(() => {
const serverless = new Serverless();
const serverless = new Serverless({ commands: [], options: {} });
serverless.setProvider('aws', new AwsProvider(serverless));
serverless.service.service = 'some-service';
serverless.service.provider.compiledCloudFormationTemplate = { Resources: {} };
Expand Down

0 comments on commit f86f691

Please sign in to comment.