Skip to content

Commit

Permalink
feat!: use current inquirer (#479)
Browse files Browse the repository at this point in the history
* feat!: use current inquirer

BREAKING CHANGE: ESM-only

BREAKING CHANGE: removes cross-cloud hooks (deployer, deauthorizer, etc)

BREAKING CHANGE: removes Prompter module

BREAKING CHANGE: remove option to barrel-import from Ux

* refactor: changes from live code review with Mike

* chore: bump oclif/core

* refactor: handing process error from QA

* refactor: rename prompt stub for consistency

* docs: prompts

* chore: correct chalk types for esm/v5

* chore(release): 5.0.14-dev.0 [skip ci]

* refactor: move prompts to ux and export

* test: add plugin-settings for xnuts

* chore(release): 5.0.14-dev.1 [skip ci]

* refactor: restore exported namespace merge

* chore(release): 5.0.14-dev.2 [skip ci]

* docs: migration doc

* docs: pr feedback

* chore: set major version

---------

Co-authored-by: svc-cli-bot <svc_cli_bot@salesforce.com>
  • Loading branch information
mshanemc and svc-cli-bot authored Jan 5, 2024
1 parent d0051fb commit 77e33ab
Show file tree
Hide file tree
Showing 45 changed files with 656 additions and 1,779 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ jobs:
- plugin-signups
- plugin-templates
- plugin-custom-metadata
- plugin-settings
- plugin-community
- plugin-user
os:
Expand Down
5 changes: 3 additions & 2 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"require": "test/init.js, ts-node/register, source-map-support/register",
"require": "ts-node/register",
"watch-extensions": ["ts", "md"],
"watch-files": ["src", "test", "messages"],
"recursive": true,
"reporter": "spec",
"timeout": 5000
"timeout": 5000,
"node-option": ["loader=ts-node/esm"]
}
929 changes: 195 additions & 734 deletions CHANGELOG.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2023, Salesforce.com, Inc.
Copyright (c) 2024, Salesforce.com, Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Expand Down
45 changes: 45 additions & 0 deletions MIGRATING_V5-V6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Migrating from v5 to v6

## ESM

v6 is ESM-only, which can't be consumed by plugins written in cjs. Salesforce-owned plugins are ESM now and you can use them as a guide for the necessary changes.

## Prompts

This library uses [`inquirer`](https://github.com/SBoudrias/Inquirer.js) for interactivity. Inquirer made some large changes, including its own ESM rewrite. To take advantage of its improved performance and smaller dependency, we've upgraded.

The API is completely different, resulting in changes to sf-plugins-core. The new philopsophy is

1. provide limited, simplified prompts for common use cases
2. plugins that need more advanced propting should import the parts of inquirer that they need

### Changes

The `Prompter` class is removed.

SfCommand contains two prompt methods

1. `confirm` provides boolean confirmation prompts
2. `secretPrompt` takes masked string input from the user

Unlike the inquirer base prompts (`confirm` and `password`, respectively) these have a built-in default timeout. Both take an object parameter that lets you change the timeout (confirm previously took a series of parameters)

These methods are also built into the `stubPrompter` method for simplified test stubbing.

If your command relies heavily on the old inquirer/prompt structure, it's possible to import that as a dependency.

### Reorganized exports

There are more "standalone" exports available. See package.json#exports for options that don't invole importing the entire library.

Also removed is the "barrel of Ux". Import what you need.

## Breaking type changes

### SfCommand.project

Project was typed as an `SfProject` but could be undefined when `requiresProject` wasn't set to true on a command. It's now typed as `SfProject | undefined`. If your command sets `requiresProject = true` you can safely assert `this.project!`.

### SfCommand.catch

Catch was previously typed to return an error, but it always threw the error. It's now properly typed as `never`. If you extended `catch`, your method should also return `never`.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ The SfCommand abstract class extends [@oclif/core's Command class](https://githu
- Provides functions that help place success messages, warnings and errors into the correct location in JSON results
- Enables additional help sections to the standard oclif command help output
- Provides access to the [cli-ux cli actions](https://github.com/oclif/cli-ux#cliaction). This avoids having to import that interface from cli-ux and manually handling the `--json` flag.

## Prompter Class

A general purpose class that prompts a user for information. See [inquirer NPM Module](https://www.npmjs.com/package/inquirer) for more information.
- Provides simple, stubbable prompts for confirmation and secrets

## Flags

Expand Down
24 changes: 15 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"name": "@salesforce/sf-plugins-core",
"version": "5.0.13",
"version": "6.0.0",
"description": "Utils for writing Salesforce CLI plugins",
"main": "lib/exported",
"types": "lib/exported.d.ts",
"type": "module",
"license": "BSD-3-Clause",
"scripts": {
"build": "wireit",
Expand All @@ -21,6 +22,13 @@
"test": "wireit",
"test:only": "wireit"
},
"exports": {
"./SfCommand": "./lib/SfCommand.js",
"./Flags": "./lib/flags/flags.js",
"./Ux": "./lib/ux/ux.js",
"./StandardColors": "./lib/ux/standardColors.js",
".": "./lib/exported.js"
},
"repository": "salesforcecli/sf-plugins-core",
"bugs": {
"url": "https://github.com/salesforcecli/sf-plugins-core/issues"
Expand All @@ -35,22 +43,20 @@
"node": ">=18.0.0"
},
"dependencies": {
"@oclif/core": "^3.15.1",
"@inquirer/confirm": "^2.0.15",
"@inquirer/password": "^1.1.14",
"@oclif/core": "^3.16.0",
"@salesforce/core": "^6.4.2",
"@salesforce/kit": "^3.0.15",
"@salesforce/ts-types": "^2.0.9",
"@types/inquirer": "^8.2.3",
"chalk": "^4",
"inquirer": "^8.2.5"
"chalk": "^5.3.0"
},
"devDependencies": {
"@oclif/test": "^3.1.5",
"@inquirer/type": "^1.1.5",
"@salesforce/dev-scripts": "^8.1.3",
"eslint-plugin-sf-plugin": "^1.17.0",
"shelljs": "0.8.5",
"strip-ansi": "6.0.1",
"ts-node": "^10.9.2",
"typescript": "^4.9.5"
"typescript": "^5.3.3"
},
"publishConfig": {
"access": "public"
Expand Down
6 changes: 3 additions & 3 deletions src/compatibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

import { Flags } from '@oclif/core';
import { Lifecycle, Messages } from '@salesforce/core';
import { orgApiVersionFlag } from './flags/orgApiVersion';
import { optionalHubFlag, optionalOrgFlag, requiredHubFlag, requiredOrgFlag } from './flags/orgFlags';
import { orgApiVersionFlag } from './flags/orgApiVersion.js';
import { optionalHubFlag, optionalOrgFlag, requiredHubFlag, requiredOrgFlag } from './flags/orgFlags.js';

/**
* Adds an alias for the deprecated sfdx-style "apiversion" and provides a warning if it is used
Expand All @@ -31,7 +31,7 @@ export const orgApiVersionFlagWithDeprecations = orgApiVersionFlag({
deprecateAliases: true,
});

Messages.importMessagesDirectory(__dirname);
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/sf-plugins-core', 'messages');
/**
* Use only for commands that maintain sfdx compatibility.
Expand Down
50 changes: 0 additions & 50 deletions src/deauthorizer.ts

This file was deleted.

110 changes: 0 additions & 110 deletions src/deployer.ts

This file was deleted.

56 changes: 56 additions & 0 deletions src/errorFormatting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2023, salesforce.com, inc.
* All rights reserved.
* 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 { Mode, Messages, envVars } from '@salesforce/core';
import type { ChalkInstance } from 'chalk';
import { StandardColors } from './ux/standardColors.js';
import { SfCommandError } from './types.js';

Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/sf-plugins-core', 'messages');

/**
* Format errors and actions for human consumption. Adds 'Error (<ErrorCode>):',
* When there are actions, we add 'Try this:' in blue
* followed by each action in red on its own line.
* If Error.code is present it is output last in parentheses
*
* @returns {string} Returns decorated messages.
*/

/**
* Utility function to format actions lines
*
* @param actions
* @param options
* @private
*/
export const formatActions = (
actions: string[],
options: { actionColor: ChalkInstance } = { actionColor: StandardColors.info }
): string[] =>
actions.length
? [
`\n${StandardColors.info(messages.getMessage('actions.tryThis'))}\n`,
...actions.map((a) => `${options.actionColor(a)}`),
]
: [];

export const formatError = (error: SfCommandError): string =>
[
`${formatErrorPrefix(error)} ${error.message}`,
...formatActions(error.actions ?? []),
error.stack && envVars.getString('SF_ENV') === Mode.DEVELOPMENT
? StandardColors.info(`\n*** Internal Diagnostic ***\n\n${error.stack}\n******\n`)
: [],
].join('\n');

const formatErrorPrefix = (error: SfCommandError): string =>
`${StandardColors.error(messages.getMessage('error.prefix', [formatErrorCode(error)]))}`;

const formatErrorCode = (error: SfCommandError): string =>
typeof error.code === 'string' || typeof error.code === 'number' ? ` (${error.code})` : '';
Loading

0 comments on commit 77e33ab

Please sign in to comment.