Skip to content

Commit

Permalink
Improve generateNewIcons script
Browse files Browse the repository at this point in the history
  • Loading branch information
dmnplb committed Oct 24, 2023
1 parent 6bb2349 commit 16607f0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
33 changes: 22 additions & 11 deletions src/components/icons/generateNewIcons.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Prerequisites:
*/

/*
The following code was initially generated by GTP-4,
The following code was first generated by GTP-4,
then improved step by step
*/

Expand All @@ -81,6 +81,8 @@ const timestampFilePath = join(__dirname, "timestamp.txt");

fs.readFile(timestampFilePath, "utf8", (err, timestamp) => {
if (err) {
// eslint-disable-next-line no-console
console.log("Timestamp file not found.");
throw err;
}

Expand All @@ -96,10 +98,9 @@ fs.readFile(timestampFilePath, "utf8", (err, timestamp) => {
const filePath = join(svgDir, file);
const fileStats = fs.statSync(filePath);

/* Only process files with a more recent creation
date later than the timestamp value */
if (fileStats.mtime > new Date(timestamp)) {
// eslint-disable-next-line no-console
console.log("Processing file:", file);

const data = fs.readFileSync(filePath, "utf8");

// Check if the file is an SVG
Expand All @@ -110,41 +111,51 @@ fs.readFile(timestampFilePath, "utf8", (err, timestamp) => {
const result = optimize(data, { path: filePath });
const tsxData = result.data
.replace(/<svg[^>]*>((.|[\n\r])*)<\/svg>/im, "$1")
/* Replace hardcoded color value with `currentColor` */
.replace(/fill="[^"]*"/g, 'fill="currentColor"')
.replace(/fill-rule/g, "fillRule")
.replace(/clip-rule/g, "clipRule")
/* Convert self-closing tags to Capital Case, because
`react-native-svg` doesn't recognize them otherwise */
.replace(
/<([a-z]+)([^>]*)\/>/g,
(match, p1, p2) =>
`<${p1.charAt(0).toUpperCase() + p1.slice(1)}${p2} />`

Check failure on line 121 in src/components/icons/generateNewIcons.js

View workflow job for this annotation

GitHub Actions / checks

Invalid operand for a '+' operation. Operands must each be a number or string. Got `any`

Check failure on line 121 in src/components/icons/generateNewIcons.js

View workflow job for this annotation

GitHub Actions / checks

Invalid operand for a '+' operation. Operands must each be a number or string. Got `any`
) // convert self-closing tags to capital case
)
/* Convert all the remaining tags to Capital Case */
.replace(
/<([a-z]+)([^>]*)>/g,
(match, p1, p2) =>
`<${p1.charAt(0).toUpperCase() + p1.slice(1)}${p2}>`

Check failure on line 127 in src/components/icons/generateNewIcons.js

View workflow job for this annotation

GitHub Actions / checks

Invalid operand for a '+' operation. Operands must each be a number or string. Got `any`

Check failure on line 127 in src/components/icons/generateNewIcons.js

View workflow job for this annotation

GitHub Actions / checks

Invalid operand for a '+' operation. Operands must each be a number or string. Got `any`
); // convert start tags to capital case
)
/* Convert SVG props to compatible React props */
.replace(/fill-rule/g, "fillRule")
.replace(/clip-rule/g, "clipRule");

const template = fs.readFileSync(templateFilePath, "utf8");
const componentData = template
.replace("IconTemplate", file.replace(".svg", ""))
.replace(/\/\/.*\n/g, "") // remove lines starting with //
.replace(`{/* SVGContent */}`, tsxData);

const tsxFilePath = join(tsxDir, file.replace(".svg", ".tsx"));
const fileWithTsxExtension = file.replace(".svg", ".tsx");
const tsxFilePath = join(tsxDir, fileWithTsxExtension);
fs.writeFileSync(
tsxFilePath,
prettier.format(componentData, { parser: "typescript" })
);

// eslint-disable-next-line no-console
console.log("Created .tsx file:", tsxFilePath);
console.log(`${file}${fileWithTsxExtension}`);
}
});

const newTimestamp = new Date().toISOString();
fs.writeFileSync(timestampFilePath, newTimestamp);

const readableItalianTime = new Date().toLocaleString("it-IT", {
timeZone: "Europe/Rome"
});

// eslint-disable-next-line no-console
console.log("Updated timestamp:", newTimestamp);
console.log("Updated timestamp:", readableItalianTime);
});
});
4 changes: 2 additions & 2 deletions src/components/icons/generateNewTimestamp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from "fs";
const fs = require("fs-extra");

Check failure on line 1 in src/components/icons/generateNewTimestamp.js

View workflow job for this annotation

GitHub Actions / checks

Require statement not part of import statement

const timestampFilePath = "./timestamp.txt"; // Adjust the path as needed
const timestampFilePath = "./timestamp.txt";
const currentTimestamp = new Date().toISOString();

fs.writeFileSync(timestampFilePath, currentTimestamp);
3 changes: 3 additions & 0 deletions src/components/icons/svg/originals/IconFingerprint.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 16607f0

Please sign in to comment.