A command-line utility for running commands in subdirectories (e.g. in a monorepo) with a set of pre-commit hooks
Project description
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
inclient
subdirectory, and the commandpoetry run black
inserver
subdirectory:repos: - repo: https://github.com/egormkn/run-in-subdirectory rev: 1.0.0 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 extractfirst
,second
orthird
-level subdirectory from the last file path, that was passed to the hook by pre-commit.Note that you should set
files
,types
and/orexclude
properties so that the hook only runs for files in that subdirectory.repos: - repo: https://github.com/egormkn/run-in-subdirectory rev: 1.0.0 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.0" 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`.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for run_in_subdirectory-1.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9fbf61abccdb4232f427db42cf2d5034b854d1f9e6774f7df8399e8c8c855809 |
|
MD5 | d5eaf022e333c72a21e76afaf87d595c |
|
BLAKE2b-256 | 455a79d6aec52e456625db82bf93b22c8d9c8ff6aedae6800191bd5ee3947cb9 |
Hashes for run_in_subdirectory-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3094121c64d427de0e8ab3e1fdc96d43c177ee83b8fe3d6a9e2bd7fbeb5f3647 |
|
MD5 | 0aed6b71a3215e7acdfa965afc6feaa5 |
|
BLAKE2b-256 | 923f42f57f10bbe960779907683861e085569be27f179e45bec97b08b5b1708b |