Skip to main content

Build Coverage PyPI - Version PyPI - License PyPI - Python Version pre-commit Code style: black Imports: isort Linting: ruff

run pre-commit in subdirectory

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 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:

    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 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.

    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 to report such cases.

    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:

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`.

Metadata

Release files for run-in-subdirectory 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for run-in-subdirectory 1.0.1
File Size Uploaded
run_in_subdirectory-1.0.1.tar.gz 8.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for run-in-subdirectory 1.0.1
File Interpreter ABI Platform
run_in_subdirectory-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 19.3 kB

Release files / run_in_subdirectory-1.0.1.tar.gz

Download URL run_in_subdirectory-1.0.1.tar.gz
Size 8.9 kB
Tags Source
SHA-256 checksum
How to use checksums
3fb900d71e91a827e78e92169f551206def054a43918d25190b7bd8e46ed398d
BLAKE2b-256 checksum
How to use checksums
588543f2a94180b2fde7e0670a8465ed06ca461e9bf3eac46009d3e2a0b5083d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.8

Release files / run_in_subdirectory-1.0.1-py3-none-any.whl

Download URL run_in_subdirectory-1.0.1-py3-none-any.whl
Size 10.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cd8fd52a5754d539d6edede24e9383bd63e32df408f65351ccfb993e3a129fa7
BLAKE2b-256 checksum
How to use checksums
834f104ded58c59efda58530bc41f654520eea66325e6d0872f6c6782022fada
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.8

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page