-
-
Save kulak-at/5e6c5b332ab5ca213424d9d981436482 to your computer and use it in GitHub Desktop.
Metaprogramming - convert color to use Design System
This file contains 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
import color from "color"; | |
import { DESIGN_SYSTEM } from "./ds"; | |
const selectToken = (color) => | |
Object.keys(DESIGN_SYSTEM) | |
.find(key => areTheSame(color, DESIGN_SYSTEM[key])); | |
const areTheSame = (c1, c2) => { | |
const { valpha: a1, color: [r1, g1, b1]} = color(c1).rgb(); | |
const { valpha: a2, color: [r2, g2, b2]} = color(c2).rgb(); | |
return a1 === a2 && r1 === r2 && g1 === g2 && b1 === b2; | |
} | |
export default (fileInfo, { jscodeshift: j }) => { | |
const code = j(fileInfo.source); | |
code.find(j.CallExpression, { | |
callee: { | |
type: "MemberExpression", | |
property: { | |
type: "Identifier", | |
name: "create", | |
}, | |
object: { | |
type: "Identifier", | |
name: "Stylesheet" | |
} | |
} | |
}) | |
.forEach(obj => j(obj.node.arguments) | |
.find(j.Property) | |
.forEach(prop => { | |
if (prop.node.key.name === "color") { | |
const token = selectToken(prop.node.value.value); | |
if (token) { | |
prop.node.value = j.memberExpression( | |
j.identifier("DESIGN_SYSTEM"), | |
j.literal(token) | |
); | |
} | |
} | |
})); | |
return code.toSource(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment