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

chore: deps husky and commitlint #19

Conversation

balaji-sivasakthi
Copy link
Contributor

@balaji-sivasakthi balaji-sivasakthi commented Jul 19, 2023

Related issue:-

Issue #16

Changes Made:-

  • commitlint with husky config
  • lint-stage config
  • readme and contributor markdown updated

Additional Context:-

Type Heading Rule Description
ci CI Continuous Integration Changes related to continuous integration.
chore Chore Maintenance tasks Other changes that don't affect production.
docs Docs Documentation Changes related to documentation.
feat Feature New Feature New feature implementations or additions.
fix Fix Bug Fixes Bug fixes or corrections.
perf Perf Performance Improvements Performance-related improvements.
refactor Refactor Code Refactoring Code changes that don't fix bugs or add features, but improve the code structure.
revert Revert Revert Previous Commits Reverting previous commits.
style Style Code Formatting or Style Changes related to code formatting or style.
assets Assets Add or Update Assets (e.g., images, files) Changes related to adding or updating assets, such as images or other files.

@wajeshubham
Copy link
Collaborator

@balaji-sivasakthi I appreciate your effort in keeping the project and commit messages tidy.

However, upon testing the implementation, I discovered that the git commit command does not fail when an ugly commit message is provided, which ideally should happen to enforce clean commit messages.

Also, is it expected that i run yarn pre-commit to lint my staged files every time before committing?

Getting the following warning upon committing.

hint: The '.husky/pre-commit' hook was ignored because it's not set as executable.
hint: You can disable this warning with `git config advice.ignoredHook false`.
hint: The '.husky/commit-msg' hook was ignored because it's not set as executable.
hint: You can disable this warning with `git config advice.ignoredHook false`.

Please let me know your thoughts on this matter, and if you agree with these points. I would love to learn more about this.

@balaji-sivasakthi
Copy link
Contributor Author

balaji-sivasakthi commented Jul 19, 2023

@wajeshubham Thank you! Husky will handle the pre-commit on its own, so there's no need to execute yarn pre-commit before every commit. However, there might be some edge cases where we can use yarn pre-commit with specific options, such as yarn pre-commit --allow-empty to style the staged files without needing a commit message.

Sorry for the trouble, It was an egde case in mac/Linux

hint: The '.husky/pre-commit' hook was ignored because it's not set as executable.
hint: You can disable this warning with `git config advice.ignoredHook false`.
hint: The '.husky/commit-msg' hook was ignored because it's not set as executable.
hint: You can disable this warning with `git config advice.ignoredHook false`.

To prevent this issue, we can allow permission to the .husky directory by executing the following command:

chmod ug+x .husky/*

This command grants execute (x) permission to the user (u) and the group (g) for all files (*) inside the .husky directory. It ensures that the necessary scripts in the .husky directory can be executed properly during the pre-commit process.

{
"prepare": "husky install | chmod ug+x .husky/*"
}

@wajeshubham
Copy link
Collaborator

wajeshubham commented Jul 19, 2023

@balaji-sivasakthi So, how about adding yarn prepare or npm run prepare in ./husky/commit-msg? So, that we don't need to manually install the husky hooks.

#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
yarn prepare # prepare husky hooks
npx --no-install commitlint --edit

Also, "prepare": "husky install | chmod ug+x .husky/*" This script works for macOS. However, just to be sure this script should not break in the windows OS.

NOTE: As this PR has changes that are involved in the contribution flow we also need to update the Contribution guide according to this new convention of commit messages.

@balaji-sivasakthi
Copy link
Contributor Author

balaji-sivasakthi commented Jul 19, 2023

"prepare": "husky install | chmod ug+x .husky/*"

Yes, this script would break on Windows. However, it can be sorted out by using cross-spawn for executing the prepare script.

import { spawnSync } from 'child_process';
import os from 'os';

// Function to run commands depending on the OS
const runCommand = (command, args) => {
  const isWindows = os.platform() === 'win32';
  const shell = isWindows ? true : false;
  const commandArgs = isWindows ? ['/C', command, ...args] : [...args];

  const result = spawnSync(isWindows ? 'cmd' : command, commandArgs, {
    stdio: 'inherit',
    shell,
  });

  return result.status;
};

// Function to prepare Husky
const prepareHusky = () => {
  // Run husky install
  const huskyInstallStatus = runCommand('npx', ['husky', 'install']);

  // Set permissions for husky on macOS/Linux
  if (os.platform() !== 'win32') {
    const setPermissionsStatus = runCommand('chmod', ['+x', '.husky/*']);
    if (setPermissionsStatus !== 0) {
      console.error('Setting permissions failed');
      process.exit(1);
    }
  }

  // Exit with appropriate status code based on husky install status
  if (huskyInstallStatus === 0) {
    console.log('Husky preparation executed successfully!');
    process.exit(0);
  } else {
    console.error('Husky installation failed');
    process.exit(1);
  }
};

prepareHusky()

@wajeshubham

NOTE: As this PR has changes that are involved in the contribution flow we also need to update the Contribution guide according to this new convention of commit messages.

Ok, Sure I will add all necessary docs.

@wajeshubham
Copy link
Collaborator

@balaji-sivasakthi script is failing in the macOS on running yarn prepare

# Error:
husky - Git hooks installed
chmod: .husky/*: No such file or directory # I have .husky folder. Testing your branch only
Setting permissions failed
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Also, seems like an extra boilerplate to just lint the commit, which no doubt is a good practice. However, it would be great if we consider and test all the edge cases before merging.

Thank you! 😀

@balaji-sivasakthi
Copy link
Contributor Author

balaji-sivasakthi commented Jul 21, 2023

@wajeshubham

# Error:
husky - Git hooks installed
chmod: .husky/*: No such file or directory # I have .husky folder. Testing your branch only
Setting permissions failed
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

linux

I've tested it in GitHub Codespace, and it works well now.

@balaji-sivasakthi
Copy link
Contributor Author

@wajeshubham I'm also updated README.md and CONTRIBUTOR.md for conventional commit message format. Please take a moment to look at this.

@jwala-anirudh
Copy link
Collaborator

@balaji-sivasakthi thanks for the PR, I will review it thoroughly and provide the approval

@balaji-sivasakthi
Copy link
Contributor Author

@wajeshubham Is this ok?

@wajeshubham
Copy link
Collaborator

Yes @balaji-sivasakthi we are reviewing it.

Copy link
Collaborator

@jwala-anirudh jwala-anirudh left a comment

Choose a reason for hiding this comment

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

I have added some comments after reviewing the changes, please go through them

.husky/pre-commit Show resolved Hide resolved
CONTRIBUTING.md Outdated Show resolved Hide resolved
prepare.js Outdated Show resolved Hide resolved
@balaji-sivasakthi
Copy link
Contributor Author

@jwala-anirudh Please check the recent changes

Copy link
Collaborator

@jwala-anirudh jwala-anirudh left a comment

Choose a reason for hiding this comment

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

Approved.

@wajeshubham wajeshubham self-requested a review July 31, 2023 10:36
Copy link
Collaborator

@wajeshubham wajeshubham 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!

@wajeshubham wajeshubham merged commit b75c4da into hiteshchoudhary:main Aug 2, 2023
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.

None yet

3 participants