Skip to content

Commit

Permalink
feat: use new inquirer (#902)
Browse files Browse the repository at this point in the history
* feat: use new inquirer

* fix: use new sf-plugins-core prompts

* refactor: real plugins-core, use built-in confirm prompt

* chore: bump deps
  • Loading branch information
mshanemc authored Jan 10, 2024
1 parent 9f96395 commit 113d56a
Show file tree
Hide file tree
Showing 21 changed files with 516 additions and 659 deletions.
12 changes: 1 addition & 11 deletions command-snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,7 @@
{
"command": "org:login:device",
"plugin": "@salesforce/plugin-auth",
"flags": [
"alias",
"client-id",
"disable-masking",
"instance-url",
"json",
"loglevel",
"set-default",
"set-default-dev-hub"
],
"flags": ["alias", "client-id", "instance-url", "json", "loglevel", "set-default", "set-default-dev-hub"],
"alias": ["force:auth:device:login", "auth:device:login"],
"flagChars": ["a", "d", "i", "r", "s"],
"flagAliases": [
Expand Down Expand Up @@ -111,7 +102,6 @@
"alias",
"browser",
"client-id",
"disable-masking",
"instance-url",
"json",
"loglevel",
Expand Down
4 changes: 0 additions & 4 deletions messages/messages.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ Do you want to authorize this org for use with the Salesforce CLI?

Don't prompt for confirmation.

# flags.disable-masking.summary

Disable masking of user input; use with problematic terminals.

# clientSecretStdin

OAuth client secret of personal connected app? Press Enter if it's not required.
Expand Down
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
"author": "Salesforce",
"bugs": "https://github.com/forcedotcom/cli/issues",
"dependencies": {
"@oclif/core": "^3.16.0",
"@salesforce/core": "^6.4.4",
"@inquirer/checkbox": "^1.5.0",
"@inquirer/select": "^1.3.1",
"@oclif/core": "^3.18.0",
"@salesforce/core": "^6.4.6",
"@salesforce/kit": "^3.0.15",
"@salesforce/sf-plugins-core": "^5.0.13",
"@salesforce/sf-plugins-core": "^7.1.1",
"@salesforce/ts-types": "^2.0.9",
"chalk": "^5.3.0",
"open": "^9.1.0"
Expand All @@ -17,10 +19,10 @@
"@oclif/plugin-command-snapshot": "^5.0.5",
"@salesforce/cli-plugins-testkit": "^5.1.3",
"@salesforce/dev-scripts": "^8.2.0",
"@salesforce/plugin-command-reference": "^3.0.59",
"@salesforce/plugin-command-reference": "^3.0.61",
"@salesforce/ts-sinon": "^1.4.19",
"eslint-plugin-sf-plugin": "^1.17.0",
"oclif": "^4.1.0",
"eslint-plugin-sf-plugin": "^1.17.1",
"oclif": "^4.2.0",
"shx": "0.3.4",
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
Expand Down
54 changes: 0 additions & 54 deletions src/authBaseCommand.ts

This file was deleted.

10 changes: 3 additions & 7 deletions src/commands/org/list/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/



import { loglevel, SfCommand } from '@salesforce/sf-plugins-core';
import { AuthInfo, Messages, OrgAuthorization } from '@salesforce/core';
type AuthListResult = Omit<OrgAuthorization, 'aliases'> & { alias: string };
export type AuthListResults = AuthListResult[];
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url)
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-auth', 'list');

export default class ListAuth extends SfCommand<AuthListResults> {
Expand Down Expand Up @@ -40,16 +38,14 @@ export default class ListAuth extends SfCommand<AuthListResults> {
});

const hasErrors = auths.filter((auth) => !!auth.error).length > 0;
let columns = {
const columns = {
alias: { header: 'ALIAS' },
username: { header: 'USERNAME' },
orgId: { header: 'ORG ID' },
instanceUrl: { header: 'INSTANCE URL' },
oauthMethod: { header: 'AUTH METHOD' },
...(hasErrors ? { error: { header: 'ERROR' } } : {}),
};
if (hasErrors) {
columns = { ...columns, ...{ error: { header: 'ERROR' } } };
}
this.styledHeader('authenticated orgs');
this.table(mappedAuths, columns);
return mappedAuths;
Expand Down
24 changes: 12 additions & 12 deletions src/commands/org/login/access-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,23 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/



import { Flags, loglevel } from '@salesforce/sf-plugins-core';
import { Flags, loglevel, SfCommand } from '@salesforce/sf-plugins-core';
import { AuthFields, AuthInfo, Messages, matchesAccessToken, SfError, StateAggregator } from '@salesforce/core';
import { env } from '@salesforce/kit';
import { Interfaces } from '@oclif/core';
import { AuthBaseCommand } from '../../../authBaseCommand.js';
import { InferredFlags } from '@oclif/core/lib/interfaces';

Messages.importMessagesDirectoryFromMetaUrl(import.meta.url)
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-auth', 'accesstoken.store');
const commonMessages = Messages.loadMessages('@salesforce/plugin-auth', 'messages');

const ACCESS_TOKEN_FORMAT = '"<org id>!<accesstoken>"';

export default class LoginAccessToken extends AuthBaseCommand<AuthFields> {
export default class LoginAccessToken extends SfCommand<AuthFields> {
public static readonly summary = messages.getMessage('summary');
public static readonly description = messages.getMessage('description');
public static readonly examples = messages.getMessages('examples');
public static readonly deprecateAliases = true;
public static aliases = ['force:auth:accesstoken:store', 'auth:accesstoken:store'];
public static readonly aliases = ['force:auth:accesstoken:store', 'auth:accesstoken:store'];

public static readonly flags = {
'instance-url': Flags.url({
Expand Down Expand Up @@ -66,7 +63,7 @@ export default class LoginAccessToken extends AuthBaseCommand<AuthFields> {
loglevel,
};

private flags!: Interfaces.InferredFlags<typeof LoginAccessToken.flags>;
private flags!: InferredFlags<typeof LoginAccessToken.flags>;

public async run(): Promise<AuthFields> {
const { flags } = await this.parse(LoginAccessToken);
Expand Down Expand Up @@ -109,16 +106,19 @@ export default class LoginAccessToken extends AuthBaseCommand<AuthFields> {
if (!this.flags['no-prompt']) {
const stateAggregator = await StateAggregator.getInstance();
if (await stateAggregator.orgs.exists(username)) {
return this.confirm(messages.getMessage('overwriteAccessTokenAuthUserFile', [username]));
return this.confirm({ message: messages.getMessage('overwriteAccessTokenAuthUserFile', [username]) });
}
}
return true;
}

private async getAccessToken(): Promise<string> {
const accessToken =
env.getString('SF_ACCESS_TOKEN') ?? env.getString('SFDX_ACCESS_TOKEN') ?? (await this.askForAccessToken());

env.getString('SF_ACCESS_TOKEN') ??
env.getString('SFDX_ACCESS_TOKEN') ??
(this.flags['no-prompt'] === true
? '' // will throw when validating
: await this.secretPrompt({ message: commonMessages.getMessage('accessTokenStdin') }));
if (!matchesAccessToken(accessToken)) {
throw new SfError(messages.getMessage('invalidAccessTokenFormat', [ACCESS_TOKEN_FORMAT]));
}
Expand Down
19 changes: 6 additions & 13 deletions src/commands/org/login/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,22 @@
*/

import { AuthFields, AuthInfo, DeviceOauthService, Messages, OAuth2Config } from '@salesforce/core';
import { Flags, loglevel } from '@salesforce/sf-plugins-core';
import { Flags, SfCommand, loglevel } from '@salesforce/sf-plugins-core';
import { DeviceCodeResponse } from '@salesforce/core/lib/deviceOauthService.js';
import { ux } from '@oclif/core';
import { AuthBaseCommand } from '../../../authBaseCommand.js';
import { Common } from '../../../common.js';
import common from '../../../common.js';

Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-auth', 'device.login');
const commonMessages = Messages.loadMessages('@salesforce/plugin-auth', 'messages');

export type DeviceLoginResult = (AuthFields & DeviceCodeResponse) | Record<string, never>;

export default class LoginDevice extends AuthBaseCommand<DeviceLoginResult> {
export default class LoginDevice extends SfCommand<DeviceLoginResult> {
public static readonly summary = messages.getMessage('summary');
public static readonly description = messages.getMessage('description');
public static readonly examples = messages.getMessages('examples');
public static aliases = ['force:auth:device:login', 'auth:device:login'];
public static readonly aliases = ['force:auth:device:login', 'auth:device:login'];
public static readonly deprecateAliases = true;

public static readonly flags = {
Expand Down Expand Up @@ -57,21 +56,15 @@ export default class LoginDevice extends AuthBaseCommand<DeviceLoginResult> {
deprecateAliases: true,
aliases: ['setalias'],
}),
'disable-masking': Flags.boolean({
summary: commonMessages.getMessage('flags.disable-masking.summary'),
hidden: true,
deprecateAliases: true,
aliases: ['disablemasking'],
}),
loglevel,
};

public async run(): Promise<DeviceLoginResult> {
const { flags } = await this.parse(LoginDevice);
if (await this.shouldExitCommand(false)) return {};
if (await common.shouldExitCommand(false)) return {};

const oauthConfig: OAuth2Config = {
loginUrl: await Common.resolveLoginUrl(flags['instance-url']?.href),
loginUrl: await common.resolveLoginUrl(flags['instance-url']?.href),
clientId: flags['client-id'],
};

Expand Down
14 changes: 7 additions & 7 deletions src/commands/org/login/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import { Flags, loglevel } from '@salesforce/sf-plugins-core';
import { Flags, SfCommand, loglevel } from '@salesforce/sf-plugins-core';
import { AuthFields, AuthInfo, AuthRemover, Logger, Messages, SfError } from '@salesforce/core';
import { Interfaces } from '@oclif/core';
import { AuthBaseCommand } from '../../../authBaseCommand.js';
import { Common } from '../../../common.js';
import common from '../../../common.js';

Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-auth', 'jwt.grant');
const commonMessages = Messages.loadMessages('@salesforce/plugin-auth', 'messages');

export default class LoginJwt extends AuthBaseCommand<AuthFields> {
export default class LoginJwt extends SfCommand<AuthFields> {
public static readonly summary = messages.getMessage('summary');
public static readonly description = messages.getMessage('description');
public static readonly examples = messages.getMessages('examples');
public static aliases = ['force:auth:jwt:grant', 'auth:jwt:grant'];
public static readonly aliases = ['force:auth:jwt:grant', 'auth:jwt:grant'];
public static readonly deprecateAliases = true;

public static readonly flags = {
username: Flags.string({
// eslint-disable-next-line sf-plugin/dash-o
char: 'o',
summary: messages.getMessage('flags.username.summary'),
required: true,
Expand Down Expand Up @@ -87,7 +87,7 @@ export default class LoginJwt extends AuthBaseCommand<AuthFields> {
this.flags = flags;
let result: AuthFields = {};

if (await this.shouldExitCommand(flags['no-prompt'])) return {};
if (await common.shouldExitCommand(flags['no-prompt'])) return {};

try {
const authInfo = await this.initAuthInfo();
Expand Down Expand Up @@ -116,7 +116,7 @@ export default class LoginJwt extends AuthBaseCommand<AuthFields> {
privateKeyFile: this.flags['jwt-key-file'],
};

const loginUrl = await Common.resolveLoginUrl(this.flags['instance-url']?.href);
const loginUrl = await common.resolveLoginUrl(this.flags['instance-url']?.href);

const oauth2Options = loginUrl ? Object.assign(oauth2OptionsBase, { loginUrl }) : oauth2OptionsBase;

Expand Down
10 changes: 5 additions & 5 deletions src/commands/org/login/sfdx-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
*/

import fs from 'node:fs/promises';
import { Flags, loglevel } from '@salesforce/sf-plugins-core';
import { Flags, SfCommand, loglevel } from '@salesforce/sf-plugins-core';
import { AuthFields, AuthInfo, Messages } from '@salesforce/core';
import { AnyJson } from '@salesforce/ts-types';
import { parseJson } from '@salesforce/kit';
import { AuthBaseCommand } from '../../../authBaseCommand.js';
import common from '../../../common.js';

Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-auth', 'sfdxurl.store');
Expand All @@ -22,11 +22,11 @@ type AuthJson = AnyJson & {
result?: AnyJson & { sfdxAuthUrl: string };
sfdxAuthUrl: string;
};
export default class LoginSfdxUrl extends AuthBaseCommand<AuthFields> {
export default class LoginSfdxUrl extends SfCommand<AuthFields> {
public static readonly summary = messages.getMessage('summary');
public static readonly description = messages.getMessage('description', [AUTH_URL_FORMAT]);
public static readonly examples = messages.getMessages('examples');
public static aliases = ['force:auth:sfdxurl:store', 'auth:sfdxurl:store'];
public static readonly aliases = ['force:auth:sfdxurl:store', 'auth:sfdxurl:store'];
public static readonly deprecateAliases = true;

public static readonly flags = {
Expand Down Expand Up @@ -78,7 +78,7 @@ export default class LoginSfdxUrl extends AuthBaseCommand<AuthFields> {

public async run(): Promise<AuthFields> {
const { flags } = await this.parse(LoginSfdxUrl);
if (await this.shouldExitCommand(flags['no-prompt'])) return {};
if (await common.shouldExitCommand(flags['no-prompt'])) return {};

const authFile = flags['sfdx-url-file'];
const authStdin = flags['sfdx-url-stdin'];
Expand Down
Loading

0 comments on commit 113d56a

Please sign in to comment.