Skip to content

Commit

Permalink
refactor(CLI Onboarding): Integrate steps from dashboard plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed May 12, 2021
1 parent a26a21f commit 105807a
Show file tree
Hide file tree
Showing 14 changed files with 97 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .github/prepare-canary.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));

packageJson.version = `${packageJson.version}-${process.env.GITHUB_SHA.slice(0, 8)}`;
packageJson.dependencies['@serverless/components'] = 'canary';
packageJson.dependencies['@serverless/enterprise-plugin'] = 'canary';
packageJson.dependencies['@serverless/dashboard-plugin'] = 'canary';

fs.writeFileSync(packageJsonPath, `${JSON.stringify(packageJson, null, 2)}\n`);
2 changes: 1 addition & 1 deletion lib/Serverless.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class Serverless {
this.isLocallyInstalled = false;
this.triggeredDeprecations = logDeprecation.triggeredDeprecations;

// TODO: Remove once "@serverless/enterprise-plugin" is integrated into this repository
// TODO: Remove once "@serverless/dashboard-plugin" is integrated into this repository
this._commandsSchema = commmandsSchema;
}

Expand Down
76 changes: 40 additions & 36 deletions lib/classes/PluginManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,48 +165,52 @@ class PluginManager {
resolveServicePlugins(servicePlugs) {
const pluginsObject = this.parsePluginsObject(servicePlugs);
const serviceDir = this.serverless.serviceDir;
return pluginsObject.modules
.filter((name) => name !== '@serverless/enterprise-plugin')
.map((name) => {
let Plugin;
try {
Plugin = requireServicePlugin(serviceDir, name, pluginsObject.localPath);
} catch (error) {
if (!isModuleNotFoundError(error, name)) throw error;

// Plugin not installed
if (
resolveCliInput().isHelpRequest ||
this.pluginIndependentCommands.has(this.cliCommands[0])
) {
// User may intend to install plugins just listed in serverless config
// Therefore skip on MODULE_NOT_FOUND case
return null;
}
return (
pluginsObject.modules
// Initially @serverless/enterprise-plugin was published as external plugin
// We ensure to not load it, if for some reason it's still in service configuration
.filter((name) => name !== '@serverless/enterprise-plugin')
.map((name) => {
let Plugin;
try {
Plugin = requireServicePlugin(serviceDir, name, pluginsObject.localPath);
} catch (error) {
if (!isModuleNotFoundError(error, name)) throw error;

// Plugin not installed
if (
resolveCliInput().isHelpRequest ||
this.pluginIndependentCommands.has(this.cliCommands[0])
) {
// User may intend to install plugins just listed in serverless config
// Therefore skip on MODULE_NOT_FOUND case
return null;
}

throw new ServerlessError(
[
`Serverless plugin "${name}" not found.`,
' Make sure it\'s installed and listed in the "plugins" section',
' of your serverless config file.',
].join(''),
'PLUGIN_NOT_FOUND'
);
}
if (!Plugin) {
throw new ServerlessError(
`Serverless plugin "${name}", didn't export Plugin constructor.`,
'MISSING_PLUGIN_NAME'
);
}
return Plugin;
});
throw new ServerlessError(
[
`Serverless plugin "${name}" not found.`,
' Make sure it\'s installed and listed in the "plugins" section',
' of your serverless config file.',
].join(''),
'PLUGIN_NOT_FOUND'
);
}
if (!Plugin) {
throw new ServerlessError(
`Serverless plugin "${name}", didn't export Plugin constructor.`,
'MISSING_PLUGIN_NAME'
);
}
return Plugin;
})
);
}

resolveEnterprisePlugin() {
if (config.getConfig().enterpriseDisabled) return null;
this.pluginIndependentCommands.add('login').add('logout').add('dashboard');
return require('@serverless/enterprise-plugin');
return require('@serverless/dashboard-plugin');
}

parsePluginsObject(servicePlugs) {
Expand Down
4 changes: 2 additions & 2 deletions lib/cli/handle-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const chalk = require('chalk');
const isStandaloneExecutable = require('../utils/isStandaloneExecutable');
const resolveLocalServerlessPath = require('./resolve-local-serverless-path');
const slsVersion = require('./../../package').version;
const sfeVersion = require('@serverless/enterprise-plugin/package.json').version;
const { platformClientVersion } = require('@serverless/enterprise-plugin');
const sfeVersion = require('@serverless/dashboard-plugin/package.json').version;
const { platformClientVersion } = require('@serverless/dashboard-plugin');
const ServerlessError = require('../serverless-error');
const tokenizeException = require('../utils/tokenize-exception');
const resolveIsLocallyInstalled = require('../utils/is-locally-installed');
Expand Down
10 changes: 8 additions & 2 deletions lib/cli/interactive-setup/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
'use strict';

const inquirer = require('@serverless/utils/inquirer');

const steps = {
service: require('./service'),
dashboardLogin: require('@serverless/dashboard-plugin/lib/cli/interactive-setup/dashboard-login'),
dashboardSetOrg: require('@serverless/dashboard-plugin/lib/cli/interactive-setup/dashboard-set-org'),
awsCredentials: require('./aws-credentials'),
autoUpdate: require('./auto-update'),
tabCompletion: require('./tab-completion'),
};

module.exports = async (context) => {
context = { ...context, inquirer };
for (const step of Object.values(steps)) {
if (await step.isApplicable(context)) {
const stepData = await step.isApplicable(context);
if (stepData) {
process.stdout.write('\n');
await step.run(context);
await step.run(context, stepData);
}
}
};
4 changes: 2 additions & 2 deletions lib/cli/render-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

const path = require('path');
const { version } = require('../../package');
const { version: dashboardPluginVersion } = require('@serverless/enterprise-plugin/package');
const { version: dashboardPluginVersion } = require('@serverless/dashboard-plugin/package');
const { version: componentsVersion } = require('@serverless/components/package');
const { platformClientVersion } = require('@serverless/enterprise-plugin');
const { platformClientVersion } = require('@serverless/dashboard-plugin');
const isStandaloneExecutable = require('../utils/isStandaloneExecutable');
const resolveLocalServerlessPath = require('./resolve-local-serverless-path');
const chalk = require('chalk');
Expand Down
24 changes: 20 additions & 4 deletions lib/utils/telemetry/generatePayload.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,31 @@ module.exports = async ({
if (!isLocallyInstalled || (await resolveIsLocallyInstalled())) {
return {
'serverless': require('../../../package').version,
'@serverless/enterprise-plugin': require('@serverless/enterprise-plugin/package').version,
'@serverless/dashboard-plugin': require('@serverless/dashboard-plugin/package').version,
};
}
const localServerlessPath = await resolveLocalServerlessPath();
return {
'serverless': require(path.resolve(localServerlessPath, 'package.json')).version,
'@serverless/enterprise-plugin': require((
await resolve(localServerlessPath, '@serverless/enterprise-plugin/package')
).realPath).version,
// Since v2.42.0 it's "@serverless/dashboard-plugin"
'@serverless/dashboard-plugin': await (async () => {
try {
return require((
await resolve(localServerlessPath, '@serverless/dashboard-plugin/package')
).realPath).version;
} catch {
return undefined;
}
})(),
'@serverless/enterprise-plugin': await (async () => {
try {
return require((
await resolve(localServerlessPath, '@serverless/enterprise-plugin/package')
).realPath).version;
} catch {
return undefined;
}
})(),
};
})();

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"dependencies": {
"@serverless/cli": "^1.5.2",
"@serverless/components": "^3.9.2",
"@serverless/enterprise-plugin": "^4.6.0",
"@serverless/dashboard-plugin": "^5.0.0",
"@serverless/utils": "^4.1.0",
"ajv": "^6.12.6",
"ajv-keywords": "^3.5.2",
Expand Down
6 changes: 3 additions & 3 deletions scripts/pkg/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ module.exports = {
// Local invocation artifacts
'../../lib/plugins/aws/invokeLocal/runtimeWrappers',
// Dashboard policies
'../../node_modules/@serverless/enterprise-plugin/lib/safeguards/policies',
'../../node_modules/@serverless/dashboard-plugin/lib/safeguards/policies',
// Dashboard wrappers
'../../node_modules/@serverless/enterprise-plugin/sdk-js/dist/index.js',
'../../node_modules/@serverless/enterprise-plugin/sdk-py',
'../../node_modules/@serverless/dashboard-plugin/sdk-js/dist/index.js',
'../../node_modules/@serverless/dashboard-plugin/sdk-py',
// Ensure npm is bundled as a dependency
'../../node_modules/npm/bin/npm-cli.js',
// Below module is not automatically traced by pkg, we need to point it manually
Expand Down
4 changes: 2 additions & 2 deletions test/unit/lib/classes/PluginManager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ describe('PluginManager', () => {
mockRequire('ServicePluginMock1', ServicePluginMock1);
mockRequire('ServicePluginMock2', ServicePluginMock2);
mockRequire('BrokenPluginMock', BrokenPluginMock);
mockRequire('@serverless/enterprise-plugin', EnterprisePluginMock);
mockRequire('@serverless/dashboard-plugin', EnterprisePluginMock);
});

it('should load only core plugins when no service plugins are given', () => {
Expand Down Expand Up @@ -742,7 +742,7 @@ describe('PluginManager', () => {
mockRequire.stop('ServicePluginMock1');
mockRequire.stop('ServicePluginMock2');
mockRequire.stop('BrokenPluginMock');
mockRequire.stop('@serverless/enterprise-plugin');
mockRequire.stop('@serverless/dashboard-plugin');
});
});

Expand Down
13 changes: 13 additions & 0 deletions test/unit/lib/cli/interactive-setup/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ describe('test/unit/lib/cli/interactive-setup/index.test.js', () => {
input: 'interactive-setup-test',
},

// dashboard-login
{
instructionString: 'Would you like to enable this?',
input: 'Y',
},
{
instructionString: 'login or register?',
input: 'j',
},

// dashboard-set-org
// Skipped, as internally depends on remote state of data and cannot be easily tested offline

// aws-credentials
{ instructionString: 'Do you want to set them up now?', input: 'Y' },
{ instructionString: 'AWS account', input: 'Y' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2588,7 +2588,7 @@ describe('lib/plugins/aws/package/compile/functions/index.test.js', () => {
// - Corresponding url is configured in CF template
// Test with "deploy" command, and configure `lastLifecycleHookName` to 'aws:deploy:deploy:uploadArtifact'
// It'll demand stubbing few other AWS calls for that follow this stub:
// https://github.com/serverless/enterprise-plugin/blob/cdd53df45dfad18d8bdd79969194a61cb8178671/lib/deployment/parse.test.js#L1585-L1627
// https://github.com/serverless/dashboard-plugin/blob/cdd53df45dfad18d8bdd79969194a61cb8178671/lib/deployment/parse.test.js#L1585-L1627
// Confirm same artifact is used for all functions
});

Expand Down
5 changes: 3 additions & 2 deletions test/unit/lib/utils/telemetry/generatePayload.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const fixtures = require('../../../../fixtures/programmatic');

const versions = {
'serverless': require('../../../../../package').version,
'@serverless/enterprise-plugin': require('@serverless/enterprise-plugin/package').version,
'@serverless/dashboard-plugin': require('@serverless/dashboard-plugin/package').version,
};

describe('test/unit/lib/utils/telemetry/generatePayload.test.js', () => {
Expand Down Expand Up @@ -224,7 +224,8 @@ describe('test/unit/lib/utils/telemetry/generatePayload.test.js', () => {
installationType: 'local:fallback',
versions: {
'serverless': '2.0.0-local',
'@serverless/enterprise-plugin': '4.0.0-local',
'@serverless/dashboard-plugin': '4.0.0-local',
'@serverless/enterprise-plugin': undefined,
},
});
});
Expand Down

0 comments on commit 105807a

Please sign in to comment.