Refactored to more efficient fs call for reading packages

- we don't need to do both a `fs.readdir` and a `fs.stat` because
  `fs.readdir` has the `withFileTypes` which returns the directory entry
  info and so this avoids an extra stat syscall
This commit is contained in:
Daniel Lockyer 2022-03-24 22:19:58 +00:00
parent 14a087536f
commit bd6a295674

View File

@ -127,22 +127,18 @@ async function readPackage(packagePath, packageName) {
* @returns {Promise<PackageList>} * @returns {Promise<PackageList>}
*/ */
async function readPackages(packagePath) { async function readPackages(packagePath) {
return Bluebird.resolve(fs.readdir(packagePath)) return Bluebird.resolve(fs.readdir(packagePath, {withFileTypes: true}))
.filter(function (packageName) { .filter(function (packageFile) {
// Filter out things which are not packages by regex // Filter out things which are not packages by regex
if (packageName.match(notAPackageRegex)) { if (packageFile.name.match(notAPackageRegex)) {
return; return;
} }
// Check the remaining items to ensure they are a directory // Check the remaining items to ensure they are a directory
return fs.stat(join(packagePath, packageName)) return packageFile.isDirectory();
.then(function (stat) {
return stat.isDirectory();
})
.catch(() => false);
}) })
.map(function readPackageJson(packageName) { .map(function readPackageJson(packageFile) {
const absolutePath = join(packagePath, packageName); const absolutePath = join(packagePath, packageFile.name);
return processPackage(absolutePath, packageName); return processPackage(absolutePath, packageFile.name);
}) })
.then(function (packages) { .then(function (packages) {
return _.keyBy(packages, 'name'); return _.keyBy(packages, 'name');