kobalt
Version:
A cli to generate a theme from figma projects.
52 lines (51 loc) • 2.19 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createStyledComponentsFormat = void 0;
const capitalize_1 = require("../utilities/capitalize");
const format_1 = require("../utilities/format");
const templates_1 = require("./templates");
const createFontName = (token) => {
if (token.attributes) {
const { type, item } = token.attributes;
return `${(0, capitalize_1.capitalize)(type)}${(0, capitalize_1.capitalize)(item)}`;
}
return "";
};
const createStyledComponentsFormat = ({ dictionary, options, }) => {
const body = dictionary.allTokens
.map((token) => {
if (typeof token.value === "object") {
token.value = Object.keys(token.value).reduce((accumulator, current) => {
const value = token.value[current];
const newKey = (0, format_1.kebabCase)(current);
accumulator[newKey] = value;
return accumulator;
}, {});
}
let value = JSON.stringify(token.value, (_, value) => {
if (typeof value === "object") {
return Object.entries(value)
.map(([key, value]) => `${key}: ${value}; `)
.join(``);
}
return `${value}`;
}, 2);
if (!token.attributes) {
throw new Error("No attributes available to read from.");
}
if (token.attributes.category === "font") {
if (options.outputReferences &&
dictionary.usesReference(token.original.value)) {
const refs = dictionary.getReferences(token.original.value);
refs.forEach((ref) => {
value = value.replace(`${ref.value}`, () => `\${({theme}) => theme.${ref.path.join(".")}.value\}`);
});
}
return (0, templates_1.createStyledComponentFont)(createFontName(token), value.slice(1, -1));
}
return;
})
.join(`\n`);
return (0, templates_1.createTemplate)(body);
};
exports.createStyledComponentsFormat = createStyledComponentsFormat;