From 48ebd6a93c50e943f10aed453453f4c1ca2e4ac8 Mon Sep 17 00:00:00 2001
From: Thomas Bui <43018778+Thomas-Boi@users.noreply.github.com>
Date: Thu, 27 May 2021 07:24:59 -0700
Subject: [PATCH] Optimize bot is now a part of the build script (#624)

Co-authored-by: Clemens Bastian <8781699+amacado@users.noreply.github.com>
---
 .github/scripts/icomoon_build.py | 17 +++++++++++++++--
 gulpfile.js                      | 26 +++-----------------------
 2 files changed, 18 insertions(+), 25 deletions(-)

diff --git a/.github/scripts/icomoon_build.py b/.github/scripts/icomoon_build.py
index ddca8c81..c3e3b9a1 100644
--- a/.github/scripts/icomoon_build.py
+++ b/.github/scripts/icomoon_build.py
@@ -1,6 +1,8 @@
 from pathlib import Path
 import sys
 from selenium.common.exceptions import TimeoutException
+import subprocess
+import json
 
 # pycharm complains that build_assets is an unresolved ref
 # don't worry about it, the script still runs
@@ -20,11 +22,22 @@ def main():
     
     runner = None
     try:
+        svgs = filehandler.get_svgs_paths(new_icons, args.icons_folder_path, icon_versions_only=False)
+        # optimizes the files
+        # do in each batch in case the command 
+        # line complains there's too many characters
+        start = 0
+        step = 10
+        for i in range(start, len(svgs), step):
+            batch = svgs[i:i + step]
+            subprocess.run(["npm", "run", "optimize-svg", "--", f"--svgFiles={json.dumps(batch)}"], shell=True)
+
+        icon_svgs = filehandler.get_svgs_paths(
+            new_icons, args.icons_folder_path, icon_versions_only=True)
         runner = SeleniumRunner(args.download_path,
                                 args.geckodriver_path, args.headless)
         runner.upload_icomoon(args.icomoon_json_path)
-        svgs = filehandler.get_svgs_paths(new_icons, args.icons_folder_path, True)
-        runner.upload_svgs(svgs)
+        runner.upload_svgs(icon_svgs)
 
         zip_name = "devicon-v1.0.zip"
         zip_path = Path(args.download_path, zip_name)
diff --git a/gulpfile.js b/gulpfile.js
index bdb93ef2..23fe450e 100644
--- a/gulpfile.js
+++ b/gulpfile.js
@@ -160,35 +160,15 @@ function cleanUp() {
  * This must be passed through the commandline arguments.
  */
 function optimizeSvg() {
-  let svgPaths = getAddedModifiedSvg(yargs.argv.filesAddedJson,
-    yargs.argv.filesModifiedJson)
-
-  return gulp.src(svgPaths)
+  let svgGlob = JSON.parse(yargs.argv.svgFiles)
+  console.log("Optimizing these files: ", svgGlob)
+  return gulp.src(svgGlob)
     .pipe(svgmin(configOptionCallback))
     .pipe(gulp.dest(file => {
       return file.base
     }))
 }
 
-/**
- * Get the svgs added and modified from the '/icons' folder only.
- * @param {*} filesAddedJson - the files that were added in this commit.
- * @param {*} filesModifiedJson - the files that were modified in this commit.
- * @returns a list of the svg file paths that were added/modified in this pr as Path. 
- * It will only return icons in '/icons' path (see https://github.com/devicons/devicon/issues/505)
- */
-function getAddedModifiedSvg(filesAddedJson, filesModifiedJson) {
-  const filesAdded = JSON.parse(filesAddedJson),
-    filesModified = JSON.parse(filesModifiedJson)
-
-  files = filesAdded.concat(filesModified)
-  return files.filter(filename => {
-    if (path.extname(filename) == ".svg" 
-      && path.dirname(filename).includes('icons/'))
-        return filename
-  })
-}
-
 /**
  * Create a config option for each file.
  * @param {Object} file - Gulp Vinyl instance of the file