https://github.com/egormkn/run-in-subdirectory
📁 A command-line utility for running commands in subdirectories (e.g. in a monorepo) with a set of pre-commit hooks
https://github.com/egormkn/run-in-subdirectory
git git-hooks hooks monorepo pre-commit pre-commit-hook pre-commit-hooks subdirectory utilities
Last synced: about 1 month ago
JSON representation
📁 A command-line utility for running commands in subdirectories (e.g. in a monorepo) with a set of pre-commit hooks
- Host: GitHub
- URL: https://github.com/egormkn/run-in-subdirectory
- Owner: egormkn
- License: mit
- Created: 2024-02-25T09:09:27.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-02T18:10:29.000Z (about 2 years ago)
- Last Synced: 2025-10-05T09:23:38.302Z (8 months ago)
- Topics: git, git-hooks, hooks, monorepo, pre-commit, pre-commit-hook, pre-commit-hooks, subdirectory, utilities
- Language: Python
- Homepage:
- Size: 107 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/egormkn/run-in-subdirectory/actions/workflows/workflow.yml)
[](https://codecov.io/gh/egormkn/run-in-subdirectory)
[](https://pypi.org/project/run-in-subdirectory/)
[](https://github.com/egormkn/run-in-subdirectory/blob/main/LICENSE)

[](https://github.com/pre-commit/pre-commit)
[](https://github.com/psf/black)
[](https://github.com/PyCQA/isort)
[](https://github.com/astral-sh/ruff)
# run-in-subdirectory
A command-line utility for running commands in subdirectories (e.g. in a monorepo) with a set of pre-commit hooks
## Usage
### As a pre-commit hook
- Use [`run-in-subdirectory`](.pre-commit-hooks.yaml) hook to run command in a subdirectory passed as the first argument.
In this example, pre-commit will run the command `npx --no -- prettier -w -u` in `client` subdirectory, and the command `poetry run black` in `server` subdirectory:
```yaml
repos:
- repo: https://github.com/egormkn/run-in-subdirectory
rev: 1.0.1
hooks:
- id: run-in-subdirectory
alias: prettier
name: Format client code with Prettier
args: ["client", "npx --no -- prettier -w -u"]
types: [ text ]
files: ^client/
- id: run-in-subdirectory
alias: black
name: Format server code with Black
args: ["server", "poetry run black"]
types: [ python ]
files: ^client/
```
- Use one of [`run-in-...-level-subdirectory`](.pre-commit-hooks.yaml) hooks to automatically extract `first`, `second` or `third`-level subdirectory from the last file path, that was passed to the hook by pre-commit.
Note that you should set `files`, `types` and/or `exclude` properties so that the hook only runs for files in that subdirectory.
```yaml
repos:
- repo: https://github.com/egormkn/run-in-subdirectory
rev: 1.0.1
hooks:
- id: run-in-first-level-subdirectory
alias: prettier
name: Format client code with Prettier
args: ["npx --no -- prettier -w -u"]
types: [ text ]
files: ^client/
- id: run-in-first-level-subdirectory
alias: black
name: Format server code with Black
args: ["poetry run black"]
types: [ python ]
files: ^client/
```
- If the available hooks are not enough for your task, use a custom Python hook and execute `run-in-subdirectory` as a command-line utility). Also, please [open an issue](https://github.com/egormkn/run-in-subdirectory/issues) to report such cases.
```yaml
repos:
- repo: local
hooks:
- id: prettier
name: Format client code with Prettier
language: python
additional_dependencies:
- "run-in-subdirectory==1.0.1"
entry: run-in-subdirectory -d client npx --no -- prettier -w -u
types: [ text ]
files: ^client/
```
### As a command-line utility
`run-in-subdirectory` can also be used as a command-line utility:
```bash
pip install run-in-subdirectory
```
```
usage: run-in-subdirectory [-h] [-v] (-l LEVEL | -d DIRECTORY) executable [args ...]
Runs the command in a subdirectory and fixes paths in arguments.
positional arguments:
executable Executable to run
args Sequence of program arguments
options:
-h, --help show this help message and exit
-v, --verbose Print information about a command to be called
-l LEVEL, --level LEVEL
Subdirectory level (0 for top-level directory)
-d DIRECTORY, --directory DIRECTORY
Subdirectory within which the subprocess will be executed
example:
When this program is executed with the following command:
run-in-subdirectory -d client npx --no prettier client/src/index.ts
Then the command will be executed:
npx --no prettier src/index.ts
with the current working directory set to `client`.
```