Skip to content
This repository was archived by the owner on Aug 26, 2021. It is now read-only.

helper.js: GetFolders -> GetModules, add package.json existence check #697

Closed
wants to merge 5 commits into from
Closed

helper.js: GetFolders -> GetModules, add package.json existence check #697

wants to merge 5 commits into from

Conversation

ghost
Copy link

@ghost ghost commented Mar 21, 2019

Suggestion to #696

Replace of GetFolders function.

Copy link
Contributor

@azerella azerella left a 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 👍

thisFile => Fs.statSync(`${ thisPath }/${ thisFile }`).isDirectory()
thisFile => {
let path = `${ thisPath }/${ thisFile }`;
return Fs.statSync(path).isDirectory() && Fs.existsSync(`${path}/package.json`)
Copy link
Contributor

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

Copy link
Author

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.

@ghost
Copy link
Author

ghost commented Mar 22, 2019

updated to async version

thisFile => Fs.statSync(`${ thisPath }/${ thisFile }`).isDirectory()
thisFile => {
let path = `${ thisPath }/${ thisFile }`;
return Fs.statSync(path).isDirectory() && Fs.existsSync(`${path}/package.json`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

Copy link
Contributor

@azerella azerella left a 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 be function( 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 and Fs.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 the fs 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();
}();

}
const GetModules = ( thisPath, verbose ) => {
return new Promise(resolve => {
Readdir(thisPath)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Readdir(thisPath)
Readdir( thisPath )

return [];
}
const GetModules = ( thisPath, verbose ) => {
return new Promise(resolve => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return new Promise(resolve => {
return new Promise( resolve => {

const GetModules = ( thisPath, verbose ) => {
return new Promise(resolve => {
Readdir(thisPath)
.then(dirContent => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.then(dirContent => {
.then( dirContent => {

Readdir(thisPath)
.then(dirContent => {
let folders = dirContent
.filter(folder => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.filter(folder => {
.filter( folder => {

let folders = dirContent
.filter(folder => {
let path = Path.normalize( `${ thisPath }/${ folder }` );
return Fs.statSync( path ).isDirectory() && Fs.existsSync( Path.normalize( `${path}/package.json` ) )
Copy link
Contributor

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.

let path = Path.normalize( `${ thisPath }/${ folder }` );
return Fs.statSync( path ).isDirectory() && Fs.existsSync( Path.normalize( `${path}/package.json` ) )
})
.filter(folder => folder !== 'core');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.filter(folder => folder !== 'core');
.filter( folder => folder !== 'core' );

})
.filter(folder => folder !== 'core');

resolve(['core', ...folders ]); // moving core to top
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
resolve(['core', ...folders ]); // moving core to top
resolve( [ 'core', ...folders ] ); // moving core to top

@azerella azerella added enhancement New feature or request. good first issue A good issue to work on first. maintenance General upgrades or updates. labels Mar 24, 2019
@ghost
Copy link
Author

ghost commented Mar 25, 2019

Hey, @adamzerella

Updated to async/await syntax. But there is one big question, see below.

Since we are refactoring we should also update the Fs.statSync and Fs.existsSync functions.

Async Fs.exists is deprecated. The only way to check file existence asynchronously is to use Fs.access. The main issue is Fs.access provides not obvious behavior, because it's a void function (return undefined), that throws an error if file is not exists. And we don't need to check that package/component is a directory via Fs.stat and straight check access to package/component/package.json.

See last commit, what you think?

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 the fs module in promises

It is node 10 experimental API, but your minimum requirement is node 8 (readme).

Copy link
Contributor

@azerella azerella left a 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 ()

@ghost
Copy link
Author

ghost commented Mar 27, 2019

Updated.

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 ()

Not necessary for now. All init functions are used synchronously

@azerella
Copy link
Contributor

azerella commented May 1, 2019

@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 helper.js file is going to be refactored in the future and possibly removed, we just aren't there yet.

@azerella azerella closed this May 1, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request. good first issue A good issue to work on first. maintenance General upgrades or updates.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant