-
-
Notifications
You must be signed in to change notification settings - Fork 10
feat: add --categories flag for manifest selection
#188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dreyfus92
wants to merge
3
commits into
e18e:main
Choose a base branch
from
dreyfus92:feat/maniflags
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import type {ManifestModule} from 'module-replacements'; | ||
| import { | ||
| nativeReplacements, | ||
| preferredReplacements, | ||
| microUtilsReplacements, | ||
| all | ||
| } from 'module-replacements'; | ||
|
|
||
| export const VALID_CATEGORIES = [ | ||
| 'native', | ||
| 'preferred', | ||
| 'micro-utilities', | ||
| 'all' | ||
| ] as const; | ||
|
|
||
| export type Category = (typeof VALID_CATEGORIES)[number]; | ||
|
|
||
| export type CategoryKey = Exclude<Category, 'all'>; | ||
|
|
||
| export type ParsedCategories = 'all' | Set<CategoryKey>; | ||
|
|
||
| export function parseCategories(raw: string | undefined): ParsedCategories { | ||
| const normalized = raw?.trim() ?? ''; | ||
| if (normalized === '' || normalized === 'all') { | ||
| return 'all'; | ||
| } | ||
|
|
||
| const segments = normalized | ||
| .split(',') | ||
| .map((s) => s.trim()) | ||
| .filter(Boolean); | ||
| if (segments.length === 0) { | ||
| return 'all'; | ||
| } | ||
|
|
||
| const invalid: string[] = []; | ||
| const parsed = new Set<CategoryKey>(); | ||
|
|
||
| for (const segment of segments) { | ||
| if (segment === 'all') { | ||
| return 'all'; | ||
| } | ||
| if (VALID_CATEGORIES.includes(segment as Category)) { | ||
| parsed.add(segment as CategoryKey); | ||
| } else { | ||
| invalid.push(segment); | ||
| } | ||
| } | ||
|
|
||
| if (invalid.length > 0) { | ||
| throw new Error( | ||
| `Invalid categories: ${invalid.join(', ')}. Valid values are: ${VALID_CATEGORIES.join(', ')}.` | ||
| ); | ||
| } | ||
|
|
||
| return parsed; | ||
| } | ||
|
|
||
| const MANIFEST_BY_CATEGORY: Record<CategoryKey, ManifestModule> = { | ||
| native: nativeReplacements, | ||
| preferred: preferredReplacements, | ||
| 'micro-utilities': microUtilsReplacements | ||
| }; | ||
|
|
||
| export function getManifestForCategories( | ||
| parsed: ParsedCategories | ||
| ): ManifestModule { | ||
| if (parsed === 'all') { | ||
| return all; | ||
| } | ||
|
|
||
| const manifest: ManifestModule = { | ||
| mappings: {}, | ||
| replacements: {} | ||
| }; | ||
|
|
||
| for (const cat of parsed) { | ||
| const m = MANIFEST_BY_CATEGORY[cat]; | ||
| Object.assign(manifest.mappings, m.mappings); | ||
| Object.assign(manifest.replacements, m.replacements); | ||
| } | ||
|
|
||
| return manifest; | ||
| } |
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import {describe, it, expect} from 'vitest'; | ||
| import { | ||
| parseCategories, | ||
| getManifestForCategories, | ||
| VALID_CATEGORIES | ||
| } from '../categories.js'; | ||
|
|
||
| describe('parseCategories', () => { | ||
| it('returns "all" for undefined', () => { | ||
| expect(parseCategories(undefined)).toBe('all'); | ||
| }); | ||
|
|
||
| it('returns "all" for empty string', () => { | ||
| expect(parseCategories('')).toBe('all'); | ||
| }); | ||
|
|
||
| it('returns "all" for whitespace-only string', () => { | ||
| expect(parseCategories(' ')).toBe('all'); | ||
| }); | ||
|
|
||
| it('returns "all" for literal "all"', () => { | ||
| expect(parseCategories('all')).toBe('all'); | ||
| }); | ||
|
|
||
| it('returns "all" for "all" with whitespace', () => { | ||
| expect(parseCategories(' all ')).toBe('all'); | ||
| }); | ||
|
|
||
| it('returns single category as Set', () => { | ||
| expect(parseCategories('native')).toEqual(new Set(['native'])); | ||
| expect(parseCategories('preferred')).toEqual(new Set(['preferred'])); | ||
| expect(parseCategories('micro-utilities')).toEqual( | ||
| new Set(['micro-utilities']) | ||
| ); | ||
| }); | ||
|
|
||
| it('returns multiple categories for comma-separated list', () => { | ||
| expect(parseCategories('native,preferred')).toEqual( | ||
| new Set(['native', 'preferred']) | ||
| ); | ||
| expect(parseCategories('native, preferred , micro-utilities')).toEqual( | ||
| new Set(['native', 'preferred', 'micro-utilities']) | ||
| ); | ||
| }); | ||
|
|
||
| it('deduplicates categories', () => { | ||
| expect(parseCategories('native,native,preferred')).toEqual( | ||
| new Set(['native', 'preferred']) | ||
| ); | ||
| }); | ||
|
|
||
| it('throws for invalid category', () => { | ||
| expect(() => parseCategories('foo')).toThrow( | ||
| /Invalid categories: foo\. Valid values are:/ | ||
| ); | ||
| expect(() => parseCategories('foo')).toThrow( | ||
| new RegExp(VALID_CATEGORIES.join(', ')) | ||
| ); | ||
| }); | ||
|
|
||
| it('throws for invalid category in comma-separated list', () => { | ||
| expect(() => parseCategories('native,invalid,preferred')).toThrow( | ||
| /Invalid categories: invalid\. Valid values are:/ | ||
| ); | ||
| }); | ||
|
|
||
| it('treats empty segments after split as omitted', () => { | ||
| expect(parseCategories('native,,preferred')).toEqual( | ||
| new Set(['native', 'preferred']) | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getManifestForCategories', () => { | ||
| it('returns merged manifest for "all"', () => { | ||
| const manifest = getManifestForCategories('all'); | ||
| expect(manifest).toHaveProperty('mappings'); | ||
| expect(manifest).toHaveProperty('replacements'); | ||
| expect(Object.keys(manifest.mappings).length).toBeGreaterThan(0); | ||
| expect(Object.keys(manifest.replacements).length).toBeGreaterThan(0); | ||
| }); | ||
|
|
||
| it('returns manifest for single category', () => { | ||
| const nativeManifest = getManifestForCategories(new Set(['native'])); | ||
| expect(nativeManifest).toHaveProperty('mappings'); | ||
| expect(nativeManifest).toHaveProperty('replacements'); | ||
| expect(Object.keys(nativeManifest.mappings).length).toBeGreaterThanOrEqual( | ||
| 0 | ||
| ); | ||
| }); | ||
|
|
||
| it('returns merged manifest for multiple categories', () => { | ||
| const manifest = getManifestForCategories(new Set(['native', 'preferred'])); | ||
| expect(manifest).toHaveProperty('mappings'); | ||
| expect(manifest).toHaveProperty('replacements'); | ||
| const allManifest = getManifestForCategories('all'); | ||
| expect(Object.keys(manifest.mappings).length).toBeLessThanOrEqual( | ||
| Object.keys(allManifest.mappings).length | ||
| ); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.