feat: expose the extractor as a library - #23
Merged
Conversation
Declare an exports map so the package can be imported, add buildLabels() which returns the tree without writing, and make the placeholder for an unknown label configurable via a fallback receiving the full key path. Also await the write in processFiles, and make its 'no existing source file' branch reachable — readFile threw before it could be hit.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The package ships
dist/index.jstoday —tsdownalready buildssrc/index.tsas an entry — but declares only abin, so nothing can import it. This makes the extractor usable as a library.Motivation: Evolve's storefront needs to combine extracted labels from a project's own source with the default messages its installed feature packages ship, then decide the output itself. That needs the extraction step without the read-merge-write around it.
Changes
exportsmap. Points at the already-builtdist/index.js/dist/index.d.ts.buildLabels({ input, source, fallback, onFile })returns the label tree and writes nothing:processFilesis now the read-merge-write wrapper around it, unchanged in behaviour.A
fallbackoption for a label with no entry in the source. It receives the full key path (["Cart", "nested", "deep"]) and defaults to the last segment — exactly what was hardcoded before, so existing output is unchanged. We want[label]rather thanlabel, because a key likefreehasfreeas a plausible English translation and nothing could then tell a finished string from an untouched one.onFileso the library stays silent and the CLI keeps its per-file logging.Two fixes, easy to drop if you'd rather they were separate
processFilesdid notawaititswriteFile, so the process could exit before the write flushed.if (!sourceFile)"no existing source file found" branch was unreachable —readFilethrows on a missing file, so the CLI errored instead of building from scratch. Now it reads through atry, and an empty file is treated as no labels rather than a JSON parse error.Testing
Existing snapshot tests pass unchanged, which is the evidence the refactor preserved behaviour. Added coverage for the default fallback, a custom fallback and the paths it receives, source values winning over the fallback, and
buildLabelsreturning without writing.18 tests pass;
pnpm checkis clean.