From d7bb95b87ab8bc6fac8cece08ff007a9f993cfcb Mon Sep 17 00:00:00 2001 From: blam Date: Fri, 1 May 2020 18:27:43 +0200 Subject: [PATCH] refactor: move to globby instead of glob library for nice promises and efficiency! --- plugins/scaffolder-backend/package.json | 2 +- .../src/lib/storage/disk.ts | 70 +++++++++---------- 2 files changed, 34 insertions(+), 38 deletions(-) diff --git a/plugins/scaffolder-backend/package.json b/plugins/scaffolder-backend/package.json index 5bfde524c7..d0cec7faea 100644 --- a/plugins/scaffolder-backend/package.json +++ b/plugins/scaffolder-backend/package.json @@ -19,7 +19,7 @@ "dependencies": { "express": "^4.17.1", "fs-extra": "^9.0.0", - "glob": "^7.1.6", + "globby": "^11.0.0", "winston": "^3.2.1" } } diff --git a/plugins/scaffolder-backend/src/lib/storage/disk.ts b/plugins/scaffolder-backend/src/lib/storage/disk.ts index 6f3c5f14dc..985313f070 100644 --- a/plugins/scaffolder-backend/src/lib/storage/disk.ts +++ b/plugins/scaffolder-backend/src/lib/storage/disk.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import glob from 'glob'; +import globby from 'globby'; import fs from 'fs-extra'; import { logger } from '../logger'; import { Template, StorageBase as Base } from '.'; @@ -29,8 +29,9 @@ export class DiskStorage implements Base { private localIndex: DiskIndexEntry[] = []; constructor(private repoDir = `${__dirname}/../../../sample-templates`) { - console.warn(require('path').resolve(repoDir)); + } + public async list(): Promise { if (this.repository.length === 0) { await this.reindex(); @@ -59,43 +60,38 @@ export class DiskStorage implements Base { } private async index(): Promise { - return new Promise((resolve, reject) => { - glob(`${this.repoDir}/**/template-info.json`, async (err, matches) => { - if (err) { - reject(err); + const matches = await globby(`${this.repoDir}/**/template-info.json`); + + const fileContents: Array<{ + location: string; + contents: string; + }> = await Promise.all( + matches.map(async (location: string) => ({ + location, + contents: await fs.readFile(location, 'utf-8'), + })), + ); + + const validFiles = fileContents.reduce( + (diskIndexEntries: DiskIndexEntry[], currentFile) => { + try { + const parsed: Template = JSON.parse(currentFile.contents); + return [ + ...diskIndexEntries, + { location: currentFile.location, contents: parsed }, + ]; + } catch (ex) { + logger.error('Failure parsing JSON for template', { + path: currentFile.location, + }); } - const fileContents: Array<{ - location: string; - contents: string; - }> = await Promise.all( - matches.map(async (location: string) => ({ - location, - contents: await fs.readFile(location, 'utf-8'), - })), - ); + return diskIndexEntries; + }, + [], + ); + + return validFiles; - const validFiles = fileContents.reduce( - (diskIndexEntries: DiskIndexEntry[], currentFile) => { - try { - const parsed: Template = JSON.parse(currentFile.contents); - return [ - ...diskIndexEntries, - { location: currentFile.location, contents: parsed }, - ]; - } catch (ex) { - logger.error('Failure parsing JSON for template', { - path: currentFile.location, - }); - } - - return diskIndexEntries; - }, - [], - ); - - resolve(validFiles); - }); - }); } }