@gesslar/sassy 1.3.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/package.json +5 -1
- package/src/Evaluator.js +7 -2
- package/src/ResolveCommand.js +102 -8
- package/src/ThemeToken.js +23 -0
- package/src/cli.js +7 -8
package/package.json
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
"name": "gesslar",
|
|
6
6
|
"url": "https://gesslar.dev"
|
|
7
7
|
},
|
|
8
|
-
"version": "
|
|
8
|
+
"version": "2.0.1",
|
|
9
9
|
"license": "Unlicense",
|
|
10
10
|
"homepage": "https://github.com/gesslar/sassy#readme",
|
|
11
11
|
"repository": {
|
|
@@ -54,6 +54,7 @@
|
|
|
54
54
|
"yaml": "^2.8.2"
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
|
+
"@docusaurus/eslint-plugin": "^3.9.2",
|
|
57
58
|
"@gesslar/uglier": "^1.2.0",
|
|
58
59
|
"eslint": "^9.39.2",
|
|
59
60
|
"typescript": "^5.9.3"
|
|
@@ -69,6 +70,9 @@
|
|
|
69
70
|
"submit": "pnpm publish --access public --//registry.npmjs.org/:_authToken=\"${NPM_ACCESS_TOKEN}\"",
|
|
70
71
|
"examples": "node ./examples/validator.js",
|
|
71
72
|
"update": "pnpm self-update && pnpx npm-check-updates -u && pnpm install",
|
|
73
|
+
"docs:dev": "pnpm --dir docs start",
|
|
74
|
+
"docs:build": "pnpm --dir docs build",
|
|
75
|
+
"docs:deploy": "gh workflow run deploy-docs.yml",
|
|
72
76
|
"pr": "gt submit -p --ai",
|
|
73
77
|
"patch": "pnpm version patch",
|
|
74
78
|
"minor": "pnpm version minor",
|
package/src/Evaluator.js
CHANGED
|
@@ -318,11 +318,16 @@ export default class Evaluator {
|
|
|
318
318
|
return null
|
|
319
319
|
|
|
320
320
|
const resolved = value.replace(captured, applied)
|
|
321
|
-
|
|
322
|
-
return new ThemeToken(value)
|
|
321
|
+
const token = new ThemeToken(value)
|
|
323
322
|
.setKind("function")
|
|
324
323
|
.setRawValue(captured)
|
|
325
324
|
.setValue(resolved)
|
|
325
|
+
|
|
326
|
+
if(resolved !== applied) {
|
|
327
|
+
token.setFunctionResult(applied)
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
return token
|
|
326
331
|
}
|
|
327
332
|
|
|
328
333
|
/**
|
package/src/ResolveCommand.js
CHANGED
|
@@ -14,6 +14,9 @@ import Theme from "./Theme.js"
|
|
|
14
14
|
* Provides introspection into the theme resolution process and variable dependencies.
|
|
15
15
|
*/
|
|
16
16
|
export default class ResolveCommand extends Command {
|
|
17
|
+
#extraOptions = null
|
|
18
|
+
#bg = null
|
|
19
|
+
|
|
17
20
|
/**
|
|
18
21
|
* Creates a new ResolveCommand instance.
|
|
19
22
|
*
|
|
@@ -28,6 +31,22 @@ export default class ResolveCommand extends Command {
|
|
|
28
31
|
"tokenColor": ["-t, --tokenColor <scope>", "resolve a tokenColors scope to its final evaluated value"],
|
|
29
32
|
"semanticTokenColor": ["-s, --semanticTokenColor <scope>", "resolve a semanticTokenColors scope to its final evaluated value"],
|
|
30
33
|
})
|
|
34
|
+
this.#extraOptions = {
|
|
35
|
+
"bg": ["--bg <hex>", "background colour for alpha swatch preview (e.g. 1a1a1a or '#1a1a1a')"],
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Builds the CLI command, adding extra options that are not mutually exclusive.
|
|
41
|
+
*
|
|
42
|
+
* @param {object} program - The commander.js program instance
|
|
43
|
+
* @returns {Promise<this>} Returns this instance for method chaining
|
|
44
|
+
*/
|
|
45
|
+
async buildCli(program) {
|
|
46
|
+
await super.buildCli(program)
|
|
47
|
+
this.addCliOptions(this.#extraOptions, false)
|
|
48
|
+
|
|
49
|
+
return this
|
|
31
50
|
}
|
|
32
51
|
|
|
33
52
|
/**
|
|
@@ -59,6 +78,16 @@ export default class ResolveCommand extends Command {
|
|
|
59
78
|
)
|
|
60
79
|
}
|
|
61
80
|
|
|
81
|
+
if(options.bg) {
|
|
82
|
+
const bg = options.bg.startsWith("#") ? options.bg : `#${options.bg}`
|
|
83
|
+
|
|
84
|
+
if(!Colour.isHex(bg)) {
|
|
85
|
+
throw Sass.new(`Invalid --bg colour: ${options.bg}`)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
this.#bg = Colour.normaliseHex(bg)
|
|
89
|
+
}
|
|
90
|
+
|
|
62
91
|
const resolveFunctionName = `resolve${Util.capitalize(optionName)}`
|
|
63
92
|
const optionValue = options[optionName]
|
|
64
93
|
const resolverFunction = this[resolveFunctionName]
|
|
@@ -334,6 +363,18 @@ export default class ResolveCommand extends Command {
|
|
|
334
363
|
}
|
|
335
364
|
}
|
|
336
365
|
|
|
366
|
+
// For function tokens embedded in a larger expression, show the
|
|
367
|
+
// direct function output before showing the full substituted result
|
|
368
|
+
const funcResult = token.getFunctionResult?.()
|
|
369
|
+
|
|
370
|
+
if(funcResult && !steps.some(s => s.value === funcResult)) {
|
|
371
|
+
steps.push({
|
|
372
|
+
value: funcResult,
|
|
373
|
+
type: "result",
|
|
374
|
+
level
|
|
375
|
+
})
|
|
376
|
+
}
|
|
377
|
+
|
|
337
378
|
// Add final result for this token
|
|
338
379
|
if(rawValue !== finalValue && !steps.some(s => s.value === finalValue)) {
|
|
339
380
|
steps.push({
|
|
@@ -352,7 +393,7 @@ export default class ResolveCommand extends Command {
|
|
|
352
393
|
|
|
353
394
|
steps.forEach(step => {
|
|
354
395
|
if(!levelMap.has(step.level)) {
|
|
355
|
-
levelMap.set(step.level,
|
|
396
|
+
levelMap.set(step.level, normalizedLevel++)
|
|
356
397
|
}
|
|
357
398
|
|
|
358
399
|
step.depth = levelMap.get(step.level)
|
|
@@ -387,13 +428,13 @@ export default class ResolveCommand extends Command {
|
|
|
387
428
|
const {value, depth, type} = step
|
|
388
429
|
const [line, kind] = this.#formatLeaf(value)
|
|
389
430
|
|
|
390
|
-
// Simple logic: only hex results get extra indentation with arrow, everything else is clean
|
|
431
|
+
// Simple logic: only hex results get extra indentation with arrow/swatch, everything else is clean
|
|
391
432
|
if(type === "result" && kind === "hex") {
|
|
392
|
-
// Hex results are indented one extra level with
|
|
433
|
+
// Hex results are indented one extra level with swatch or arrow
|
|
393
434
|
const prefix = " ".repeat(depth + 1)
|
|
394
|
-
const
|
|
435
|
+
const indicator = this.#makeIndicator(value, this.#bg)
|
|
395
436
|
|
|
396
|
-
out.push(`${prefix}${
|
|
437
|
+
out.push(`${prefix}${indicator} ${line}`)
|
|
397
438
|
} else {
|
|
398
439
|
// Everything else just gets clean indentation
|
|
399
440
|
const prefix = " ".repeat(depth)
|
|
@@ -409,6 +450,59 @@ export default class ResolveCommand extends Command {
|
|
|
409
450
|
#sub = Evaluator.sub
|
|
410
451
|
#hex = value => Colour.isHex(value)
|
|
411
452
|
|
|
453
|
+
/**
|
|
454
|
+
* Creates a truecolor swatch glyph from a hex value.
|
|
455
|
+
*
|
|
456
|
+
* @private
|
|
457
|
+
* @param {string} hex - A 6- or 8-digit hex colour.
|
|
458
|
+
* @returns {string} A truecolor `■` character.
|
|
459
|
+
*/
|
|
460
|
+
#swatch(hex) {
|
|
461
|
+
const solid = Colour.parseHexColour(hex).colour
|
|
462
|
+
const r = parseInt(solid.slice(1, 3), 16)
|
|
463
|
+
const g = parseInt(solid.slice(3, 5), 16)
|
|
464
|
+
const b = parseInt(solid.slice(5, 7), 16)
|
|
465
|
+
|
|
466
|
+
return `\x1b[38;2;${r};${g};${b}m■\x1b[0m`
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
* Creates a colour swatch or fallback arrow indicator for a hex value.
|
|
471
|
+
* When the colour has alpha and no --bg is provided, shows two swatches
|
|
472
|
+
* (against black and white). With --bg, shows a single swatch composited
|
|
473
|
+
* against the specified background.
|
|
474
|
+
*
|
|
475
|
+
* @private
|
|
476
|
+
* @param {string} hex - The hex colour value.
|
|
477
|
+
* @param {string|null} bg - Optional background hex for alpha compositing.
|
|
478
|
+
* @returns {string} Swatch indicator(s) or styled arrow.
|
|
479
|
+
*/
|
|
480
|
+
#makeIndicator(hex, bg = null) {
|
|
481
|
+
if(!Term.hasColor) {
|
|
482
|
+
return c`{arrow}→{/}`
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
const parsed = Colour.parseHexColour(hex)
|
|
486
|
+
const hasAlpha = !!parsed.alpha
|
|
487
|
+
|
|
488
|
+
if(!hasAlpha) {
|
|
489
|
+
return this.#swatch(hex)
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
const alphaPercent = Math.round(parsed.alpha.decimal * 100)
|
|
493
|
+
|
|
494
|
+
if(bg) {
|
|
495
|
+
const composited = Colour.mix(parsed.colour, bg, alphaPercent)
|
|
496
|
+
|
|
497
|
+
return this.#swatch(composited)
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
const onBlack = Colour.mix(parsed.colour, "#000000", alphaPercent)
|
|
501
|
+
const onWhite = Colour.mix(parsed.colour, "#ffffff", alphaPercent)
|
|
502
|
+
|
|
503
|
+
return `${this.#swatch(onBlack)}${this.#swatch(onWhite)}`
|
|
504
|
+
}
|
|
505
|
+
|
|
412
506
|
/**
|
|
413
507
|
* Formats a single ThemeToken for display in the theme resolution output,
|
|
414
508
|
* applying colour and style based on its type.
|
|
@@ -432,12 +526,12 @@ export default class ResolveCommand extends Command {
|
|
|
432
526
|
}
|
|
433
527
|
|
|
434
528
|
if(this.#func.test(value)) {
|
|
435
|
-
const
|
|
529
|
+
const match = this.#func.exec(value)
|
|
436
530
|
|
|
437
|
-
if(!
|
|
531
|
+
if(!match?.groups)
|
|
438
532
|
return [c`{leaf}${value}{/}`, "literal"]
|
|
439
533
|
|
|
440
|
-
const {func, args} =
|
|
534
|
+
const {func, args} = match.groups
|
|
441
535
|
|
|
442
536
|
return [
|
|
443
537
|
c`{func}${func}{/}{parens}${"("}{/}{leaf}${args}{/}{parens}${")"}{/}`,
|
package/src/ThemeToken.js
CHANGED
|
@@ -30,6 +30,7 @@ export default class ThemeToken {
|
|
|
30
30
|
#parentTokenKey = null
|
|
31
31
|
#trail = new Array()
|
|
32
32
|
#parsedColor = null
|
|
33
|
+
#functionResult = null
|
|
33
34
|
|
|
34
35
|
/**
|
|
35
36
|
* Constructs a ThemeToken with a given token name.
|
|
@@ -242,6 +243,28 @@ export default class ThemeToken {
|
|
|
242
243
|
return this.#parsedColor
|
|
243
244
|
}
|
|
244
245
|
|
|
246
|
+
/**
|
|
247
|
+
* Sets the direct result of a colour function evaluation, before
|
|
248
|
+
* substitution back into the enclosing expression.
|
|
249
|
+
*
|
|
250
|
+
* @param {string} result - The direct function output.
|
|
251
|
+
* @returns {ThemeToken} This token instance.
|
|
252
|
+
*/
|
|
253
|
+
setFunctionResult(result) {
|
|
254
|
+
this.#functionResult = result
|
|
255
|
+
|
|
256
|
+
return this
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Gets the direct result of a colour function evaluation.
|
|
261
|
+
*
|
|
262
|
+
* @returns {string|null} The direct function output or null.
|
|
263
|
+
*/
|
|
264
|
+
getFunctionResult() {
|
|
265
|
+
return this.#functionResult
|
|
266
|
+
}
|
|
267
|
+
|
|
245
268
|
/**
|
|
246
269
|
* Checks if this token has an ancestor with the given token name.
|
|
247
270
|
*
|
package/src/cli.js
CHANGED
|
@@ -80,14 +80,13 @@ void (async function main() {
|
|
|
80
80
|
c.alias.set("loc", "{F148}")
|
|
81
81
|
|
|
82
82
|
// Resolve command
|
|
83
|
-
c.alias.set("head", "{
|
|
84
|
-
c.alias.set("leaf", "{
|
|
85
|
-
c.alias.set("func", "{
|
|
86
|
-
c.alias.set("parens", "{
|
|
87
|
-
c.alias.set("
|
|
88
|
-
c.alias.set("hex", "{
|
|
89
|
-
c.alias.set("
|
|
90
|
-
c.alias.set("hexAlpha", "{F127}{<I}")
|
|
83
|
+
c.alias.set("head", "{F250}")
|
|
84
|
+
c.alias.set("leaf", "{F243}")
|
|
85
|
+
c.alias.set("func", "{<B}")
|
|
86
|
+
c.alias.set("parens", "{F208}")
|
|
87
|
+
c.alias.set("hash", "{F172}")
|
|
88
|
+
c.alias.set("hex", "{F025}")
|
|
89
|
+
c.alias.set("hexAlpha", "{F073}{<I}")
|
|
91
90
|
c.alias.set("arrow", "{F033}")
|
|
92
91
|
|
|
93
92
|
const cache = new Cache()
|