From f5e29aac01ac83181e35d7844cc0752203ef896b Mon Sep 17 00:00:00 2001 From: Anton <38910496+toxamiz@users.noreply.github.com> Date: Thu, 21 Mar 2019 23:24:03 +0300 Subject: [PATCH 1/5] helper.js: GetFolders -> GetModules, add package.json existence check --- scripts/helper.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/scripts/helper.js b/scripts/helper.js index 7ad6b9bb0..04d226136 100644 --- a/scripts/helper.js +++ b/scripts/helper.js @@ -145,17 +145,21 @@ const GetDepTree = ( name ) => { /** - * Get all folders within a given path + * Get all modules within a given path. + * Module is folder with package.json * * @param {string} thisPath - The path that contains the desired folders * @param {boolean} verbose - Verbose flag either undefined or true * * @return {array} - An array of names of each folder */ -const GetFolders = ( thisPath, verbose ) => { +const GetModules = ( thisPath, verbose ) => { try { let folders = Fs.readdirSync( thisPath ).filter( - thisFile => Fs.statSync(`${ thisPath }/${ thisFile }`).isDirectory() + thisFile => { + let path = `${ thisPath }/${ thisFile }`; + return Fs.statSync(path).isDirectory() && Fs.existsSync(`${path}/package.json`) + } ).filter( thisFile => thisFile !== 'core' ); @@ -620,7 +624,7 @@ HELPER.generate = (() => { */ init: () => { const packagesPath = Path.normalize(`${ __dirname }/../packages/`); - const allModules = GetFolders( packagesPath ); + const allModules = GetModules( packagesPath ); HELPER.generate.json( allModules ); HELPER.generate.index( allModules ); @@ -889,7 +893,7 @@ HELPER.test = (() => { return { init: () => { const packagesPath = Path.normalize(`${ __dirname }/../packages/`); - const allModules = GetFolders( packagesPath ); + const allModules = GetModules( packagesPath ); HELPER.test.dependencies( allModules ); HELPER.test.packagejson( allModules ); From 01980ad0371277037d4852d185d13e18319e7ec4 Mon Sep 17 00:00:00 2001 From: Anton <38910496+toxamiz@users.noreply.github.com> Date: Fri, 22 Mar 2019 14:17:07 +0300 Subject: [PATCH 2/5] Code review fixes --- scripts/helper.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/scripts/helper.js b/scripts/helper.js index 04d226136..4aa7eb08f 100644 --- a/scripts/helper.js +++ b/scripts/helper.js @@ -145,8 +145,8 @@ const GetDepTree = ( name ) => { /** - * Get all modules within a given path. - * Module is folder with package.json + * Get all modules within a given path + * The 'module' is a component folder * * @param {string} thisPath - The path that contains the desired folders * @param {boolean} verbose - Verbose flag either undefined or true @@ -157,8 +157,8 @@ const GetModules = ( thisPath, verbose ) => { try { let folders = Fs.readdirSync( thisPath ).filter( thisFile => { - let path = `${ thisPath }/${ thisFile }`; - return Fs.statSync(path).isDirectory() && Fs.existsSync(`${path}/package.json`) + let path = Path.normalize( `${ thisPath }/${ thisFile }` ); + return Fs.statSync( path ).isDirectory() && Fs.existsSync( Path.normalize( `${path}/package.json` ) ) } ).filter( thisFile => thisFile !== 'core' From fe1523413e3e04d7b5904743a7facdf4e8f53387 Mon Sep 17 00:00:00 2001 From: Anton <38910496+toxamiz@users.noreply.github.com> Date: Fri, 22 Mar 2019 17:12:52 +0300 Subject: [PATCH 3/5] async version of GetModules --- scripts/helper.js | 53 ++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/scripts/helper.js b/scripts/helper.js index 4aa7eb08f..ab1240558 100644 --- a/scripts/helper.js +++ b/scripts/helper.js @@ -17,8 +17,10 @@ const Chalk = require(`chalk`); const Path = require(`path`); const Fs = require(`fs`); const Os = require(`os`); +const Util = require('util'); const PKG = require( Path.normalize(`${ process.cwd() }/package.json`) ); +const Readdir = Util.promisify(Fs.readdir); //-------------------------------------------------------------------------------------------------------------------------------------------------------------- @@ -151,24 +153,23 @@ const GetDepTree = ( name ) => { * @param {string} thisPath - The path that contains the desired folders * @param {boolean} verbose - Verbose flag either undefined or true * - * @return {array} - An array of names of each folder + * @return {Promise} - A promise to an array of names of each folder */ const GetModules = ( thisPath, verbose ) => { - try { - let folders = Fs.readdirSync( thisPath ).filter( - thisFile => { - let path = Path.normalize( `${ thisPath }/${ thisFile }` ); - return Fs.statSync( path ).isDirectory() && Fs.existsSync( Path.normalize( `${path}/package.json` ) ) - } - ).filter( - thisFile => thisFile !== 'core' - ); - - return ['core', ...folders ]; // moving core to top - } - catch( error ) { - return []; - } + return new Promise(resolve => { + Readdir(thisPath) + .then(dirContent => { + let folders = dirContent + .filter(folder => { + let path = Path.normalize( `${ thisPath }/${ folder }` ); + return Fs.statSync( path ).isDirectory() && Fs.existsSync( Path.normalize( `${path}/package.json` ) ) + }) + .filter(folder => folder !== 'core'); + + resolve(['core', ...folders ]); // moving core to top + }) + .catch(() => resolve([])) + }) }; @@ -624,11 +625,11 @@ HELPER.generate = (() => { */ init: () => { const packagesPath = Path.normalize(`${ __dirname }/../packages/`); - const allModules = GetModules( packagesPath ); - - HELPER.generate.json( allModules ); - HELPER.generate.index( allModules ); - HELPER.generate.readme( allModules ); + GetModules( packagesPath ).then(allModules => { + HELPER.generate.json( allModules ); + HELPER.generate.index( allModules ); + HELPER.generate.readme( allModules ); + }); }, /** @@ -893,11 +894,11 @@ HELPER.test = (() => { return { init: () => { const packagesPath = Path.normalize(`${ __dirname }/../packages/`); - const allModules = GetModules( packagesPath ); - - HELPER.test.dependencies( allModules ); - HELPER.test.packagejson( allModules ); - HELPER.test.changelog( allModules ); + GetModules( packagesPath ).then(allModules => { + HELPER.test.dependencies( allModules ); + HELPER.test.packagejson( allModules ); + HELPER.test.changelog( allModules ); + }); }, /** From 5ac88576fa1f8096e09c6053e363fe4b22587934 Mon Sep 17 00:00:00 2001 From: Anton <38910496+toxamiz@users.noreply.github.com> Date: Mon, 25 Mar 2019 22:05:30 +0300 Subject: [PATCH 4/5] Rework GetModules to async/await syntax --- scripts/helper.js | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/scripts/helper.js b/scripts/helper.js index ab1240558..b7a93b3df 100644 --- a/scripts/helper.js +++ b/scripts/helper.js @@ -21,6 +21,7 @@ const Util = require('util'); const PKG = require( Path.normalize(`${ process.cwd() }/package.json`) ); const Readdir = Util.promisify(Fs.readdir); +const Access = Util.promisify(Fs.access); //-------------------------------------------------------------------------------------------------------------------------------------------------------------- @@ -155,21 +156,27 @@ const GetDepTree = ( name ) => { * * @return {Promise} - A promise to an array of names of each folder */ -const GetModules = ( thisPath, verbose ) => { - return new Promise(resolve => { - Readdir(thisPath) - .then(dirContent => { - let folders = dirContent - .filter(folder => { - let path = Path.normalize( `${ thisPath }/${ folder }` ); - return Fs.statSync( path ).isDirectory() && Fs.existsSync( Path.normalize( `${path}/package.json` ) ) - }) - .filter(folder => folder !== 'core'); - - resolve(['core', ...folders ]); // moving core to top - }) - .catch(() => resolve([])) - }) +const GetModules = async ( thisPath, verbose ) => { + try { + let dirContent = await Readdir( thisPath ); + let folders = ( await Promise.all( + dirContent.map( dirItem => { + let pkg = Path.normalize( `${ thisPath }/${ dirItem }/package.json` ); + return ( async () => { + try { + return await Access( pkg ) || dirItem; + } catch ( e ) { + return '' + } + } )() + } ) + ) ) + .filter( folder => folder !== '' && folder !== 'core' ); + + return [ 'core', ...folders ]; // moving core to top + } catch (e) { + return []; + } }; From 858e4317c4a91ed8b5a4f707fa87fb0ddf8749c2 Mon Sep 17 00:00:00 2001 From: Anton <38910496+toxamiz@users.noreply.github.com> Date: Wed, 27 Mar 2019 10:36:57 +0300 Subject: [PATCH 5/5] GetModules: log error --- scripts/helper.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/helper.js b/scripts/helper.js index b7a93b3df..bfde45385 100644 --- a/scripts/helper.js +++ b/scripts/helper.js @@ -174,7 +174,8 @@ const GetModules = async ( thisPath, verbose ) => { .filter( folder => folder !== '' && folder !== 'core' ); return [ 'core', ...folders ]; // moving core to top - } catch (e) { + } catch ( error ) { + console.error(error); return []; } };