Skip to content

Script to generate SVG files from token vectors #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
28 changes: 28 additions & 0 deletions packages/foundations/generate-icons.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import fs from "fs/promises";
import path from "path";
import { fileURLToPath } from "url";

const cwd = path.dirname(fileURLToPath(import.meta.url));

const readVectorFile = async (vectorFile) => {
const vectorData = await fs.readFile(vectorFile, "utf8");
return JSON.parse(vectorData);
};

const generateIconFromVectors = async (vectorData) => {
vectorData.forEach(async (icon) => {
const iconName = `${icon.name}_${icon.properties.Size}_${icon.properties['Icon type']}_color${icon.properties['Icon colour']}.svg`;
const iconFile = path.join(cwd, 'svg', iconName)
await fs
.writeFile(iconFile, icon.source.replace('\n', '').replace('\'', ''))
.then(() => console.log(`SVG ${iconName} created`))
.catch((err) => console.error(err));
});
}

(async function main() {
const vectorFile = path.resolve(cwd, ".", "styles", "vector.json");
const vectorData = await readVectorFile(vectorFile);
console.log(vectorData);
await generateIconFromVectors(vectorData);
})();
4 changes: 3 additions & 1 deletion packages/foundations/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
"version": "4.1.0",
"description": "",
"main": "index.js",
"type": "module",
"exports": {
".": "./index.ts",
"./styles": "./index.css"
},
"scripts": {
"build": "radius-toolkit generate tokens.json -o styles/index.css",
"build:custom": "radius-toolkit generate tokens.json -c myCustomTemplate -T ./templates -o styles/vector.json",
"build:tailwind": "radius-toolkit generate tokens.json -t tailwind"
"build:tailwind": "radius-toolkit generate tokens.json -t tailwind",
"g": "node ./generate-icons.mjs"
},
"keywords": [],
"author": "",
Expand Down
24 changes: 24 additions & 0 deletions packages/foundations/styles/vector.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[
{
"name": "Box - Moving",
"properties": {
"Size": "S",
"Icon type": "Stroke",
"Icon colour": "Default",
"Frame type": "None",
"Frame fill": "None"
},
"source": "<svg width=\"16\" height=\"16\" viewBox=\"0 0 16 16\" fill=\"none\" xmlns=\"http://www.w3.org/2000/svg\">\n<path d=\"M2.60817 8.19257V4.34122M2.60817 4.34122L8.77033 0.875L11.8514 2.60811M2.60817 4.34122L5.68925 6.21666M14.9325 4.34122V11.5304L8.51357 15.125M14.9325 4.34122L8.51357 7.93581M14.9325 4.34122L11.8514 2.60811M8.51357 15.125L6.45952 13.8412M8.51357 15.125V7.93581M8.51357 7.93581L5.68925 6.21666M5.68925 6.21666L11.8514 2.60811M1.06763 11.7872L4.91898 9.47635M1.06763 14.3547L6.45952 11.0169\" stroke=\"#00549A\" stroke-width=\"1.5\" stroke-linecap=\"round\" stroke-linejoin=\"round\"/>\n</svg>\n"
},
{
"name": "Box - Moving",
"properties": {
"Size": "M",
"Icon type": "Stroke",
"Icon colour": "Default",
"Frame type": "None",
"Frame fill": "None"
},
"source": "<svg width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" fill=\"none\" xmlns=\"http://www.w3.org/2000/svg\">\n<path d=\"M3.48651 12.3041V6.22297M3.48651 6.22297L13.2162 0.75L18.0811 3.48649M3.48651 6.22297L8.35137 9.1842M22.946 6.22297V17.5743L12.8108 23.25M22.946 6.22297L12.8108 11.8986M22.946 6.22297L18.0811 3.48649M12.8108 23.25L9.56759 21.223M12.8108 23.25V11.8986M12.8108 11.8986L8.35137 9.1842M8.35137 9.1842L18.0811 3.48649M1.05408 17.9797L7.13516 14.3311M1.05408 22.0338L9.56759 16.7635\" stroke=\"#00549A\" stroke-width=\"1.5\" stroke-linecap=\"round\" stroke-linejoin=\"round\"/>\n</svg>\n"
}
]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion packages/foundations/templates/myCustomTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import type { TemplateModule } from "radius-toolkit";
export default {
name: "my-custom-template",
render: ({ layers, vectors }, options) => {
console.log("data", JSON.stringify(layers, null, 2));
const transformed = vectors?.map(({ name, properties, source }) => {
return {
name,
properties: JSON.stringify(properties),
properties,
source,
};
});
Expand Down
Loading