-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #512 from ably/automate-icon-manifests
[WEB-4025] Autogenerate icon manifests
- Loading branch information
Showing
12 changed files
with
302 additions
and
338 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,5 @@ yarn-error.log | |
.idea/* | ||
types | ||
index.d.ts | ||
computed-colors-*.json | ||
computed-colors-*.json | ||
computed-icons.ts |
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,104 @@ | ||
import path from "path"; | ||
import fs from "fs"; | ||
import svgSprite from "svg-sprite"; | ||
|
||
// Configuration for svg-sprite | ||
const config = { | ||
dest: "dist/core", | ||
mode: { | ||
symbol: { | ||
inline: true, | ||
sprite: "../sprites", | ||
}, | ||
}, | ||
shape: { | ||
id: { | ||
generator: "sprite-%s", | ||
}, | ||
}, | ||
svg: { | ||
transform: [ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
function (svg: any) { | ||
let globalDefs = ""; | ||
|
||
return svg | ||
.replace(/<defs>(.+?)<\/defs>/g, (_match: string, def: string) => { | ||
globalDefs += def; | ||
}) | ||
.replace("<symbol", `<defs>${globalDefs}</defs><symbol`); | ||
}, | ||
], | ||
}, | ||
}; | ||
|
||
// Create a new SVG sprite instance | ||
const sprite = new svgSprite(config); | ||
|
||
// Directory where your individual SVG files are located | ||
const svgDir = path.resolve(__dirname, "../src/core/icons"); | ||
|
||
// Object to store the grouped filenames | ||
const iconGroups: Record<string, string[]> = {}; | ||
|
||
// Read all SVG files from the directory and add them to the sprite | ||
fs.readdirSync(svgDir).forEach((file) => { | ||
if (file.endsWith(".svg")) { | ||
try { | ||
// Extract the key from the filename | ||
const match = file.match(/^icon-([^-]+)/); | ||
if (match) { | ||
const key = match[1]; | ||
if (!iconGroups[key]) { | ||
iconGroups[key] = []; | ||
} | ||
|
||
// Remove the .svg extension before adding to the group | ||
const fileNameWithoutExtension = file.replace(".svg", ""); | ||
iconGroups[key].push(fileNameWithoutExtension); | ||
} | ||
|
||
sprite.add( | ||
path.resolve(svgDir, file), | ||
null, | ||
fs.readFileSync(path.resolve(svgDir, file), "utf-8"), | ||
); | ||
} catch (error) { | ||
console.error(`Error processing file ${file}:`, error); | ||
} | ||
} | ||
}); | ||
|
||
// Compile the sprite | ||
sprite.compile((err, result) => { | ||
if (err) { | ||
console.error("Error generating SVG sprite:", err); | ||
} else { | ||
try { | ||
// Write the compiled sprite to the destination directory | ||
fs.mkdirSync(path.resolve(__dirname, "dist/core"), { recursive: true }); | ||
fs.writeFileSync( | ||
path.resolve(__dirname, "dist", result.symbol.sprite.path), | ||
result.symbol.sprite.contents, | ||
); | ||
|
||
// Write the icon groups to a JSON file | ||
const iconJsonPath = path.resolve( | ||
__dirname, | ||
"../src/core/Icon/computed-icons.ts", | ||
); | ||
|
||
const generatedIconGroups = | ||
`// AUTOGENERATED BY build:sprites - DO NOT EDIT\n\nexport const computedIcons = ${JSON.stringify(iconGroups, null, 2)}`.replace( | ||
/]/g, | ||
"] as const", | ||
); | ||
|
||
fs.writeFileSync(iconJsonPath, generatedIconGroups, "utf-8"); | ||
|
||
console.log("🖼️ SVG sprites and icon manifest generated successfully!"); | ||
} catch (error) { | ||
console.log("SVG sprite/manifest generation failed:", error); | ||
} | ||
} | ||
}); |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.