From 2be01e7cbd7c7670256ad58947dbeae79bb0fd2c Mon Sep 17 00:00:00 2001 From: Daniel Lockyer Date: Wed, 25 Nov 2020 10:44:15 +0000 Subject: [PATCH] Refactored remaining function in package-json lib to use async-await - this helps simplify the code and gets rid of Promise chaining - apparently I can't easily use an async function within filter, so I've left it for now --- ghost/package-json/lib/package-json.js | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/ghost/package-json/lib/package-json.js b/ghost/package-json/lib/package-json.js index 0b3045fd77..e417e43907 100644 --- a/ghost/package-json/lib/package-json.js +++ b/ghost/package-json/lib/package-json.js @@ -150,10 +150,10 @@ module.exports = class PackageJson { } } - readPackages(packagePath) { - const self = this; + async readPackages(packagePath) { + const dirContents = await fs.readdir(packagePath); - return Promise.resolve(fs.readdir(packagePath)) + const packageNames = dirContents .filter(function (packageName) { // Filter out things which are not packages by regex if (packageName.match(notAPackageRegex)) { @@ -163,13 +163,14 @@ module.exports = class PackageJson { return fs.stat(join(packagePath, packageName)).then(function (stat) { return stat.isDirectory(); }); - }) - .map(function readPackageJson(packageName) { - const absolutePath = join(packagePath, packageName); - return self.processPackage(absolutePath, packageName); - }) - .then(function (packages) { - return _.keyBy(packages, 'name'); }); + + const packages = await Promise.all(packageNames + .map((packageName) => { + const absolutePath = join(packagePath, packageName); + return this.processPackage(absolutePath, packageName); + })); + + return _.keyBy(packages, 'name'); } };