Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ node_modules
*.css
*.cssresult.ts
*-styles.ts
tokens/versions/**/*-meta.scss
*.map
*.d.ts
!types/*.d.ts
Expand Down
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,20 @@
"**/*.css",
"**/*.css.map",
"!catalog/"
],
"dependencies": [
"build:sass:generate"
]
},
"build:sass:generate": {
"command": "find tokens/versions/ -type f -name '_md-*.scss' ! -name '*-meta.scss' -print0 | xargs -0 node scripts/generate-sass-token-meta.js",
"files": [
"tokens/versions/**/_md-*.scss",
"!tokens/versions/**/*-meta.scss"
],
"output": ["tokens/versions/**/*-meta.scss"],
"dependencies": ["build:scripts"]
},
"test": {
"command": "wtr",
"dependencies": [
Expand Down
77 changes: 77 additions & 0 deletions scripts/generate-sass-token-meta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* @license
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

/** @fileoverview Generates tokens/versions/latest/_md-{name}-meta.scss files. */

import * as fs from 'fs';
import * as util from 'util';
function generateMetaFile(inputFilePath: string, outputFilePath: string) {
const content = fs.readFileSync(inputFilePath, {encoding: 'utf8'});
const metaVars: string[] = [];

// Regex to match variable declarations: $name: value;
const varRegex = /^\s*\$([\w-]+)\s*:\s*([^;]+);/gm;
let match: RegExpExecArray | null;
while ((match = varRegex.exec(content)) !== null) {
const name = match[1];
let value = match[2].trim();

// Resolve the value to a CSS var() if it references another token.
// (e.g. md-sys-color.$primary or $primary)
const refMatch = value.match(/^(?:([\w-]+)\.)?\$([\w-]+)$/);
if (refMatch) {
const refModule = refMatch[1] || null;
const refToken = refMatch[2];
value = `var(--${refModule ? `${refModule}-` : ''}${refToken})`;
}

metaVars.push(`$${name}--resolved: ${value};`);
}

fs.writeFileSync(
outputFilePath,
`//
// Copyright 2026 Google LLC
// SPDX-License-Identifier: Apache-2.0
//
// Auto-generated token metadata.
${metaVars.join('\n')}
`,
);
}

const {values, positionals} = util.parseArgs({
allowPositionals: true,
options: {
outputs: {type: 'string'},
},
});

const inputs: string[] = positionals;
const outputs: string[] = [];

if (inputs.length === 0) {
throw new Error(
`Usage: node generate-sass-token-meta.js <input.scss>... [--outputs "out1-meta.scss out2-meta.scss"]`,
);
}
if (values.outputs) {
outputs.push(...values.outputs.trim().split(/\s+/).filter(Boolean));
}
if (outputs.length === 0) {
for (const input of inputs) {
outputs.push(input.replace('.scss', '-meta.scss'));
}
}
if (inputs.length !== outputs.length) {
throw new Error(
`Inputs and outputs length mismatch: ${inputs.length} vs ${outputs.length}`,
);
}

for (let i = 0; i < inputs.length; i++) {
generateMetaFile(inputs[i], outputs[i]);
}
Loading