@token-forge/core 0.3.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Badi Labassi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@token-forge/core",
3
+ "version": "0.3.0",
4
+ "description": "Token Forge pipeline bundle — profile, Terrazzo plugins, token schema, Figma import",
5
+ "license": "MIT",
6
+ "author": "Badi Labassi",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/blabassi/token-forge.git",
10
+ "directory": "packages/core"
11
+ },
12
+ "files": [
13
+ "src"
14
+ ],
15
+ "type": "module",
16
+ "exports": {
17
+ ".": "./src/index.ts",
18
+ "./bootstrap": "./src/bootstrap.ts",
19
+ "./terrazzo": "./src/terrazzo-pipeline.ts"
20
+ },
21
+ "publishConfig": {
22
+ "access": "public"
23
+ },
24
+ "dependencies": {
25
+ "@terrazzo/cli": "^2.2.0",
26
+ "@terrazzo/plugin-css": "^2.2.0",
27
+ "@token-forge/plugins": "0.3.0",
28
+ "@token-forge/figma-importer": "0.3.0",
29
+ "@token-forge/lib": "0.3.0",
30
+ "@token-forge/token-schema": "0.3.0"
31
+ },
32
+ "devDependencies": {
33
+ "@types/node": "^24.0.0",
34
+ "typescript": "^7.0.2"
35
+ },
36
+ "engines": {
37
+ "node": ">=24"
38
+ },
39
+ "scripts": {
40
+ "typecheck": "tsc --noEmit -p ./"
41
+ }
42
+ }
@@ -0,0 +1,28 @@
1
+ import {
2
+ systemIds,
3
+ type DesignSystemProfile,
4
+ parseDesignSystemProfile,
5
+ setDesignSystemProfile,
6
+ } from "@token-forge/lib";
7
+
8
+ export type BootstrappedDesignSystem = {
9
+ profile: DesignSystemProfile;
10
+ systems: string[];
11
+ isSystem: (value: string) => boolean;
12
+ };
13
+
14
+ /**
15
+ * Register the consumer's design-system profile for this process.
16
+ * Usually unnecessary when the repo uses `defineDesignSystemProfile` in `design-system.config.ts`
17
+ * (import that module at CLI/test entry instead).
18
+ */
19
+ export function bootstrapDesignSystem(profile: DesignSystemProfile | unknown): BootstrappedDesignSystem {
20
+ const parsed = parseDesignSystemProfile(profile);
21
+ setDesignSystemProfile(parsed);
22
+ const systems = systemIds(parsed);
23
+ return {
24
+ profile: parsed,
25
+ systems,
26
+ isSystem: (value: string) => systems.includes(value),
27
+ };
28
+ }
package/src/index.ts ADDED
@@ -0,0 +1,19 @@
1
+ /**
2
+ * @token-forge/core — workspace bundle of the generic token pipeline.
3
+ *
4
+ * Consumer repos depend on this package (or the individual `@token-forge/*` packages)
5
+ * and provide `design-system.config.ts` + `tokens/` + a thin `terrazzo.config.ts`.
6
+ */
7
+
8
+ export { bootstrapDesignSystem, type BootstrappedDesignSystem } from "./bootstrap";
9
+ export {
10
+ createTerrazzoPipeline,
11
+ loadOutputPlugins,
12
+ resolveOutputPluginsSync,
13
+ type SystemBuildOptions,
14
+ type CreateTerrazzoPipelineOptions,
15
+ } from "./terrazzo-pipeline";
16
+
17
+ export * from "@token-forge/lib";
18
+ export * from "@token-forge/plugins";
19
+ export * from "@token-forge/token-schema";
@@ -0,0 +1,213 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import {
4
+ bootstrapCssPluginOptions,
5
+ buildOutDir,
6
+ buildSourceOrder,
7
+ cssPermutationsForSystem,
8
+ profileColorCssOutput,
9
+ resolverContextsBySystem,
10
+ resolverRelPath,
11
+ resolveColorCssOutput,
12
+ shouldExcludeLiquidPlugin,
13
+ tokenLayerRelPaths,
14
+ type ColorFormatPluginOptions,
15
+ type DesignSystemProfile,
16
+ type OutputPluginOverrides,
17
+ type ResolverContexts,
18
+ } from "@token-forge/lib";
19
+ import {
20
+ getBuildPlugin,
21
+ loadOutputPlugins,
22
+ registerBuiltinBuildPlugins,
23
+ type ResolvedOutputPlugin,
24
+ type TokenForgeBuildPlugin,
25
+ } from "@token-forge/plugins";
26
+ import { profileOutputPluginEntries } from "@token-forge/lib";
27
+ import { defineConfig } from "@terrazzo/cli";
28
+ import css from "@terrazzo/plugin-css";
29
+ import { bootstrapDesignSystem } from "./bootstrap";
30
+
31
+ export type { ColorFormatPluginOptions };
32
+ export type SystemBuildOptions = ColorFormatPluginOptions & OutputPluginOverrides;
33
+
34
+ export type CreateTerrazzoPipelineOptions<SystemId extends string = string> = {
35
+ profile: DesignSystemProfile;
36
+ /** Design-system entry ids to build (`profile.designSystems[].id`). */
37
+ systems: readonly SystemId[];
38
+ /** Consumer root (directory with `tokens/`, `resolvers/`, `design-system.config.ts`). */
39
+ repoRoot: string;
40
+ /** When true (default), calls `bootstrapDesignSystem(profile)` for this process. */
41
+ bootstrap?: boolean;
42
+ /**
43
+ * Pre-loaded build plugins (from {@link loadOutputPlugins}).
44
+ * When omitted, resolves from the registry (builtins + already-imported path modules).
45
+ */
46
+ outputPlugins?: ResolvedOutputPlugin[] | TokenForgeBuildPlugin[];
47
+ };
48
+
49
+ const sharedLint = {
50
+ build: { enabled: true },
51
+ rules: {
52
+ "core/valid-color": "error",
53
+ "core/valid-dimension": "error",
54
+ "core/valid-font-family": "error",
55
+ // "warn" not "error": Figma exports italic weights as names ("medium italic", …)
56
+ // which the rule rejects; plugins map them to numeric weights (FONT_WEIGHT_NAMES).
57
+ "core/valid-font-weight": "warn",
58
+ "core/valid-duration": "error",
59
+ "core/valid-cubic-bezier": "error",
60
+ "core/valid-number": "error",
61
+ "core/valid-link": "error",
62
+ "core/valid-boolean": "error",
63
+ "core/valid-string": "error",
64
+ "core/valid-stroke-style": "error",
65
+ "core/valid-border": "error",
66
+ "core/valid-transition": "error",
67
+ "core/valid-shadow": "error",
68
+ "core/valid-gradient": "error",
69
+ "core/valid-typography": "error",
70
+ "core/consistent-naming": "warn",
71
+ },
72
+ } as const;
73
+
74
+ function isPathRef(ref: string): boolean {
75
+ return ref.startsWith("./") || ref.startsWith("../") || path.isAbsolute(ref);
76
+ }
77
+
78
+ function derivedIdFromPath(modulePath: string): string {
79
+ return path.basename(modulePath, path.extname(modulePath));
80
+ }
81
+
82
+ /**
83
+ * Resolve plugin refs from the registry without dynamic import.
84
+ * Path refs must already be registered (side-effect import or prior `loadOutputPlugins`).
85
+ */
86
+ export function resolveOutputPluginsSync(
87
+ profile: DesignSystemProfile,
88
+ overrides: OutputPluginOverrides = {},
89
+ ): ResolvedOutputPlugin[] {
90
+ registerBuiltinBuildPlugins();
91
+ const entries = profileOutputPluginEntries(profile);
92
+ if (!entries.length) {
93
+ throw new Error("profile.outputs.plugins must be a non-empty list");
94
+ }
95
+ const plugins = entries.map((entry) => {
96
+ const id = isPathRef(entry.id) ? derivedIdFromPath(entry.id) : entry.id;
97
+ try {
98
+ return { plugin: getBuildPlugin(id), options: entry.options };
99
+ } catch {
100
+ throw new Error(
101
+ `Build plugin "${entry.id}" is not registered. Import the module for its side effect, ` +
102
+ `or await loadOutputPlugins(profile, consumerDir) before createTerrazzoPipeline.`,
103
+ );
104
+ }
105
+ });
106
+ if (shouldExcludeLiquidPlugin(overrides)) {
107
+ return plugins.filter((entry) => entry.plugin.id !== "liquid");
108
+ }
109
+ return plugins;
110
+ }
111
+
112
+ function asResolved(plugins: ResolvedOutputPlugin[] | TokenForgeBuildPlugin[]): ResolvedOutputPlugin[] {
113
+ return plugins.map((entry) =>
114
+ "plugin" in entry && typeof (entry as ResolvedOutputPlugin).plugin?.create === "function"
115
+ ? (entry as ResolvedOutputPlugin)
116
+ : { plugin: entry as TokenForgeBuildPlugin },
117
+ );
118
+ }
119
+
120
+ /**
121
+ * Build Terrazzo `configForSystem` / `pluginsForSystem` from a design-system profile.
122
+ * Consumer repos keep a thin `terrazzo.config.ts` that calls this helper.
123
+ */
124
+ export function createTerrazzoPipeline<SystemId extends string>(options: CreateTerrazzoPipelineOptions<SystemId>) {
125
+ const { profile, systems, repoRoot, bootstrap = true, outputPlugins: providedPlugins } = options;
126
+ if (!systems?.length) {
127
+ throw new Error("createTerrazzoPipeline requires non-empty `systems`");
128
+ }
129
+ if (bootstrap) {
130
+ bootstrapDesignSystem(profile);
131
+ }
132
+
133
+ registerBuiltinBuildPlugins();
134
+ const systemResolverContexts = resolverContextsBySystem(profile) as Record<SystemId, ResolverContexts>;
135
+
136
+ function activeOutputPlugins(buildOptions: SystemBuildOptions = {}): ResolvedOutputPlugin[] {
137
+ const base = asResolved(providedPlugins ?? resolveOutputPluginsSync(profile));
138
+ if (shouldExcludeLiquidPlugin(buildOptions)) {
139
+ return base.filter((entry) => entry.plugin.id !== "liquid");
140
+ }
141
+ return base;
142
+ }
143
+
144
+ function loadTokenDocsForSystem(system: SystemId): Record<string, unknown>[] {
145
+ return tokenLayerRelPaths(profile, system).map((relPath) => {
146
+ const fullPath = path.join(repoRoot, relPath);
147
+ return JSON.parse(fs.readFileSync(fullPath, "utf8")) as Record<string, unknown>;
148
+ });
149
+ }
150
+
151
+ function tokenOrderForSystem(system: SystemId): Map<string, number> {
152
+ return buildSourceOrder(loadTokenDocsForSystem(system));
153
+ }
154
+
155
+ function pluginsForSystem(
156
+ system: SystemId,
157
+ tokenOrder: Map<string, number>,
158
+ resolverContexts: ResolverContexts,
159
+ buildOptions: SystemBuildOptions = {},
160
+ ) {
161
+ const colorCssOutput = resolveColorCssOutput(buildOptions.colorCssOutput, profileColorCssOutput(profile));
162
+
163
+ const terrazzoPlugins = [
164
+ css({
165
+ filename: `_${system}-css-transforms.css`,
166
+ skipBuild: true,
167
+ permutations: cssPermutationsForSystem(profile, resolverContexts),
168
+ ...bootstrapCssPluginOptions(colorCssOutput),
169
+ }),
170
+ ];
171
+
172
+ for (const entry of activeOutputPlugins(buildOptions)) {
173
+ terrazzoPlugins.push(
174
+ entry.plugin.create({
175
+ system,
176
+ tokenOrder,
177
+ resolverContexts,
178
+ colorCssOutput,
179
+ options: entry.options,
180
+ }),
181
+ );
182
+ }
183
+
184
+ return terrazzoPlugins;
185
+ }
186
+
187
+ function configForSystem(system: SystemId, buildOptions: SystemBuildOptions = {}) {
188
+ const tokenOrder = tokenOrderForSystem(system);
189
+ const resolverContexts = systemResolverContexts[system];
190
+
191
+ return defineConfig({
192
+ tokens: [resolverRelPath(profile, system)],
193
+ outDir: buildOutDir(profile, system),
194
+ plugins: pluginsForSystem(system, tokenOrder, resolverContexts, buildOptions),
195
+ lint: sharedLint,
196
+ });
197
+ }
198
+
199
+ const systemConfigs = systems.map((system) => configForSystem(system));
200
+
201
+ return {
202
+ profile,
203
+ systems,
204
+ repoRoot,
205
+ pluginsForSystem,
206
+ configForSystem,
207
+ systemConfigs,
208
+ tokenOrderForSystem,
209
+ activeOutputPlugins,
210
+ };
211
+ }
212
+
213
+ export { loadOutputPlugins };