Skip to content

Commit a589695

Browse files
asynclizcopybara-github
authored andcommitted
chore: add script to generate Sass token metadata files
PiperOrigin-RevId: 895583199
1 parent fb8a059 commit a589695

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ node_modules
55
*.css
66
*.cssresult.ts
77
*-styles.ts
8+
tokens/versions/**/*-meta.scss
89
*.map
910
*.d.ts
1011
!types/*.d.ts

package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,20 @@
158158
"**/*.css",
159159
"**/*.css.map",
160160
"!catalog/"
161+
],
162+
"dependencies": [
163+
"build:sass:generate"
161164
]
162165
},
166+
"build:sass:generate": {
167+
"command": "find tokens/versions/ -type f -name '_md-*.scss' ! -name '*-meta.scss' -print0 | xargs -0 node scripts/generate-sass-token-meta.js",
168+
"files": [
169+
"tokens/versions/**/_md-*.scss",
170+
"!tokens/versions/**/*-meta.scss"
171+
],
172+
"output": ["tokens/versions/**/*-meta.scss"],
173+
"dependencies": ["build:scripts"]
174+
},
163175
"test": {
164176
"command": "wtr",
165177
"dependencies": [
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* @license
3+
* Copyright 2026 Google LLC
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/** @fileoverview Generates tokens/versions/latest/_md-{name}-meta.scss files. */
8+
9+
import * as fs from 'fs';
10+
import * as util from 'util';
11+
function generateMetaFile(inputFilePath: string, outputFilePath: string) {
12+
const content = fs.readFileSync(inputFilePath, {encoding: 'utf8'});
13+
const metaVars: string[] = [];
14+
15+
// Regex to match variable declarations: $name: value;
16+
const varRegex = /^\s*\$([\w-]+)\s*:\s*([^;]+);/gm;
17+
let match: RegExpExecArray | null;
18+
while ((match = varRegex.exec(content)) !== null) {
19+
const name = match[1];
20+
let value = match[2].trim();
21+
22+
// Resolve the value to a CSS var() if it references another token.
23+
// (e.g. md-sys-color.$primary or $primary)
24+
const refMatch = value.match(/^(?:([\w-]+)\.)?\$([\w-]+)$/);
25+
if (refMatch) {
26+
const refModule = refMatch[1] || null;
27+
const refToken = refMatch[2];
28+
value = `var(--${refModule ? `${refModule}-` : ''}${refToken})`;
29+
}
30+
31+
metaVars.push(`$${name}--resolved: ${value};`);
32+
}
33+
34+
fs.writeFileSync(
35+
outputFilePath,
36+
`//
37+
// Copyright 2026 Google LLC
38+
// SPDX-License-Identifier: Apache-2.0
39+
//
40+
// Auto-generated token metadata.
41+
${metaVars.join('\n')}
42+
`,
43+
);
44+
}
45+
46+
const {values, positionals} = util.parseArgs({
47+
allowPositionals: true,
48+
options: {
49+
outputs: {type: 'string'},
50+
},
51+
});
52+
53+
const inputs: string[] = positionals;
54+
const outputs: string[] = [];
55+
56+
if (inputs.length === 0) {
57+
throw new Error(
58+
`Usage: node generate-sass-token-meta.js <input.scss>... [--outputs "out1-meta.scss out2-meta.scss"]`,
59+
);
60+
}
61+
if (values.outputs) {
62+
outputs.push(...values.outputs.trim().split(/\s+/).filter(Boolean));
63+
}
64+
if (outputs.length === 0) {
65+
for (const input of inputs) {
66+
outputs.push(input.replace('.scss', '-meta.scss'));
67+
}
68+
}
69+
if (inputs.length !== outputs.length) {
70+
throw new Error(
71+
`Inputs and outputs length mismatch: ${inputs.length} vs ${outputs.length}`,
72+
);
73+
}
74+
75+
for (let i = 0; i < inputs.length; i++) {
76+
generateMetaFile(inputs[i], outputs[i]);
77+
}

0 commit comments

Comments
 (0)