Skip to content

Commit

Permalink
feat: import messages with 2 fewer imports
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Dec 7, 2023
1 parent 32e6641 commit 43e0739
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import * as util from 'node:util';
import { fileURLToPath } from 'node:url';
import { AnyJson, asString, ensureJsonMap, ensureString, isJsonMap, isObject } from '@salesforce/ts-types';
import { ensureArray, NamedError, upperFirst } from '@salesforce/kit';
import { SfError } from './sfError';
Expand Down Expand Up @@ -172,6 +173,9 @@ const jsAndJsonLoader: FileParser = (filePath: string, fileContents: string): St
* // Create loader functions for all files in the messages directory
* Messages.importMessagesDirectory(__dirname);
*
* // or, for ESM code
* Messages.importMessagesDirectoryFromMetaUrl(import.meta.url)
*
* // Now you can use the messages from anywhere in your code or file.
* // If using importMessageDirectory, the bundle name is the file name.
* const messages: Messages = Messages.loadMessages(packageName, bundleName);
Expand Down Expand Up @@ -300,6 +304,22 @@ export class Messages<T extends string> {
}
}

/**
* Support ESM plugins who can't use __dirname
*
* @param metaUrl pass in `import.meta.url`
* @param truncateToProjectPath 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.
* @param packageName The npm package name. Figured out from the root directory's package.json.
*/
public static importMessagesDirectoryFromMetaUrl(
metaUrl: string,
truncateToProjectPath = true,
packageName?: string
): void {
return Messages.importMessagesDirectory(path.dirname(fileURLToPath(metaUrl)), truncateToProjectPath, packageName);
}

/**
* Import all json and js files in a messages directory. Use the file name as the bundle key when
* {@link Messages.loadMessages} is called. By default, we're assuming the moduleDirectoryPart is a
Expand Down

2 comments on commit 43e0739

@svc-cli-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logger Benchmarks - ubuntu-latest

Benchmark suite Current: 43e0739 Previous: 1e405c0 Ratio
Child logger creation 481077 ops/sec (±0.72%) 480258 ops/sec (±2.09%) 1.00
Logging a string on root logger 749309 ops/sec (±7.93%) 789628 ops/sec (±6.77%) 1.05
Logging an object on root logger 557452 ops/sec (±7.90%) 583739 ops/sec (±6.50%) 1.05
Logging an object with a message on root logger 13357 ops/sec (±197.27%) 9114 ops/sec (±204.59%) 0.68
Logging an object with a redacted prop on root logger 430078 ops/sec (±9.20%) 433663 ops/sec (±9.81%) 1.01
Logging a nested 3-level object on root logger 355432 ops/sec (±8.07%) 360522 ops/sec (±7.40%) 1.01

This comment was automatically generated by workflow using github-action-benchmark.

@svc-cli-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logger Benchmarks - windows-latest

Benchmark suite Current: 43e0739 Previous: 1e405c0 Ratio
Child logger creation 326171 ops/sec (±0.72%) 316867 ops/sec (±0.82%) 0.97
Logging a string on root logger 697714 ops/sec (±6.14%) 771190 ops/sec (±6.72%) 1.11
Logging an object on root logger 536010 ops/sec (±5.45%) 645520 ops/sec (±6.63%) 1.20
Logging an object with a message on root logger 15124 ops/sec (±186.66%) 3443 ops/sec (±223.65%) 0.23
Logging an object with a redacted prop on root logger 417471 ops/sec (±9.10%) 504648 ops/sec (±6.26%) 1.21
Logging a nested 3-level object on root logger 320087 ops/sec (±6.80%) 353608 ops/sec (±5.01%) 1.10

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.