-
Notifications
You must be signed in to change notification settings - Fork 110
helper.js: GetFolders -> GetModules, add package.json existence check #697
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@toxamiz good stuff!, I've left some review comments, let me know what you think 👍
scripts/helper.js
Outdated
thisFile => Fs.statSync(`${ thisPath }/${ thisFile }`).isDirectory() | ||
thisFile => { | ||
let path = `${ thisPath }/${ thisFile }`; | ||
return Fs.statSync(path).isDirectory() && Fs.existsSync(`${path}/package.json`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we are going to refactor this function I think making it asynchronous would be really beneficial. Currently a lot of the code in helper.js
is synchronous and as a good rule of thumb for any I/O this stuff should be async.
I like the readability changes but if we're updating this, we should make it really nice
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, there are a lot of stuff which can be run asynchronous.
OK, i can "promisify" this function and change calling code.
updated to async version |
scripts/helper.js
Outdated
thisFile => Fs.statSync(`${ thisPath }/${ thisFile }`).isDirectory() | ||
thisFile => { | ||
let path = `${ thisPath }/${ thisFile }`; | ||
return Fs.statSync(path).isDirectory() && Fs.existsSync(`${path}/package.json`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return Fs.statSync(path).isDirectory() && Fs.existsSync(`${path}/package.json`) | |
return Fs.statSync( path ).isDirectory() && Fs.existsSync( `${path}/package.json` ) |
We might need to put a Path.normalize( ${path}/package.json )
here also
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few things still need to be addressed:
- Keep an eye out on the spacing with arguments,
function(arg1)
should befunction( arg1 )
. To be fair we should provide contributors with a coding style guide, we'll look at this in the future. - Since we are refactoring we should also update the
Fs.statSync
andFs.existsSync
functions. - Rather than promisifying the functions, we'd prefer, for readability sake to use the
async/await
syntax. Node has an experimental API at the moment that wraps thefs
module in promises. e.g
const Fsp = require('fs').promises;
( async() => {
let file = await Fsp.readFile( `file.txt`, `utf-8` );
let fileExists = ( await Fsp.stat( `file.txt` ) ).isDirectory();
}();
scripts/helper.js
Outdated
} | ||
const GetModules = ( thisPath, verbose ) => { | ||
return new Promise(resolve => { | ||
Readdir(thisPath) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Readdir(thisPath) | |
Readdir( thisPath ) |
scripts/helper.js
Outdated
return []; | ||
} | ||
const GetModules = ( thisPath, verbose ) => { | ||
return new Promise(resolve => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return new Promise(resolve => { | |
return new Promise( resolve => { |
scripts/helper.js
Outdated
const GetModules = ( thisPath, verbose ) => { | ||
return new Promise(resolve => { | ||
Readdir(thisPath) | ||
.then(dirContent => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.then(dirContent => { | |
.then( dirContent => { |
scripts/helper.js
Outdated
Readdir(thisPath) | ||
.then(dirContent => { | ||
let folders = dirContent | ||
.filter(folder => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.filter(folder => { | |
.filter( folder => { |
scripts/helper.js
Outdated
let folders = dirContent | ||
.filter(folder => { | ||
let path = Path.normalize( `${ thisPath }/${ folder }` ); | ||
return Fs.statSync( path ).isDirectory() && Fs.existsSync( Path.normalize( `${path}/package.json` ) ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function could be refactored to read nicer and is also still synchronous and IO based.
scripts/helper.js
Outdated
let path = Path.normalize( `${ thisPath }/${ folder }` ); | ||
return Fs.statSync( path ).isDirectory() && Fs.existsSync( Path.normalize( `${path}/package.json` ) ) | ||
}) | ||
.filter(folder => folder !== 'core'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.filter(folder => folder !== 'core'); | |
.filter( folder => folder !== 'core' ); |
scripts/helper.js
Outdated
}) | ||
.filter(folder => folder !== 'core'); | ||
|
||
resolve(['core', ...folders ]); // moving core to top |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolve(['core', ...folders ]); // moving core to top | |
resolve( [ 'core', ...folders ] ); // moving core to top |
Hey, @adamzerella Updated to async/await syntax. But there is one big question, see below.
Async See last commit, what you think?
It is node 10 experimental API, but your minimum requirement is node 8 (readme). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR still needs has some linting issues.
It would be cleaner to stick with one fs
import and just access the function access()
and readdir()
as needed. I'm curious if the GetModules()
function needs to be updated now that it is async
and other functions that call it like init: ()
now being init: async ()
Updated.
Not necessary for now. All init functions are used synchronously |
@toxamiz I'm going to close this PR as it doesn't resolve the original issue it was referencing #696. It still has some outstanding formatting issues, this isn't your fault. As you brought up in #710 we should have linting functionality to address this but that carries over other issues as we've previously discussed. The |
Suggestion to #696
Replace of
GetFolders
function.