-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: use ES2022 #793
Merged
Merged
fix: use ES2022 #793
Changes from 24 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
933bb82
fix: use ES2022
mdonnalley 4d14780
test: use --json for config unset
mdonnalley 57794c3
feat: stop using getters and setters for flags
mdonnalley a893f34
chore: clean up
mdonnalley 52f1b12
feat: expose json flag
mdonnalley 178b9a9
feat: remove pass through getter and setter
mdonnalley f50ddf5
fix: correct order of flags in toCached
mdonnalley ea315ea
chore: clean up
mdonnalley f6db50f
fix: flag merge order
mdonnalley 7d80d6f
chore: documentation
mdonnalley ea1a137
test: use oclif/test v3
mdonnalley a12dbf7
feat: set spinner style on windows too
mdonnalley 45a777f
fix: handle cmd with baseFlags but no flags
mdonnalley bd8c6c8
fix: some circular deps
mdonnalley fc59ae2
fix: circular deps in help
mdonnalley cdff4b6
fix: ts-node and config circular deps
mdonnalley 8195c03
fix: toCached circular dep in help
mdonnalley 5a19e53
chore: organize utils
mdonnalley 38d6d3d
test: enforce no circular deps
mdonnalley 6d20d62
chore: remove Flags.json
mdonnalley ce3441f
chore: add prettier config
mdonnalley 811efa1
test: add nyc
mdonnalley e365ad1
test: improve test coverage
mdonnalley 920ea0e
test: windows unit tests
mdonnalley 42282e4
chore: revert change to automerge.yml
mdonnalley 09c9b66
chore: code review
mdonnalley f51da6b
perf: parallelize cacheCommand
mdonnalley File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,11 @@ | ||
{ | ||
"check-coverage": true, | ||
"lines": 80, | ||
"statements": 70, | ||
"functions": 70, | ||
"branches": 60, | ||
"reporter": ["lcov", "text"], | ||
"extension": [".ts"], | ||
"include": ["**/*.ts"], | ||
"exclude": ["**/*.d.ts", "test/**"] | ||
} |
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 @@ | ||
"@oclif/prettier-config" |
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 |
---|---|---|
|
@@ -38,7 +38,8 @@ | |
"@commitlint/config-conventional": "^12.1.4", | ||
"@oclif/plugin-help": "^5.2.8", | ||
"@oclif/plugin-plugins": "^3.3.0", | ||
"@oclif/test": "^2.4.7", | ||
"@oclif/prettier-config": "^0.1.1", | ||
"@oclif/test": "^3.0.0-beta.1", | ||
"@types/ansi-styles": "^3.2.1", | ||
"@types/benchmark": "^2.1.2", | ||
"@types/chai": "^4.3.4", | ||
|
@@ -67,8 +68,10 @@ | |
"fancy-test": "^3.0.0-beta.2", | ||
"globby": "^11.1.0", | ||
"husky": "6", | ||
"madge": "^6.1.0", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 for madge |
||
"mocha": "^10.2.0", | ||
"nock": "^13.3.0", | ||
"nyc": "^15.1.0", | ||
"shx": "^0.3.4", | ||
"sinon": "^11.1.2", | ||
"tsd": "^0.29.0", | ||
|
@@ -105,13 +108,14 @@ | |
"commitlint": "commitlint", | ||
"compile": "tsc", | ||
"lint": "eslint . --ext .ts", | ||
"posttest": "yarn lint", | ||
"posttest": "yarn lint && yarn test:circular-deps", | ||
"prepack": "yarn run build", | ||
"pretest": "yarn build --noEmit && tsc -p test --noEmit --skipLibCheck", | ||
"pretest": "yarn build && tsc -p test --noEmit --skipLibCheck", | ||
"test:circular-deps": "madge lib/ -c", | ||
"test:e2e": "mocha --forbid-only \"test/**/*.e2e.ts\" --parallel --timeout 1200000", | ||
"test:esm-cjs": "cross-env DEBUG=e2e:* ts-node test/integration/esm-cjs.ts", | ||
"test:perf": "ts-node test/perf/parser.perf.ts", | ||
"test": "mocha --forbid-only \"test/**/*.test.ts\"" | ||
"test": "nyc mocha --forbid-only \"test/**/*.test.ts\"" | ||
}, | ||
"types": "lib/index.d.ts" | ||
} |
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 |
---|---|---|
@@ -1,15 +1,17 @@ | ||
import chalk from 'chalk' | ||
import {format} from 'node:util' | ||
import {stdout} from '../stream' | ||
|
||
import {ux} from '../../index' | ||
const info = (output: string) => stdout.write(format(output) + '\n') | ||
|
||
export default function styledJSON(obj: unknown): void { | ||
const json = JSON.stringify(obj, null, 2) | ||
if (!chalk.level) { | ||
ux.info(json) | ||
info(json) | ||
return | ||
} | ||
|
||
const cardinal = require('cardinal') | ||
const theme = require('cardinal/themes/jq') | ||
ux.info(cardinal.highlight(json, {json: true, theme})) | ||
info(cardinal.highlight(json, {json: true, theme})) | ||
} |
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 |
---|---|---|
|
@@ -25,7 +25,7 @@ import {LoadOptions} from './interfaces/config' | |
import {PJSON} from './interfaces' | ||
import {Plugin} from './interfaces/plugin' | ||
import {PrettyPrintableError} from './errors' | ||
import {boolean} from './flags' | ||
import {aggregateFlags} from './util/aggregate-flags' | ||
import chalk from 'chalk' | ||
import {fileURLToPath} from 'node:url' | ||
import {ux} from './cli-ux' | ||
|
@@ -42,13 +42,6 @@ stdout.on('error', (err: any) => { | |
throw err | ||
}) | ||
|
||
const jsonFlag = { | ||
json: boolean({ | ||
description: 'Format output as json.', | ||
helpGroup: 'GLOBAL', | ||
}), | ||
} | ||
|
||
/** | ||
* An abstract class which acts as the base for each command | ||
* in your project. | ||
|
@@ -126,37 +119,7 @@ export abstract class Command { | |
|
||
public static hasDynamicHelp = false | ||
|
||
protected static '_--' = false | ||
|
||
protected static _enableJsonFlag = false | ||
|
||
public static get enableJsonFlag(): boolean { | ||
return this._enableJsonFlag | ||
} | ||
|
||
public static set enableJsonFlag(value: boolean) { | ||
this._enableJsonFlag = value | ||
if (value === true) { | ||
this.baseFlags = jsonFlag | ||
} else { | ||
delete this.baseFlags?.json | ||
this.flags = {} // force the flags setter to run | ||
delete this.flags?.json | ||
} | ||
} | ||
|
||
public static get '--'(): boolean { | ||
return Command['_--'] | ||
} | ||
|
||
public static set '--'(value: boolean) { | ||
Command['_--'] = value | ||
} | ||
|
||
public get passThroughEnabled(): boolean { | ||
return Command['_--'] | ||
} | ||
|
||
public static enableJsonFlag = false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this whole file is a LOT better. I know all the complexity moved to the prototype crawler, but that's good for consumers. |
||
/** | ||
* instantiate and run the command | ||
* | ||
|
@@ -184,29 +147,10 @@ export abstract class Command { | |
return cmd._run<ReturnType<T['run']>>() | ||
} | ||
|
||
protected static _baseFlags: FlagInput | ||
|
||
static get baseFlags(): FlagInput { | ||
return this._baseFlags | ||
} | ||
|
||
static set baseFlags(flags: FlagInput) { | ||
// eslint-disable-next-line prefer-object-spread | ||
this._baseFlags = Object.assign({}, this.baseFlags, flags) | ||
this.flags = {} // force the flags setter to run | ||
} | ||
public static baseFlags: FlagInput | ||
|
||
/** A hash of flags for the command */ | ||
protected static _flags: FlagInput | ||
|
||
public static get flags(): FlagInput { | ||
return this._flags | ||
} | ||
|
||
public static set flags(flags: FlagInput) { | ||
// eslint-disable-next-line prefer-object-spread | ||
this._flags = Object.assign({}, this._flags ?? {}, this.baseFlags, flags) | ||
} | ||
public static flags: FlagInput | ||
|
||
public id: string | undefined | ||
|
||
|
@@ -284,16 +228,19 @@ export abstract class Command { | |
* @returns {boolean} true if the command supports json and the --json flag is present | ||
*/ | ||
public jsonEnabled(): boolean { | ||
// if the command doesn't support json, return false | ||
// If the command doesn't support json, return false | ||
if (!this.ctor.enableJsonFlag) return false | ||
// if the command parameter pass through is enabled, return true if the --json flag is before the '--' separator | ||
if (this.passThroughEnabled) { | ||
const ptIndex = this.argv.indexOf('--') | ||
const jsonIndex = this.argv.indexOf('--json') | ||
return jsonIndex > -1 && (ptIndex === -1 || jsonIndex < ptIndex) | ||
} | ||
|
||
return this.argv.includes('--json') || this.config.scopedEnvVar?.('CONTENT_TYPE')?.toLowerCase() === 'json' | ||
// If the CONTENT_TYPE env var is set to json, return true | ||
if (this.config.scopedEnvVar?.('CONTENT_TYPE')?.toLowerCase() === 'json') return true | ||
|
||
const passThroughIndex = this.argv.indexOf('--') | ||
const jsonIndex = this.argv.indexOf('--json') | ||
return passThroughIndex === -1 | ||
// If '--' is not present, then check for `--json` in this.argv | ||
? jsonIndex > -1 | ||
// If '--' is present, return true only the --json flag exists and is before the '--' | ||
: jsonIndex > -1 && jsonIndex < passThroughIndex | ||
} | ||
|
||
/** | ||
|
@@ -312,8 +259,13 @@ export abstract class Command { | |
} | ||
|
||
protected warnIfFlagDeprecated(flags: Record<string, unknown>): void { | ||
const allFlags = aggregateFlags( | ||
this.ctor.flags, | ||
this.ctor.baseFlags, | ||
this.ctor.enableJsonFlag, | ||
) | ||
for (const flag of Object.keys(flags)) { | ||
const flagDef = this.ctor.flags[flag] | ||
const flagDef = allFlags[flag] | ||
const deprecated = flagDef?.deprecated | ||
if (deprecated) { | ||
this.warn(formatFlagDeprecationWarning(flag, deprecated)) | ||
|
@@ -352,12 +304,22 @@ export abstract class Command { | |
} | ||
} | ||
|
||
protected async parse<F extends FlagOutput, B extends FlagOutput, A extends ArgOutput>(options?: Input<F, B, A>, argv = this.argv): Promise<ParserOutput<F, B, A>> { | ||
protected async parse<F extends FlagOutput, B extends FlagOutput, A extends ArgOutput>( | ||
options?: Input<F, B, A>, | ||
argv = this.argv, | ||
): Promise<ParserOutput<F, B, A>> { | ||
if (!options) options = this.ctor as Input<F, B, A> | ||
const opts = {context: this, ...options} | ||
// the spread operator doesn't work with getters so we have to manually add it here | ||
opts.flags = options?.flags | ||
opts.args = options?.args | ||
|
||
const opts = { | ||
context: this, | ||
...options, | ||
flags: aggregateFlags<F, B>( | ||
options.flags, | ||
options.baseFlags, | ||
options.enableJsonFlag, | ||
), | ||
} | ||
|
||
const results = await Parser.parse<F, B, A>(argv, opts) | ||
this.warnIfFlagDeprecated(results.flags ?? {}) | ||
|
||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this won't work (secrets:inherit won't pass across github enterprise boundaries).
This would work (passing secrets explicitly)