Skip to content

Commit

Permalink
fix(W-16338640): Backward compatibility for CLI commands (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
justinwilaby authored Aug 8, 2024
1 parent 351dc1f commit 45632b3
Show file tree
Hide file tree
Showing 4 changed files with 334 additions and 167 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
lib/
node_modules/
package-lock.json
.idea/
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
"@types/sinon": "^10.0.13",
"@types/supports-color": "^5.3.0",
"@types/uuid": "^8.3.0",
"@types/yargs-parser": "^21.0.3",
"@types/yargs-unparser": "^2.0.3",
"@typescript-eslint/eslint-plugin": "6.21.0",
"@typescript-eslint/parser": "6.21.0",
"chai": "^4.4.1",
Expand All @@ -48,6 +50,10 @@
"tslint": "^6.1.3",
"typescript": "^4.8.4"
},
"peerDependencies": {
"yargs-parser": ">=18.x",
"yargs-unparser": "^2.0.0"
},
"engines": {
"node": ">= 16.20.0"
},
Expand Down
35 changes: 35 additions & 0 deletions src/command.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import {Command as Base} from '@oclif/core'
import {ArgOutput, FlagOutput, Input, ParserOutput} from '@oclif/core/lib/interfaces/parser'
import {NonExistentFlagsError} from '@oclif/core/lib/parser/errors'
import {deprecate} from 'util'
import parser from 'yargs-parser'
import unparser from 'yargs-unparser'

const pjson = require('../package.json')

Check warning on line 8 in src/command.ts

View workflow job for this annotation

GitHub Actions / test (windows-latest, 16.x)

Do not use "require"

Check warning on line 8 in src/command.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 16.x)

Do not use "require"

Check warning on line 8 in src/command.ts

View workflow job for this annotation

GitHub Actions / test (macos-latest, 16.x)

Do not use "require"

Expand All @@ -14,6 +18,7 @@ export abstract class Command extends Base {
base = `${pjson.name}@${pjson.version}`
_heroku!: APIClient
_legacyHerokuClient: any
allowArbitraryFlags: boolean = false;

get heroku(): APIClient {
if (this._heroku) return this._heroku
Expand Down Expand Up @@ -43,4 +48,34 @@ export abstract class Command extends Base {
get out(): any {
return deprecatedCLI()
}

protected async parse<F extends FlagOutput, B extends FlagOutput, A extends ArgOutput>(options?: Input<F, B, A>, argv?: string[]): Promise<ParserOutput<F, B, A>> {
if (this.allowArbitraryFlags) {
try {
return await super.parse(options, argv)
} catch (error) {
const {flags: nonExistentFlags} = error as NonExistentFlagsError
const parsed = parser(this.argv)
const nonExistentFlagsWithValues = {...parsed}

for (const flag of nonExistentFlags) {
const key = flag.replace('--', '')
delete parsed[key]
}

for (const key in parsed) {
if (Reflect.has(parsed, key)) {
delete nonExistentFlagsWithValues[key]
}
}

this.argv = unparser(parsed as unparser.Arguments)
const result = await super.parse(options, argv)
result.nonExistentFlags = unparser(nonExistentFlagsWithValues as unparser.Arguments)
return result
}
}

return super.parse(options, argv)
}
}
Loading

0 comments on commit 45632b3

Please sign in to comment.