• Stars
    star
    470
  • Rank 90,069 (Top 2 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 6 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

✨ Painlessly apply Prettier by only formatting lines you have modified anyway!

Precise Commits

Travis GitHub license NPM Version NPM Downloads Commitizen friendly semantic-release


Why precise-commits?

πŸ”Ž It is simply the most exact and least disruptive way to add consistent code formatting (by Prettier) to an existing codebase.

✨ You only reformat the exact code you have modified anyway as part of your normal development!

Tool Staged files Existing commits PR Build Arbitrary commands Precision
precise-commits βœ… βœ… βœ… ❌ Individual character ranges
lint-staged βœ… ❌ ❌ βœ… Entire File
pretty-quick βœ… ❌ ❌ ❌ Entire File

Background

Implementing a new code-style in an existing codebase can be really tricky.

Prettier is an amazing automated code formatting tool, but that does not mean that introducing it into an existing codebase is trivial.

Regardless of how consistent the existing code-style might be, introducing Prettier will result in larger diffs, which:

  1. Increases the complexity and review time burden of PRs on senior team members.
  2. Potentially increases the amount of time it takes to complete a PR in the first place.
  3. Can block concurrent work on the same codebase and/or result in nasty merge conflicts with outstanding feature branches.

Other tools, such as lint-staged, made an excellent first step towards mitigating the scope of the impact of the points above, by only running linters and formatters on files which have changed.

This is great for small codebases, in which the authors do not mind much that they are polluting the git history of the files they are touching, but it is not enough.

In large and enterprise codebases (particularly those organized as monorepos), the git history of each file is really important.

If I make a change on line 10 of a 4000 line file, I shouldn't be forced to reformat all 4000s lines (thus making me the last git user to update all of them) as part of my PR.

I should just need to reformat line 10.

This is where precise-commits comes in!


Our ideal end-goal...

  1. All developers on our team develop using consistent, automated formatting as they write their code. e.g. Running an IDE-based plugin, such as "vscode-prettier" with "format on save" enabled.

  2. Each time they commit, a precommit hook is triggered to ensure that the staged code is formatted consistently.

  3. Each time a Pull Request opened on our remote repo, a build is triggered on a CI server, during which the formatting is checked to ensure that all the files touched for that PR were formatted consistently.


How precise-commits helps us get there...

  1. All developers on our team write their code as they always have.

  2. Each time they commit, a precommit hook is triggered which will run precise-commits on the code and ensure that the exact code they already modified is formatted consistently. Any untouched existing code will not be mutated.

  3. Each time a Pull Request opened on our remote repo, a build is triggered on a CI server, during which precise-commits runs to ensure that all the committed lines of code for that PR was formatted consistently.


...and after enough time has passed, our codebase will be formatted consistently, despite us never having to disrupt our feature-building momentum!


How it works

Through analyzing your staged files (or any files modified between two given commit SHAs) precise-commits will work out exactly what lines and characters within those files have actually been changed or added.

It then uses this information to run Prettier in a very focused way, allowing it to only reformat what is relevant for your current work, and allowing you to keep your PRs small and explicit!


Installation

precise-commits expects prettier to be available as a peerDependency, so you will need to install this yourself as a devDependency of your project.

npm install --save-dev prettier precise-commits

Usage

It is intended that you will run precise-commits as a CLI, and it will automatically pick up on any of the standard Prettier configuration files you may have in your project, including .prettierignore files.

1. Running it manually

  1. Add an npm script to your package.json, such as:
{
  //...
  "scripts": {
    "precise-commits": "precise-commits"
  }
  //...
}
  1. Execute the npm script, e.g. for the one above run:
npm run precise-commits

2. "Precommit" Hook

The recommended way to run precise-commits is as a "precommit" hook.

A great tool for setting up the hook is husky. You can install and run it as follows:

npm install --save-dev husky

Update the "scripts" section of your package.json:

{
  //...
  "scripts": {
    "precise-commits": "precise-commits",
    "precommit": "npm run precise-commits"
  }
  //...
}

3. As part of a PR build

When running a build for your PR, you can run precise-commits to ensure that the author's changes are all formatted consistently.

The key things you need to configure are:

  1. The --check-only flag for precise-commits so that it will error out if it finds any inconsistent formatting
  2. The --head and --base flags so that precise-commits knows what commits it should consider when resolving modified files. Most CI servers will have environment variables you can use to resolve these.

For example, if your PR is building on Travis, your config might look like this:

.travis.yml

# ... Other config options here ...
install:
  - yarn install
script:
  - 'if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then precise-commits --whitelist="src/**/*.ts" --check-only --head=$TRAVIS_PULL_REQUEST_SHA --base=$(git merge-base HEAD $TRAVIS_BRANCH); fi'
  - yarn test
  - yarn e2e
# ... Other config options here ...

CLI Configuration Options

As was hinted at above, the precise-commits CLI supports a few different configuration options:

  • --whitelist: [String, Default: "*"]
    • Whitelist is a glob pattern (the glob syntax from the glob module is used).
    • It is used to inform what files are considered when resolving modified files (by default all are considered).
    • Don't forget the quotes around the globs! The quotes make sure that precise-commits expands the globs rather than your shell.

  • --formatter: [String, Default: "prettier"]
    • Currently only prettier is supported
    • If you are interested in adding support for a different formatter, all you need to do is provide an object which implements this interface.

  • --check-only: [Boolean, Default: false]
    • Only check the code formatting is consistent with the resolved config

  • --base: [String, NO DEFAULT]
    • Base commit SHA to be used in conjunction with the --head flag

  • --head: [String, CONDITIONAL DEFAULT]
    • Later commit SHA (e.g. the HEAD of a PR branch) to be used in conjunction with the --base flag
    • NOTE on conditional default: If no value is provided for --head, but a value is given for --base, it will default to checking "HEAD"

More Repositories

1

nx

Smart Monorepos Β· Fast CI
TypeScript
22,163
star
2

nx-console

Nx Console is the user interface for Nx & Lerna.
TypeScript
1,273
star
3

nx-examples

Example repo for Nx workspace
TypeScript
843
star
4

webpack-plugin-critical

Webpack wrapper for @addyosmani's critical library.
TypeScript
295
star
5

monorepo.tools

Your defacto guide on monorepos, and in depth feature comparisons of tooling solutions.
TypeScript
271
star
6

nx-recipes

Common recipes to productively use Nx with various technologies and in different setups. Made with ❀️ by the Nx Team
TypeScript
213
star
7

nx-workshop

Companion labs for the workshop: "Develop at Scale with Nx Monorepos"
TypeScript
171
star
8

nx-set-shas

✨ A Github Action which sets the base and head SHAs required for `nx affected` commands in CI
TypeScript
141
star
9

nx-labs

A collection of Nx plugins
TypeScript
128
star
10

nx-azure-build

Example of setting up distributed Azure build for Nx workspace
TypeScript
113
star
11

nx-react-native

86
star
12

react-module-federation

Example showing how to use Nx's new module federation support to speed up the builds of React apps
TypeScript
69
star
13

nx-incremental-large-repo

TypeScript
65
star
14

nx-jenkins-build

Example of setting up distributed Jenkins build for Nx workspace
TypeScript
63
star
15

ci

TypeScript
47
star
16

nx-react-workshop

TypeScript
43
star
17

last-successful-commit-action

GitHub action for identifying the last successful commit for a given workflow and branch.
JavaScript
43
star
18

node-microservices

TypeScript
42
star
19

nx-angular-and-react

TypeScript
42
star
20

blog-post-nx-production

TypeScript
36
star
21

nx-apollo-react-example

TypeScript
34
star
22

gatsby

Nx plugin for Gatsby
TypeScript
32
star
23

nx-bazel-example

Python
31
star
24

nx-distributed-cache-example

Nx workspace connected to Nx Cloud's distributed cache
TypeScript
31
star
25

ng-module-federation

Example showing to use new Nx's module federation support to speed up the builds of Angular apps
TypeScript
31
star
26

example-nx-fullstack

TypeScript
29
star
27

nx-example-multirepo

28
star
28

react-nx-example

Example for the blog post
TypeScript
27
star
29

nx-apollo-angular-example

TypeScript
26
star
30

ngrx-workshop-app

NgRx Workshop Example Application based on Shopping Cart from Angular.io docs
TypeScript
25
star
31

ngrx-example

Application Showing How to Use NgRx
TypeScript
22
star
32

add-nx

JavaScript
22
star
33

ts-aoc-starter

TypeScript
22
star
34

nx-cloud-helm

Nx Cloud Helm Charts
Smarty
21
star
35

playbook-angular-v6-elements-example

TypeScript
21
star
36

board-game-hoard

TypeScript
20
star
37

workshop-nx-labs

Lab exercises for the Nx Enterprise workshop(s)
20
star
38

workshop-nx-starter

Starter repo for the Nx Enterprise workshop(s).
TypeScript
20
star
39

nx-orb

✨ A CircleCI Orb which includes helpful commands for running Nx commands in the CI
JavaScript
19
star
40

nx-expo

Official Expo plugin for Nx (alpha)
19
star
41

ngrx_race_condition

TypeScript
18
star
42

angular-reactive-forms-course

TypeScript
18
star
43

bazel-demo-app

Bazel Demo Application for Blog Post
TypeScript
18
star
44

angular-vscode

Useful VSCode plugins for Angular Development
TypeScript
17
star
45

nx-apollo-example

TypeScript
15
star
46

nx-go-project-graph-plugin

ℹ️ Please take a look at the plugin https://github.com/nx-go/nx-go if you want to use Go and Nx together
Go
15
star
47

nx-tag-successful-ci-run

✨ A Github Action which tags the repo, intended to be used only when on the main branch of your repo at the end of an Nx powered CI run
13
star
48

full-stack-react-example-app

TypeScript
12
star
49

bazel-rollup

TypeScript
11
star
50

nxcloud-k8s-setup

Example of running Nx Cloud on K8s
10
star
51

cra-to-nx

Tool for converting CRA project to Nx workspace
TypeScript
9
star
52

advent-of-code-starter

TypeScript
9
star
53

nx-cypress-command-sharing

Example repo to showcase how to share Cypress commands within various e2e tests in a Nx monorepo
TypeScript
8
star
54

pokemon-supabase-example

TypeScript
8
star
55

nx-trpc-demo

TypeScript
7
star
56

nx-ecosystem-ci

TypeScript
7
star
57

upgrade-with-elements

TypeScript
7
star
58

nx-shops

TypeScript
6
star
59

nx-custom-layout

TypeScript
6
star
60

bazel-cli-build

JavaScript
6
star
61

nx-migrate-angularjs-example

An example repo demonstrating a migration of an AngularJS application into an Nx workspace
JavaScript
6
star
62

bazel-docker-example

Python
6
star
63

cra-to-nx-migration

TypeScript
6
star
64

tic-tac-toe-playwright

TypeScript
6
star
65

nx-devkit-schematics

An example library that uses Nx Devkit to author Angular Schematics
TypeScript
6
star
66

zack-live-stream

The code base being worked on by Zack's livestream
TypeScript
6
star
67

nxcloud-k8s

Nx Cloud Kubernetes Example
6
star
68

angular-react-node

TypeScript
6
star
69

nx-cloud-workflows

JavaScript
5
star
70

nx-plugin-community-example

TypeScript
5
star
71

trpc-livestream

TypeScript
5
star
72

stackblitz-nx-angular

StackBlitz template for an Nx monorepo with Angular
TypeScript
4
star
73

react-to-solid

TypeScript
4
star
74

large-ts-monorepo

TypeScript
4
star
75

simple-portal-example

TypeScript
4
star
76

workshop-ngconf-18-labs

Lab exercises for the ng-conf 2018 workshop
4
star
77

transfer-state

TypeScript
3
star
78

lazy-spinners

TypeScript
3
star
79

angular-console

3
star
80

nx-incremental

Example showing how to set up incremental builds with Nx
TypeScript
3
star
81

workshop-pwa-fishgoco

Demo project for PWA workshop
TypeScript
3
star
82

nx-example-workspace

TypeScript
3
star
83

count-contributors-sample

Sample Repo for Node CLI exercise
JavaScript
2
star
84

bug-repro-circular

TypeScript
2
star
85

nx-example-sharedlib

TypeScript
2
star
86

hybrid-sample

TypeScript
2
star
87

nrwl-coding-assignment

TypeScript
2
star
88

release-js-and-rust

Example of versioning and publishing JS and Rust code together using nx release
Rust
2
star
89

spring-boot-livestream

TypeScript
2
star
90

stackblitz-nx-react

StackBlitz template for an Nx monorepo with React
TypeScript
2
star
91

using-tsc-b-with-next

TypeScript
2
star
92

angular-module-federation-example

TypeScript
2
star
93

nx-coding-assignment

Coding assignment for JavaScript/TypeScript developers
TypeScript
2
star
94

workshop-upgrade-angular-project

Angular version of TuskDesk demo project for upgrade workshops
TypeScript
2
star
95

nrwl-coding-assignment-angular

TypeScript
2
star
96

named-outlets

TypeScript
2
star
97

workshop-upgrade-angularjs-starter

AngularJS version of TuskDesk demo project for workshops
JavaScript
2
star
98

gatsby-example

Example repository that shows Gatsby and Nx in action
TypeScript
2
star
99

nx-14-5-cypress-10-example

TypeScript
1
star
100

.github

Nrwl GitHub Org profile
1
star