Skip to content

Commit 38f0a54

Browse files
authored
fix(compass-preferences-model): tell yargs about types for kebab case options (#3566)
Yargs is informed about the types of options where they can be determined, and performs camel-case expansion on options specified in kebab case. However, it does not apply those types in the latter case, making it so that `--showed-network-opt-in foo` would lead to a `false` value for `showedNetworkOptIn`, unlike `--showedNetworkOptIn foo`. Address this by also telling yargs about the kebab-case versions explicitly.
1 parent 1b82702 commit 38f0a54

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

package-lock.json

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/compass-preferences-model/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"ampersand-state": "5.0.3",
5353
"bson": "^4.7.0",
5454
"js-yaml": "^4.1.0",
55+
"lodash": "^4.17.21",
5556
"storage-mixin": "^5.1.0",
5657
"yargs-parser": "^21.1.1"
5758
},

packages/compass-preferences-model/src/global-config.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ describe('Global config file handling', function () {
8282
expect(result.cli).to.deep.equal({ enableMaps: true, theme: '' });
8383
});
8484

85+
it('knows the expected types of cli options when followed by an extra positional argument', async function () {
86+
const result = await parseAndValidateGlobalPreferences({
87+
globalConfigPaths: [],
88+
argv: ['--showed-network-opt-in', 'about:blank'],
89+
});
90+
expect(result.cli).to.deep.equal({ showedNetworkOptIn: true });
91+
});
92+
8593
it('ignores CLI options that should be ignored', async function () {
8694
const result = await parseAndValidateGlobalPreferences({
8795
globalConfigPaths: [],

packages/compass-preferences-model/src/global-config.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { EJSON } from 'bson';
44
import yaml from 'js-yaml';
55
import type { Options as YargsOptions } from 'yargs-parser';
66
import yargsParser from 'yargs-parser';
7+
import { kebabCase } from 'lodash';
78
import type { AmpersandType, AllPreferences } from './preferences';
89
import { allPreferencesProps } from './preferences';
910

@@ -91,9 +92,13 @@ const cliProps = Object.entries(allPreferencesProps).filter(
9192
([, definition]) => definition.cli
9293
);
9394
function getCliPropNamesByType(type: AmpersandType<any>): string[] {
94-
return cliProps
95-
.filter(([, definition]) => definition.type === type)
96-
.map(([key]) => key);
95+
return [
96+
...new Set(
97+
cliProps
98+
.filter(([, definition]) => definition.type === type)
99+
.flatMap(([key]) => [key, kebabCase(key)])
100+
),
101+
];
97102
}
98103

99104
const yargsOptions: YargsOptions = {
@@ -106,6 +111,7 @@ const yargsOptions: YargsOptions = {
106111
'parse-numbers': false,
107112
// We validate options, so we don't want to keep --export-connections if we get --exportConnections
108113
'strip-dashed': true,
114+
'camel-case-expansion': true,
109115
},
110116
};
111117

0 commit comments

Comments
 (0)