From 16607f00959820f0fa674868dad4a37fe94f9195 Mon Sep 17 00:00:00 2001 From: Damiano Plebani Date: Tue, 24 Oct 2023 17:42:41 +0200 Subject: [PATCH] Improve `generateNewIcons` script --- src/components/icons/generateNewIcons.js | 33 ++++++++++++------- src/components/icons/generateNewTimestamp.js | 4 +-- .../icons/svg/originals/IconFingerprint.svg | 3 ++ 3 files changed, 27 insertions(+), 13 deletions(-) create mode 100644 src/components/icons/svg/originals/IconFingerprint.svg diff --git a/src/components/icons/generateNewIcons.js b/src/components/icons/generateNewIcons.js index 0698fbb0..e06ff53f 100644 --- a/src/components/icons/generateNewIcons.js +++ b/src/components/icons/generateNewIcons.js @@ -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 */ @@ -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; } @@ -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 @@ -110,19 +111,24 @@ fs.readFile(timestampFilePath, "utf8", (err, timestamp) => { const result = optimize(data, { path: filePath }); const tsxData = result.data .replace(/]*>((.|[\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} />` - ) // 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}>` - ); // 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 @@ -130,21 +136,26 @@ fs.readFile(timestampFilePath, "utf8", (err, timestamp) => { .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); }); }); diff --git a/src/components/icons/generateNewTimestamp.js b/src/components/icons/generateNewTimestamp.js index e2b17ec3..3ecaaa98 100644 --- a/src/components/icons/generateNewTimestamp.js +++ b/src/components/icons/generateNewTimestamp.js @@ -1,6 +1,6 @@ -import fs from "fs"; +const fs = require("fs-extra"); -const timestampFilePath = "./timestamp.txt"; // Adjust the path as needed +const timestampFilePath = "./timestamp.txt"; const currentTimestamp = new Date().toISOString(); fs.writeFileSync(timestampFilePath, currentTimestamp); diff --git a/src/components/icons/svg/originals/IconFingerprint.svg b/src/components/icons/svg/originals/IconFingerprint.svg new file mode 100644 index 00000000..a8995eaa --- /dev/null +++ b/src/components/icons/svg/originals/IconFingerprint.svg @@ -0,0 +1,3 @@ + + +