lefthook_dart

pub.flutter-io.cn badge GitHub Maintained Pub points GitHub License Lefthook

Add fast and reliable Git hooks to your Flutter and Dart projects with this Dart wrapper around Lefthook.

About this project

This is a continuation of the original project-cemetery/lefthook package, which served as a simple Dart CLI wrapper around the Lefthook binary.

The original repository is no longer maintained. This fork updates the tool for modern Dart/Flutter projects, ensuring compatibility, reliability, and ease of use.

For detailed usage and configuration options, refer to the official Lefthook documentation.

Installation

Step 1: Enable the lefthook command to run from any place in your terminal

If you already have the original Lefthook installed, you will need to uninstall it, as it may cause conflicts.

flutter pub global activate lefthook_dart

This will ensure that the lefthook command uses lefthook_dart, a Dart-based wrapper around original Lefthook.

Step 2: Create lefthook.yml in root of your project.

# lefthook.yml
pre-commit:
  commands:
    prettify:
      glob: "*.dart"
      run: dart format --line-length 80 {staged_files}
      stage_fixed: true

Step 3: Run lefthook install inside the project directory where you want Git hooks to be managed.

If it's the first run it will download the native Lefthook binary for your OS, install Git hooks using lefthook install, and prepare your repo for hook execution.

lefthook install

Now, every time you run git commit, this hook will automatically format staged Dart files and proceed commit with formatted code. 👉 For a working Flutter example, see example/README.md

Step 4: Test hook manually (optional)

lefthook run pre-commit

Step 5: To uninstall (optional)

flutter pub global deactivate lefthook_dart

If you are using Melos, you can automate Lefthook setup by adding these scripts to your melos.yaml.

scripts:
  ###############################################
  ##          GIT HOOK COMMANDS                ##
  ###############################################
  hooks:install:
    run: flutter pub global activate lefthook_dart && lefthook install
  
  hooks:uninstall:
    run: flutter pub global deactivate lefthook_dart
  
  hooks:run:
    run: lefthook run pre-commit

Example:

After verifying that Lefthook is working, you can start customizing it by adding additional rules like the following:

  • For wide screen we increase max length formatting to 120
  • For Melos we specify packages/ root folder
  • For files generated by build_runner we exclude them
pre-push:
  parallel: true
  commands:
    #flutter-test:
    #  root: packages/
    #  run: flutter test
    #  fail_text: "Could not pass tests"
    flutter-analyze:
      glob: "*.dart"
      run: melos exec --fail-fast -- flutter analyze

pre-commit:
  commands:
    flutter_lint_code:
      root: "packages/"
      glob: "*.dart"
      exclude: "*.{g,freezed}.dart"
      run: |
        for file in {staged_files}; do
          dart fix --apply "$file"
        done
      stage_fixed: true
    prettify:
      root: "packages/"
      glob: "*.dart"
      exclude: "*.{g,freezed}.dart"
      run: dart format --line-length 120 {staged_files}
      stage_fixed: true

How to prepare and publish a new version of the package

edit CHANGELOG.md
edit lefthook_dart.dart to have the new version (const pubspecVersion = '1.0.2';)
edit pubspec.yaml to have the new version (version -> 1.0.2)
dart pub publish --dry-run                                                                                                                                                                                    
git tag 1.0.2                                                                                                                                                                                                 
git push origin 1.0.2
dart pub publish

Libraries