design-constraint-validator 1.0.0 → 2.0.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.
- package/LICENSE +21 -21
- package/README.md +229 -659
- package/adapters/README.md +46 -46
- package/adapters/css.ts +116 -116
- package/adapters/decisionthemes.d.ts +44 -0
- package/adapters/decisionthemes.d.ts.map +1 -0
- package/adapters/decisionthemes.js +35 -0
- package/adapters/decisionthemes.ts +59 -0
- package/adapters/js.ts +14 -14
- package/adapters/json.ts +45 -45
- package/cli/build-css.ts +32 -32
- package/cli/commands/build.ts +65 -65
- package/cli/commands/graph.d.ts.map +1 -1
- package/cli/commands/graph.js +26 -10
- package/cli/commands/graph.ts +180 -137
- package/cli/commands/index.ts +7 -7
- package/cli/commands/patch-apply.ts +80 -80
- package/cli/commands/patch.ts +22 -22
- package/cli/commands/set.d.ts.map +1 -1
- package/cli/commands/set.js +12 -4
- package/cli/commands/set.ts +239 -225
- package/cli/commands/utils.ts +50 -50
- package/cli/commands/validate.d.ts.map +1 -1
- package/cli/commands/validate.js +89 -33
- package/cli/commands/validate.ts +180 -115
- package/cli/commands/why.d.ts.map +1 -1
- package/cli/commands/why.js +86 -20
- package/cli/commands/why.ts +158 -46
- package/cli/config-schema.ts +27 -27
- package/cli/config.ts +35 -35
- package/cli/constraint-registry.d.ts +101 -0
- package/cli/constraint-registry.d.ts.map +1 -0
- package/cli/constraint-registry.js +225 -0
- package/cli/constraint-registry.ts +304 -0
- package/cli/constraints-loader.d.ts.map +1 -0
- package/cli/cross-axis-loader.d.ts +91 -0
- package/cli/cross-axis-loader.d.ts.map +1 -0
- package/cli/cross-axis-loader.js +222 -0
- package/cli/cross-axis-loader.ts +289 -0
- package/cli/dcv.js +4 -0
- package/cli/dcv.ts +111 -107
- package/cli/engine-helpers.d.ts.map +1 -1
- package/cli/graph-poset.ts +74 -74
- package/cli/json-output.d.ts +69 -0
- package/cli/json-output.d.ts.map +1 -0
- package/cli/json-output.js +109 -0
- package/cli/json-output.ts +184 -0
- package/cli/result.ts +27 -27
- package/cli/run.ts +54 -54
- package/cli/smoke-test.ts +40 -40
- package/cli/types.d.ts +6 -0
- package/cli/types.d.ts.map +1 -1
- package/cli/types.ts +84 -78
- package/cli/version-banner.d.ts +20 -0
- package/cli/version-banner.d.ts.map +1 -0
- package/cli/version-banner.js +49 -0
- package/cli/version-banner.ts +61 -0
- package/core/breakpoints.ts +50 -50
- package/core/cli-format.ts +31 -31
- package/core/color.ts +148 -148
- package/core/constraints/cross-axis.ts +114 -114
- package/core/constraints/monotonic-lightness.ts +38 -38
- package/core/constraints/monotonic.ts +74 -74
- package/core/constraints/threshold.ts +43 -43
- package/core/constraints/wcag.ts +70 -70
- package/core/cross-axis-config.d.ts.map +1 -1
- package/core/engine.d.ts +95 -0
- package/core/engine.d.ts.map +1 -1
- package/core/engine.js +22 -0
- package/core/engine.ts +167 -65
- package/core/flatten.ts +116 -116
- package/core/image-export.ts +48 -48
- package/core/index.d.ts +9 -30
- package/core/index.d.ts.map +1 -1
- package/core/index.js +7 -54
- package/core/index.ts +10 -72
- package/core/patch.ts +134 -134
- package/core/poset.ts +311 -311
- package/core/why.ts +63 -63
- package/package.json +96 -90
- package/themes/color.lg.order.json +15 -15
- package/themes/color.md.order.json +15 -15
- package/themes/color.order.json +15 -15
- package/themes/color.sm.order.json +15 -15
- package/themes/cross-axis.rules.json +35 -35
- package/themes/cross-axis.sm.rules.json +12 -12
- package/themes/layout.lg.order.json +18 -18
- package/themes/layout.md.order.json +18 -18
- package/themes/layout.order.json +18 -18
- package/themes/layout.sm.order.json +18 -18
- package/themes/spacing.order.json +14 -14
- package/themes/typography.lg.order.json +15 -15
- package/themes/typography.md.order.json +15 -15
- package/themes/typography.order.json +15 -15
- package/themes/typography.sm.order.json +15 -15
- package/cli/engine-helpers.d.ts +0 -8
- package/cli/engine-helpers.js +0 -70
- package/cli/engine-helpers.ts +0 -61
- package/core/cross-axis-config.d.ts +0 -5
- package/core/cross-axis-config.js +0 -144
- package/core/cross-axis-config.ts +0 -152
- package/dist/test-overrides-removal.json +0 -4
- package/dist/tmp.patch.json +0 -35
- package/tokens/overrides/base.json +0 -22
- package/tokens/overrides/lg.json +0 -20
- package/tokens/overrides/md.json +0 -16
- package/tokens/overrides/sm.json +0 -16
- package/tokens/overrides/viol.color.json +0 -6
- package/tokens/overrides/viol.typography.json +0 -6
- package/tokens/tokens.demo-violations.json +0 -116
- package/tokens/tokens.example.json +0 -128
- package/tokens/tokens.json +0 -67
- package/tokens/tokens.multi-violations.json +0 -21
- package/tokens/tokens.schema.d.ts +0 -2298
- package/tokens/tokens.schema.d.ts.map +0 -1
- package/tokens/tokens.schema.js +0 -148
- package/tokens/tokens.schema.ts +0 -196
- package/tokens/tokens.test.json +0 -38
- package/tokens/tokens.touch-violation.json +0 -8
- package/tokens/typography.classes.css +0 -11
- package/tokens/typography.css +0 -20
package/cli/commands/set.ts
CHANGED
|
@@ -1,225 +1,239 @@
|
|
|
1
|
-
import { join } from 'node:path';
|
|
2
|
-
import { readFileSync, existsSync } from 'node:fs';
|
|
3
|
-
import { loadConfig } from '../config.js';
|
|
4
|
-
import {
|
|
5
|
-
import { flattenTokens } from '../../core/flatten.js';
|
|
6
|
-
import type { OverridesTree, SetOptions, ValuesPatch } from '../types.js';
|
|
7
|
-
import { loadTokens, outputResult } from './utils.js';
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
dp[0]
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
const
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
function
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
if (
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
const
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
const
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
const
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
function
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
const
|
|
145
|
-
if (
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
if (
|
|
158
|
-
|
|
159
|
-
if (
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
const
|
|
169
|
-
|
|
170
|
-
const
|
|
171
|
-
if (
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
if (
|
|
175
|
-
else
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
const
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
const
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
if (options.
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
1
|
+
import { join } from 'node:path';
|
|
2
|
+
import { readFileSync, existsSync } from 'node:fs';
|
|
3
|
+
import { loadConfig } from '../config.js';
|
|
4
|
+
import { Engine } from '../../core/engine.js';
|
|
5
|
+
import { flattenTokens, type FlatToken } from '../../core/flatten.js';
|
|
6
|
+
import type { OverridesTree, SetOptions, ValuesPatch } from '../types.js';
|
|
7
|
+
import { loadTokens, outputResult } from './utils.js';
|
|
8
|
+
import { setupConstraints } from '../constraint-registry.js';
|
|
9
|
+
|
|
10
|
+
// Lightweight suggestion helpers (kept local – why command uses core formatter instead)
|
|
11
|
+
function levenshtein(a: string, b: string): number {
|
|
12
|
+
const al = a.length, bl = b.length;
|
|
13
|
+
const dp: number[] = Array(bl + 1).fill(0).map((_, i) => i);
|
|
14
|
+
for (let i = 1; i <= al; i++) {
|
|
15
|
+
let prev = dp[0];
|
|
16
|
+
dp[0] = i;
|
|
17
|
+
for (let j = 1; j <= bl; j++) {
|
|
18
|
+
const tmp = dp[j];
|
|
19
|
+
const cost = a[i - 1] === b[j - 1] ? 0 : 1;
|
|
20
|
+
dp[j] = Math.min(dp[j] + 1, dp[j - 1] + 1, prev + cost);
|
|
21
|
+
prev = tmp;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return dp[bl];
|
|
25
|
+
}
|
|
26
|
+
function suggestIds(id: string, candidates: string[], k = 3): { id: string; d: number }[] {
|
|
27
|
+
return candidates.map(c => ({ id: c, d: levenshtein(id, c) }))
|
|
28
|
+
.sort((a, b) => a.d - b.d).slice(0, k);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function parseSetExpression(expr: string): { id: string; value: string | number } {
|
|
32
|
+
const match = expr.match(/^([^=]+)=(.+)$/);
|
|
33
|
+
if (!match) throw new Error(`Invalid set expression: ${expr}. Use format: token.id=value`);
|
|
34
|
+
const [, id, rawValue] = match;
|
|
35
|
+
const numValue = Number(rawValue);
|
|
36
|
+
const value = !isNaN(numValue) && isFinite(numValue) ? numValue : rawValue;
|
|
37
|
+
return { id: id.trim(), value };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Batch parsing helpers (scoped to set command)
|
|
41
|
+
type BatchEntry = { id: string; value?: unknown; unset?: boolean };
|
|
42
|
+
function isPlainObject(x: unknown) { return x && typeof x === 'object' && !Array.isArray(x); }
|
|
43
|
+
function collectDeep(obj: unknown, prefix: string[] = [], out: BatchEntry[] = []): BatchEntry[] {
|
|
44
|
+
if (!isPlainObject(obj)) { if (prefix.length) out.push({ id: prefix.join('.'), value: obj, unset: obj === null }); return out; }
|
|
45
|
+
const hasValue = Object.prototype.hasOwnProperty.call(obj, '$value');
|
|
46
|
+
const hasUnset = Object.prototype.hasOwnProperty.call(obj, '$unset');
|
|
47
|
+
if (hasValue || hasUnset) {
|
|
48
|
+
const rec = obj as { [k: string]: unknown; $value?: unknown; $unset?: boolean };
|
|
49
|
+
const v = rec.$value; const u = rec.$unset === true || v === null;
|
|
50
|
+
if (prefix.length) out.push({ id: prefix.join('.'), value: v, unset: u });
|
|
51
|
+
return out;
|
|
52
|
+
}
|
|
53
|
+
for (const k of Object.keys(obj as object)) {
|
|
54
|
+
collectDeep((obj as Record<string, unknown>)[k], [...prefix, k], out);
|
|
55
|
+
}
|
|
56
|
+
return out;
|
|
57
|
+
}
|
|
58
|
+
function normalizeBatch(raw: unknown): BatchEntry[] {
|
|
59
|
+
if (Array.isArray(raw)) return raw.map(e => ({ id: e.id, value: Object.prototype.hasOwnProperty.call(e, 'value') ? e.value : (Object.prototype.hasOwnProperty.call(e, '$value') ? e.$value : undefined), unset: e.unset === true || e.$unset === true || e.value === null || e.$value === null }));
|
|
60
|
+
if (isPlainObject(raw)) {
|
|
61
|
+
const hasNested = Object.values(raw as Record<string, unknown>).some(v => isPlainObject(v));
|
|
62
|
+
if (hasNested) {
|
|
63
|
+
const entries = collectDeep(raw).filter(e => e.id && (e.unset || typeof e.value !== 'undefined'));
|
|
64
|
+
if (entries.length) return entries;
|
|
65
|
+
}
|
|
66
|
+
const r = raw as Record<string, unknown>;
|
|
67
|
+
return Object.keys(r).map(k => ({ id: k, value: r[k], unset: r[k] === null }));
|
|
68
|
+
}
|
|
69
|
+
return [];
|
|
70
|
+
}
|
|
71
|
+
function parseValue(v: string): string | number { const num = Number(v); return !isNaN(num) && isFinite(num) ? num : v; }
|
|
72
|
+
|
|
73
|
+
export async function setCommand(options: SetOptions): Promise<void> {
|
|
74
|
+
const debugSet = process.env.DCV_DEBUG_SET === '1' || process.argv.includes('--debug-set');
|
|
75
|
+
const tokensPath = options.tokens || 'tokens/tokens.json';
|
|
76
|
+
const cfgRes = loadConfig(options.config);
|
|
77
|
+
if (!cfgRes.ok) { console.error(cfgRes.error); process.exit(2); }
|
|
78
|
+
const config = cfgRes.value;
|
|
79
|
+
const tokens = loadTokens(tokensPath);
|
|
80
|
+
|
|
81
|
+
// Create engine with flattened tokens
|
|
82
|
+
const { flat, edges } = flattenTokens(tokens);
|
|
83
|
+
const init: Record<string, string | number> = {};
|
|
84
|
+
for (const t of Object.values(flat)) {
|
|
85
|
+
init[(t as FlatToken).id] = (t as FlatToken).value;
|
|
86
|
+
}
|
|
87
|
+
const engine = new Engine(init, edges);
|
|
88
|
+
const knownIds = new Set(Object.keys(init));
|
|
89
|
+
|
|
90
|
+
// Discover and attach all constraints via centralized registry
|
|
91
|
+
setupConstraints(
|
|
92
|
+
engine,
|
|
93
|
+
{ config, constraintsDir: 'themes' },
|
|
94
|
+
{ knownIds },
|
|
95
|
+
);
|
|
96
|
+
function ensureKnownOrSuggest(id: string) {
|
|
97
|
+
if (!knownIds.has(id)) {
|
|
98
|
+
const suggestions = suggestIds(id, Array.from(knownIds));
|
|
99
|
+
console.error(`Unknown id: ${id}`);
|
|
100
|
+
if (suggestions.length) {
|
|
101
|
+
console.error('Did you mean:');
|
|
102
|
+
suggestions.forEach(s => console.error(` - ${s.id} (d=${s.d})`));
|
|
103
|
+
}
|
|
104
|
+
process.exit(1);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (options.theme) {
|
|
109
|
+
const themePath = join('tokens/themes', `${options.theme}.json`);
|
|
110
|
+
if (existsSync(themePath)) {
|
|
111
|
+
const themeTokens = JSON.parse(readFileSync(themePath, 'utf8'));
|
|
112
|
+
for (const [id, value] of Object.entries(themeTokens)) {
|
|
113
|
+
engine.commit(id, value as string | number);
|
|
114
|
+
}
|
|
115
|
+
} else {
|
|
116
|
+
console.warn(`Theme file not found: ${themePath}`);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
let finalResult: ValuesPatch = {};
|
|
121
|
+
|
|
122
|
+
function setDeep(obj: OverridesTree, parts: string[], v: unknown) {
|
|
123
|
+
let cur: OverridesTree = obj;
|
|
124
|
+
for (let i = 0; i < parts.length; i++) {
|
|
125
|
+
const k = parts[i];
|
|
126
|
+
const leaf = i === parts.length - 1;
|
|
127
|
+
if (leaf) {
|
|
128
|
+
const existing = (cur[k] && typeof cur[k] === 'object') ? cur[k] as OverridesTree : {} as OverridesTree;
|
|
129
|
+
cur[k] = { ...existing, $value: v as string | number | null | undefined };
|
|
130
|
+
} else {
|
|
131
|
+
if (!cur[k] || typeof cur[k] !== 'object') cur[k] = {} as OverridesTree;
|
|
132
|
+
cur = cur[k] as OverridesTree;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
function deleteDeep(obj: OverridesTree, parts: string[]): boolean {
|
|
137
|
+
if (!obj || typeof obj !== 'object') return false;
|
|
138
|
+
const [k, ...rest] = parts;
|
|
139
|
+
if (!(k in obj)) return false;
|
|
140
|
+
if (rest.length === 0) {
|
|
141
|
+
delete obj[k];
|
|
142
|
+
} else {
|
|
143
|
+
const child = obj[k];
|
|
144
|
+
const removed = child && typeof child === 'object' ? deleteDeep(child as OverridesTree, rest) : false;
|
|
145
|
+
if (removed || (child && typeof child === 'object' && Object.keys(child).length === 0)) delete obj[k];
|
|
146
|
+
}
|
|
147
|
+
return Object.keys(obj).length === 0;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Batch JSON mode
|
|
151
|
+
const rawArgv = process.argv.slice(2);
|
|
152
|
+
const jsonFlagIdx = rawArgv.indexOf('--json');
|
|
153
|
+
let jsonSource: string | undefined = options.json;
|
|
154
|
+
if (!jsonSource && jsonFlagIdx !== -1) {
|
|
155
|
+
const candidate = rawArgv[jsonFlagIdx + 1];
|
|
156
|
+
if (candidate && !candidate.startsWith('--')) jsonSource = candidate;
|
|
157
|
+
if (!jsonSource && rawArgv[jsonFlagIdx].includes('=')) {
|
|
158
|
+
const eq = rawArgv[jsonFlagIdx].split('=')[1];
|
|
159
|
+
if (eq) jsonSource = eq;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
if (!jsonSource && options.expressions?.includes('-')) {
|
|
163
|
+
jsonSource = '-';
|
|
164
|
+
options.expressions = options.expressions.filter(e => e !== '-');
|
|
165
|
+
}
|
|
166
|
+
if (jsonSource) {
|
|
167
|
+
const fs = await import('node:fs');
|
|
168
|
+
const rawText = jsonSource === '-' ? fs.readFileSync(0, 'utf8') : fs.readFileSync(jsonSource, 'utf8');
|
|
169
|
+
let parsed: unknown;
|
|
170
|
+
try { parsed = JSON.parse(rawText); } catch (e) { const msg = e instanceof Error ? e.message : String(e); console.error('Failed to parse JSON batch:', msg); process.exit(2); }
|
|
171
|
+
if (debugSet) {
|
|
172
|
+
console.log('[set:batch] raw length:', rawText.length);
|
|
173
|
+
if (Array.isArray(parsed)) console.log('[set:batch] parsed top-level: (array)');
|
|
174
|
+
else if (parsed && typeof parsed === 'object') console.log('[set:batch] parsed top-level keys:', Object.keys(parsed as Record<string, unknown>));
|
|
175
|
+
else console.log('[set:batch] parsed value non-object');
|
|
176
|
+
}
|
|
177
|
+
const batchEntries = normalizeBatch(parsed);
|
|
178
|
+
for (const expr of options.expressions || []) { const { id, value } = parseSetExpression(expr); batchEntries.push({ id, value }); }
|
|
179
|
+
for (const u of (options.unset as unknown as string[] || [])) batchEntries.push({ id: u, unset: true });
|
|
180
|
+
const map = new Map<string, BatchEntry>();
|
|
181
|
+
for (const ent of batchEntries) { if (!ent.id) continue; map.set(ent.id, ent); }
|
|
182
|
+
const entries = Array.from(map.values());
|
|
183
|
+
for (const ent of entries) ensureKnownOrSuggest(ent.id);
|
|
184
|
+
const debug = process.argv.includes('--debug-set') || process.env.DCV_DEBUG_SET === '1';
|
|
185
|
+
if (debug) { console.log('[set:batch] parsed entries:'); for (const e of entries) console.log(' ', e); }
|
|
186
|
+
const dryRun = process.argv.includes('--dry-run');
|
|
187
|
+
for (const { id, value, unset } of entries) {
|
|
188
|
+
if (unset) { if (!options.quiet) console.log(`preview: unset ${id}`); }
|
|
189
|
+
else {
|
|
190
|
+
const commitVal = parseValue(String(value));
|
|
191
|
+
if (!options.quiet) console.log(`preview: ${id} = ${commitVal}`);
|
|
192
|
+
if (!dryRun) { const result = engine.commit(id, commitVal); Object.assign(finalResult, result.patch); }
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
const format = options.format || 'json';
|
|
196
|
+
outputResult(finalResult, format, options.output);
|
|
197
|
+
if (options.write && !dryRun) {
|
|
198
|
+
const path = 'tokens/overrides/local.json';
|
|
199
|
+
let local: OverridesTree = {} as OverridesTree;
|
|
200
|
+
try { local = JSON.parse(fs.readFileSync(path, 'utf8')); } catch { /* new */ }
|
|
201
|
+
for (const { id, value, unset } of entries) { if (unset) deleteDeep(local, id.split('.')); else setDeep(local, id.split('.'), value); }
|
|
202
|
+
fs.mkdirSync('tokens/overrides', { recursive: true });
|
|
203
|
+
fs.writeFileSync(path, JSON.stringify(local, null, 2) + '\n');
|
|
204
|
+
console.log(`✓ Applied ${entries.length} change(s) to ${path}`);
|
|
205
|
+
} else if (dryRun && options.write) { console.log('Dry-run: changes not written'); }
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// Positional expressions path
|
|
210
|
+
for (const expr of options.expressions) {
|
|
211
|
+
const { id, value } = parseSetExpression(expr);
|
|
212
|
+
ensureKnownOrSuggest(id);
|
|
213
|
+
const result = engine.commit(id, value);
|
|
214
|
+
if (!options.quiet) {
|
|
215
|
+
console.log(`Set ${id} = ${value}`);
|
|
216
|
+
if (result.affected.length > 0) console.log(`Affected tokens: ${result.affected.join(', ')}`);
|
|
217
|
+
if (result.issues.length > 0) console.warn(`Issues: ${result.issues.map(i => i.message).join(', ')}`);
|
|
218
|
+
}
|
|
219
|
+
Object.assign(finalResult, result.patch);
|
|
220
|
+
}
|
|
221
|
+
const format = options.format || 'json';
|
|
222
|
+
outputResult(finalResult, format, options.output);
|
|
223
|
+
if (options.write || (options.unset && options.unset.length)) {
|
|
224
|
+
const fs = await import('node:fs');
|
|
225
|
+
const path = 'tokens/overrides/local.json';
|
|
226
|
+
let local: OverridesTree = {} as OverridesTree;
|
|
227
|
+
try { local = JSON.parse(fs.readFileSync(path, 'utf8')); } catch { /* new */ }
|
|
228
|
+
if (options.write) {
|
|
229
|
+
for (const expr of options.expressions) { const { id, value } = parseSetExpression(expr); setDeep(local, id.split('.'), value); }
|
|
230
|
+
}
|
|
231
|
+
if (options.unset) { for (const id of options.unset) deleteDeep(local, id.split('.')); }
|
|
232
|
+
const total = (options.write ? options.expressions.length : 0) + (options.unset?.length || 0);
|
|
233
|
+
if (total) {
|
|
234
|
+
fs.mkdirSync('tokens/overrides', { recursive: true });
|
|
235
|
+
fs.writeFileSync(path, JSON.stringify(local, null, 2) + '\n', 'utf8');
|
|
236
|
+
console.log(`Persisted override update (${options.write ? options.expressions.length : 0} set, ${options.unset?.length || 0} unset) to ${path}`);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
package/cli/commands/utils.ts
CHANGED
|
@@ -1,50 +1,50 @@
|
|
|
1
|
-
import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'node:fs';
|
|
2
|
-
import { resolve, dirname } from 'node:path';
|
|
3
|
-
import { valuesToCss } from '../../adapters/css.js';
|
|
4
|
-
import type { TokenNode, TokenValue } from '../../core/flatten.js';
|
|
5
|
-
|
|
6
|
-
// Shared helpers for command modules
|
|
7
|
-
|
|
8
|
-
export function loadTokens(tokensPath: string): TokenNode {
|
|
9
|
-
const resolvedPath = resolve(tokensPath);
|
|
10
|
-
if (!existsSync(resolvedPath)) {
|
|
11
|
-
throw new Error(`Token file not found: ${resolvedPath}`);
|
|
12
|
-
}
|
|
13
|
-
const data = JSON.parse(readFileSync(resolvedPath, 'utf8'));
|
|
14
|
-
if (typeof data !== 'object' || data === null) {
|
|
15
|
-
throw new Error(`Invalid token file: expected object, got ${typeof data}`);
|
|
16
|
-
}
|
|
17
|
-
return data as TokenNode;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export function outputResult(data: unknown, format: string, outputPath?: string): void {
|
|
21
|
-
let content: string;
|
|
22
|
-
switch (format) {
|
|
23
|
-
case 'json':
|
|
24
|
-
content = JSON.stringify(data, null, 2);
|
|
25
|
-
break;
|
|
26
|
-
case 'css': {
|
|
27
|
-
if (data && typeof data === 'object' && 'patch' in (data as { patch?: unknown })) {
|
|
28
|
-
const patch = (data as { patch: Record<string, TokenValue> }).patch;
|
|
29
|
-
content = valuesToCss(patch);
|
|
30
|
-
} else if (data && typeof data === 'object') {
|
|
31
|
-
content = valuesToCss(data as Record<string, TokenValue>);
|
|
32
|
-
} else {
|
|
33
|
-
content = valuesToCss({});
|
|
34
|
-
}
|
|
35
|
-
break; }
|
|
36
|
-
case 'js':
|
|
37
|
-
content = `export default ${JSON.stringify(data, null, 2)};`;
|
|
38
|
-
break;
|
|
39
|
-
default:
|
|
40
|
-
content = JSON.stringify(data, null, 2);
|
|
41
|
-
}
|
|
42
|
-
if (outputPath) {
|
|
43
|
-
const dir = dirname(outputPath);
|
|
44
|
-
if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
|
|
45
|
-
writeFileSync(outputPath, content, 'utf8');
|
|
46
|
-
console.log(`Output written to: ${outputPath}`);
|
|
47
|
-
} else {
|
|
48
|
-
console.log(content);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
1
|
+
import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'node:fs';
|
|
2
|
+
import { resolve, dirname } from 'node:path';
|
|
3
|
+
import { valuesToCss } from '../../adapters/css.js';
|
|
4
|
+
import type { TokenNode, TokenValue } from '../../core/flatten.js';
|
|
5
|
+
|
|
6
|
+
// Shared helpers for command modules
|
|
7
|
+
|
|
8
|
+
export function loadTokens(tokensPath: string): TokenNode {
|
|
9
|
+
const resolvedPath = resolve(tokensPath);
|
|
10
|
+
if (!existsSync(resolvedPath)) {
|
|
11
|
+
throw new Error(`Token file not found: ${resolvedPath}`);
|
|
12
|
+
}
|
|
13
|
+
const data = JSON.parse(readFileSync(resolvedPath, 'utf8'));
|
|
14
|
+
if (typeof data !== 'object' || data === null) {
|
|
15
|
+
throw new Error(`Invalid token file: expected object, got ${typeof data}`);
|
|
16
|
+
}
|
|
17
|
+
return data as TokenNode;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function outputResult(data: unknown, format: string, outputPath?: string): void {
|
|
21
|
+
let content: string;
|
|
22
|
+
switch (format) {
|
|
23
|
+
case 'json':
|
|
24
|
+
content = JSON.stringify(data, null, 2);
|
|
25
|
+
break;
|
|
26
|
+
case 'css': {
|
|
27
|
+
if (data && typeof data === 'object' && 'patch' in (data as { patch?: unknown })) {
|
|
28
|
+
const patch = (data as { patch: Record<string, TokenValue> }).patch;
|
|
29
|
+
content = valuesToCss(patch);
|
|
30
|
+
} else if (data && typeof data === 'object') {
|
|
31
|
+
content = valuesToCss(data as Record<string, TokenValue>);
|
|
32
|
+
} else {
|
|
33
|
+
content = valuesToCss({});
|
|
34
|
+
}
|
|
35
|
+
break; }
|
|
36
|
+
case 'js':
|
|
37
|
+
content = `export default ${JSON.stringify(data, null, 2)};`;
|
|
38
|
+
break;
|
|
39
|
+
default:
|
|
40
|
+
content = JSON.stringify(data, null, 2);
|
|
41
|
+
}
|
|
42
|
+
if (outputPath) {
|
|
43
|
+
const dir = dirname(outputPath);
|
|
44
|
+
if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
|
|
45
|
+
writeFileSync(outputPath, content, 'utf8');
|
|
46
|
+
console.log(`Output written to: ${outputPath}`);
|
|
47
|
+
} else {
|
|
48
|
+
console.log(content);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validate.d.ts","sourceRoot":"","sources":["validate.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"validate.d.ts","sourceRoot":"","sources":["validate.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAOnD,wBAAsB,eAAe,CAAC,QAAQ,EAAE,eAAe,GAAG,OAAO,CAAC,IAAI,CAAC,CAuK9E"}
|