Create a new messages bundle.
Note: Use {Messages.loadMessages} unless you are writing your own loader function.
The bundle name.
The locale.
The messages. Can not be modified once created.
Readonly
bundleThe bundle name.
Readonly
localeThe locale of the messages in this bundle.
Readonly
messagesThe messages. Can not be modified once created.
Convenience method to create errors using message labels.
error.name
will be the upper-cased key, remove prefixed error.
and will always end in Error.
error.actions
will be loaded using ${key}.actions
if available.
The key of the error message.
The error message tokens.
The action messages tokens.
Optional
exitCodeOrCause: number | ErrorThe exit code which will be used by SfdxCommand or the underlying error that caused this error to be raised.
Optional
cause: ErrorThe underlying error that caused this error to be raised.
Convenience method to create info using message labels.
info.name
will be the upper-cased key, remove prefixed info.
and will always end in Info.
info.actions
will be loaded using ${key}.actions
if available.
Convenience method to create warning using message labels.
warning.name
will be the upper-cased key, remove prefixed warning.
and will always end in Warning.
warning.actions
will be loaded using ${key}.actions
if available.
Get a message using a message key and use the tokens as values for tokenization.
If the key happens to be an array of messages, it will combine with OS.EOL.
The key of the message.
The values to substitute in the message.
See https://nodejs.org/api/util.html#util_util_format_format_args Note Unlike util.format(), specifiers are required for a token to be rendered.
Get messages using a message key and use the tokens as values for tokenization.
This will return all messages if the key is an array in the messages file.
{
"myKey": [ "message1", "message2" ]
}
# myKey
* message1
* message2
The key of the messages.
The values to substitute in the message.
See https://nodejs.org/api/util.html#util_util_format_format_args Note Unlike util.format(), specifiers are required for a token to be rendered.
Static
generateGenerate a file loading function. Use Messages.importMessageFile unless overriding the bundleName is required, then manually pass the loader function to Messages.setLoaderFunction.
The name of the bundle.
The messages file path.
Static
getStatic
importAdd a single message file to the list of loading functions using the file name as the bundle name. The loader will only be added if the bundle name is not already taken.
The npm package name.
The path of the file.
Static
importImport all json and js files in a messages directory. Use the file name as the bundle key when Messages.loadMessages is called. By default, we're assuming the moduleDirectoryPart is a typescript project and will truncate to root path (where the package.json file is). If your messages directory is in another spot or you are not using typescript, pass in false for truncateToProjectPath.
// e.g. If your message directory is in the project root, you would do:
Messages.importMessagesDirectory(__dirname);
The path to load the messages folder.
Will look for the messages directory in the project root (where the package.json file is located). i.e., the module is typescript and the messages folder is in the top level of the module directory.
Optional
packageName: stringThe npm package name. Figured out from the root directory's package.json.
Static
importSupport ESM plugins who can't use __dirname
pass in import.meta.url
Will look for the messages directory in the project root (where the package.json file is located). i.e., the module is typescript and the messages folder is in the top level of the module directory.
Optional
packageName: stringThe npm package name. Figured out from the root directory's package.json.
Static
isStatic
loadLoad messages for a given package and bundle. If the bundle is not already cached, use the loader function created from Messages.setLoaderFunction or Messages.importMessagesDirectory.
Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('packageName', 'bundleName');
The name of the npm package.
The name of the bundle to load.
Static
setSet a custom loader function for a package and bundle that will be called on Messages.loadMessages.
The npm package name.
The name of the bundle.
The loader function.
The core message framework manages messages and allows them to be accessible by all plugins and consumers of sfdx-core. It is set up to handle localization down the road at no additional effort to the consumer. Messages can be used for anything from user output (like the console), to error messages, to returned data from a method.
Messages are loaded from loader functions. The loader functions will only run when a message is required. This prevents all messages from being loaded into memory at application startup. The functions can load from memory, a file, or a server.
In the beginning of your app or file, add the loader functions to be used later. If using json or js files in a root messages directory (
<moduleRoot>/messages
), load the entire directory automatically with Messages.importMessagesDirectory. Message files must be the following formates.A
.json
file:A
.js
file:A
.md
file:The values support util.format style strings that apply the tokens passed into Message.getMessage
Note: When running unit tests individually, you may see errors that the messages aren't found. This is because
index.js
isn't loaded when tests run like they are when the package is required. To allow tests to run, import the message directory in each test (it will only do it once) or load the message file the test depends on individually.