Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(CLI): Seclude version output functionality out of CLI class
- Loading branch information
Showing
5 changed files
with
123 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(''); | ||
}); | ||
}); |