Skip to content

Commit

Permalink
refactor(CLI): Cleanup components resolution logic
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Jul 22, 2020
1 parent dc826b4 commit cf1d51d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 48 deletions.
30 changes: 20 additions & 10 deletions bin/serverless.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,30 @@ if (require('../lib/utils/isStandaloneExecutable')) {

const nodeVersion = Number(process.version.split('.')[0].slice(1));

// only check for components if user is running Node 8
// CLI Triage
// Serverless Components work only in Node.js v8+
if (nodeVersion >= 8) {
const componentsV1 = require('../lib/components-v1');
const componentsV2 = require('../lib/components-v2');
try {
const componentsV1 = require('@serverless/cli');
const componentsV2 = require('@serverless/components');

if (componentsV1 && componentsV1.runningComponents()) {
componentsV1.runComponents();
return;
}
if (componentsV1.runningComponents()) {
// Serverless Components v1 CLI (deprecated)
componentsV1.runComponents();
return;
}

if (componentsV2 && componentsV2.runningComponents()) {
componentsV2.runComponents();
return;
if (componentsV2.runningComponents()) {
// Serverless Components CLI
componentsV2.runComponents();
return;
}
} catch (error) {
if (process.env.SLS_DEBUG) {
require('../lib/classes/Error').logWarning(`CLI triage crashed with: ${error.stack}`);
}
}
}

// Serverless Framework CLI
require('../scripts/serverless');
10 changes: 8 additions & 2 deletions lib/classes/CLI.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,14 @@ functionalities related to given service or current environment.`
const userNodeVersion = Number(process.version.split('.')[0].slice(1));
// only show components version if user is running Node 8+
if (userNodeVersion >= 8) {
const components = require('../components-v2');
this.consoleLog(`Components: ${components ? components.componentsVersion : 'Unavailable'}`);
const componentsVersion = (() => {
try {
return require('@serverless/components/package').version;
} catch (error) {
return 'Unavailable';
}
})();
this.consoleLog(`Components: ${componentsVersion}`);
}
}

Expand Down
16 changes: 8 additions & 8 deletions lib/classes/Error.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@ module.exports.logError = (exception, { forceExit = false } = {}) => {
// only show components version if user is running Node 8+
const userNodeVersion = Number(process.version.split('.')[0].slice(1));
if (userNodeVersion >= 8) {
const components = require('../components-v2');
consoleLog(
chalk.yellow(
` Components Version: ${
components ? components.componentsVersion : 'Unavailable'
}`
)
);
const componentsVersion = (() => {
try {
return require('@serverless/components/package').version;
} catch (error) {
return 'Unavailable';
}
})();
consoleLog(chalk.yellow(` Components Version: ${componentsVersion}`));
}
consoleLog(' ');

Expand Down
14 changes: 0 additions & 14 deletions lib/components-v1.js

This file was deleted.

14 changes: 0 additions & 14 deletions lib/components-v2.js

This file was deleted.

0 comments on commit cf1d51d

Please sign in to comment.