@tokens-studio/tokenscript-schemas 0.1.3 → 0.2.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 +5 -0
- package/dist/cli/index.cjs +128 -81
- package/dist/cli/index.cjs.map +1 -1
- package/dist/cli/index.js +127 -80
- package/dist/cli/index.js.map +1 -1
- package/package.json +1 -1
- package/src/cli/commands/build-dir.test.ts +354 -0
- package/src/cli/commands/build-dir.ts +90 -0
- package/src/cli/commands/bundle.ts +11 -14
- package/src/cli/index.ts +15 -0
package/dist/cli/index.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import cac from 'cac';
|
|
3
|
-
import
|
|
3
|
+
import anylogger from 'ulog';
|
|
4
|
+
import { existsSync, readFileSync } from 'fs';
|
|
4
5
|
import { mkdir, writeFile, readFile, readdir, access } from 'fs/promises';
|
|
5
6
|
import { dirname, join } from 'path';
|
|
6
7
|
import { fileURLToPath } from 'url';
|
|
7
8
|
import 'child_process';
|
|
8
|
-
import { readFileSync } from 'fs';
|
|
9
9
|
|
|
10
10
|
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
11
11
|
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
@@ -13,49 +13,6 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
|
|
|
13
13
|
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
14
14
|
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
15
15
|
});
|
|
16
|
-
|
|
17
|
-
// src/bundler/presets/css.ts
|
|
18
|
-
var css = {
|
|
19
|
-
name: "CSS",
|
|
20
|
-
description: "CSS color types",
|
|
21
|
-
types: [
|
|
22
|
-
"hex-color",
|
|
23
|
-
"rgb-color",
|
|
24
|
-
"hsl-color",
|
|
25
|
-
"oklch-color",
|
|
26
|
-
"oklab-color",
|
|
27
|
-
// Converting colors to css strings
|
|
28
|
-
"css-color"
|
|
29
|
-
],
|
|
30
|
-
functions: ["lighten", "darken", "saturate", "desaturate", "mix", "invert"]
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
// src/bundler/presets/index.ts
|
|
34
|
-
var BUNDLE_PRESETS = {
|
|
35
|
-
css
|
|
36
|
-
};
|
|
37
|
-
function expandPresetSchemas(schemas) {
|
|
38
|
-
const expanded = [];
|
|
39
|
-
for (const schema of schemas) {
|
|
40
|
-
if (schema.startsWith("preset:")) {
|
|
41
|
-
const presetName = schema.slice(7);
|
|
42
|
-
const preset = BUNDLE_PRESETS[presetName];
|
|
43
|
-
if (preset) {
|
|
44
|
-
for (const type of preset.types) {
|
|
45
|
-
expanded.push(type === "*" ? "*" : `type:${type}`);
|
|
46
|
-
}
|
|
47
|
-
for (const func of preset.functions) {
|
|
48
|
-
expanded.push(func === "*" ? "*" : `function:${func}`);
|
|
49
|
-
}
|
|
50
|
-
} else {
|
|
51
|
-
console.warn(`\u26A0 Unknown preset: ${presetName}`);
|
|
52
|
-
}
|
|
53
|
-
} else {
|
|
54
|
-
expanded.push(schema);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
return expanded;
|
|
58
|
-
}
|
|
59
16
|
async function buildSchemaFromDirectory(schemaDir, options) {
|
|
60
17
|
const schemaJsonPath = join(schemaDir, "schema.json");
|
|
61
18
|
const schemaContent = await readFile(schemaJsonPath, "utf-8");
|
|
@@ -126,6 +83,92 @@ function addBaseUrl(uri, baseUrl) {
|
|
|
126
83
|
}
|
|
127
84
|
return uri;
|
|
128
85
|
}
|
|
86
|
+
|
|
87
|
+
// src/cli/commands/build-dir.ts
|
|
88
|
+
var log = anylogger("build-dir");
|
|
89
|
+
async function buildSchemaDir(schemaDir, options = {}) {
|
|
90
|
+
const resolvedDir = resolveSchemaDir(schemaDir);
|
|
91
|
+
if (!existsSync(resolvedDir)) {
|
|
92
|
+
throw new Error(`Directory not found: ${resolvedDir}`);
|
|
93
|
+
}
|
|
94
|
+
const schemaJsonPath = join(resolvedDir, "schema.json");
|
|
95
|
+
if (!existsSync(schemaJsonPath)) {
|
|
96
|
+
throw new Error(`schema.json not found in: ${resolvedDir}`);
|
|
97
|
+
}
|
|
98
|
+
log.info(`Building schema from: ${resolvedDir}`);
|
|
99
|
+
const schema = await buildSchemaFromDirectory(resolvedDir);
|
|
100
|
+
const output = options.pretty ? JSON.stringify(schema, null, 2) : JSON.stringify(schema);
|
|
101
|
+
if (options.output) {
|
|
102
|
+
await mkdir(dirname(options.output), { recursive: true });
|
|
103
|
+
await writeFile(options.output, output, "utf-8");
|
|
104
|
+
log.info(`Output written to: ${options.output}`);
|
|
105
|
+
console.log(`\u2713 Built ${schema.type}:${schema.name} \u2192 ${options.output}`);
|
|
106
|
+
} else {
|
|
107
|
+
console.log(output);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
function resolveSchemaDir(schemaDir) {
|
|
111
|
+
if (existsSync(schemaDir)) {
|
|
112
|
+
return schemaDir;
|
|
113
|
+
}
|
|
114
|
+
const cwd = process.cwd();
|
|
115
|
+
const fromCwd = join(cwd, schemaDir);
|
|
116
|
+
if (existsSync(fromCwd)) {
|
|
117
|
+
return fromCwd;
|
|
118
|
+
}
|
|
119
|
+
return schemaDir;
|
|
120
|
+
}
|
|
121
|
+
async function handleBuildCommand(schemaDir, options = {}) {
|
|
122
|
+
try {
|
|
123
|
+
await buildSchemaDir(schemaDir, options);
|
|
124
|
+
} catch (error) {
|
|
125
|
+
log.error("Build failed:", error);
|
|
126
|
+
throw error;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// src/bundler/presets/css.ts
|
|
131
|
+
var css = {
|
|
132
|
+
name: "CSS",
|
|
133
|
+
description: "CSS color types",
|
|
134
|
+
types: [
|
|
135
|
+
"hex-color",
|
|
136
|
+
"rgb-color",
|
|
137
|
+
"hsl-color",
|
|
138
|
+
"oklch-color",
|
|
139
|
+
"oklab-color",
|
|
140
|
+
// Converting colors to css strings
|
|
141
|
+
"css-color"
|
|
142
|
+
],
|
|
143
|
+
functions: ["lighten", "darken", "saturate", "desaturate", "mix", "invert"]
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
// src/bundler/presets/index.ts
|
|
147
|
+
var BUNDLE_PRESETS = {
|
|
148
|
+
css
|
|
149
|
+
};
|
|
150
|
+
function expandPresetSchemas(schemas) {
|
|
151
|
+
const expanded = [];
|
|
152
|
+
for (const schema of schemas) {
|
|
153
|
+
if (schema.startsWith("preset:")) {
|
|
154
|
+
const presetName = schema.slice(7);
|
|
155
|
+
const preset = BUNDLE_PRESETS[presetName];
|
|
156
|
+
if (preset) {
|
|
157
|
+
for (const type of preset.types) {
|
|
158
|
+
expanded.push(type === "*" ? "*" : `type:${type}`);
|
|
159
|
+
}
|
|
160
|
+
for (const func of preset.functions) {
|
|
161
|
+
expanded.push(func === "*" ? "*" : `function:${func}`);
|
|
162
|
+
}
|
|
163
|
+
} else {
|
|
164
|
+
console.warn(`\u26A0 Unknown preset: ${presetName}`);
|
|
165
|
+
}
|
|
166
|
+
} else {
|
|
167
|
+
expanded.push(schema);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return expanded;
|
|
171
|
+
}
|
|
129
172
|
var LOG_LEVELS = {
|
|
130
173
|
error: 1,
|
|
131
174
|
warn: 2,
|
|
@@ -133,9 +176,9 @@ var LOG_LEVELS = {
|
|
|
133
176
|
log: 4,
|
|
134
177
|
debug: 5
|
|
135
178
|
};
|
|
136
|
-
var
|
|
179
|
+
var log2 = anylogger("schema-registry");
|
|
137
180
|
var logLevel = process.env.LOG_LEVEL || "error";
|
|
138
|
-
|
|
181
|
+
log2.level = LOG_LEVELS[logLevel] || LOG_LEVELS.error;
|
|
139
182
|
|
|
140
183
|
// src/utils/type.ts
|
|
141
184
|
var isSome = (v) => {
|
|
@@ -267,7 +310,7 @@ async function collectRequiredSchemas(slugOrUri, type, options = {}) {
|
|
|
267
310
|
async function traverse(currentSlugOrUri, currentType) {
|
|
268
311
|
const ref = resolveSchemaReference(currentSlugOrUri);
|
|
269
312
|
if (!ref) {
|
|
270
|
-
|
|
313
|
+
log2.warn(`Could not resolve schema reference: ${currentSlugOrUri}`);
|
|
271
314
|
return;
|
|
272
315
|
}
|
|
273
316
|
const effectiveType = currentType || ref.type;
|
|
@@ -284,7 +327,7 @@ async function collectRequiredSchemas(slugOrUri, type, options = {}) {
|
|
|
284
327
|
const schemaDir = join(resolvedSchemasDir, categoryDir, slug);
|
|
285
328
|
spec = await buildSchemaFromDirectory(schemaDir, baseUrl ? { baseUrl } : void 0);
|
|
286
329
|
} catch (error) {
|
|
287
|
-
|
|
330
|
+
log2.warn(`Failed to load schema ${slug} (${effectiveType}):`, error);
|
|
288
331
|
return;
|
|
289
332
|
}
|
|
290
333
|
const requirements = extractRequirements(spec, extractOptions);
|
|
@@ -349,7 +392,7 @@ async function collectDependencyTree(schemas, options = {}) {
|
|
|
349
392
|
dependencies: dependencySlugs
|
|
350
393
|
});
|
|
351
394
|
} catch (error) {
|
|
352
|
-
|
|
395
|
+
log2.warn(`Failed to load schema ${schema.slug} (${schema.type}):`, error);
|
|
353
396
|
}
|
|
354
397
|
}
|
|
355
398
|
return tree;
|
|
@@ -540,7 +583,7 @@ function generateOutput(options) {
|
|
|
540
583
|
}
|
|
541
584
|
|
|
542
585
|
// src/cli/commands/bundle.ts
|
|
543
|
-
var
|
|
586
|
+
var log3 = anylogger("bundle");
|
|
544
587
|
async function loadConfig(configPath) {
|
|
545
588
|
try {
|
|
546
589
|
const content = await readFile(configPath, "utf-8");
|
|
@@ -605,28 +648,24 @@ function findSchemasDir() {
|
|
|
605
648
|
const __dirname2 = dirname(__filename2);
|
|
606
649
|
const fromDist = join(__dirname2, "../../src/schemas");
|
|
607
650
|
const fromSource = join(__dirname2, "../../schemas");
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
if (fs.existsSync(fromDist)) {
|
|
611
|
-
return fromDist;
|
|
612
|
-
}
|
|
613
|
-
if (fs.existsSync(fromSource)) {
|
|
614
|
-
return fromSource;
|
|
615
|
-
}
|
|
616
|
-
} catch {
|
|
651
|
+
if (existsSync(fromSource)) {
|
|
652
|
+
return fromSource;
|
|
617
653
|
}
|
|
618
|
-
|
|
654
|
+
if (existsSync(fromDist)) {
|
|
655
|
+
return fromDist;
|
|
656
|
+
}
|
|
657
|
+
return fromSource;
|
|
619
658
|
}
|
|
620
659
|
async function bundleSchemas(schemas, schemasDir, cliArgs) {
|
|
621
660
|
const resolvedSchemasDir = schemasDir || findSchemasDir();
|
|
622
|
-
|
|
623
|
-
|
|
661
|
+
log3.info("Bundling schemas:", schemas);
|
|
662
|
+
log3.debug("Schemas directory:", resolvedSchemasDir);
|
|
624
663
|
const result = await bundleSelectiveSchemas({
|
|
625
664
|
schemas,
|
|
626
665
|
schemasDir: resolvedSchemasDir,
|
|
627
666
|
cliArgs
|
|
628
667
|
});
|
|
629
|
-
|
|
668
|
+
log3.info(
|
|
630
669
|
`Resolved ${result.metadata.resolvedDependencies.length} schemas (including dependencies)`
|
|
631
670
|
);
|
|
632
671
|
const output = generateOutput({
|
|
@@ -645,7 +684,7 @@ async function handleBundleCommand(schemas, options = {}) {
|
|
|
645
684
|
let configSchemas = schemas;
|
|
646
685
|
let outputPath = options.output || "./tokenscript-schemas.js";
|
|
647
686
|
if (isSome(options.config)) {
|
|
648
|
-
|
|
687
|
+
log3.info(`Loading config from ${options.config}`);
|
|
649
688
|
const config = await loadConfig(options.config);
|
|
650
689
|
configSchemas = config.schemas;
|
|
651
690
|
if (config.output) {
|
|
@@ -674,7 +713,7 @@ async function handleBundleCommand(schemas, options = {}) {
|
|
|
674
713
|
}
|
|
675
714
|
const customSchemasDir = options.schemasDir;
|
|
676
715
|
if (customSchemasDir) {
|
|
677
|
-
|
|
716
|
+
log3.info(`Using custom schema directory: ${customSchemasDir}`);
|
|
678
717
|
}
|
|
679
718
|
const { output, metadata, dependencyTree } = await bundleSchemas(
|
|
680
719
|
configSchemas,
|
|
@@ -691,15 +730,15 @@ async function handleBundleCommand(schemas, options = {}) {
|
|
|
691
730
|
}
|
|
692
731
|
await mkdir(dirname(outputPath), { recursive: true });
|
|
693
732
|
await writeFile(outputPath, output, "utf-8");
|
|
694
|
-
|
|
695
|
-
|
|
733
|
+
log3.info(`Successfully bundled ${metadata.resolvedDependencies.length} schemas`);
|
|
734
|
+
log3.info(`Output written to: ${outputPath}`);
|
|
696
735
|
console.log(`\u2713 Bundled ${metadata.resolvedDependencies.length} schemas \u2192 ${outputPath}`);
|
|
697
736
|
} catch (error) {
|
|
698
|
-
|
|
737
|
+
log3.error("Bundle failed:", error);
|
|
699
738
|
throw error;
|
|
700
739
|
}
|
|
701
740
|
}
|
|
702
|
-
var
|
|
741
|
+
var log4 = anylogger("list");
|
|
703
742
|
function findSchemasDir2() {
|
|
704
743
|
const __filename2 = fileURLToPath(import.meta.url);
|
|
705
744
|
const __dirname2 = dirname(__filename2);
|
|
@@ -731,7 +770,7 @@ async function listSchemas(schemasDir, options = {}) {
|
|
|
731
770
|
...typeEntries.filter((entry) => entry.isDirectory()).map((entry) => entry.name).sort()
|
|
732
771
|
);
|
|
733
772
|
} catch (error) {
|
|
734
|
-
|
|
773
|
+
log4.warn("Could not read types directory:", error);
|
|
735
774
|
}
|
|
736
775
|
}
|
|
737
776
|
if (showFunctions) {
|
|
@@ -742,7 +781,7 @@ async function listSchemas(schemasDir, options = {}) {
|
|
|
742
781
|
...funcEntries.filter((entry) => entry.isDirectory()).map((entry) => entry.name).sort()
|
|
743
782
|
);
|
|
744
783
|
} catch (error) {
|
|
745
|
-
|
|
784
|
+
log4.warn("Could not read functions directory:", error);
|
|
746
785
|
}
|
|
747
786
|
}
|
|
748
787
|
return { types, functions };
|
|
@@ -774,7 +813,7 @@ async function handleListCommand(options = {}) {
|
|
|
774
813
|
const output = formatListOutput(result, options);
|
|
775
814
|
console.log(output);
|
|
776
815
|
}
|
|
777
|
-
var
|
|
816
|
+
var log5 = anylogger("presets");
|
|
778
817
|
function formatPresetInfo() {
|
|
779
818
|
const lines = [];
|
|
780
819
|
lines.push("=".repeat(60));
|
|
@@ -818,24 +857,32 @@ function formatPresetInfo() {
|
|
|
818
857
|
}
|
|
819
858
|
async function handlePresetsCommand() {
|
|
820
859
|
try {
|
|
821
|
-
|
|
860
|
+
log5.info("Listing available presets");
|
|
822
861
|
const output = formatPresetInfo();
|
|
823
862
|
console.log(output);
|
|
824
|
-
|
|
863
|
+
log5.info(`Listed ${Object.keys(BUNDLE_PRESETS).length} presets`);
|
|
825
864
|
} catch (error) {
|
|
826
|
-
|
|
865
|
+
log5.error("Failed to list presets:", error);
|
|
827
866
|
throw error;
|
|
828
867
|
}
|
|
829
868
|
}
|
|
830
869
|
|
|
831
870
|
// src/cli/index.ts
|
|
832
|
-
var
|
|
871
|
+
var log6 = anylogger("cli");
|
|
833
872
|
var cli = cac("tokenscript-schemas");
|
|
834
873
|
cli.command("bundle [...schemas]", "Bundle schemas into a JS file").option("-c, --config <path>", "Path to config file").option("-o, --output <path>", "Output file path", { default: "./tokenscript-schemas.js" }).option("-d, --dry-run", "Preview what would be bundled without writing").option("-s, --schemas-dir <path>", "Custom schema directory (overrides default)").action(async (schemas, options) => {
|
|
835
874
|
try {
|
|
836
875
|
await handleBundleCommand(schemas, options);
|
|
837
876
|
} catch (error) {
|
|
838
|
-
|
|
877
|
+
log6.error("Error:", error);
|
|
878
|
+
process.exit(1);
|
|
879
|
+
}
|
|
880
|
+
});
|
|
881
|
+
cli.command("build <directory>", "Build an individual schema directory").option("-o, --output <path>", "Output file path (defaults to stdout)").option("-p, --pretty", "Pretty print JSON output").action(async (directory, options) => {
|
|
882
|
+
try {
|
|
883
|
+
await handleBuildCommand(directory, options);
|
|
884
|
+
} catch (error) {
|
|
885
|
+
log6.error("Error:", error);
|
|
839
886
|
process.exit(1);
|
|
840
887
|
}
|
|
841
888
|
});
|
|
@@ -843,7 +890,7 @@ cli.command("list", "List available schemas").option("--types", "List only type
|
|
|
843
890
|
try {
|
|
844
891
|
await handleListCommand(options);
|
|
845
892
|
} catch (error) {
|
|
846
|
-
|
|
893
|
+
log6.error("Error:", error);
|
|
847
894
|
process.exit(1);
|
|
848
895
|
}
|
|
849
896
|
});
|
|
@@ -851,7 +898,7 @@ cli.command("presets", "List available bundle presets").action(async () => {
|
|
|
851
898
|
try {
|
|
852
899
|
await handlePresetsCommand();
|
|
853
900
|
} catch (error) {
|
|
854
|
-
|
|
901
|
+
log6.error("Error:", error);
|
|
855
902
|
process.exit(1);
|
|
856
903
|
}
|
|
857
904
|
});
|