Minify: Replace `async.forEach()` with `Promise.all()`

pull/4765/head
Richard Hansen 2021-02-11 15:16:45 -05:00 committed by John McLear
parent 0c428e068e
commit 5cc191f185
1 changed files with 27 additions and 39 deletions

View File

@ -23,7 +23,6 @@
const ERR = require('async-stacktrace'); const ERR = require('async-stacktrace');
const settings = require('./Settings'); const settings = require('./Settings');
const async = require('async');
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const plugins = require('../../static/js/pluginfw/plugin_defs'); const plugins = require('../../static/js/pluginfw/plugin_defs');
@ -32,6 +31,7 @@ const urlutil = require('url');
const mime = require('mime-types'); const mime = require('mime-types');
const Threads = require('threads'); const Threads = require('threads');
const log4js = require('log4js'); const log4js = require('log4js');
const util = require('util');
const logger = log4js.getLogger('Minify'); const logger = log4js.getLogger('Minify');
@ -213,25 +213,21 @@ const getAceFile = (callback) => {
// Request the contents of the included file on the server-side and write // Request the contents of the included file on the server-side and write
// them into the file. // them into the file.
async.forEach(filenames, (filename, callback) => { Promise.all(filenames.map(async (filename) => {
// Hostname "invalid.invalid" is a dummy value to allow parsing as a URI. // Hostname "invalid.invalid" is a dummy value to allow parsing as a URI.
const baseURI = 'http://invalid.invalid'; const baseURI = 'http://invalid.invalid';
let resourceURI = baseURI + path.normalize(path.join('/static/', filename)); let resourceURI = baseURI + path.normalize(path.join('/static/', filename));
resourceURI = resourceURI.replace(/\\/g, '/'); // Windows (safe generally?) resourceURI = resourceURI.replace(/\\/g, '/'); // Windows (safe generally?)
requestURI(resourceURI, 'GET', {}).then(([status, headers, body]) => { const [status, , body] = await requestURI(resourceURI, 'GET', {});
const error = !(status === 200 || status === 404); const error = !(status === 200 || status === 404);
if (!error) { if (!error) {
data += `Ace2Editor.EMBEDED[${JSON.stringify(filename)}] = ${ data += `Ace2Editor.EMBEDED[${JSON.stringify(filename)}] = ${
JSON.stringify(status === 200 ? body || '' : null)};\n`; JSON.stringify(status === 200 ? body || '' : null)};\n`;
} else { } else {
console.error(`getAceFile(): error getting ${resourceURI}. Status code: ${status}`); console.error(`getAceFile(): error getting ${resourceURI}. Status code: ${status}`);
} }
callback(); })).then(() => callback(null, data), (err) => callback(err || new Error(err)));
});
}, (error) => {
callback(error, data);
});
}); });
}; };
@ -279,35 +275,27 @@ const lastModifiedDateOfEverything = (callback) => {
const folders2check = [`${ROOT_DIR}js/`, `${ROOT_DIR}css/`]; const folders2check = [`${ROOT_DIR}js/`, `${ROOT_DIR}css/`];
let latestModification = 0; let latestModification = 0;
// go through this two folders // go through this two folders
async.forEach(folders2check, (path, callback) => { Promise.all(folders2check.map(async (path) => {
// read the files in the folder // read the files in the folder
fs.readdir(path, (err, files) => { const files = await util.promisify(fs.readdir)(path);
if (ERR(err, callback)) return;
// we wanna check the directory itself for changes too // we wanna check the directory itself for changes too
files.push('.'); files.push('.');
// go through all files in this folder // go through all files in this folder
async.forEach(files, (filename, callback) => { await Promise.all(files.map(async (filename) => {
// get the stat data of this file // get the stat data of this file
fs.stat(`${path}/${filename}`, (err, stats) => { const stats = await util.promisify(fs.stat)(`${path}/${filename}`);
if (ERR(err, callback)) return;
// get the modification time // get the modification time
const modificationTime = stats.mtime.getTime(); const modificationTime = stats.mtime.getTime();
// compare the modification time to the highest found // compare the modification time to the highest found
if (modificationTime > latestModification) { if (modificationTime > latestModification) {
latestModification = modificationTime; latestModification = modificationTime;
} }
}));
callback(); })).then(() => callback(null, latestModification), (err) => callback(err || new Error(err)));
});
}, callback);
});
}, () => {
callback(null, latestModification);
});
}; };
// This should be provided by the module, but until then, just use startup // This should be provided by the module, but until then, just use startup