react-native-iconfont-cli icon indicating copy to clipboard operation
react-native-iconfont-cli copied to clipboard

[patch-package] Using hash key for local icons instead of increasement key to prevent conflict

Open Misaka-0x447f opened this issue 3 years ago • 0 comments

This patch issue is submitted by patch-package.

In my project I found that if more than one commit is adding local icon to project, the key of local icon will be conflict in a large scale. So I made this patch:

diff --git a/node_modules/react-native-iconfont-cli/libs/generateComponent.js b/node_modules/react-native-iconfont-cli/libs/generateComponent.js
index ddeae3b..6f5ee3f 100644
--- a/node_modules/react-native-iconfont-cli/libs/generateComponent.js
+++ b/node_modules/react-native-iconfont-cli/libs/generateComponent.js
@@ -12,10 +12,14 @@ var getTemplate_1 = require("./getTemplate");
 var replace_1 = require("./replace");
 var whitespace_1 = require("./whitespace");
 var copyTemplate_1 = require("./copyTemplate");
+var crypto = require('crypto')
 var SVG_MAP = {
     path: 'Path',
 };
 var ATTRIBUTE_FILL_MAP = ['path'];
+
+const sha1 = (data) => crypto.createHash("sha1").update(data, "binary").digest("hex");
+
 exports.generateComponent = function (data, localSvg, config) {
     var svgComponents = new Set();
     var names = [];
@@ -80,7 +84,6 @@ exports.generateComponent = function (data, localSvg, config) {
             typeDefinitionFile = replace_1.replaceComponentName(typeDefinitionFile, componentName);
             fs_1.default.writeFileSync(path_1.default.join(saveDir, componentName + '.d.ts'), typeDefinitionFile);
         }
         console.log(colors_1.default.green('√') + " Generated icon \"" + colors_1.default.yellow(iconId) + "\"");
     });
     /**
      * 本地文件添加
@@ -97,7 +100,7 @@ exports.generateComponent = function (data, localSvg, config) {
         names.push(name);
         cases += whitespace_1.whitespace(4) + "case '" + name + "':\n";
         imports.push(componentName);
-        cases += whitespace_1.whitespace(6) + "return <" + componentName + " key=\"L" + (index + 1) + "\" {...rest} />;\n";
+        cases += whitespace_1.whitespace(6) + "return <" + componentName + " key=\"L-" + sha1(componentName) + "\" {...rest} />;\n";
         singleFile = getTemplate_1.getTemplate('LocalSingleIcon' + jsxExtension);
         singleFile = replace_1.replaceSize(singleFile, config.default_icon_size);
         singleFile = replace_1.replaceSvgComponents(singleFile, currentSvgComponents);
@@ -110,7 +113,6 @@ exports.generateComponent = function (data, localSvg, config) {
             typeDefinitionFile = replace_1.replaceComponentName(typeDefinitionFile, componentName);
             fs_1.default.writeFileSync(path_1.default.join(saveDir, componentName + '.d.ts'), typeDefinitionFile);
         }
         console.log(colors_1.default.green('√') + " Generated local icon \"" + colors_1.default.yellow(name) + "\"");
     });
     var iconFile = getTemplate_1.getTemplate('Icon' + jsxExtension);
     iconFile = replace_1.replaceSize(iconFile, config.default_icon_size);

This issue body was partially generated by patch-package.

Misaka-0x447f avatar May 25 '22 06:05 Misaka-0x447f