Skip to content

Commit

Permalink
refactor(CLI): Seclude version output functionality out of CLI class
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Jan 11, 2021
1 parent b4fef7d commit b61621a
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 112 deletions.
37 changes: 0 additions & 37 deletions lib/classes/CLI.js
@@ -1,8 +1,6 @@
'use strict';

const version = require('../../package.json').version;
const enterpriseVersion = require('@serverless/enterprise-plugin/package.json').version;
const { sdkVersion } = require('@serverless/enterprise-plugin');
const _ = require('lodash');
const os = require('os');
const chalk = require('chalk');
Expand Down Expand Up @@ -89,21 +87,13 @@ class CLI {

switch (commands.length) {
case 0:
if (options.version || options.v) {
this.getVersionNumber();
return true;
}
if (options['help-interactive']) {
this.generateInteractiveCliHelp();
return true;
}
this.generateMainHelp();
return true;
case 1:
if (commands[0] === 'version') {
this.getVersionNumber();
return true;
}
if (commands[0] === 'help') {
this.generateMainHelp();
return true;
Expand Down Expand Up @@ -358,33 +348,6 @@ functionalities related to given service or current environment.`
return null;
}

getVersionNumber() {
const installationModePostfix = (() => {
if (this.serverless.isStandaloneExecutable) return ' (standalone)';
if (this.serverless.isLocallyInstalled) return ' (local)';
return '';
})();

this.consoleLog(
`Framework Core: ${version}${installationModePostfix}\n` +
`Plugin: ${enterpriseVersion}\n` +
`SDK: ${sdkVersion}`
);

const userNodeVersion = Number(process.version.split('.')[0].slice(1));
// only show components version if user is running Node 8+
if (userNodeVersion >= 8) {
const componentsVersion = (() => {
try {
return require('@serverless/components/package').version;
} catch (error) {
return 'Unavailable';
}
})();
this.consoleLog(`Components: ${componentsVersion}`);
}
}

asciiGreeting() {
let art = '';
art = `${art} _______ __${os.EOL}`;
Expand Down
39 changes: 39 additions & 0 deletions lib/cli/eventually-list-version.js
@@ -0,0 +1,39 @@
'use strict';

const path = require('path');
const { version } = require('../../package');
const { version: dashboardPluginVersion } = require('@serverless/enterprise-plugin/package');
const { version: componentsVersion } = require('@serverless/components/package');
const { sdkVersion } = require('@serverless/enterprise-plugin');
const isStandaloneExecutable = require('../utils/isStandaloneExecutable');
const resolveLocalServerlessPath = require('./resolve-local-serverless-path');

const serverlessPath = path.resolve(__dirname, '../Serverless.js');

module.exports = async () => {
const cliParams = new Set(process.argv.slice(2));
if (!cliParams.has('--version')) {
// Ideally we should output version info in whatever context "--version" or "-v" params
// are used. Still "-v" is defined also as a "--verbose" alias in some commands.
// Support for "--verbose" is expected to go away with
// https://github.com/serverless/serverless/issues/1720
// Until that's addressed we can recognize "-v" only as top-level param
if (cliParams.size !== 1) return false;
if (!cliParams.has('-v')) return false;
}

const installationModePostfix = await (async () => {
if (isStandaloneExecutable) return ' (standalone)';
if (serverlessPath === (await resolveLocalServerlessPath())) return ' (local)';
return '';
})();

process.stdout.write(
`Framework Core: ${version}${installationModePostfix}\n` +
`Plugin: ${dashboardPluginVersion}\n` +
`SDK: ${sdkVersion}\n` +
`Components: ${componentsVersion}\n`
);

return true;
};
2 changes: 2 additions & 0 deletions scripts/serverless.js
Expand Up @@ -32,6 +32,8 @@ const processSpanPromise = (async () => {
serverlessExecutionSpan: processSpanPromise,
});

if (await require('../lib/cli/eventually-list-version')()) return;

const uuid = require('uuid');

const invocationId = uuid.v4();
Expand Down
76 changes: 1 addition & 75 deletions test/unit/lib/classes/CLI.test.js
Expand Up @@ -230,30 +230,6 @@ describe('CLI', () => {
expect(helpDisplayed).to.equal(true);
});

it('should return true when the "version" parameter is given', () => {
cli = new CLI(serverless, ['version']);
const processedInput = cli.processInput();
const helpDisplayed = cli.displayHelp(processedInput);

expect(helpDisplayed).to.equal(true);
});

it('should return true when the "--version" parameter is given', () => {
cli = new CLI(serverless, ['--version']);
const processedInput = cli.processInput();
const helpDisplayed = cli.displayHelp(processedInput);

expect(helpDisplayed).to.equal(true);
});

it('should return true when the "-v" parameter is given', () => {
cli = new CLI(serverless, ['-v']);
const processedInput = cli.processInput();
const helpDisplayed = cli.displayHelp(processedInput);

expect(helpDisplayed).to.equal(true);
});

it('should return true when the "-h" parameter is given with a command', () => {
cli = new CLI(serverless, ['test', '-h']);
serverless.cli = cli;
Expand Down Expand Up @@ -332,7 +308,7 @@ describe('CLI', () => {
expect(helpDisplayed).to.equal(true);
});

it('should return false if no "help" or "version" related command / option is given', () => {
it('should return false if no "help" related command / option is given', () => {
cli = new CLI(serverless, ['test']);
serverless.cli = cli;
class PluginMock {
Expand Down Expand Up @@ -430,32 +406,6 @@ describe('CLI', () => {
});
});

describe('#getVersionNumber()', () => {
let consoleLogSpy;

beforeEach(() => {
cli = new CLI(serverless);
consoleLogSpy = sinon.spy(cli, 'consoleLog');
});

afterEach(() => {
cli.consoleLog.restore();
});

it('should log the version numbers', () => {
cli.getVersionNumber();

expect(consoleLogSpy.args[0][0]).to.include('Framework Core');
expect(consoleLogSpy.args[0][0]).to.include('Plugin');
expect(consoleLogSpy.args[0][0]).to.include('SDK');

const userNodeVersion = Number(process.version.split('.')[0].slice(1));
if (userNodeVersion >= 8) {
expect(consoleLogSpy.args[1][0]).to.include('Components');
}
});
});

describe('#processInput()', () => {
it('should only return the commands when only commands are given', () => {
cli = new CLI(serverless, ['deploy', 'functions']);
Expand Down Expand Up @@ -669,30 +619,6 @@ describe('CLI [new tests]', () => {
expect(stdoutData).to.include('Documentation: http://slss.io/docs');
}));

it('Should show version when requested and no commands are used', () =>
runServerless({
fixture: 'function',
cliArgs: ['-v'],
}).then(({ stdoutData }) => {
expect(stdoutData).to.include('Framework Core: ');
}));

it('Should not show version with command', () =>
runServerless({
fixture: 'customProvider',
cliArgs: ['info', '-v'],
})
.then(({ stdoutData }) => {
expect(stdoutData).to.not.include('Framework Core: ');
return runServerless({
fixture: 'customProvider',
cliArgs: ['info', '--version'],
});
})
.then(({ stdoutData }) => {
expect(stdoutData).to.not.include('Framework Core: ');
}));

it('Should handle incomplete command configurations', async () => {
const { stdoutData } = await runServerless({
fixture: 'plugin',
Expand Down
81 changes: 81 additions & 0 deletions test/unit/lib/cli/eventually-list-version.test.js
@@ -0,0 +1,81 @@
'use strict';

const { expect } = require('chai');
const overrideStdoutWrite = require('process-utils/override-stdout-write');
const overrideArgv = require('process-utils/override-argv');
const eventuallyListVersion = require('../../../../lib/cli/eventually-list-version');

describe('test/unit/lib/cli/eventually-list-version.test.js', () => {
it('should log version on top level --version param', async () => {
let stdoutData = '';
expect(
await overrideArgv({ args: ['serverless', '--version'] }, () =>
overrideStdoutWrite(
(data) => (stdoutData += data),
() => eventuallyListVersion()
)
)
).to.equal(true);
expect(stdoutData).to.have.string('Framework Core: ');
});
it('should log version on deep --version param', async () => {
let stdoutData = '';
expect(
await overrideArgv({ args: ['serverless', 'deploy', 'function', '--version'] }, () =>
overrideStdoutWrite(
(data) => (stdoutData += data),
() => eventuallyListVersion()
)
)
).to.equal(true);
expect(stdoutData).to.have.string('Framework Core: ');
});
it('should log version on top level -v param', async () => {
let stdoutData = '';
expect(
await overrideArgv({ args: ['serverless', '-v'] }, () =>
overrideStdoutWrite(
(data) => (stdoutData += data),
() => eventuallyListVersion()
)
)
).to.equal(true);
expect(stdoutData).to.have.string('Framework Core: ');
});
it('should not log version on deep -v param', async () => {
let stdoutData = '';
expect(
await overrideArgv({ args: ['serverless', 'deploy', 'function', '-v'] }, () =>
overrideStdoutWrite(
(data) => (stdoutData += data),
() => eventuallyListVersion()
)
)
).to.equal(false);
expect(stdoutData).to.equal('');
});
it('should not log version when no params', async () => {
let stdoutData = '';
expect(
await overrideArgv({ args: ['serverless'] }, () =>
overrideStdoutWrite(
(data) => (stdoutData += data),
() => eventuallyListVersion()
)
)
).to.equal(false);
expect(stdoutData).to.equal('');
});
it('should not log version when no version params', async () => {
let stdoutData = '';
expect(
await overrideArgv({ args: ['serverless', 'deploy', 'function'] }, () =>
overrideStdoutWrite(
(data) => (stdoutData += data),
() => eventuallyListVersion()
)
)
).to.equal(false);
expect(stdoutData).to.equal('');
});
});

0 comments on commit b61621a

Please sign in to comment.