Skip to content

Commit

Permalink
feat: add script to adapt SVGs
Browse files Browse the repository at this point in the history
  • Loading branch information
fran-ink committed Nov 12, 2024
1 parent b4fc02d commit b6518b0
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 19 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"format:check": "prettier --check \"**/*.{ts,tsx,md,mdx,css,scss}\"",
"test": "playwright test",
"fix:all": "pnpm run lint:fix && pnpm run format",
"prepublish": "build"
"prepublish": "build",
"import-svgs": "node scripts/import-svgs.mjs"
},
"keywords": [],
"author": "",
Expand Down
52 changes: 52 additions & 0 deletions scripts/import-svgs.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/** To import SVGs:
* 1. In Figma, select one of the icons inside the Icons container and select them with CTRL+A (or ⌘+A on Mac). Export them with the SVG type.
* 2. Unzip the downloaded file, and copy the SVGs into the \`src/icons\` directory.
* 3. Run this script
**/

import fs from "fs/promises";
import path from "path";

const currentDir = import.meta.dirname;
const iconsDir = path.join(currentDir, "../src/icons");

const svgs = await fs.readdir(iconsDir);
await Promise.all(
svgs
.filter((svg) => svg.endsWith(".svg"))
.map(async (svg) => {
const svgContent = await fs.readFile(path.join(iconsDir, svg), "utf8");
const result = svgContent
.replace(/stroke="#160F1F"/g, 'stroke="currentColor"')
.replace(/fill="#160F1F"/g, 'fill="currentColor"')
.replace(/width="24"/g, 'width="100%"')
.replace(/height="24"/g, 'height="100%"');
await fs.writeFile(path.join(iconsDir, svg), result, "utf8");
})
);

const iconNameMapping = {
home: "Home",
"link-discord": "LinkDiscord",
"link-github": "LinkGithub",
"link-x-social": "LinkXSocial",
};

function getIconName(svg) {
const result = svg.replace(".svg", "").replace("Type=", "");
return iconNameMapping[result] ?? result;
}

const header = `/**\n * This file is auto-generated by the \`import-svgs.mjs\` script.\n */`;
const content = svgs
.filter((svg) => svg.endsWith(".svg"))
.map(
(svg) => `export { default as ${getIconName(svg)} } from "./${svg}?react";`
)
.join("\n");

await fs.writeFile(
path.join(iconsDir, "index.ts"),
`${header}\n\n${content}\n`,
"utf8"
);
4 changes: 1 addition & 3 deletions src/icons/Type=Arrow.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 1 addition & 2 deletions src/icons/Type=Chevron.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions src/icons/Type=Close.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/icons/Type=link-discord.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/icons/Type=link-github.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/icons/Type=link-x-social.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit b6518b0

Please sign in to comment.