You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ra...@apache.org on 2020/10/06 07:04:56 UTC

[cordova-android] branch master updated: refactor(utils): reduce number of utils (#1085)

This is an automated email from the ASF dual-hosted git repository.

raphinesse pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cordova-android.git


The following commit(s) were added to refs/heads/master by this push:
     new 5d3591b  refactor(utils): reduce number of utils (#1085)
5d3591b is described below

commit 5d3591b8537647c0cc145308028e537c83e7ffd6
Author: Raphael von der GrĂ¼n <ra...@gmail.com>
AuthorDate: Tue Oct 6 09:04:48 2020 +0200

    refactor(utils): reduce number of utils (#1085)
    
    * refactor(utils): remove utils.grep
    
    * refactor(utils): replace utils.scanDirectory w/ fast-glob
    
    Note that fast-glob is already in our dependency graph anyway.
---
 bin/templates/cordova/lib/prepare.js | 12 ++++----
 bin/templates/cordova/lib/utils.js   | 56 ------------------------------------
 package-lock.json                    |  6 ++--
 package.json                         |  1 +
 4 files changed, 10 insertions(+), 65 deletions(-)

diff --git a/bin/templates/cordova/lib/prepare.js b/bin/templates/cordova/lib/prepare.js
index cbe3fbe..ba68244 100644
--- a/bin/templates/cordova/lib/prepare.js
+++ b/bin/templates/cordova/lib/prepare.js
@@ -20,6 +20,7 @@
 var fs = require('fs-extra');
 var path = require('path');
 const nopt = require('nopt');
+const glob = require('fast-glob');
 var events = require('cordova-common').events;
 var AndroidManifest = require('./AndroidManifest');
 var checkReqs = require('./check_reqs');
@@ -237,9 +238,9 @@ function updateProjectAccordingTo (platformConfig, locations) {
 
     // Java file paths shouldn't be hard coded
     const javaDirectory = path.join(locations.javaSrc, manifestId.replace(/\./g, '/'));
-    const javaPattern = /\.java$/;
-    const java_files = utils.scanDirectory(javaDirectory, javaPattern, true).filter(function (f) {
-        return utils.grep(f, /extends\s+CordovaActivity/g) !== null;
+    const java_files = glob.sync('**/*.java', { cwd: javaDirectory, absolute: true }).filter(f => {
+        const contents = fs.readFileSync(f, 'utf-8');
+        return /extends\s+CordovaActivity/.test(contents);
     });
 
     if (java_files.length === 0) {
@@ -664,9 +665,8 @@ function cleanIcons (projectRoot, projectConfig, platformResourcesDir) {
  */
 function mapImageResources (rootDir, subDir, type, resourceName) {
     const pathMap = {};
-    const pattern = new RegExp(type + '-.+');
-    utils.scanDirectory(path.join(rootDir, subDir), pattern).forEach(function (drawableFolder) {
-        const imagePath = path.join(subDir, path.basename(drawableFolder), resourceName);
+    glob.sync(type + '-*', { cwd: path.join(rootDir, subDir) }).forEach(drawableFolder => {
+        const imagePath = path.join(subDir, drawableFolder, resourceName);
         pathMap[imagePath] = null;
     });
     return pathMap;
diff --git a/bin/templates/cordova/lib/utils.js b/bin/templates/cordova/lib/utils.js
index 760eda6..a74a740 100644
--- a/bin/templates/cordova/lib/utils.js
+++ b/bin/templates/cordova/lib/utils.js
@@ -24,7 +24,6 @@
 // TODO: Perhaps this should live in cordova-common?
 
 const fs = require('fs-extra');
-const path = require('path');
 
 /**
  * Reads, searches, and replaces the found occurences with replacementString and then writes the file back out.
@@ -40,58 +39,3 @@ exports.replaceFileContents = function (file, searchRegex, replacementString) {
     contents = contents.replace(searchRegex, replacementString);
     fs.writeFileSync(file, contents);
 };
-
-/**
- * Reads a file and scans for regex. Returns the line of the first occurence or null if no occurences are found.
- *
- * @param {string} file A file path
- * @param {RegExp} regex A search regex
- * @returns string|null
- */
-exports.grep = function (file, regex) {
-    const contents = fs.readFileSync(file).toString().replace(/\\r/g, '').split('\n');
-    for (let i = 0; i < contents.length; i++) {
-        const line = contents[i];
-        if (regex.test(line)) {
-            return line;
-        }
-    }
-    return null;
-};
-
-/**
- * Scans directories and outputs a list of found paths that matches the regex
- *
- * @param {string} directory The starting directory
- * @param {RegExp} regex The search regex
- * @param {boolean} recursive Enables recursion
- * @returns Array<string>
- */
-exports.scanDirectory = function (directory, regex, recursive) {
-    let output = [];
-
-    if (fs.existsSync(directory)) {
-        const items = fs.readdirSync(directory);
-
-        for (let i = 0; i < items.length; i++) {
-            const item = items[i];
-            const itemPath = path.join(directory, item);
-            const stats = fs.statSync(itemPath);
-
-            if (regex.test(itemPath)) {
-                output.push(itemPath);
-            }
-
-            if (stats.isDirectory()) {
-                if (recursive) {
-                    output = output.concat(exports.scanDirectory(itemPath, regex, recursive));
-                } else {
-                    // Move onto the next item
-                    continue;
-                }
-            }
-        }
-    }
-
-    return output;
-};
diff --git a/package-lock.json b/package-lock.json
index a14a6ff..b79ee4c 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1325,9 +1325,9 @@
       "dev": true
     },
     "fast-glob": {
-      "version": "3.2.2",
-      "resolved": "https://registry.npmjs.org/fast-glob/-/fast-glob-3.2.2.tgz",
-      "integrity": "sha512-UDV82o4uQyljznxwMxyVRJgZZt3O5wENYojjzbaGEGZgeOxkLFf+V4cnUD+krzb2F72E18RhamkMZ7AdeggF7A==",
+      "version": "3.2.4",
+      "resolved": "https://registry.npmjs.org/fast-glob/-/fast-glob-3.2.4.tgz",
+      "integrity": "sha512-kr/Oo6PX51265qeuCYsyGypiO5uJFgBS0jksyG7FUeCyQzNwYnzrNIMR1NXfkZXsMYXYLRAHgISHBz8gQcxKHQ==",
       "requires": {
         "@nodelib/fs.stat": "^2.0.2",
         "@nodelib/fs.walk": "^1.2.3",
diff --git a/package.json b/package.json
index 367292f..b38ef78 100644
--- a/package.json
+++ b/package.json
@@ -28,6 +28,7 @@
     "android-versions": "^1.5.0",
     "cordova-common": "^4.0.1",
     "execa": "^4.0.2",
+    "fast-glob": "^3.2.4",
     "fs-extra": "^9.0.1",
     "is-path-inside": "^3.0.2",
     "nopt": "^4.0.3",


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org