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.
This commit is contained in:
Raphael von der Grün 2020-10-06 09:04:48 +02:00 committed by GitHub
parent 206238893b
commit 5d3591b853
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 10 additions and 65 deletions

View File

@ -20,6 +20,7 @@
var fs = require('fs-extra'); var fs = require('fs-extra');
var path = require('path'); var path = require('path');
const nopt = require('nopt'); const nopt = require('nopt');
const glob = require('fast-glob');
var events = require('cordova-common').events; var events = require('cordova-common').events;
var AndroidManifest = require('./AndroidManifest'); var AndroidManifest = require('./AndroidManifest');
var checkReqs = require('./check_reqs'); var checkReqs = require('./check_reqs');
@ -237,9 +238,9 @@ function updateProjectAccordingTo (platformConfig, locations) {
// Java file paths shouldn't be hard coded // Java file paths shouldn't be hard coded
const javaDirectory = path.join(locations.javaSrc, manifestId.replace(/\./g, '/')); const javaDirectory = path.join(locations.javaSrc, manifestId.replace(/\./g, '/'));
const javaPattern = /\.java$/; const java_files = glob.sync('**/*.java', { cwd: javaDirectory, absolute: true }).filter(f => {
const java_files = utils.scanDirectory(javaDirectory, javaPattern, true).filter(function (f) { const contents = fs.readFileSync(f, 'utf-8');
return utils.grep(f, /extends\s+CordovaActivity/g) !== null; return /extends\s+CordovaActivity/.test(contents);
}); });
if (java_files.length === 0) { if (java_files.length === 0) {
@ -664,9 +665,8 @@ function cleanIcons (projectRoot, projectConfig, platformResourcesDir) {
*/ */
function mapImageResources (rootDir, subDir, type, resourceName) { function mapImageResources (rootDir, subDir, type, resourceName) {
const pathMap = {}; const pathMap = {};
const pattern = new RegExp(type + '-.+'); glob.sync(type + '-*', { cwd: path.join(rootDir, subDir) }).forEach(drawableFolder => {
utils.scanDirectory(path.join(rootDir, subDir), pattern).forEach(function (drawableFolder) { const imagePath = path.join(subDir, drawableFolder, resourceName);
const imagePath = path.join(subDir, path.basename(drawableFolder), resourceName);
pathMap[imagePath] = null; pathMap[imagePath] = null;
}); });
return pathMap; return pathMap;

View File

@ -24,7 +24,6 @@
// TODO: Perhaps this should live in cordova-common? // TODO: Perhaps this should live in cordova-common?
const fs = require('fs-extra'); 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. * 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); contents = contents.replace(searchRegex, replacementString);
fs.writeFileSync(file, contents); 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;
};

6
package-lock.json generated
View File

@ -1325,9 +1325,9 @@
"dev": true "dev": true
}, },
"fast-glob": { "fast-glob": {
"version": "3.2.2", "version": "3.2.4",
"resolved": "https://registry.npmjs.org/fast-glob/-/fast-glob-3.2.2.tgz", "resolved": "https://registry.npmjs.org/fast-glob/-/fast-glob-3.2.4.tgz",
"integrity": "sha512-UDV82o4uQyljznxwMxyVRJgZZt3O5wENYojjzbaGEGZgeOxkLFf+V4cnUD+krzb2F72E18RhamkMZ7AdeggF7A==", "integrity": "sha512-kr/Oo6PX51265qeuCYsyGypiO5uJFgBS0jksyG7FUeCyQzNwYnzrNIMR1NXfkZXsMYXYLRAHgISHBz8gQcxKHQ==",
"requires": { "requires": {
"@nodelib/fs.stat": "^2.0.2", "@nodelib/fs.stat": "^2.0.2",
"@nodelib/fs.walk": "^1.2.3", "@nodelib/fs.walk": "^1.2.3",

View File

@ -28,6 +28,7 @@
"android-versions": "^1.5.0", "android-versions": "^1.5.0",
"cordova-common": "^4.0.1", "cordova-common": "^4.0.1",
"execa": "^4.0.2", "execa": "^4.0.2",
"fast-glob": "^3.2.4",
"fs-extra": "^9.0.1", "fs-extra": "^9.0.1",
"is-path-inside": "^3.0.2", "is-path-inside": "^3.0.2",
"nopt": "^4.0.3", "nopt": "^4.0.3",