Skip to content
This repository has been archived by the owner on Jul 10, 2023. It is now read-only.

Commit

Permalink
Merge pull request #193 from salesforcecli/sm/fix-string-parsing
Browse files Browse the repository at this point in the history
fix: sfdx string flags parse
  • Loading branch information
iowillhoit authored Nov 21, 2022
2 parents e5981e2 + e598599 commit fc19c47
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
47 changes: 47 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Attach",
"port": 9229,
"skipFiles": ["<node_internals>/**"]
},
{
"name": "Run All Tests",
"type": "node",
"request": "launch",
"protocol": "inspector",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": ["--inspect", "--no-timeouts", "--colors", "test/**/*.test.ts"],
"env": {
"NODE_ENV": "development",
"SFDX_ENV": "development"
},
"sourceMaps": true,
"smartStep": true,
"internalConsoleOptions": "openOnSessionStart",
"preLaunchTask": "Compile"
},
{
"type": "node",
"request": "launch",
"name": "Run Current Test",
"sourceMaps": true,
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": ["--inspect", "--no-timeouts", "--colors", "${file}"],
"env": {
"NODE_ENV": "development",
"SFDX_ENV": "development",
"OCLIF_TS_NODE": "0"
},
"smartStep": true,
"internalConsoleOptions": "openOnSessionStart",
"preLaunchTask": "Compile"
}
]
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@
"publishConfig": {
"access": "public"
}
}
}
8 changes: 4 additions & 4 deletions src/sfdxFlags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
Omit,
Optional,
} from '@salesforce/ts-types';
import { CustomOptionFlag } from '@oclif/core/lib/interfaces/parser';
import { CustomOptionFlag, FlagParser } from '@oclif/core/lib/interfaces/parser';
import { Deprecation } from './ux';

Messages.importMessagesDirectory(__dirname);
Expand Down Expand Up @@ -108,7 +108,7 @@ function merge<T>(
function option<T>(
kind: flags.Kind,
options: flags.Option<T>,
parse: (val: string, ctx: unknown) => Promise<T>
parse: ((val: string, ctx: unknown) => Promise<T>) | FlagParser<T, string>
): flags.Discriminated<flags.Option<T>> {
const flag = OclifFlags.option({ ...options, parse });
return merge<T>(kind, flag, options);
Expand Down Expand Up @@ -221,7 +221,7 @@ function buildOption<T>(
}

function buildString(options: flags.String): flags.Discriminated<flags.String> {
return option('string', options, (val: string) => Promise.resolve(val));
return option('string', options, options.parse ?? ((val: string) => Promise.resolve(val)));
}

function buildVersion(options?: flags.BaseBoolean<boolean>): flags.Discriminated<flags.Boolean<void>> {
Expand Down Expand Up @@ -281,7 +281,7 @@ function buildMappedArray<T>(kind: flags.Kind, options: flags.MappedArray<T>): f
function buildStringArray(kind: flags.Kind, options: flags.Array<string>): flags.Discriminated<flags.Option<string[]>> {
const { options: values, ...rest } = options;
const allowed = new Set(values);
return option(kind, rest, (val): Promise<string[]> => {
return option(kind, rest, (val: string): Promise<string[]> => {
const vals = convertArrayFlagToArray(val, options.delimiter);
validateArrayValues(kind, val, vals, options.validate);
validateArrayOptions(kind, val, vals, allowed);
Expand Down
12 changes: 12 additions & 0 deletions test/unit/sfdxFlags.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -561,4 +561,16 @@ describe('SfdxFlags', () => {
});
});
});

describe('parse', () => {
it('parse on string flag', async () => {
const flag = flags.string({
description: 'test',
parse: (input: string) => Promise.resolve(input.toUpperCase()),
});
if (!hasFunction(flag, 'parse')) throw new MissingPropertyError('parse', 'array');

expect(await flag.parse('foo', undefined, undefined)).to.equal('FOO');
});
});
});

0 comments on commit fc19c47

Please sign in to comment.