@vertekum/schema-builder 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/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2026 Thomas Schemmer
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,125 @@
1
+ # @vertekum/schema-builder
2
+
3
+ Declare a token vocabulary as grammar; build it into ordinary JSON Schema files.
4
+
5
+ A vocabulary schema governs **names and order** — which token names may exist, in what
6
+ order, and where the order ends. Writing that shape by hand means dozens of lines of JSON
7
+ Schema per branch. This extension adds a definition language (`.dfn` files) that states the
8
+ same thing in one expression, and a build step that materializes the JSON Schema files your
9
+ `schemas` config binds. Core, `check`, and every consumer keep seeing plain schema files —
10
+ the grammar is authoring source, never a runtime notation.
11
+
12
+ ## Get started
13
+
14
+ 1. Add the extension to your config:
15
+
16
+ ```ts
17
+ import { defineConfig } from '@vertekum/core';
18
+ import { schemaBuilderExtension } from '@vertekum/schema-builder';
19
+
20
+ export default defineConfig({
21
+ collection: './tokens',
22
+ extensions: [schemaBuilderExtension],
23
+ schemas: [{ from: './schemas', domain: 'vocabulary', use: { 'house.json': '*.json' } }],
24
+ });
25
+ ```
26
+
27
+ 2. Declare the vocabulary — `schemas/house.dfn`:
28
+
29
+ ```dfn
30
+ emphasis = subtle | bold
31
+
32
+ root = color.text.[neutral | brand | success].<emphasis>
33
+ ```
34
+
35
+ 3. Build it, then check as usual:
36
+
37
+ ```bash
38
+ npx vertekum schema build # writes schemas/house.json beside the module
39
+ npx vertekum check # the built schema now governs your token names
40
+ ```
41
+
42
+ `schema build` accepts a single module argument, defaults to every `.dfn` under
43
+ `./schemas`, and supports `--dry-run` (list without writing), `--json`, and `--check`
44
+ (verify built files are current; exit 1 when stale — the CI guard).
45
+
46
+ ## The definition language
47
+
48
+ A module is line-oriented: `#` comments, pragmas, `use` imports, and productions. An
49
+ indented line continues the statement above it.
50
+
51
+ ### Statements
52
+
53
+ | statement | meaning |
54
+ | --- | --- |
55
+ | `name = expression` | a **production**: a named fragment (a denotation, a branch, a whole subtree) |
56
+ | `root = expression` | the reserved production `build` materializes; one per module |
57
+ | `use "./other.dfn"` | import a module (relative path or package specifier) |
58
+ | `id "…"` `title "…"` `description "…"` | pragmas: the built schema's `$id`, `title`, `description` |
59
+ | `scope "branch"` | the schema governs only its named top-level branches — the document root stays unsealed so sibling vocabularies can bind over the same files (default: `"document"`, which seals it) |
60
+
61
+ ### Expressions
62
+
63
+ | syntax | meaning |
64
+ | --- | --- |
65
+ | `a.b.c` | nesting — one name-tree level per step |
66
+ | `a \| b` | alternation — the set of permitted names |
67
+ | `[ … ]` | grouping; groups may hold full sub-paths (branches) |
68
+ | `<name>` | reference a local production |
69
+ | `<@name>` | reference an imported production; an imported module's *root* goes by its basename |
70
+ | `<name [a, b]>` | **pick** — only the listed members of the set |
71
+ | `<name ![a, b]>` | **omit** — the set minus the listed members |
72
+ | `<name*>` / `[a \| b *]` | **open set** — additions beyond the listed names are permitted, and every member (listed or added) takes the same tail |
73
+ | `step?` | **optional slot** — the step may be skipped: `<role>.<emphasis>?.<state>?` grants `role`, `role.<emphasis>`, `role.<state>`, and the full path |
74
+ | `100-900/100` | a numeric range with step, inclusive both ends, enumerated at build |
75
+
76
+ Pick and omit validate every listed name against the set's members — a typo is a build
77
+ error, and a modified set is a new set. Naming one derives a narrowed denotation:
78
+
79
+ ```dfn
80
+ use "./colors.dfn"
81
+
82
+ limited-accents = <@accent-color [red, blue]>
83
+ ```
84
+
85
+ ### Style: the root reads as the syntagm
86
+
87
+ State the high-level shape in the root and let unused slots collapse; give forks their own
88
+ named productions instead of nesting:
89
+
90
+ ```dfn
91
+ property = background | text | icon | border
92
+ role = brand | danger | neutral | success | warning
93
+ emphasis = subtle | bold
94
+ state = hovered | pressed
95
+
96
+ root = color.[
97
+ <property>.<role>?.<emphasis>?.<state>?
98
+ | <code>
99
+ ]
100
+
101
+ code = text.code.[comments | keywords | strings]
102
+ ```
103
+
104
+ ## Built artifacts and ownership
105
+
106
+ A built file lands beside its module (`house.dfn` → `house.json`) and carries a `$comment`
107
+ stamp naming its source. The stamp is the ownership contract: `schema build` regenerates
108
+ stamped files and **never overwrites a file whose stamp was removed** — hand-edit the JSON
109
+ and delete the stamp to take ownership, and the builder leaves it alone from then on.
110
+
111
+ ## Ejecting grammar
112
+
113
+ `vertekum schema eject` copies any resolvable file verbatim — including `.dfn` modules
114
+ from packages that ship them:
115
+
116
+ ```bash
117
+ npx vertekum schema eject @vertekum/schema-atlassian/color.dfn ./schemas/color.dfn
118
+ npx vertekum schema build
119
+ ```
120
+
121
+ Ejecting the grammar (rather than the built JSON) means your edits stay one-line grammar
122
+ edits with a rebuild, instead of surgery on generated schema. Two notes: an ejected module
123
+ that `use`s relative imports needs them ejected alongside it; and the config hint `eject`
124
+ prints applies to schemas you bind directly — an ejected `.dfn` is not bound, its *built*
125
+ `.json` is.
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@vertekum/schema-builder",
3
+ "version": "0.1.0",
4
+ "description": "Declare token vocabularies as grammar; build them into JSON Schema files",
5
+ "license": "Apache-2.0",
6
+ "keywords": [
7
+ "design-tokens",
8
+ "dtcg",
9
+ "schema",
10
+ "vocabulary",
11
+ "grammar"
12
+ ],
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/moros-oxus/vertekum.git",
16
+ "directory": "packages/schemas/builder"
17
+ },
18
+ "homepage": "https://github.com/moros-oxus/vertekum",
19
+ "bugs": "https://github.com/moros-oxus/vertekum/issues",
20
+ "files": [
21
+ "CHANGELOG.md",
22
+ "src",
23
+ "tsconfig.json",
24
+ "!src/**/*.test.ts"
25
+ ],
26
+ "publishConfig": {
27
+ "access": "public"
28
+ },
29
+ "type": "module",
30
+ "main": "./src/index.ts",
31
+ "types": "./src/index.ts",
32
+ "exports": {
33
+ ".": "./src/index.ts",
34
+ "./api": "./src/api.ts",
35
+ "./cli": "./src/cli.ts"
36
+ },
37
+ "peerDependencies": {
38
+ "@vertekum/core": "0.1.0"
39
+ },
40
+ "dependencies": {
41
+ "zod": "^3.25.76"
42
+ },
43
+ "devDependencies": {
44
+ "@vertekum/core": "0.1.0",
45
+ "@vertekum/schema-vocabulary": "0.1.0"
46
+ }
47
+ }
package/src/api.ts ADDED
@@ -0,0 +1,16 @@
1
+ import type { ActivateContext } from '@vertekum/core';
2
+ import { schemaBuildCommand } from './cli';
3
+ import type { schemaBuilderManifest } from './index';
4
+
5
+ export { assertOpenSetsAreNameSets, build, type TreeNode } from './build';
6
+ export { buildModule, schemaBuildCommand } from './cli';
7
+ export { emit, isStamped, STAMP_PREFIX, stamp } from './emit';
8
+ export { parse } from './parser';
9
+ export { type ResolvedModule, resolveModule } from './resolve';
10
+
11
+ /** Headless activation: contributes the `schema build` command and nothing else. */
12
+ export function activate(
13
+ ctx: ActivateContext<typeof schemaBuilderManifest>,
14
+ ): void {
15
+ ctx.commands.register(schemaBuildCommand);
16
+ }
package/src/ast.ts ADDED
@@ -0,0 +1,86 @@
1
+ /**
2
+ * The definition-file AST — a normalized description of a `.dfn` module. Nodes describe the NAME
3
+ * TREE a vocabulary permits: nesting (`.`), alternation (`|`), references (`<x>` / `<@x>`), and
4
+ * range terms. The parser knows the grammar, not the semantics: a `ref` node knows its name is
5
+ * `color-role`, not what that production expands to — expansion lives in the builder.
6
+ */
7
+
8
+ /** A parsed module: imports, named productions, and the reserved `root` (absent in fragments). */
9
+ export interface Module {
10
+ /** `use "<specifier>"` statements, in order: relative paths or package specifiers. */
11
+ uses: string[];
12
+ /** Productions by name. `root` is never stored here. */
13
+ productions: Map<string, Node>;
14
+ /** The reserved `root` production, when declared. */
15
+ root: Node | undefined;
16
+ /** Pragmas (`id "…"`, `title "…"`, `description "…"`) — document metadata for emission. */
17
+ meta: ModuleMeta;
18
+ }
19
+
20
+ /** The pragma statements a module may carry, each at most once. */
21
+ export interface ModuleMeta {
22
+ id?: string;
23
+ title?: string;
24
+ description?: string;
25
+ /**
26
+ * `scope "branch"`: the schema governs only the top-level branches it names — the document
27
+ * root stays unsealed, so sibling vocabularies can bind over the same files. The default,
28
+ * `document`, seals the root: nothing beyond the granted names may exist.
29
+ */
30
+ scope?: 'document' | 'branch';
31
+ }
32
+
33
+ export type Node = Alt | Path | Name | Range | Ref | Group;
34
+
35
+ /** `a | b | c` — set union of the options' name trees. */
36
+ export interface Alt {
37
+ kind: 'alt';
38
+ options: Node[];
39
+ }
40
+
41
+ /** `a.b.c` — nesting, one level per step, left to right. */
42
+ export interface Path {
43
+ kind: 'path';
44
+ steps: Step[];
45
+ }
46
+
47
+ /** One step of a path; `optional` records a trailing `?` (parsed, reserved — no artifact effect). */
48
+ export interface Step {
49
+ term: Name | Range | Ref | Group;
50
+ optional: boolean;
51
+ }
52
+
53
+ /** A literal name segment — an identifier or a bare number. */
54
+ export interface Name {
55
+ kind: 'name';
56
+ value: string;
57
+ }
58
+
59
+ /** `min-max/step` — an inclusive numeric name set, enumerated at build time. */
60
+ export interface Range {
61
+ kind: 'range';
62
+ min: number;
63
+ max: number;
64
+ step: number;
65
+ }
66
+
67
+ /**
68
+ * `<name>` / `<@name>`; `open` records a trailing `*` (the set admits additions). Set
69
+ * modifiers: `[a, b]` is PICK (only the listed members), `![a, b]` is OMIT (the set minus
70
+ * them) — a modified reference is a new set.
71
+ */
72
+ export interface Ref {
73
+ kind: 'ref';
74
+ name: string;
75
+ imported: boolean;
76
+ open: boolean;
77
+ pick: string[];
78
+ omit: string[];
79
+ }
80
+
81
+ /** `[ … ]`; `open` records a trailing `*` before the closing bracket. */
82
+ export interface Group {
83
+ kind: 'group';
84
+ node: Node;
85
+ open: boolean;
86
+ }