Skip to content
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

feat: update custom flag definitions #67

Merged
merged 6 commits into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ jobs:
name: Install dependencies
command: yarn
- run: yarn remove @salesforce/sf-plugins-core
- run: yarn upgrade @oclif/core@latest
# windows/powershell does envs differently so we have to have conditional steps
- when:
condition:
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@salesforce/sf-plugins-core",
"version": "1.13.2",
"version": "1.14.1",
"description": "Utils for writing deploy and retrieve plugins",
"main": "lib/exported",
"types": "lib/exported.d.ts",
Expand Down Expand Up @@ -32,7 +32,7 @@
"/messages"
],
"dependencies": {
"@oclif/core": "^1.13.9",
"@oclif/core": "^1.15.0",
"@salesforce/core": "^3.25.0",
"@salesforce/kit": "^1.5.44",
"@salesforce/ts-types": "^1.5.20",
Expand Down
26 changes: 9 additions & 17 deletions src/flags/duration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { Flags, Interfaces } from '@oclif/core';
import { Flags } from '@oclif/core';
import { Messages } from '@salesforce/core';
import { Duration } from '@salesforce/kit';

Expand All @@ -13,12 +13,12 @@ const messages = Messages.loadMessages('@salesforce/sf-plugins-core', 'messages'

type DurationUnit = Lowercase<keyof typeof Duration.Unit>;

export interface DurationFlagConfig extends Partial<Interfaces.OptionFlag<Duration>> {
export type DurationFlagConfig = {
unit: Required<DurationUnit>;
defaultValue?: number;
min?: number;
max?: number;
}
};

/**
* Duration flag with built-in default and min/max validation
Expand All @@ -37,20 +37,12 @@ export interface DurationFlagConfig extends Partial<Interfaces.OptionFlag<Durati
* }),
* }
*/
export function durationFlag(
durationConfig: DurationFlagConfig & ({ required: true } | { default: Interfaces.Default<Duration> })
): Interfaces.OptionFlag<Duration>;
export function durationFlag(durationConfig: DurationFlagConfig): Interfaces.OptionFlag<Duration | undefined>;
export function durationFlag(
durationConfig: DurationFlagConfig
): Interfaces.OptionFlag<Duration> | Interfaces.OptionFlag<Duration | undefined> {
const { defaultValue, min, max, unit, ...baseProps } = durationConfig;
return Flags.build({
...baseProps,
parse: async (input: string) => validate(input, { min, max, unit }),
default: defaultValue ? async () => toDuration(defaultValue, unit) : undefined,
})();
}
export const durationFlag = Flags.custom<Duration, DurationFlagConfig>({
parse: async (input, _, opts) => validate(input, opts),
default: async (context) => {
return context.options.defaultValue ? toDuration(context.options.defaultValue, context.options.unit) : undefined;
},
});

const validate = (input: string, config: DurationFlagConfig): Duration => {
const { min, max, unit } = config || {};
Expand Down
4 changes: 2 additions & 2 deletions src/flags/orgApiVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export const maxDeprecatedUrl = 'https://help.salesforce.com/s/articleView?id=00
* CAVEAT: unlike the apiversion flag on sfdxCommand, this does not set the version on the org/connection
* We leave this up to the plugins to implement
*/
export const orgApiVersionFlag = Flags.build({
parse: async (input: string) => validate(input),
export const orgApiVersionFlag = Flags.custom({
parse: async (input) => validate(input),
default: async () => await getDefaultFromConfig(),
description: messages.getMessage('flags.apiVersion.description'),
});
Expand Down
6 changes: 3 additions & 3 deletions src/flags/orgFlags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const getHubOrThrow = async (aliasOrUsername?: string): Promise<Org> => {
* }),
* }
*/
export const optionalOrgFlag = Flags.build({
export const optionalOrgFlag = Flags.custom({
char: 'e',
parse: async (input: string | undefined) => await maybeGetOrg(input),
default: async () => await maybeGetOrg(),
Expand All @@ -89,7 +89,7 @@ export const optionalOrgFlag = Flags.build({
* }),
* }
*/
export const requiredOrgFlag = Flags.build({
export const requiredOrgFlag = Flags.custom({
char: 'e',
parse: async (input: string | undefined) => await getOrgOrThrow(input),
default: async () => await getOrgOrThrow(),
Expand All @@ -115,7 +115,7 @@ export const requiredOrgFlag = Flags.build({
* }),
* }
*/
export const requiredHubFlag = Flags.build({
export const requiredHubFlag = Flags.custom({
char: 'v',
parse: async (input: string | undefined) => await getHubOrThrow(input),
default: async () => await getHubOrThrow(),
Expand Down
24 changes: 7 additions & 17 deletions src/flags/salesforceId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { Flags, Interfaces } from '@oclif/core';
import { Flags } from '@oclif/core';
import { Messages, sfdc } from '@salesforce/core';

Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/sf-plugins-core', 'messages');

export interface IdFlagConfig extends Partial<Interfaces.OptionFlag<string>> {
export type IdFlagConfig = {
/**
* Can specify if the version must be 15 or 18 characters long. Leave blank to allow either 15 or 18.
*/
Expand All @@ -19,7 +19,7 @@ export interface IdFlagConfig extends Partial<Interfaces.OptionFlag<string>> {
* If the ID belongs to a certain sobject type, specify the 3 character prefix.
*/
startsWith?: string;
}
};

/**
* Id flag with built-in validation. Short character is `i`
Expand All @@ -40,20 +40,10 @@ export interface IdFlagConfig extends Partial<Interfaces.OptionFlag<string>> {
* }),
* }
*/
export function salesforceIdFlag(
inputs: IdFlagConfig & ({ required: true } | { default: Interfaces.Default<string> })
): Interfaces.OptionFlag<string>;
export function salesforceIdFlag(inputs?: IdFlagConfig): Interfaces.OptionFlag<string | undefined>;
export function salesforceIdFlag(
inputs: IdFlagConfig = {}
): Interfaces.OptionFlag<string> | Interfaces.OptionFlag<string | undefined> {
const { length, startsWith, ...baseProps } = inputs;
return Flags.build({
char: 'i',
...baseProps,
parse: async (input: string) => validate(input, { length, startsWith }),
})();
}
export const salesforceIdFlag = Flags.custom<string, IdFlagConfig>({
parse: async (input, _ctx, opts) => validate(input, opts),
char: 'i',
});

const validate = (input: string, config?: IdFlagConfig): string => {
const { length, startsWith } = config || {};
Expand Down
42 changes: 4 additions & 38 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -471,44 +471,10 @@
is-wsl "^2.1.1"
tslib "^2.0.0"

"@oclif/core@^1.13.9":
version "1.13.9"
resolved "https://registry.yarnpkg.com/@oclif/core/-/core-1.13.9.tgz#9e359c5fd93adcd10f45daf9dda2358e598fafe4"
integrity sha512-Ti82naHb8TpkMUX7WEiA9Ee0DmuMnm1WWm6L5CAIhbwFL8aysh6LyKIDfelDVN7xfVUJhYYB3ZqjzLmyt2eysw==
dependencies:
"@oclif/linewrap" "^1.0.0"
"@oclif/screen" "^3.0.2"
ansi-escapes "^4.3.2"
ansi-styles "^4.3.0"
cardinal "^2.1.1"
chalk "^4.1.2"
clean-stack "^3.0.1"
cli-progress "^3.10.0"
debug "^4.3.4"
ejs "^3.1.6"
fs-extra "^9.1.0"
get-package-type "^0.1.0"
globby "^11.1.0"
hyperlinker "^1.0.0"
indent-string "^4.0.0"
is-wsl "^2.2.0"
js-yaml "^3.14.1"
natural-orderby "^2.0.3"
object-treeify "^1.1.33"
password-prompt "^1.1.2"
semver "^7.3.7"
string-width "^4.2.3"
strip-ansi "^6.0.1"
supports-color "^8.1.1"
supports-hyperlinks "^2.2.0"
tslib "^2.3.1"
widest-line "^3.1.0"
wrap-ansi "^7.0.0"

"@oclif/core@^1.3.1":
version "1.13.0"
resolved "https://registry.yarnpkg.com/@oclif/core/-/core-1.13.0.tgz#65a0f6e2b4d939f654d484607b45e821146c40b5"
integrity sha512-/cn36jfnjUxodiJZEGHtGKkUAD15qeHHBC/+FiPkKQYAvGtcht3XRL3wDidOkp3awotN6DzxhHOu5ZrDKowmTQ==
"@oclif/core@^1.15.0", "@oclif/core@^1.3.1":
version "1.15.0"
resolved "https://registry.yarnpkg.com/@oclif/core/-/core-1.15.0.tgz#af015520456568a362a3592656b845d973494ffe"
integrity sha512-H+l0SybcYJiVPRXTu88TsEXNQZV9ZZ6k/xtiHbgE6LItPk77/st9HH4uI/IKK1nMOJS8KkxNmkLKyrcuiL7Bjw==
dependencies:
"@oclif/linewrap" "^1.0.0"
"@oclif/screen" "^3.0.2"
Expand Down