-
Notifications
You must be signed in to change notification settings - Fork 610
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
4,203 additions
and
103 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
const fs = require("fs"); | ||
const { green } = require("chalk"); | ||
const path = require("path"); | ||
const aliasTokensExport = require("./importFromFigma/exports/Alias tokens.json"); | ||
const { normalizeFigmaExport } = require("./importFromFigma/normalizeFigmaExport"); | ||
const { | ||
createTailwindConfigCustomizations | ||
} = require("./importFromFigma/createTailwindConfigCustomizations"); | ||
const { createStylesScss } = require("./importFromFigma/createStylesScss"); | ||
const { formatCode } = require("./importFromFigma/formatCode"); | ||
|
||
const saveFileAndFormat = async (filePath, content) => { | ||
fs.writeFileSync(filePath, content); | ||
await formatCode(filePath); | ||
}; | ||
|
||
(async () => { | ||
const normalizedFigmaExport = normalizeFigmaExport(aliasTokensExport); | ||
const tailwindConfigCustomizations = createTailwindConfigCustomizations(normalizedFigmaExport); | ||
const stylesScss = createStylesScss(normalizedFigmaExport); | ||
|
||
const paths = { | ||
cwd: process.cwd(), | ||
normalizedFigmaExport: path.join(__dirname, "../.normalizedFigmaExport.json"), | ||
tailwindConfigCustomizations: path.join(__dirname, "../tailwind.config.customizations.js"), | ||
stylesScss: path.join(__dirname, "../src/styles2.scss") | ||
}; | ||
|
||
console.log("Storing..."); | ||
console.log( | ||
`‣ normalized Figma export (${green( | ||
path.relative(paths.cwd, paths.normalizedFigmaExport) | ||
)}).` | ||
); | ||
console.log( | ||
`‣ Tailwind config customizations (${green( | ||
path.relative(paths.cwd, paths.tailwindConfigCustomizations) | ||
)}).` | ||
); | ||
console.log(`‣ styles.scss (${green(path.relative(paths.cwd, paths.stylesScss))}).`); | ||
|
||
await saveFileAndFormat( | ||
paths.normalizedFigmaExport, | ||
JSON.stringify(normalizedFigmaExport, null, 2) | ||
); | ||
|
||
await saveFileAndFormat( | ||
paths.tailwindConfigCustomizations, | ||
`module.exports = ${JSON.stringify(tailwindConfigCustomizations, null, 2)};` | ||
); | ||
|
||
await saveFileAndFormat(paths.stylesScss, stylesScss); | ||
|
||
console.log("Done."); | ||
})(); |
168 changes: 168 additions & 0 deletions
168
packages/admin-ui/scripts/importFromFigma/createStylesScss.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
const fs = require("fs"); | ||
|
||
const createStylesScss = normalizedFigmaExport => { | ||
// Generate `styles.scss` file. | ||
let stylesScss = fs.readFileSync(__dirname + "/templates/styles.scss.txt", "utf8"); | ||
|
||
// 1. Background colors. | ||
{ | ||
let currentBgColorGroup = null; | ||
const bgColors = normalizedFigmaExport | ||
.filter(item => item.type === "backgroundColor") | ||
.map(variable => { | ||
const [colorGroup] = variable.variantName.split("-"); | ||
const cssVar = `--bg-${variable.variantName}: ${variable.hsla.h}, ${variable.hsla.s}%, ${variable.hsla.l}%;`; | ||
|
||
if (!currentBgColorGroup) { | ||
currentBgColorGroup = colorGroup; | ||
return cssVar; | ||
} | ||
|
||
if (!currentBgColorGroup || currentBgColorGroup !== colorGroup) { | ||
currentBgColorGroup = colorGroup; | ||
return ["", cssVar]; | ||
} | ||
return cssVar; | ||
}) | ||
.flat(); | ||
|
||
stylesScss = stylesScss.replace("{BACKGROUND_COLORS}", bgColors.join("\n")); | ||
} | ||
|
||
// 2. Text colors. | ||
{ | ||
let currentTextColor = null; | ||
const textColors = normalizedFigmaExport | ||
.filter(item => item.type === "textColor") | ||
.map(variable => { | ||
const [colorGroup] = variable.variantName.split("-"); | ||
const cssVar = `--text-${variable.variantName}: ${variable.hsla.h}, ${variable.hsla.s}%, ${variable.hsla.l}%;`; | ||
|
||
if (!currentTextColor) { | ||
currentTextColor = colorGroup; | ||
return cssVar; | ||
} | ||
|
||
if (!currentTextColor || currentTextColor !== colorGroup) { | ||
currentTextColor = colorGroup; | ||
return ["", cssVar]; | ||
} | ||
return cssVar; | ||
}) | ||
.flat(); | ||
|
||
stylesScss = stylesScss.replace("{TEXT_COLORS}", textColors.join("\n")); | ||
} | ||
|
||
// 3. Border colors. | ||
{ | ||
let currentBorderColor = null; | ||
const borderColors = normalizedFigmaExport | ||
.filter(item => item.type === "borderColor") | ||
.map(variable => { | ||
const [colorGroup] = variable.variantName.split("-"); | ||
const cssVar = `--border-${variable.variantName}: ${variable.hsla.h}, ${variable.hsla.s}%, ${variable.hsla.l}%;`; | ||
|
||
if (!currentBorderColor) { | ||
currentBorderColor = colorGroup; | ||
return cssVar; | ||
} | ||
|
||
if (!currentBorderColor || currentBorderColor !== colorGroup) { | ||
currentBorderColor = colorGroup; | ||
return ["", cssVar]; | ||
} | ||
return cssVar; | ||
}) | ||
.flat(); | ||
|
||
stylesScss = stylesScss.replace("{BORDER_COLORS}", borderColors.join("\n")); | ||
} | ||
|
||
// 4. Border radius. | ||
{ | ||
const borderRadius = normalizedFigmaExport | ||
.filter(item => item.type === "borderRadius") | ||
.map(variable => { | ||
return `--radius-${variable.variantName}: ${variable.resolvedValue}px;`; | ||
}); | ||
|
||
stylesScss = stylesScss.replace("{BORDER_RADIUS}", borderRadius.join("\n")); | ||
} | ||
|
||
// 5. Border width. | ||
{ | ||
const borderWidth = normalizedFigmaExport | ||
.filter(item => item.type === "borderWidth") | ||
.map( | ||
variable => `--border-width-${variable.variantName}: ${variable.resolvedValue}px;` | ||
); | ||
|
||
stylesScss = stylesScss.replace("{BORDER_WIDTH}", borderWidth.join("\n")); | ||
} | ||
|
||
// 6. Padding. | ||
{ | ||
const padding = normalizedFigmaExport | ||
.filter(item => item.type === "padding") | ||
.map(variable => `--padding-${variable.variantName}: ${variable.resolvedValue}px;`); | ||
|
||
stylesScss = stylesScss.replace("{PADDING}", padding.join("\n")); | ||
} | ||
|
||
// 7. Elevation. | ||
{ | ||
const elevation = normalizedFigmaExport | ||
.filter(item => item.type === "elevation") | ||
.map(variable => `--elevation-${variable.variantName}: ${variable.resolvedValue}px;`); | ||
|
||
stylesScss = stylesScss.replace("{ELEVATION}", elevation.join("\n")); | ||
} | ||
|
||
// 8. Fill. | ||
{ | ||
let currentFillColor = null; | ||
const textColors = normalizedFigmaExport | ||
.filter(item => item.type === "fill") | ||
.map(variable => { | ||
const [colorGroup] = variable.variantName.split("-"); | ||
const cssVar = `--fill-${variable.variantName}: ${variable.hsla.h}, ${variable.hsla.s}%, ${variable.hsla.l}%;`; | ||
|
||
if (!currentFillColor) { | ||
currentFillColor = colorGroup; | ||
return cssVar; | ||
} | ||
|
||
if (!currentFillColor || currentFillColor !== colorGroup) { | ||
currentFillColor = colorGroup; | ||
return ["", cssVar]; | ||
} | ||
return cssVar; | ||
}) | ||
.flat(); | ||
|
||
stylesScss = stylesScss.replace("{FILL}", textColors.join("\n")); | ||
} | ||
|
||
// 9. Spacing. | ||
{ | ||
const spacing = normalizedFigmaExport | ||
.filter(item => item.type === "spacing") | ||
.map(variable => `--spacing-${variable.variantName}: ${variable.resolvedValue}px;`); | ||
|
||
stylesScss = stylesScss.replace("{SPACING}", spacing.join("\n")); | ||
} | ||
|
||
// 10. Margin. | ||
{ | ||
const margin = normalizedFigmaExport | ||
.filter(item => item.type === "margin") | ||
.map(variable => `--margin-${variable.variantName}: ${variable.resolvedValue}px;`); | ||
|
||
stylesScss = stylesScss.replace("{MARGIN}", margin.join("\n")); | ||
} | ||
|
||
return stylesScss; | ||
}; | ||
|
||
module.exports = { createStylesScss }; |
57 changes: 57 additions & 0 deletions
57
packages/admin-ui/scripts/importFromFigma/createTailwindConfigCustomizations.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
const createTailwindConfigCustomizations = normalizedFigmaExport => { | ||
return { | ||
backgroundColor: normalizedFigmaExport.reduce((acc, { type, variantName }) => { | ||
if (type === "backgroundColor") { | ||
const [color, variant] = variantName.split("-"); | ||
if (!acc[color]) { | ||
acc[color] = { | ||
DEFAULT: `var(--bg-${color}-default)` | ||
}; | ||
} | ||
|
||
acc[color][variant] = `var(--bg-${variantName})`; | ||
} | ||
return acc; | ||
}, {}), | ||
borderColor: normalizedFigmaExport.reduce((acc, { type, variantName }) => { | ||
if (type === "borderColor") { | ||
const [color, variant] = variantName.split("-"); | ||
if (!acc[color]) { | ||
acc[color] = { | ||
DEFAULT: `var(--border-${color}-default)` | ||
}; | ||
} | ||
|
||
acc[color][variant] = `var(--border-${variantName})`; | ||
} | ||
return acc; | ||
}, {}), | ||
textColor: normalizedFigmaExport.reduce((acc, { type, variantName }) => { | ||
if (type === "textColor") { | ||
const [color, variant] = variantName.split("-"); | ||
if (!acc[color]) { | ||
acc[color] = { | ||
DEFAULT: `var(--text-${color}-default)` | ||
}; | ||
} | ||
|
||
acc[color][variant] = `var(--text-${variantName})`; | ||
} | ||
return acc; | ||
}, {}), | ||
borderRadius: normalizedFigmaExport.reduce((acc, { type, variantName }) => { | ||
if (type === "borderRadius") { | ||
acc[variantName] = `var(--radius-${variantName})`; | ||
} | ||
return acc; | ||
}, {}), | ||
borderWidth: normalizedFigmaExport.reduce((acc, { type, variantName }) => { | ||
if (type === "borderWidth") { | ||
acc[variantName] = `var(--border-width-${variantName})`; | ||
} | ||
return acc; | ||
}, {}) | ||
}; | ||
}; | ||
|
||
module.exports = { createTailwindConfigCustomizations }; |
Oops, something went wrong.