@primitiv-ui/tokens 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,120 @@
1
+ # @primitiv-ui/tokens
2
+
3
+ The DTCG-conformant token layer that sits between the Figma source of
4
+ truth and any downstream consumer.
5
+
6
+ Internal-only — this package is not published. It is the destination
7
+ the `primitiv-sync-figma-plugin` writes to, and the source any future
8
+ token transformer (e.g. CSS variables, Tailwind config) reads from.
9
+
10
+ ## Layout
11
+
12
+ | File | Contents |
13
+ | --- | --- |
14
+ | `src/dtcg.ts` | Pure transform: Figma-shaped variables → DTCG group |
15
+ | `src/server.ts` | Local HTTP server (`POST /sync`) that writes the five DTCG files atomically |
16
+ | `src/serve.ts` | Boot script for the sync server |
17
+ | `src/index.ts` | Public entry; re-exports the transform, the server, and types |
18
+ | `src/*.test.ts` | Vitest unit tests, 100% coverage |
19
+ | `src/{primitives,palette,intent,context,interaction}.json` | DTCG output written by the sync server. **Committed** — these files are the repo's source of truth for downstream transformers. |
20
+
21
+ ## Backing up tokens (end-to-end)
22
+
23
+ The backup cycle pushes the current state of the Figma variables
24
+ into `src/*.json` so the repo stays the source of truth. Two pieces
25
+ run side by side: this package's local HTTP server, and the
26
+ `primitiv-sync-figma-plugin` loaded into Figma desktop. You need
27
+ the **Figma desktop app** — figma.com in a browser cannot load
28
+ local plugins.
29
+
30
+ ### 1. Install dependencies (first time, or after a pull)
31
+
32
+ ```sh
33
+ pnpm install
34
+ ```
35
+
36
+ If `pnpm tokens:sync` later errors with `tsx: not found`, you
37
+ skipped or missed this step.
38
+
39
+ ### 2. Boot the sync server — terminal 1
40
+
41
+ ```sh
42
+ pnpm tokens:sync
43
+ ```
44
+
45
+ Binds to `http://localhost:4477`. Logs the bind on success. Leave
46
+ this running for the duration of the backup. `Ctrl-C` to stop.
47
+
48
+ ### 3. Watch-build the plugin — terminal 2
49
+
50
+ ```sh
51
+ pnpm --filter primitiv-sync-figma-plugin dev
52
+ ```
53
+
54
+ Produces `apps/primitiv-sync-figma-plugin/dist/code.js` (sandbox)
55
+ and `dist/index.html` (UI). Stays running and rebuilds on change.
56
+
57
+ ### 4. Load the plugin into Figma desktop (one-time per machine)
58
+
59
+ In Figma desktop: **Plugins → Development → Import plugin from
60
+ manifest…** and select
61
+ `apps/primitiv-sync-figma-plugin/manifest.json`. After this Figma
62
+ remembers it under **Plugins → Development → Primitiv Sync**.
63
+
64
+ ### 5. Run the plugin against your design file
65
+
66
+ Open the Figma file that owns the variables. **Plugins →
67
+ Development → Primitiv Sync**. The UI banner shows
68
+ *Connected to: <page name>* once the sandbox sends `plugin-ready`.
69
+
70
+ ### 6. Turn the **Live sync** toggle on
71
+
72
+ Off by default. With Live sync on, Export POSTs to the running
73
+ server. With it off, Export gives you five download anchors
74
+ (`primitives.json`, `palette.json`, `intent.json`, `context.json`,
75
+ `interaction.json`) — save each into `packages/tokens/src/` by hand.
76
+
77
+ ### 7. Click **Export tokens**
78
+
79
+ The plugin extracts every local collection + variable via
80
+ `figma.variables.*`, runs `figmaVarsToDtcg`, and POSTs
81
+ `{ primitives, palette, intent, context, interaction }` to
82
+ `localhost:4477/sync`. The UI confirms *Synced to localhost:4477*
83
+ on success; the server writes each file atomically (write to
84
+ `.tmp`, then rename).
85
+
86
+ ### 8. Verify and commit
87
+
88
+ ```sh
89
+ git status packages/tokens/src
90
+ git diff packages/tokens/src
91
+ git add packages/tokens/src/{primitives,palette,intent,context,interaction}.json
92
+ git commit -m "tokens: sync from figma"
93
+ git push
94
+ ```
95
+
96
+ Downstream transformers (CSS variables, Tailwind config, etc.)
97
+ read from these five files — committing them is what makes the
98
+ backup "real".
99
+
100
+ ## Troubleshooting
101
+
102
+ | Symptom | Cause | Fix |
103
+ | --- | --- | --- |
104
+ | `sh: 1: tsx: not found` when running `pnpm tokens:sync` | `node_modules` missing or stale | `pnpm install`, then retry |
105
+ | Plugin UI reports an error on Export with Live sync **on** | Server isn't running, or `4477` is owned by another process | Check terminal 1 is alive; `lsof -i :4477` if you suspect a port clash |
106
+ | Unknown Figma collections appear in the file | Collections not in the known routing list are silently dropped | If you need them routed, add them to the whitelist in `figmaVarsToDtcg` in `src/dtcg.ts` and extend `src/dtcg.test.ts` |
107
+ | Want a one-off backup without booting the server | Live sync is unnecessary for a single pull | Leave Live sync **off**, click Export, save the five download anchors into `src/` manually |
108
+
109
+ ## Conventions
110
+
111
+ - Slash-separated Figma variable names (`font-family/heading`) become
112
+ nested DTCG groups (`{ "font-family": { "heading": {…} } }`).
113
+ - Each token has `$type` and `$value`. Aliases (next cycle) will use
114
+ DTCG's `{group.sub.name}` reference string.
115
+ - Colours emit as hex: `#rrggbb` opaque, `#rrggbbaa` translucent.
116
+ - Multi-mode collections (`Primitives / Palette`, `Intent`, `Context`)
117
+ are iterated per mode; the lowercase mode name becomes the top-level
118
+ key in that file (e.g. `palette.json` has `light` and `dark` keys).
119
+ - Single-mode collections (`Primitives`, `Interaction`) are read from
120
+ `defaultModeId` and emitted flat.
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@primitiv-ui/tokens",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "main": "src/index.ts",
6
+ "types": "src/index.ts",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./src/index.ts",
10
+ "default": "./src/index.ts"
11
+ }
12
+ },
13
+ "files": [
14
+ "src"
15
+ ],
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "https://github.com/primitiv-ui/primitiv.git",
19
+ "directory": "packages/tokens"
20
+ },
21
+ "devDependencies": {
22
+ "@types/node": "^25.5.0",
23
+ "@vitest/coverage-v8": "^4.1.4",
24
+ "tsx": "^4.19.2",
25
+ "typescript": "~6.0.2",
26
+ "vitest": "^4.1.4"
27
+ },
28
+ "publishConfig": {
29
+ "access": "public"
30
+ },
31
+ "scripts": {
32
+ "lint": "tsc --noEmit",
33
+ "qa": "pnpm qa:units",
34
+ "qa:units": "vitest run --coverage",
35
+ "qa:units:watch": "vitest --coverage",
36
+ "sync:serve": "tsx src/serve.ts"
37
+ }
38
+ }