diff --git a/package.json b/package.json index 2767a933..0fdb2197 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,9 @@ { "name": "@serverless/enterprise-plugin", "version": "0.5.0", + "engines": { + "node": ">=6.0" + }, "description": "The Serverless Enterprise plugin", "main": "dist/index.js", "scripts": { diff --git a/src/lib/plugin.js b/src/lib/plugin.js index 38c669e1..277ef38b 100644 --- a/src/lib/plugin.js +++ b/src/lib/plugin.js @@ -25,46 +25,70 @@ class ServerlessEnterprisePlugin { const user = getLoggedInUser() const currentCommand = sls.processedInput.commands[0] - // Skip everything if user is not logged in and not trying to log in or out... - if ( - !user && - (currentCommand !== 'login' && - currentCommand !== 'logout' && - !process.env.SERVERLESS_ACCESS_KEY) - ) { - const errorMessage = `You are not currently logged in. To log in, use: $ serverless login` - console.log('') // eslint-disable-line - sls.cli.log(errorMessage, 'Serverless Enterprise') // eslint-disable-line - throw new Error(errorMessage) // eslint-disable-line + // default hook, only applies if user isn't using SFE. gets overridden if they are + this.hooks = { + 'after:aws:deploy:finalize:cleanup': () => + sls.cli.log( + 'Run `serverless login` and deploy again to explore, monitor, secure your serverless project for free.', + 'Serverless Enterprise' + ) } - // Defaults - this.sls = sls - this.state = {} // Useful for storing data across hooks - this.state.secretsUsed = new Set() - this.provider = this.sls.getProvider('aws') - // Check if Enterprise is configured const missing = [] - if (!this.sls.service.tenant) { + if (!sls.service.tenant) { missing.push('tenant') } - if (!this.sls.service.app) { + if (!sls.service.app) { missing.push('app') } - if (!this.sls.service.service) { + if (!sls.service.service) { missing.push('service') } - if (missing.length > 0) { - this.sls.cli.log( + + // Skip everything if user is not logged in and not trying to log in or out... + if ( + !user && + (currentCommand !== 'login' && + currentCommand !== 'logout' && + !process.env.SERVERLESS_ACCESS_KEY) + ) { + if (missing.includes('tenant') && missing.includes('app')) { + return // user isn't trying to use SFE + } + const errorMessage = `You are not currently logged in. To log in, use: $ serverless login` + console.log('') // eslint-disable-line + sls.cli.log(errorMessage, 'Serverless Enterprise') // eslint-disable-line + throw new Error(errorMessage) // eslint-disable-line + } + if (currentCommand !== 'login' && currentCommand !== 'logout' && missing.length > 0) { + sls.cli.log( `Warning: The Enterprise Plugin requires a ${missing .map((opt) => `"${opt}"`) .join(', ')} property in your "serverless.yml" and will not work without it.`, 'Serverless Enterprise' ) + // replace the default hook with a message about configuring sls enterprise + this.hooks = { + 'after:aws:deploy:finalize:cleanup': () => + sls.cli.log( + `Update your "serverless.yml" with ${missing + .map((opt) => `"${opt}"`) + .join( + ', ' + )} properties and deploy again to explore, monitor, secure your serverless project for free.`, + 'Serverless Enterprise' + ) + } return } + // Defaults + this.sls = sls + this.state = {} // Useful for storing data across hooks + this.state.secretsUsed = new Set() + this.provider = this.sls.getProvider('aws') + // Add commands this.commands = { login: { diff --git a/src/lib/plugin.test.js b/src/lib/plugin.test.js index 10d5d02f..dc70930c 100644 --- a/src/lib/plugin.test.js +++ b/src/lib/plugin.test.js @@ -10,6 +10,7 @@ import { saveDeployment } from './deployment' import { hookIntoVariableGetter } from './variables' import { generate } from './generateEvent' import injectLogsIamRole from './injectLogsIamRole' +import _ from 'lodash' afterAll(() => jest.restoreAllMocks()) @@ -97,7 +98,7 @@ describe('plugin', () => { }) it('construct requires tenant', () => { - const slsClone = Object.assign({}, sls) + const slsClone = _.cloneDeep(sls) delete slsClone.service.tenant const instance = new ServerlessEnterprisePlugin(slsClone) // eslint-disable-line expect(slsClone.getProvider).toBeCalledWith('aws')