Skip to content

Commit

Permalink
feat: use version details from core (#153)
Browse files Browse the repository at this point in the history
* fix: use core version details for the command

@#W-12647786@

* feat!: bump major for breaking change

@W-12647786@

* feat: use version details from core

@W-12647786@

* chore: fix tests

* chore: debug test

* chore: test debugging

* chore: ut fix

* chore: bump oclif/core

* chore: remove partial narrowing
  • Loading branch information
peternhale authored Mar 15, 2023
1 parent 37c5b7a commit 693c93c
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 113 deletions.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
"author": "Salesforce",
"bugs": "https://github.com/oclif/plugin-version/issues",
"dependencies": {
"@oclif/core": "^2.0.3"
"@oclif/core": "^2.6.3"
},
"devDependencies": {
"@commitlint/config-conventional": "^12.1.4",
"@oclif/plugin-help": "5.1.12",
"@oclif/test": "^2.1.1",
"@oclif/test": "^2.3.10",
"@types/chai": "^4.3.1",
"@types/mocha": "^8.0.0",
"@types/node": "^16",
Expand All @@ -19,6 +19,7 @@
"eslint": "^7.3.1",
"eslint-config-oclif": "^4",
"eslint-config-oclif-typescript": "^1.0.2",
"fancy-test": "^2.0.13",
"husky": "6",
"mocha": "^8.2.1",
"oclif": "^3.4.6",
Expand Down Expand Up @@ -60,4 +61,4 @@
"version": "oclif readme && git add README.md"
},
"types": "lib/index.d.ts"
}
}
62 changes: 30 additions & 32 deletions src/commands/version.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import {Command, Flags} from '@oclif/core'
import {Command, Flags, Interfaces} from '@oclif/core'
// eslint-disable-next-line unicorn/prefer-node-protocol
import {EOL, type as osType, release as osRelease} from 'os'
import {EOL} from 'os'

export interface VersionDetail {
cliVersion: string;
architecture: string;
nodeVersion: string;
export type VersionDetail = Omit<Interfaces.VersionDetails, 'pluginVersions'> & {
pluginVersions?: string[];
osVersion?: string;
shell?: string;
rootPath?: string;
}

export default class Version extends Command {
Expand All @@ -24,41 +18,28 @@ export default class Version extends Command {

async run(): Promise<VersionDetail> {
const {flags} = await this.parse(Version)
const {pluginVersions, ...theRest} = this.config.versionDetails
const versionDetail: VersionDetail = {...theRest}

const [cliVersion, architecture, nodeVersion] = this.config.userAgent.split(' ')

const versionDetail:VersionDetail = {cliVersion, architecture, nodeVersion}
let output = `${this.config.userAgent}`

if (flags.verbose) {
const pluginVersions = []
for (const plugin of this.config.plugins.sort((a, b) => a.name > b.name ? 1 : -1)) {
if (this.config.name !== plugin.name) {
pluginVersions.push(`${this.getFriendlyName(plugin.name)} ${plugin.version} (${plugin.type}) ${plugin.type === 'link' ? plugin.root : ''}`.trim())
}
}

const osVersion = `${osType()} ${osRelease()}`

versionDetail.pluginVersions = pluginVersions
versionDetail.osVersion = osVersion
versionDetail.shell = this.config.shell
versionDetail.rootPath = this.config.root
versionDetail.pluginVersions = this.formatPlugins(pluginVersions ?? {})
versionDetail.shell ??= 'unknown'

output = ` CLI Version:
\t${cliVersion}
\t${versionDetail.cliVersion}
Architecture:
\t${architecture}
\t${versionDetail.architecture}
Node Version:
\t${nodeVersion}
\t${versionDetail.nodeVersion}
Plugin Version:
\t${pluginVersions.join(`${EOL}\t`)}
\t${flags.verbose ? (versionDetail.pluginVersions ?? []).join(EOL + '\t') : ''}
OS and Version:
\t${osVersion}
\t${versionDetail.osVersion}
Shell:
\t${versionDetail.shell}
Expand All @@ -70,7 +51,24 @@ export default class Version extends Command {

this.log(output)

return versionDetail
return flags.verbose ?
versionDetail :
{
cliVersion: versionDetail.cliVersion,
architecture: versionDetail.architecture,
nodeVersion: versionDetail.nodeVersion,
}
}

private formatPlugins(plugins: Record<string, Interfaces.PluginVersionDetail>): string[] {
return Object.entries(plugins)
.map(([name, plugin]) => ({name, ...plugin}))
.sort((a, b) => (a.name > b.name ? 1 : -1))
.map(plugin =>
`${this.getFriendlyName(plugin.name)} ${plugin.version} (${plugin.type}) ${
plugin.type === 'link' ? plugin.root : ''
}`.trim(),
)
}

private getFriendlyName(name: string): string {
Expand Down
40 changes: 22 additions & 18 deletions test/commands/version.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,31 @@ describe('version', () => {
.stdout()
.command(['version', '--verbose'])
.end('runs version --verbose', output => {
expect(output.stdout).to.contain(` CLI Version:
\t@oclif/plugin-version/${pjson.version}`)
expect(output.stdout).to.contain(` Architecture:
\t${process.platform}-${process.arch}`)
expect(output.stdout).to.contain(` Node Version:
\tnode-${process.version}`)
expect(output.stdout).to.contain(' CLI Version:')
expect(output.stdout).to.contain(`\t@oclif/plugin-version/${pjson.version}`)
expect(output.stdout).to.contain(' Architecture:')
expect(output.stdout).to.contain(`\t${process.platform}-${process.arch}`)
expect(output.stdout).to.contain(' Node Version:')
expect(output.stdout).to.contain(`\tnode-${process.version}`)
expect(output.stdout).to.contain(' Plugin Version:')
expect(output.stdout).to.contain(` OS and Version:
\t${osType()} ${osRelease()}`)
expect(output.stdout).to.contain(` Shell:
\t${getShell()}`)
expect(output.stdout).to.contain(` Root Path:
\t${process.cwd()}`)
expect(output.stdout).to.contain(' OS and Version:')
expect(output.stdout).to.contain(`\t${osType()} ${osRelease()}`)
expect(output.stdout).to.contain(' Shell:')
expect(output.stdout).to.contain(`\t${getShell()}`)
expect(output.stdout).to.contain(' Root Path:')
expect(output.stdout).to.contain(`\t${process.cwd()}`)
})

test
.stdout()
.command(['version', '--json'])
.end('runs version --json', output => {
expect(JSON.parse(output.stdout)).to.deep.equal({
cliVersion: `@oclif/plugin-version/${pjson.version}`,
architecture: `${process.platform}-${process.arch}`,
nodeVersion: `node-${process.version}`,
})
const json = JSON.parse(output.stdout)
expect(json).to.have.keys([
'cliVersion',
'architecture',
'nodeVersion',
])
})

test
Expand All @@ -58,7 +59,10 @@ describe('version', () => {
expect(json).to.have.property('nodeVersion', `node-${process.version}`)
expect(json).to.have.property('osVersion', `${osType()} ${osRelease()}`)
expect(json).to.have.property('pluginVersions')
expect(json).to.have.property('shell', getShell())
expect(json.pluginVersions).to.an('array')
expect(json).to.have.property('shell')
expect(json.shell).to.be.equal(getShell(), `json.shell: ${json.shell} getShell(): ${getShell()}`)
expect(json).to.have.property('rootPath', process.cwd())
})
})
// {"uid":1001,"gid":123,"username":"runner","homedir":"/home/runner","shell":"/bin/bash"}
87 changes: 27 additions & 60 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -622,41 +622,7 @@
supports-color "^8.1.1"
tslib "^2"

"@oclif/core@^1.24.0", "@oclif/core@^1.3.6":
version "1.24.0"
resolved "https://registry.yarnpkg.com/@oclif/core/-/core-1.24.0.tgz#e94e2f3a1259ebb3cc867d04b12c5bfc31c21f50"
integrity sha512-J41suyV4fLfLcRRySZdtgFYSjIlpYqD90SY01Fm4+ZJUMcfDh/eQAD4sogyYOsIT0bfEzYOyYCjmfHmgcoX5aA==
dependencies:
"@oclif/linewrap" "^1.0.0"
"@oclif/screen" "^3.0.4"
ansi-escapes "^4.3.2"
ansi-styles "^4.3.0"
cardinal "^2.1.1"
chalk "^4.1.2"
clean-stack "^3.0.1"
cli-progress "^3.10.0"
debug "^4.3.4"
ejs "^3.1.6"
fs-extra "^9.1.0"
get-package-type "^0.1.0"
globby "^11.1.0"
hyperlinker "^1.0.0"
indent-string "^4.0.0"
is-wsl "^2.2.0"
js-yaml "^3.14.1"
natural-orderby "^2.0.3"
object-treeify "^1.1.33"
password-prompt "^1.1.2"
semver "^7.3.7"
string-width "^4.2.3"
strip-ansi "^6.0.1"
supports-color "^8.1.1"
supports-hyperlinks "^2.2.0"
tslib "^2.4.1"
widest-line "^3.1.0"
wrap-ansi "^7.0.0"

"@oclif/core@^1.24.2":
"@oclif/core@^1.24.0", "@oclif/core@^1.24.2", "@oclif/core@^1.3.6":
version "1.24.2"
resolved "https://registry.yarnpkg.com/@oclif/core/-/core-1.24.2.tgz#1d2e4b8b27db7e98b0c1d7b367934196a2f2c202"
integrity sha512-0wfAa6HG4sJ4j5c4/GV4BWZwALtJyw2ZO6titnrWKcowxU1BWd8mBM45ilTPnDhClMowz7+8EtK4kqUGc1rNwA==
Expand Down Expand Up @@ -690,20 +656,20 @@
widest-line "^3.1.0"
wrap-ansi "^7.0.0"

"@oclif/core@^2.0.3":
version "2.0.3"
resolved "https://registry.yarnpkg.com/@oclif/core/-/core-2.0.3.tgz#ce71efbfc6e4c2655579ce81adce6471be2bc799"
integrity sha512-Dr6tghg32KKEivcWYqrx9bgC8FDE4i47FLR1wjnLg3jNJWf+blTYorj6aHize2WF0wiwkvB28Cr5TUNBooB4AQ==
"@oclif/core@^2.6.2", "@oclif/core@^2.6.3":
version "2.6.3"
resolved "https://registry.yarnpkg.com/@oclif/core/-/core-2.6.3.tgz#2f02e4510eb76d694d00c488b3ea957436289f51"
integrity sha512-kLwg+lmeZt4vfUlYN1XI8bPkcNBozfPOqciFOJMzHkPnq18lmoMu3+Xnm4wL1A9dXEyzoa5jidFhdyP/kZMBCw==
dependencies:
"@types/cli-progress" "^3.11.0"
ansi-escapes "^4.3.2"
ansi-styles "^4.3.0"
cardinal "^2.1.1"
chalk "^4.1.2"
clean-stack "^3.0.1"
cli-progress "^3.10.0"
cli-progress "^3.12.0"
debug "^4.3.4"
ejs "^3.1.6"
ejs "^3.1.8"
fs-extra "^9.1.0"
get-package-type "^0.1.0"
globby "^11.1.0"
Expand All @@ -719,7 +685,8 @@
strip-ansi "^6.0.1"
supports-color "^8.1.1"
supports-hyperlinks "^2.2.0"
tslib "^2.4.1"
ts-node "^10.9.1"
tslib "^2.5.0"
widest-line "^3.1.0"
wordwrap "^1.0.0"
wrap-ansi "^7.0.0"
Expand Down Expand Up @@ -771,13 +738,13 @@
resolved "https://registry.yarnpkg.com/@oclif/screen/-/screen-3.0.4.tgz#663db0ecaf23f3184e7f01886ed578060e4a7f1c"
integrity sha512-IMsTN1dXEXaOSre27j/ywGbBjrzx0FNd1XmuhCWCB9NTPrhWI1Ifbz+YLSEcstfQfocYsrbrIessxXb2oon4lA==

"@oclif/test@^2.1.1":
version "2.2.21"
resolved "https://registry.yarnpkg.com/@oclif/test/-/test-2.2.21.tgz#f0a64773100b12191b9f9ea5efb9a632ec2c922a"
integrity sha512-uIjn0PM6R9qlpYzoB9eYBwh3uMd5EdTux2dhBnYMA3DWcLOV4/+2TxKeVurfHFHfQfRp7imYSab7vEwTfiSo7g==
"@oclif/test@^2.3.10":
version "2.3.10"
resolved "https://registry.yarnpkg.com/@oclif/test/-/test-2.3.10.tgz#0bbdd0564f3cbd59a3c3b4ccfa127daae3b6b58f"
integrity sha512-4Rb4HmFBHfsOywae6lU9YYed/KZaH1hZas5W/lCsd03jumUOpmWHf5ny6B8Q56BV43IN4Q6iUAYB04xoDsSZwQ==
dependencies:
"@oclif/core" "^1.24.0"
fancy-test "^2.0.11"
"@oclif/core" "^2.6.2"
fancy-test "^2.0.13"

"@octokit/auth-token@^2.4.4":
version "2.5.0"
Expand Down Expand Up @@ -1696,10 +1663,10 @@ cli-cursor@^3.1.0:
dependencies:
restore-cursor "^3.1.0"

cli-progress@^3.10.0:
version "3.11.2"
resolved "https://registry.yarnpkg.com/cli-progress/-/cli-progress-3.11.2.tgz#f8c89bd157e74f3f2c43bcfb3505670b4d48fc77"
integrity sha512-lCPoS6ncgX4+rJu5bS3F/iCz17kZ9MPZ6dpuTtI0KXKABkhyXIdYB3Inby1OpaGti3YlI3EeEkM9AuWpelJrVA==
cli-progress@^3.10.0, cli-progress@^3.12.0:
version "3.12.0"
resolved "https://registry.yarnpkg.com/cli-progress/-/cli-progress-3.12.0.tgz#807ee14b66bcc086258e444ad0f19e7d42577942"
integrity sha512-tRkV3HJ1ASwm19THiiLIXLO7Im7wlTuKnvkYaTkyoAPefqjNg7W7DHKUlGRxy9vxDvbyCYQkQozvptuMkGCg8A==
dependencies:
string-width "^4.2.3"

Expand Down Expand Up @@ -2444,10 +2411,10 @@ external-editor@^3.0.3:
iconv-lite "^0.4.24"
tmp "^0.0.33"

fancy-test@^2.0.11:
version "2.0.12"
resolved "https://registry.yarnpkg.com/fancy-test/-/fancy-test-2.0.12.tgz#a93cd92ffc23f70b069c39f19940d34f64c6ca67"
integrity sha512-S7qVQNaViLTMzn71huZvrUCV59ldq+enQ1EQOkdNbl4q4Om97gwqbYKvZoglsnzCWRRFaFP+qHynpdqaLdiZqg==
fancy-test@^2.0.13:
version "2.0.13"
resolved "https://registry.yarnpkg.com/fancy-test/-/fancy-test-2.0.13.tgz#861323fe91f5f4b3d6d2e53104756f64274d7dfe"
integrity sha512-n+rbO0zU861qNT4L3vIy2CEZlsmLR1sp8sy/uZDEOdmRtfnBBwN9OgT37X0zhQJZTlu2vkdKRu51BsyekgB13Q==
dependencies:
"@types/chai" "*"
"@types/lodash" "*"
Expand Down Expand Up @@ -5161,10 +5128,10 @@ tslib@^1.8.1:
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00"
integrity sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==

tslib@^2, tslib@^2.1.0, tslib@^2.3.1, tslib@^2.4.1:
version "2.4.1"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.4.1.tgz#0d0bfbaac2880b91e22df0768e55be9753a5b17e"
integrity sha512-tGyy4dAjRIEwI7BzsB0lynWgOpfqjUdq91XXAlIWD2OwKBH7oCl/GZG/HT4BOHrTlPMOASlMQ7veyTqpmRcrNA==
tslib@^2, tslib@^2.1.0, tslib@^2.3.1, tslib@^2.4.1, tslib@^2.5.0:
version "2.5.0"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.5.0.tgz#42bfed86f5787aeb41d031866c8f402429e0fddf"
integrity sha512-336iVw3rtn2BUK7ORdIAHTyxHGRIHVReokCR3XjbckJMK7ms8FysBfhLR8IXnAgy7T0PTPNBWKiH514FOW/WSg==

tsutils@^3.21.0:
version "3.21.0"
Expand Down

0 comments on commit 693c93c

Please sign in to comment.