Skip to content

Commit a638353

Browse files
committed
Add a readme
1 parent ca3c58f commit a638353

File tree

1 file changed

+187
-0
lines changed

1 file changed

+187
-0
lines changed

README.md

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
# 🔃 run-if-changed [![Build Status](https://travis-ci.org/hkdobrev/run-if-changed.svg?branch=master)](https://travis-ci.org/hkdobrev/run-if-changed)
2+
3+
Run a command if a file changes via Git hooks.
4+
Useful for lock files or build systems to keep dependencies and generated files up to date when changing branches, pulling or commiting.
5+
6+
Inspired by [`lint-staged`](https://github.com/okonet/lint-staged) and recommended to be used with [`husky`](https://github.com/typicode/husky).
7+
8+
#### State of the project
9+
10+
run-if-changes is functional as-is, but it's still quite basic and rough as it has just been published. So issues, feature requests and pull requests are most welcome!
11+
12+
## Installation and setup
13+
14+
In `package.json`:
15+
16+
```json
17+
"husky": {
18+
"hooks": {
19+
"post-commit": "run-if-changed",
20+
"post-checkout": "run-if-changed",
21+
"post-merge": "run-if-changed",
22+
"post-rewrite": "run-if-changed"
23+
}
24+
}
25+
```
26+
27+
#### Install with Yarn:
28+
29+
```shell
30+
yarn add -D husky @hkdobrev/run-if-changed
31+
```
32+
33+
##### Recommended setup:
34+
35+
```json
36+
"run-if-changed": {
37+
"yarn.lock": "yarn install"
38+
}
39+
```
40+
41+
#### Install with npm:
42+
43+
```shell
44+
npm install --save-dev husky @hkdobrev/run-if-changed
45+
```
46+
47+
##### Recommended setup:
48+
49+
```json
50+
"run-if-changed": {
51+
"package-lock.json": "npm install"
52+
}
53+
```
54+
55+
## Why
56+
57+
The use case for `run-if-changed` is mostly for a team working on a project and push and pull code in different branches. When you share dependencies, database migrations or compilable code in the shared Git repository often some commands need to be run when a file or folder gets updated.
58+
59+
Check out the [common use cases](#use-cases).
60+
61+
## Configuration
62+
63+
* `run-if-changed` object in your `package.json`
64+
* `.run-if-changedrc` file in JSON or YML format
65+
* `run-if-changed.config.js` file in JS format
66+
67+
See [cosmiconfig](https://github.com/davidtheclark/cosmiconfig) for more details on what formats are supported.
68+
69+
Configuration should be an object where each key is a file or directory mathed by Git and the value is either a signle command or an array of commands to run if the file have changed since the last Git operation.
70+
71+
## What commands are supported?
72+
73+
For now, only globally available commands are allowed. PRs welcome so local scripts are used first.
74+
75+
Sequences of commands are supported. Pass an array of commands instead of a single one and they will run sequentially.
76+
77+
## Use cases
78+
79+
#### Install or update dependencies when lock file changes
80+
81+
If you use a dependency manager with a lock file like npm, Yarn, Composer, Bundler or others, you would usually add a dependency and the dependency manager would install it and add it to the lock file in a single run. However, when someone else has updated a dependency and you pull new code or checkout their branch you need to manually run the install command of your dependency manager.
82+
83+
Here's example configuration of `run-if-changed`:
84+
85+
<details open>
86+
<summary><b>When using Yarn:</b></summary>
87+
88+
`package.json`:
89+
90+
```json
91+
{
92+
"run-if-changed": {
93+
"yarn.lock": "yarn install"
94+
}
95+
}
96+
```
97+
98+
`.run-if-changedrc`:
99+
100+
```json
101+
{
102+
"yarn.lock": "yarn install"
103+
}
104+
```
105+
</details>
106+
107+
<details>
108+
<summary><b>When using npm:</b></summary>
109+
110+
`package.json`:
111+
112+
```json
113+
{
114+
"run-if-changed": {
115+
"package-lock.json": "npm install"
116+
}
117+
}
118+
```
119+
120+
`.run-if-changedrc`:
121+
122+
```json
123+
{
124+
"package-lock.json": "npm install"
125+
}
126+
```
127+
128+
</details>
129+
130+
<details>
131+
<summary><b>When using Composere:</b></summary>
132+
133+
`package.json`:
134+
135+
```json
136+
{
137+
"run-if-changed": {
138+
"composer.lock": "composer install"
139+
}
140+
}
141+
```
142+
143+
</details>
144+
145+
<details>
146+
<summary><b>When using Bundler:</b></summary>
147+
148+
`package.json`:
149+
150+
```json
151+
{
152+
"run-if-changed": {
153+
"Gemfile.lock": "bundle install"
154+
}
155+
}
156+
```
157+
158+
</details>
159+
160+
#### Run database migrations if there are new migrations
161+
162+
If you keep database migrations in your repository, you'd usually want to run them when you check out a branch or pull from master.
163+
164+
`package.json`:
165+
166+
```json
167+
{
168+
"run-if-changed": {
169+
"migrations": "./console db:migrate --allow-no-migration --no-interaction"
170+
}
171+
}
172+
```
173+
174+
The above example assumes PHP Doctrine migrations.
175+
176+
#### Compile sources in a build folder after pulling new code.
177+
178+
`package.json`:
179+
180+
```json
181+
{
182+
"run-if-changed": {
183+
"src": "yarn build"
184+
}
185+
}
186+
```
187+

0 commit comments

Comments
 (0)