Skip to content

Commit

Permalink
Merge branch 'main' into sbosio/missing-shared-behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
sbosio committed Aug 15, 2024
2 parents d1c32e8 + efedf17 commit 1c4e9e7
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 4 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/
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@heroku-cli/command",
"description": "base class for Heroku CLI commands",
"version": "11.1.2",
"version": "11.2.2",
"author": "Heroku",
"bugs": "https://github.com/heroku/heroku-cli-command/issues",
"dependencies": {
Expand All @@ -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
55 changes: 55 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 @@ -47,4 +52,54 @@ 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}

if (nonExistentFlags && nonExistentFlags.length > 0) {
this.warn(`You’re using a deprecated syntax with the [${nonExistentFlags}] flag.\nAdd a '--' (end of options) separator before the flags you’re passing through.`)
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)

for (let index = 0; index < result.nonExistentFlags.length; index++) {
const positionalValue = result.nonExistentFlags[index]
const doubleHyphenRegex = /^--/
const positionalValueIsFlag = doubleHyphenRegex.test(positionalValue)
if (positionalValueIsFlag) {
const nextElement = result.nonExistentFlags[index + 1] ? result.nonExistentFlags[index + 1] : ''
const nextElementIsFlag = doubleHyphenRegex.test(nextElement)
// eslint-disable-next-line max-depth
if (nextElement && !nextElementIsFlag) {
result.argv.push(`${positionalValue}=${nextElement}`)
} else if (!nextElement || nextElementIsFlag) {
result.argv.push(`${positionalValue}=${true}`)
}
}
}

return result
}
}

return super.parse(options, argv)
}
}
16 changes: 13 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,16 @@
resolved "https://registry.npmjs.org/@types/uuid/-/uuid-8.3.0.tgz"
integrity sha512-eQ9qFW/fhfGJF8WKHGEHZEyVWfZxrT+6CLIJGBcZPfxUh/+BnEj+UCGYMlr9qZuX/2AltsvwrGqp0LhEW8D0zQ==

"@types/yargs-parser@^21.0.3":
version "21.0.3"
resolved "https://registry.npmjs.org/@types/yargs-parser/-/yargs-parser-21.0.3.tgz"
integrity sha512-I4q9QU9MQv4oEOz4tAHJtNz1cwuLxn2F3xcc2iV5WdqLPpUnj30aUuxt1mAxYTG+oe8CZMV/+6rU4S4gRDzqtQ==

"@types/yargs-unparser@^2.0.3":
version "2.0.3"
resolved "https://registry.npmjs.org/@types/yargs-unparser/-/yargs-unparser-2.0.3.tgz"
integrity sha512-4yUMRVOqW0s+sohpHbHmNf3G+cAK6gw9vHPHdWQ9w5/nzS7vEPHRvlLVhCJ748h9zYkWkAtDn8pTAuLwCvn3DQ==

"@typescript-eslint/[email protected]":
version "6.21.0"
resolved "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-6.21.0.tgz"
Expand Down Expand Up @@ -1706,7 +1716,7 @@ define-data-property@^1.0.1, define-data-property@^1.1.4:

define-lazy-prop@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/define-lazy-prop/-/define-lazy-prop-2.0.0.tgz#3f7ae421129bcaaac9bc74905c98a0009ec9ee7f"
resolved "https://registry.npmjs.org/define-lazy-prop/-/define-lazy-prop-2.0.0.tgz"
integrity sha512-Ds09qNh8yw3khSjiJjiUInaGX9xlqZDY7JVryGxdxV7NPeuqQfplOpQ66yJFZut3jLa5zOwkXw1g9EI2uKh4Og==

define-properties@^1.2.0, define-properties@^1.2.1:
Expand Down Expand Up @@ -4104,7 +4114,7 @@ open@^7.3.0:

open@^8.4.2:
version "8.4.2"
resolved "https://registry.yarnpkg.com/open/-/open-8.4.2.tgz#5b5ffe2a8f793dcd2aad73e550cb87b59cb084f9"
resolved "https://registry.npmjs.org/open/-/open-8.4.2.tgz"
integrity sha512-7x81NCL719oNbsq/3mh+hVrAWmFuEYUqrq/Iw3kUzH8ReypT9QQ0BLoJS7/G9k6N81XjW4qHWtjWwe/9eLy1EQ==
dependencies:
define-lazy-prop "^2.0.0"
Expand Down Expand Up @@ -4869,7 +4879,7 @@ sprintf-js@~1.0.2:

stdout-stderr@^0.1.13:
version "0.1.13"
resolved "https://registry.yarnpkg.com/stdout-stderr/-/stdout-stderr-0.1.13.tgz#54e3450f3d4c54086a49c0c7f8786a44d1844b6f"
resolved "https://registry.npmjs.org/stdout-stderr/-/stdout-stderr-0.1.13.tgz"
integrity sha512-Xnt9/HHHYfjZ7NeQLvuQDyL1LnbsbddgMFKCuaQKwGCdJm8LnstZIXop+uOY36UR1UXXoHXfMbC1KlVdVd2JLA==
dependencies:
debug "^4.1.1"
Expand Down

0 comments on commit 1c4e9e7

Please sign in to comment.