Repository navigation
feat(web,mobile): environment icons can be picked from a shared Lucide list #4
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
+1,650
−53
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4db741b
feat(web,mobile): environment icons can be picked from a shared Lucid…
amanthanvi 5c96c13
docs(web): name the check that backs the Lucide icon name cast
amanthanvi 7898327
refactor(mobile): drop the generator's unused --check mode
amanthanvi 64668ff
refactor(web,mobile): tighter Lucide paths and a parsed generator input
amanthanvi 3129537
refactor(web,mobile): one branch builds a colored Lucide or curated p…
amanthanvi 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,90 @@ | ||
| import * as NodeChildProcess from "node:child_process"; | ||
| import * as NodeFS from "node:fs"; | ||
| import * as NodeModule from "node:module"; | ||
| import * as NodePath from "node:path"; | ||
| import { ENVIRONMENT_LUCIDE_ICON_IDS } from "@t3tools/contracts"; | ||
| import * as Schema from "effect/Schema"; | ||
|
|
||
| const GENERATED_MODULE_PATH = NodePath.resolve( | ||
| import.meta.dirname, | ||
| "../src/components/environmentLucideIcons.generated.ts", | ||
| ); | ||
|
|
||
| // Path data comes from the same lucide-react build web draws, resolved from | ||
| // the web workspace so both clients stay on one icon version. | ||
| const webRequire = NodeModule.createRequire( | ||
| NodePath.resolve(import.meta.dirname, "../../web/package.json"), | ||
| ); | ||
| const lucideRoot = NodePath.dirname(webRequire.resolve("lucide-react/package.json")); | ||
| const lucideVersion = Schema.decodeUnknownSync( | ||
| Schema.fromJsonString(Schema.Struct({ version: Schema.String })), | ||
| )(NodeFS.readFileSync(NodePath.join(lucideRoot, "package.json"), "utf8")).version; | ||
|
|
||
| const decodeIconNode = Schema.decodeUnknownSync( | ||
| Schema.fromJsonString( | ||
| Schema.Array(Schema.Tuple([Schema.String, Schema.Record(Schema.String, Schema.String)])), | ||
| ), | ||
| ); | ||
|
|
||
| type IconNode = ReadonlyArray<readonly [string, Readonly<Record<string, string>>]>; | ||
|
|
||
| /** | ||
| * Reads the `__iconNode` array out of one lucide-react icon module. The | ||
| * module is plain ESM with a single array literal, parsed as JSON after | ||
| * quoting its keys, so nothing is executed. | ||
| */ | ||
| function readIconNode(id: string): IconNode { | ||
| const source = NodeFS.readFileSync( | ||
| NodePath.join(lucideRoot, "dist/esm/icons", `${id}.js`), | ||
| "utf8", | ||
| ); | ||
| const literal = /const __iconNode = (\[[\s\S]*?\]);\n/u.exec(source)?.[1]; | ||
| if (literal === undefined) throw new Error(`lucide-react icon ${id} has no __iconNode literal.`); | ||
| let parsed: ReturnType<typeof decodeIconNode>; | ||
| try { | ||
| parsed = decodeIconNode(literal.replaceAll(/(\w+): /gu, '"$1": ')); | ||
| } catch (cause) { | ||
| throw new Error(`lucide-react icon ${id} has an unexpected node.`, { cause }); | ||
| } | ||
| // React needs the key only to reconcile a list; mobile builds a static tree. | ||
| return parsed.map(([element, { key: _key, ...attributes }]) => [element, attributes]); | ||
| } | ||
|
|
||
| /** | ||
| * The module text, passed through the repository formatter so the committed | ||
| * file matches what the pre-commit hook would write and the freshness test | ||
| * compares like with like. | ||
| */ | ||
| export function renderEnvironmentLucideIconsModule(): string { | ||
| const entries = ENVIRONMENT_LUCIDE_ICON_IDS.map( | ||
| (id) => ` "${id}": ${JSON.stringify(readIconNode(id))},`, | ||
| ); | ||
| const raw = [ | ||
| "// Generated by scripts/generate-environment-lucide-icons.mts. Do not edit.", | ||
| `// Source: lucide-react ${lucideVersion}, ids from ENVIRONMENT_LUCIDE_ICON_IDS in contracts.`, | ||
| 'import type { EnvironmentLucideIconId } from "@t3tools/contracts";', | ||
| "", | ||
| "export type LucideIconNode = ReadonlyArray<", | ||
| " readonly [element: string, attributes: Readonly<Record<string, string>>]", | ||
| ">;", | ||
| "", | ||
| "export const ENVIRONMENT_LUCIDE_ICON_NODES: Record<EnvironmentLucideIconId, LucideIconNode> = {", | ||
| ...entries, | ||
| "};", | ||
| "", | ||
| ].join("\n"); | ||
| // The `vp` bin is a shell shim, so it runs through the shell, not node. | ||
| const formatted = NodeChildProcess.spawnSync( | ||
| NodePath.resolve(import.meta.dirname, "../../../node_modules/.bin/vp"), | ||
| ["fmt", `--stdin-filepath=${GENERATED_MODULE_PATH}`], | ||
| { input: raw, encoding: "utf8", maxBuffer: 16 * 1024 * 1024 }, | ||
| ); | ||
| if (formatted.status !== 0) { | ||
| throw new Error(`vp fmt failed while formatting the generated module: ${formatted.stderr}`); | ||
| } | ||
| return formatted.stdout; | ||
| } | ||
|
|
||
| if (import.meta.main) { | ||
| NodeFS.writeFileSync(GENERATED_MODULE_PATH, renderEnvironmentLucideIconsModule()); | ||
| } |
18 changes: 18 additions & 0 deletions
18
apps/mobile/scripts/generate-environment-lucide-icons.test.ts
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,18 @@ | ||
| import * as NodeFS from "node:fs"; | ||
| import * as NodePath from "node:path"; | ||
| import { describe, expect, it } from "vite-plus/test"; | ||
|
|
||
| import { renderEnvironmentLucideIconsModule } from "./generate-environment-lucide-icons.mts"; | ||
|
|
||
| describe("generate environment Lucide icons", () => { | ||
| it("keeps the committed module current", () => { | ||
| const generated = NodePath.resolve( | ||
| import.meta.dirname, | ||
| "../src/components/environmentLucideIcons.generated.ts", | ||
| ); | ||
| expect( | ||
| NodeFS.existsSync(generated) ? NodeFS.readFileSync(generated, "utf8") : null, | ||
| "Run `vp run --filter @t3tools/mobile generate:lucide-icons` and commit the output.", | ||
| ).toBe(renderEnvironmentLucideIconsModule()); | ||
| }); | ||
| }); |
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,96 @@ | ||
| import type { EnvironmentLucideIconId } from "@t3tools/contracts"; | ||
| import { memo } from "react"; | ||
| import Svg, { Circle, Ellipse, Line, Path, Polygon, Polyline, Rect } from "react-native-svg"; | ||
| import { withUniwind } from "uniwind"; | ||
|
|
||
| import { | ||
| ENVIRONMENT_LUCIDE_ICON_NODES, | ||
| type LucideIconNode, | ||
| } from "./environmentLucideIcons.generated"; | ||
|
|
||
| const ThemedSvg = withUniwind(Svg); | ||
|
|
||
| /** | ||
| * Draws one of the shared Lucide icons from generated path data, on the same | ||
| * 24 unit grid and 2 unit stroke web uses. Only the element kinds Lucide | ||
| * ships are handled, and the generator emits only those kinds. | ||
| */ | ||
| function renderNode(node: LucideIconNode) { | ||
| // The node lists are static, so position is a stable key. | ||
| return node.map(([element, attributes], key) => { | ||
| // A handful of icons fill a dot with currentColor; everything else strokes. | ||
| const fill = attributes.fill === "currentColor" ? "currentColor" : "none"; | ||
| switch (element) { | ||
| case "path": | ||
| return <Path key={key} d={attributes.d} fill={fill} />; | ||
| case "circle": | ||
| return ( | ||
| <Circle key={key} cx={attributes.cx} cy={attributes.cy} r={attributes.r} fill={fill} /> | ||
| ); | ||
| case "rect": | ||
| return ( | ||
| <Rect | ||
| key={key} | ||
| x={attributes.x} | ||
| y={attributes.y} | ||
| width={attributes.width} | ||
| height={attributes.height} | ||
| rx={attributes.rx} | ||
| ry={attributes.ry} | ||
| fill={fill} | ||
| /> | ||
| ); | ||
| case "line": | ||
| return ( | ||
| <Line | ||
| key={key} | ||
| x1={attributes.x1} | ||
| y1={attributes.y1} | ||
| x2={attributes.x2} | ||
| y2={attributes.y2} | ||
| /> | ||
| ); | ||
| case "polyline": | ||
| return <Polyline key={key} points={attributes.points} fill={fill} />; | ||
| case "polygon": | ||
| return <Polygon key={key} points={attributes.points} fill={fill} />; | ||
| case "ellipse": | ||
| return ( | ||
| <Ellipse | ||
| key={key} | ||
| cx={attributes.cx} | ||
| cy={attributes.cy} | ||
| rx={attributes.rx} | ||
| ry={attributes.ry} | ||
| fill={fill} | ||
| /> | ||
| ); | ||
| default: | ||
| return null; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| export const EnvironmentLucideIcon = memo(function EnvironmentLucideIcon(props: { | ||
| readonly id: EnvironmentLucideIconId; | ||
| readonly size: number; | ||
| readonly colorClassName: string; | ||
| readonly accessibilityLabel?: string; | ||
| }) { | ||
| return ( | ||
| <ThemedSvg | ||
| accessibilityLabel={props.accessibilityLabel} | ||
| width={props.size} | ||
| height={props.size} | ||
| viewBox="0 0 24 24" | ||
| fill="none" | ||
| stroke="currentColor" | ||
| strokeWidth={2} | ||
| strokeLinecap="round" | ||
| strokeLinejoin="round" | ||
| colorClassName={props.colorClassName} | ||
| > | ||
| {renderNode(ENVIRONMENT_LUCIDE_ICON_NODES[props.id])} | ||
| </ThemedSvg> | ||
| ); | ||
| }); | ||
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
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.