Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prettier auto formatting #233

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

itegika
Copy link

@itegika itegika commented Aug 4, 2023

Hi there!

This PR aims to enable Prettier code formatting on every commit using a pre-commit hook.

  1. Devs can't commit changes in hidden .git folder due to security reasons
  2. To address this and avoid each dev to do pre-commit hook copy-paste and setups locally (and in time project will grow and more hooks will be used), one widely used approach is to create in root directory a hidden folder to keep there those hooks, make them executables, config custom git hooks path. This PR is an implementation of pre-commit hook with that approach.
  3. After cloning repo other devs needs only to set the path for git hooks to use that new hooks folder (readme file updated with instruction).

I'll be glad to answer any further questions. Thanks!

@itegika itegika requested a review from lugenx as a code owner August 4, 2023 12:57
@itegika
Copy link
Author

itegika commented Aug 5, 2023

This PR aims to enable Prettier code formatting on every commit using a pre-commit hook

Copy link
Collaborator

@tsimian tsimian left a comment

Choose a reason for hiding this comment

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

Looks good to me @lugenx, great work @itegika!

Copy link
Owner

@lugenx lugenx left a comment

Choose a reason for hiding this comment

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

Thank you, so I have a couple of questions guys: @itegika, @vishwaszadte, @tsimian, @rcamach7
Can you please check my comments?

const { existsSync, promises: fsPromises } = require("fs");
const { join, resolve } = require("path");

const excludedDirs = ["node_modules"];
Copy link
Owner

Choose a reason for hiding this comment

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

Do you guys think it's a good idea to make this code read and exclude whatever is in the .gitignore file, instead of hard coding node_modules, in case we have something else in the future that we don't want to format (since we don't need to format anything that is not going to be pushed)

Copy link
Collaborator

Choose a reason for hiding this comment

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

That is a good point, it should exclude anything in the .gitignore file.

Copy link
Author

Choose a reason for hiding this comment

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

Yes guys, you are right. I was considering the same approach, as it offers more flexibility.
However, considering that:

  1. We are formatting .js files only
  2. Among the directories/files listed in .gitignore, only the node_modules directory contains .js files.
  3. After checking a list of most frequently "gitignored" files/directories, it seems unlikely that we will encounter other .js files within .gitignore.
  4. The .gitignore file is typically not very dynamic once the project architecture is established.
  5. The execution time using the hardcoded approach is significantly faster.

Taking all these factors into account, I hdecided to go for the hardcoded approach, as it strikes a suitable balance between speed and efficiency. However, if you still prefer to pursue the .gitignore approach, I would be glad to refactor the code accordingly


console.log("Prettier formatting applied to all JS files.");

const gitAddCmd = `git add ${allJsFiles.join(" ")}`;
Copy link
Owner

Choose a reason for hiding this comment

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

Is this going to add (stage) everything even if I try to add one file?

Copy link
Author

Choose a reason for hiding this comment

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

@lugenx you can add just one file. However, during git commit execution, all .js files will be formatted with prettier, to maintain consistent styling even if local formatting tools/settings are used by some devs.
Then formatted files are staged, ensuring accurate tracking by Git version control, which might not always catch formatting changes as effectively as content changes.
If your added file is the only one with style or content differences, only that one gets pushed

Copy link
Collaborator

Choose a reason for hiding this comment

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

@lugenx seeing that this PR itself formatted all files to fit the prettier configuration settings, any updated forks making commits after this merge will be formatted the same. So any files that are updated in future PRs should be the only files pushed.

Copy link
Author

Choose a reason for hiding this comment

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

yes, you are right @tsimian

Copy link
Collaborator

@rcamach7 rcamach7 left a comment

Choose a reason for hiding this comment

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

Haven't used this approach. Excited to try this out!

@lugenx
Copy link
Owner

lugenx commented Aug 28, 2023

hey @itegika,

thanks for the PR on automating prettier formatting.

here's where I'm pausing though: our main hiccup was ensuring devs didn't have to manually adjust settings to avoid unnecessary diffs. but with the current solution, we're still asking them to manually set a custom path using git config core.hooksPath .git-hooks. so, we still end up with a manual step, which was our initial challenge.

got any ideas on streamlining this part? I'm trying to brainstorm some solutions on my end as well.

cheers for diving into this!

@itegika
Copy link
Author

itegika commented Sep 10, 2023

Thanks for your comment @lugenx

I explored various options while working on this PR. The criteria for a good solution included simplicity, predictable output, and flexibility.

While options like a postinstall script or a custom post-clone hook were considered, they come with limitations in some cases. Depending on the package manager devs may use or the way they clone the repo, manual setting may still be required. To unify the setup for all, we would need to restrict the tools and workflows devs can use when working on the project. Another option is Dockerizing the app (should only be pursued if it aligns with the project's needs) but formatting automation should't be the main reason for that.

The current approach simplifies the setup from multiple steps to just one: setting up the path once, after cloning the repo, reducing the chances of errors in the process. Therefore, I believe that providing clear instructions in our Readme and having devs set up the hooks path is a simple, predictable solution that remains compatible with various workflows and tools.

If any of you, @lugenx @tsimian @vishwaszadte @rcamach7, have alternative solutions in mind, I'd appreciate hearing them

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants