Last active
July 27, 2025 18:29
-
-
Save ilikescience/01bacb8fe45c480c1839c6cbb2cb7e08 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
/* | |
MIT License | |
Copyright (c) 2025 Matt Ström-Awn | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
--- | |
Color Token Converter | |
--------------------- | |
This script converts color tokens from the previous draft format to the current draft format. | |
USAGE: | |
node updateColorTokens.js <input.json> <output.json> | |
- <input.json>: Path to the input JSON file containing tokens in the old format. | |
- <output.json>: Path to the output JSON file to write the converted tokens. | |
Only tokens with "$type": "color" are converted. All other tokens are ignored. | |
Example: | |
node updateColorTokens.js tokens.json tokens-new.json | |
*/ | |
// Converts color tokens from the previoius draft format to the current draft format | |
const fs = require('fs'); | |
const path = require('path'); | |
function hexToRgba(hex) { | |
// Remove leading # | |
hex = hex.replace(/^#/, ''); | |
let r, g, b, a = 1; | |
if (hex.length === 8) { | |
// RRGGBBAA | |
r = parseInt(hex.slice(0, 2), 16) / 255; | |
g = parseInt(hex.slice(2, 4), 16) / 255; | |
b = parseInt(hex.slice(4, 6), 16) / 255; | |
a = parseInt(hex.slice(6, 8), 16) / 255; | |
hex = hex.slice(0, 6); // for fallback | |
} else if (hex.length === 6) { | |
r = parseInt(hex.slice(0, 2), 16) / 255; | |
g = parseInt(hex.slice(2, 4), 16) / 255; | |
b = parseInt(hex.slice(4, 6), 16) / 255; | |
} else if (hex.length === 4) { | |
// RGBA | |
r = parseInt(hex[0] + hex[0], 16) / 255; | |
g = parseInt(hex[1] + hex[1], 16) / 255; | |
b = parseInt(hex[2] + hex[2], 16) / 255; | |
a = parseInt(hex[3] + hex[3], 16) / 255; | |
hex = hex.slice(0, 3); | |
} else if (hex.length === 3) { | |
r = parseInt(hex[0] + hex[0], 16) / 255; | |
g = parseInt(hex[1] + hex[1], 16) / 255; | |
b = parseInt(hex[2] + hex[2], 16) / 255; | |
} else { | |
throw new Error('Invalid hex color: ' + hex); | |
} | |
return { | |
colorSpace: 'srgb', | |
components: [Number(r.toFixed(4)), Number(g.toFixed(4)), Number(b.toFixed(4))], | |
alpha: Number(a.toFixed(4)), | |
hex: '#' + hex.toLowerCase() | |
}; | |
} | |
function convertToken(token) { | |
if (typeof token !== 'object' || token === null) return token; | |
if ('$value' in token && token['$type'] === 'color') { | |
const value = token['$value']; | |
if (typeof value === 'string' && value.startsWith('#')) { | |
return { | |
$type: 'color', | |
$value: hexToRgba(value) | |
}; | |
} | |
} else if ('$type' in token && token['$type'] !== 'color') { | |
// Ignore non-color tokens | |
return undefined; | |
} | |
// Recursively process nested tokens | |
const result = Array.isArray(token) ? [] : {}; | |
for (const key in token) { | |
const converted = convertToken(token[key]); | |
if (converted !== undefined) { | |
result[key] = converted; | |
} | |
} | |
// If result is empty, return undefined to prune empty branches | |
if ((Array.isArray(result) && result.length === 0) || (Object.keys(result).length === 0)) { | |
return undefined; | |
} | |
return result; | |
} | |
function main() { | |
const args = process.argv.slice(2); | |
if (args.length < 2) { | |
console.error('Usage: node updateColorTokens.js <input.json> <output.json>'); | |
process.exit(1); | |
} | |
const inputPath = path.resolve(args[0]); | |
const outputPath = path.resolve(args[1]); | |
if (!fs.existsSync(inputPath)) { | |
console.error('Input file does not exist:', inputPath); | |
process.exit(1); | |
} | |
const raw = fs.readFileSync(inputPath, 'utf8'); | |
const tokens = JSON.parse(raw); | |
const converted = convertToken(tokens); | |
fs.writeFileSync(outputPath, JSON.stringify(converted, null, 2), 'utf8'); | |
console.log(`Converted tokens written to ${outputPath}`); | |
} | |
if (require.main === module) { | |
main(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment