@design-token-kit/cli 0.1.1

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.
Files changed (4) hide show
  1. package/LICENSE +159 -0
  2. package/README.md +96 -0
  3. package/bin/index.mjs +198 -0
  4. package/package.json +52 -0
package/LICENSE ADDED
@@ -0,0 +1,159 @@
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, and
10
+ distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by the copyright
13
+ owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all other entities
16
+ that control, are controlled by, or are under common control with that entity.
17
+ For the purposes of this definition, "control" means (i) the power, direct or
18
+ indirect, to cause the direction or management of such entity, whether by
19
+ contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the
20
+ outstanding shares, or (iii) beneficial ownership of such entity.
21
+
22
+ "You" (or "Your") shall mean an individual or Legal Entity exercising
23
+ permissions granted by this License.
24
+
25
+ "Source" form shall mean the preferred form for making modifications, including
26
+ but not limited to software source code, documentation source, and configuration
27
+ files.
28
+
29
+ "Object" form shall mean any form resulting from mechanical transformation or
30
+ translation of a Source form, including but not limited to compiled object code,
31
+ generated documentation, and conversions to other media types.
32
+
33
+ "Work" shall mean the work of authorship, whether in Source or Object form, made
34
+ available under the License, as indicated by a copyright notice that is included
35
+ in or attached to the work (an example is provided in the Appendix below).
36
+
37
+ "Derivative Works" shall mean any work, whether in Source or Object form, that
38
+ is based on (or derived from) the Work and for which the editorial revisions,
39
+ annotations, elaborations, or other modifications represent, as a whole, an
40
+ original work of authorship. For the purposes of this License, Derivative Works
41
+ shall not include works that remain separable from, or merely link (or bind by
42
+ name) to the interfaces of, the Work and Derivative Works thereof.
43
+
44
+ "Contribution" shall mean any work of authorship, including the original version
45
+ of the Work and any modifications or additions to that Work or Derivative Works
46
+ thereof, that is intentionally submitted to Licensor for inclusion in the Work
47
+ by the copyright owner or by an individual or Legal Entity authorized to submit
48
+ on behalf of the copyright owner. For the purposes of this definition,
49
+ "submitted" means any form of electronic, verbal, or written communication sent
50
+ to the Licensor or its representatives, including but not limited to
51
+ communication on electronic mailing lists, source code control systems, and
52
+ issue tracking systems that are managed by, or on behalf of, the Licensor for
53
+ the purpose of discussing and improving the Work, but excluding communication
54
+ that is conspicuously marked or otherwise designated in writing by the copyright
55
+ owner as "Not a Contribution."
56
+
57
+ "Contributor" shall mean Licensor and any individual or Legal Entity on behalf
58
+ of whom a Contribution has been received by Licensor and subsequently
59
+ incorporated within the Work.
60
+
61
+ 2. Grant of Copyright License. Subject to the terms and conditions of this
62
+ License, each Contributor hereby grants to You a perpetual, worldwide,
63
+ non-exclusive, no-charge, royalty-free, irrevocable copyright license to
64
+ reproduce, prepare Derivative Works of, publicly display, publicly perform,
65
+ sublicense, and distribute the Work and such Derivative Works in Source or
66
+ Object form.
67
+
68
+ 3. Grant of Patent License. Subject to the terms and conditions of this License,
69
+ each Contributor hereby grants to You a perpetual, worldwide, non-exclusive,
70
+ no-charge, royalty-free, irrevocable (except as stated in this section) patent
71
+ license to make, have made, use, offer to sell, sell, import, and otherwise
72
+ transfer the Work, where such license applies only to those patent claims
73
+ licensable by such Contributor that are necessarily infringed by their
74
+ Contribution(s) alone or by combination of their Contribution(s) with the Work
75
+ to which such Contribution(s) was submitted. If You institute patent litigation
76
+ against any entity (including a cross-claim or counterclaim in a lawsuit)
77
+ alleging that the Work or a Contribution incorporated within the Work
78
+ constitutes direct or contributory patent infringement, then any patent licenses
79
+ granted to You under this License for that Work shall terminate as of the date
80
+ such litigation is filed.
81
+
82
+ 4. Redistribution. You may reproduce and distribute copies of the Work or
83
+ Derivative Works thereof in any medium, with or without modifications, and in
84
+ Source or Object form, provided that You meet the following conditions:
85
+
86
+ (a) You must give any other recipients of the Work or Derivative Works a copy of
87
+ this License; and
88
+
89
+ (b) You must cause any modified files to carry prominent notices stating that
90
+ You changed the files; and
91
+
92
+ (c) You must retain, in the Source form of any Derivative Works that You
93
+ distribute, all copyright, patent, trademark, and attribution notices from the
94
+ Source form of the Work, excluding those notices that do not pertain to any part
95
+ of the Derivative Works; and
96
+
97
+ (d) If the Work includes a "NOTICE" text file as part of its distribution, then
98
+ any Derivative Works that You distribute must include a readable copy of the
99
+ attribution notices contained within such NOTICE file, excluding those notices
100
+ that do not pertain to any part of the Derivative Works, in at least one of the
101
+ following places: within a NOTICE text file distributed as part of the
102
+ Derivative Works; within the Source form or documentation, if provided along
103
+ with the Derivative Works; or, within a display generated by the Derivative
104
+ Works, if and wherever such third-party notices normally appear. The contents of
105
+ the NOTICE file are for informational purposes only and do not modify the
106
+ License. You may add Your own attribution notices within Derivative Works that
107
+ You distribute, alongside or as an addendum to the NOTICE text from the Work,
108
+ provided that such additional attribution notices cannot be construed as
109
+ modifying the License.
110
+
111
+ You may add Your own copyright statement to Your modifications and may provide
112
+ additional or different license terms and conditions for use, reproduction, or
113
+ distribution of Your modifications, or for any such Derivative Works as a whole,
114
+ provided Your use, reproduction, and distribution of the Work otherwise complies
115
+ with the conditions stated in this License.
116
+
117
+ 5. Submission of Contributions. Unless You explicitly state otherwise, any
118
+ Contribution intentionally submitted for inclusion in the Work by You to the
119
+ Licensor shall be under the terms and conditions of this License, without any
120
+ additional terms or conditions. Notwithstanding the above, nothing herein shall
121
+ supersede or modify the terms of any separate license agreement you may have
122
+ executed with Licensor regarding such Contributions.
123
+
124
+ 6. Trademarks. This License does not grant permission to use the trade names,
125
+ trademarks, service marks, or product names of the Licensor, except as required
126
+ for reasonable and customary use in describing the origin of the Work and
127
+ reproducing the content of the NOTICE file.
128
+
129
+ 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in
130
+ writing, Licensor provides the Work (and each Contributor provides its
131
+ Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
132
+ KIND, either express or implied, including, without limitation, any warranties
133
+ or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
134
+ PARTICULAR PURPOSE. You are solely responsible for determining the
135
+ appropriateness of using or redistributing the Work and assume any risks
136
+ associated with Your exercise of permissions under this License.
137
+
138
+ 8. Limitation of Liability. In no event and under no legal theory, whether in
139
+ contract, tort (including negligence), or otherwise, unless required by
140
+ applicable law (such as deliberate and grossly negligent acts) or agreed to in
141
+ writing, shall any Contributor be liable to You for damages, including any
142
+ direct, indirect, special, incidental, or consequential damages of any character
143
+ arising as a result of this License or out of the use or inability to use the
144
+ Work (including but not limited to damages for loss of goodwill, work stoppage,
145
+ computer failure or malfunction, or any and all other commercial damages or
146
+ losses), even if such Contributor has been advised of the possibility of such
147
+ damages.
148
+
149
+ 9. Accepting Warranty or Additional Liability. While redistributing the Work or
150
+ Derivative Works thereof, You may choose to offer, and charge a fee for,
151
+ acceptance of support, warranty, indemnity, or other liability obligations
152
+ and/or rights consistent with this License. However, in accepting such
153
+ obligations, You may act only on Your own behalf and on Your sole responsibility,
154
+ not on behalf of any other Contributor, and only if You agree to indemnify,
155
+ defend, and hold each Contributor harmless for any liability incurred by, or
156
+ claims asserted against, such Contributor by reason of your accepting any such
157
+ warranty or additional liability.
158
+
159
+ END OF TERMS AND CONDITIONS
package/README.md ADDED
@@ -0,0 +1,96 @@
1
+ # @design-token-kit/cli
2
+
3
+ Command line interface for Design Token Kit.
4
+
5
+ It provides commands to:
6
+
7
+ - validate design tokens
8
+ - convert between DTCG JSON, HRDT YAML, and CSS
9
+ - generate HTML showcase pages
10
+ - run round-trip read/write checks
11
+
12
+ Node.js 18 or newer is required.
13
+
14
+ ## Install
15
+
16
+ Run without installation:
17
+
18
+ ```bash
19
+ npx @design-token-kit/cli validate tokens.json
20
+ ```
21
+
22
+ Install globally:
23
+
24
+ ```bash
25
+ npm install -g @design-token-kit/cli
26
+ dtokens validate tokens.json
27
+ ```
28
+
29
+ Install as a local dependency:
30
+
31
+ ```bash
32
+ npm install @design-token-kit/cli
33
+ ```
34
+
35
+ ## Commands
36
+
37
+ ### `validate`
38
+
39
+ Validate one or more DTCG JSON or HRDT YAML token files.
40
+
41
+ ```bash
42
+ dtokens validate tokens.json
43
+ dtokens validate tokens.yaml
44
+ dtokens validate light.json dark.yaml
45
+ ```
46
+
47
+ ### `convert`
48
+
49
+ Convert a token file to DTCG JSON, HRDT YAML, or CSS.
50
+
51
+ Defaults:
52
+
53
+ - `--inform` defaults to `dtcg`
54
+ - `--outform` defaults to `css`
55
+
56
+ ```bash
57
+ dtokens convert tokens.json
58
+ dtokens convert tokens.json --out tokens.css
59
+ dtokens convert tokens.yaml --inform hrdt --outform css
60
+ dtokens convert tokens.yaml --inform hrdt --outform dtcg
61
+ dtokens convert tokens.json --outform hrdt
62
+ ```
63
+
64
+ Options:
65
+
66
+ - `--inform <format>`: input format override, `dtcg` or `hrdt`
67
+ - `--outform <format>`: output format, `dtcg`, `hrdt`, or `css`
68
+ - `--out <file>`: write output to a file instead of stdout
69
+
70
+ ### `showcase`
71
+
72
+ Generate an HTML showcase from token files or CSS.
73
+
74
+ ```bash
75
+ dtokens showcase tokens.json
76
+ dtokens showcase tokens.css --out showcase.html
77
+ dtokens showcase tokens.json --out showcase.html
78
+ ```
79
+
80
+ ### `test`
81
+
82
+ Load a token file, build the internal model, and print it back in the same or
83
+ requested format.
84
+
85
+ ```bash
86
+ dtokens test tokens.json
87
+ dtokens test tokens.yaml
88
+ dtokens test tokens.yaml --outform hrdt
89
+ dtokens test tokens.json --outform dtcg
90
+ ```
91
+
92
+ ## Supported Formats
93
+
94
+ - `dtcg` - Design Tokens Community Group JSON format
95
+ - `hrdt` - Human-Readable Design Tokens YAML format
96
+ - `css` - CSS custom properties output
package/bin/index.mjs ADDED
@@ -0,0 +1,198 @@
1
+ #!/usr/bin/env node
2
+
3
+ // src/index.ts
4
+ import { Command as Command5 } from "commander";
5
+
6
+ // src/commands/validate.ts
7
+ import { Command } from "commander";
8
+ import {
9
+ DtcgTokenValidator
10
+ } from "@design-token-kit/core";
11
+ function formatIssue(issue) {
12
+ const location = issue.line === void 0 ? "" : issue.column === void 0 ? `:${issue.line}` : `:${issue.line}:${issue.column}`;
13
+ return `[${issue.name}] ${issue.severity}: ${issue.sourcePath}${location} - ${issue.message}`;
14
+ }
15
+ function printIssues(issues) {
16
+ for (const issue of issues) {
17
+ const stream = issue.severity === "error" ? console.error : console.warn;
18
+ stream(formatIssue(issue));
19
+ }
20
+ }
21
+ function hasErrors(issues) {
22
+ return issues.some((i) => i.severity === "error");
23
+ }
24
+ var validateCommand = new Command("validate").description("Validate DTCG JSON or HRDT YAML token files.").argument("<sources...>", "Paths to token JSON or HRDT files").action(async (sources) => {
25
+ try {
26
+ const validator = new DtcgTokenValidator();
27
+ const issues = await validator.validate(sources);
28
+ printIssues(issues);
29
+ if (hasErrors(issues)) {
30
+ process.exit(2);
31
+ }
32
+ console.log("Validation passed.");
33
+ } catch (error) {
34
+ console.error("Validation error:", error.message);
35
+ process.exit(1);
36
+ }
37
+ });
38
+
39
+ // src/commands/convert.ts
40
+ import { Command as Command2 } from "commander";
41
+ import { Source } from "@design-token-kit/core";
42
+ import { writeFile } from "fs/promises";
43
+
44
+ // src/commands/formats.ts
45
+ import {
46
+ DtcgTokenCssConverter,
47
+ DtcgJsonReader,
48
+ DtcgJsonWriter,
49
+ HrdtTokenReader,
50
+ HrdtTokenWriter
51
+ } from "@design-token-kit/core";
52
+ function detectDocumentFormat(file) {
53
+ return /\.(ya?ml)$/i.test(file) ? "hrdt" /* HRDT */ : "dtcg" /* DTCG */;
54
+ }
55
+ function getReader(format) {
56
+ return readers[toDocumentFormat(format)];
57
+ }
58
+ function getWriter(format) {
59
+ return writers[toOutputFormat(format)];
60
+ }
61
+ function getRoundTripWriter(format, file) {
62
+ return writers[toDocumentFormat(format, detectDocumentFormat(file))];
63
+ }
64
+ function getDetectedReader(file) {
65
+ return readers[detectDocumentFormat(file)];
66
+ }
67
+ var readers = {
68
+ ["dtcg" /* DTCG */]: {
69
+ read: (content) => new DtcgJsonReader().parse(content)
70
+ },
71
+ ["hrdt" /* HRDT */]: {
72
+ read: (content) => new HrdtTokenReader().parse(content)
73
+ }
74
+ };
75
+ var writers = {
76
+ ["dtcg" /* DTCG */]: {
77
+ write: (doc) => new DtcgJsonWriter().write(doc)
78
+ },
79
+ ["hrdt" /* HRDT */]: {
80
+ write: (doc) => new HrdtTokenWriter().write(doc)
81
+ },
82
+ ["css" /* CSS */]: {
83
+ write: (doc) => new DtcgTokenCssConverter().convertDocument(doc)
84
+ }
85
+ };
86
+ function toDocumentFormat(format, fallback = "dtcg" /* DTCG */) {
87
+ const resolved = format ?? fallback;
88
+ if (resolved === "dtcg" /* DTCG */ || resolved === "hrdt" /* HRDT */) return resolved;
89
+ throw new Error(`Unknown format "${resolved}". Available: ${"dtcg" /* DTCG */}, ${"hrdt" /* HRDT */}`);
90
+ }
91
+ function toOutputFormat(format, fallback = "css" /* CSS */) {
92
+ const resolved = format ?? fallback;
93
+ if (resolved === "dtcg" /* DTCG */ || resolved === "hrdt" /* HRDT */ || resolved === "css" /* CSS */) return resolved;
94
+ throw new Error(`Unknown format "${resolved}". Available: ${"dtcg" /* DTCG */}, ${"hrdt" /* HRDT */}, ${"css" /* CSS */}`);
95
+ }
96
+
97
+ // src/commands/convert.ts
98
+ var convertCommand = new Command2("convert").description("Convert DTCG JSON or HRDT YAML tokens to DTCG JSON, HRDT YAML, or CSS.").argument("<file>", "Path to a token JSON or HRDT file").option("-i, --inform <format>", "Input format override: dtcg, hrdt").option("-f, --outform <format>", "Output format: dtcg, hrdt, css (default: css)").option("-o, --out <file>", "Output file").action(async (file, options) => {
99
+ try {
100
+ const reader = getReader(options.inform);
101
+ const content = await new Source(file).getContent();
102
+ const doc = reader.read(content);
103
+ const writer = getWriter(options.outform);
104
+ const output = writer.write(doc);
105
+ if (options.out) {
106
+ await writeFile(options.out, output);
107
+ } else {
108
+ process.stdout.write(output);
109
+ }
110
+ } catch (error) {
111
+ console.error("Conversion failed:", error.message);
112
+ process.exit(1);
113
+ }
114
+ });
115
+
116
+ // src/commands/showcase.ts
117
+ import { Command as Command3 } from "commander";
118
+ import { writeFile as writeFile2 } from "fs/promises";
119
+ import { spawn } from "child_process";
120
+ import path from "path";
121
+ import { tmpdir } from "os";
122
+ import { Source as Source2, createTokenHtmlShowcase } from "@design-token-kit/core";
123
+ function resolveOutputPath(output) {
124
+ if (path.isAbsolute(output)) {
125
+ return output;
126
+ }
127
+ if (path.dirname(output) !== ".") {
128
+ return output;
129
+ }
130
+ return path.join(tmpdir(), output);
131
+ }
132
+ function openFile(filePath) {
133
+ const platform = process.platform;
134
+ if (platform === "win32") {
135
+ spawn("cmd", ["/c", "start", "", filePath], { stdio: "ignore", detached: true });
136
+ return;
137
+ }
138
+ if (platform === "darwin") {
139
+ spawn("open", [filePath], { stdio: "ignore", detached: true });
140
+ return;
141
+ }
142
+ spawn("xdg-open", [filePath], { stdio: "ignore", detached: true });
143
+ }
144
+ var showcaseCommand = new Command3("showcase").description("Create HTML showcase from DTCG JSON, HRDT YAML, or CSS.").argument("<files...>", "Paths to token JSON, HRDT, or CSS files").option("-o, --out <file>", "Output HTML file name or path").action(async (files, options) => {
145
+ try {
146
+ const showcase = createTokenHtmlShowcase();
147
+ const sources = files.map((f) => new Source2(f));
148
+ const filePaths = await Promise.all(sources.map((s) => s.getFile()));
149
+ const html = await showcase.showcase(filePaths);
150
+ if (options.out) {
151
+ const targetFile = resolveOutputPath(options.out);
152
+ await writeFile2(targetFile, html);
153
+ console.log(`Saved HTML to: ${targetFile}`);
154
+ openFile(targetFile);
155
+ } else {
156
+ process.stdout.write(html);
157
+ }
158
+ } catch (error) {
159
+ console.error("Showcase failed:", error.message);
160
+ process.exit(1);
161
+ }
162
+ });
163
+
164
+ // src/commands/test.ts
165
+ import { Command as Command4 } from "commander";
166
+ import { Source as Source3 } from "@design-token-kit/core";
167
+ var testCommand = new Command4("test").description("Load a DTCG JSON or HRDT YAML file, build the model, and print it back (round-trip check).").argument("<file>", "Path to a DTCG JSON or HRDT file").option("-f, --outform <format>", "Output format: dtcg, hrdt (default: same as input)").action(async (file, options) => {
168
+ try {
169
+ const reader = getDetectedReader(file);
170
+ const writer = getRoundTripWriter(options.outform, file);
171
+ const content = await new Source3(file).getContent();
172
+ const doc = reader.read(content);
173
+ process.stdout.write(writer.write(doc));
174
+ } catch (error) {
175
+ console.error(`Error: ${error.message}`);
176
+ process.exit(1);
177
+ }
178
+ });
179
+
180
+ // src/index.ts
181
+ var program = new Command5().name("dtokens").description("CLI for DTCG JSON and HRDT YAML: validate, convert, showcase.").addCommand(testCommand).addCommand(validateCommand).addCommand(convertCommand).addCommand(showcaseCommand).addHelpText("after", ({ command }) => command.name() === "dtokens" ? `
182
+ Examples:
183
+ $ dtokens test tokens.json
184
+ $ dtokens validate tokens.json
185
+ $ dtokens validate tokens.yaml
186
+ $ dtokens validate tokens.json tokens.dark.yaml --engine ajv
187
+ $ dtokens convert tokens.yaml --inform hrdt --outform css --out ./dist/tokens.css
188
+ $ dtokens convert tokens.yaml --outform dtcg
189
+ $ dtokens convert tokens.json --outform hrdt
190
+ $ dtokens showcase tokens.yaml
191
+ $ dtokens showcase tokens.css
192
+ $ dtokens showcase tokens.yaml --out out.html
193
+ $ dtokens showcase tokens.yaml --out ./dist/showcase.html
194
+ ` : "");
195
+ program.parseAsync(process.argv).catch((error) => {
196
+ console.error(error instanceof Error ? error.message : error);
197
+ process.exit(1);
198
+ });
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@design-token-kit/cli",
3
+ "version": "0.1.1",
4
+ "description": "CLI for Design Token Kit: validate, convert, showcase.",
5
+ "keywords": [
6
+ "design-tokens",
7
+ "dtcg",
8
+ "hrdt",
9
+ "tokens",
10
+ "cli",
11
+ "css"
12
+ ],
13
+ "license": "Apache-2.0",
14
+ "homepage": "https://github.com/design-token-kit/design-token-kit",
15
+ "bugs": {
16
+ "url": "https://github.com/design-token-kit/design-token-kit/issues"
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/design-token-kit/design-token-kit.git"
21
+ },
22
+ "publishConfig": {
23
+ "access": "public",
24
+ "registry": "https://registry.npmjs.org/"
25
+ },
26
+ "type": "module",
27
+ "bin": {
28
+ "dtokens": "bin/index.mjs"
29
+ },
30
+ "files": [
31
+ "bin"
32
+ ],
33
+ "imports": {
34
+ "#/*": "./src/*"
35
+ },
36
+ "engines": {
37
+ "node": ">=18"
38
+ },
39
+ "scripts": {
40
+ "build": "tsc --noEmit && tsup",
41
+ "dist": "node ../scripts/stage-package.mjs cli"
42
+ },
43
+ "dependencies": {
44
+ "@design-token-kit/core": "0.1.1",
45
+ "commander": "14.0.1"
46
+ },
47
+ "devDependencies": {
48
+ "@types/node": "24.10.1",
49
+ "tsup": "8.5.1",
50
+ "typescript": "5.9.3"
51
+ }
52
+ }