@j178/prek 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. package/README.md +255 -0
  2. package/install.js +125 -0
  3. package/package.json +51 -0
  4. package/prek.js +37 -0
  5. package/utils.js +87 -0
package/README.md ADDED
@@ -0,0 +1,255 @@
1
+ <div align="center">
2
+
3
+ # prek
4
+
5
+ <img width="220" alt="prek" src="./docs/assets/logo_512.webp" />
6
+
7
+ [![CI](https://github.com/j178/prek/actions/workflows/ci.yml/badge.svg)](https://github.com/j178/prek/actions/workflows/ci.yml)
8
+ [![codecov](https://codecov.io/github/j178/prek/graph/badge.svg?token=MP6TY24F43)](https://codecov.io/github/j178/prek)
9
+ [![GitHub Downloads](https://img.shields.io/github/downloads/j178/prek/total?logo=github)](https://github.com/j178/prek/releases)
10
+ [![PyPI Downloads](https://img.shields.io/pypi/dm/prek?logo=python)](https://pepy.tech/projects/prek)
11
+ [![Discord](https://img.shields.io/discord/1403581202102878289?logo=discord)](https://discord.gg/3NRJUqJz86)
12
+
13
+ </div>
14
+
15
+ <!-- description:start -->
16
+ [pre-commit](https://pre-commit.com/) is a framework to run hooks written in many languages, and it manages the
17
+ language toolchain and dependencies for running the hooks.
18
+
19
+ *prek* is a reimagined version of pre-commit, built in Rust.
20
+ It is designed to be a faster, dependency-free and drop-in alternative for it,
21
+ while also providing some additional long-requested features.
22
+ <!-- description:end -->
23
+
24
+ > [!WARNING]
25
+ > prek is not production-ready yet. Some subcommands and languages are not implemented. See the current gaps for drop-in parity: [TODO](https://prek.j178.dev/todo/).
26
+ >
27
+ > It's already being adopted by [some projects](#who-is-using-prek), please give it a try - we'd love your feedback!
28
+
29
+ <!-- features:start -->
30
+ ## Features
31
+
32
+ - 🚀 A single binary with no dependencies, does not require Python or any other runtime.
33
+ - ⚡ About [10x faster](https://prek.j178.dev/benchmark/) than `pre-commit` and uses only half the disk space.
34
+ - 🔄 Fully compatible with the original pre-commit configurations and hooks.
35
+ - 🏗️ Built-in support for monorepos (i.e. [workspace mode](https://prek.j178.dev/workspace/)).
36
+ - 🐍 Integration with [`uv`](https://github.com/astral-sh/uv) for managing Python virtual environments and dependencies.
37
+ - 🛠️ Improved toolchain installations for Python, Node.js, Go, Rust and Ruby, shared between hooks.
38
+ - 📦 [Built-in](https://prek.j178.dev/builtin/) Rust-native implementation of some common hooks.
39
+ <!-- features:end -->
40
+
41
+ ## How to migrate
42
+
43
+ prek is designed as a drop-in replacement:
44
+
45
+ - [Install prek](#installation)
46
+ - Replace `pre-commit` with `prek` in your commands
47
+ - Your existing `.pre-commit-config.yaml` works unchanged
48
+
49
+ ```console
50
+ $ prek run
51
+ trim trailing whitespace.................................................Passed
52
+ fix end of files.........................................................Passed
53
+ typos....................................................................Passed
54
+ cargo fmt................................................................Passed
55
+ cargo clippy.............................................................Passed
56
+ ```
57
+
58
+ For configuring `.pre-commit-config.yaml` and writing hooks, you can refer to the [pre-commit documentation](https://pre-commit.com/) as prek is fully compatible with it.
59
+
60
+ <!-- why:start -->
61
+ ## Why prek?
62
+
63
+ ### prek is way faster
64
+
65
+ - It is about [10x faster](https://prek.j178.dev/benchmark/) than `pre-commit` and uses only half the disk space.
66
+ - It redesigned how hook environments and toolchains are managed, they are all shared between hooks, which reduces the disk space usage and speeds up the installation process.
67
+ - Repositories are cloned in parallel, and hooks are installed in parallel if their dependencies are disjoint.
68
+ - It uses [`uv`](https://github.com/astral-sh/uv) for creating Python virtualenvs and installing dependencies, which is known for its speed and efficiency.
69
+ - It implements some common hooks in Rust, [built in prek](https://prek.j178.dev/builtin/), which are faster than their Python counterparts.
70
+
71
+ ### prek provides a better user experience
72
+
73
+ - No need to install Python or any other runtime, just download a single binary.
74
+ - No hassle with your Python version or virtual environments, prek automatically installs the required Python version and creates a virtual environment for you.
75
+ - Built-in support for workspaces (or monorepos), each subproject can have its own `.pre-commit-config.yaml` file.
76
+ - `prek run` has some nifty improvements over `pre-commit run`, such as:
77
+ - `prek run --directory <dir>` runs hooks for files in the specified directory, no need to use `git ls-files -- <dir> | xargs pre-commit run --files` anymore.
78
+ - `prek run --last-commit` runs hooks for files changed in the last commit.
79
+ - `prek run [HOOK] [HOOK]` selects and runs multiple hooks.
80
+ - `prek list` command lists all available hooks, their ids, and descriptions, providing a better overview of the configured hooks.
81
+ - prek provides shell completions for `prek run <hook_id>` command, making it easier to run specific hooks without remembering their ids.
82
+
83
+ For more detailed improvements prek offers, take a look at [Difference from pre-commit](https://prek.j178.dev/diff/).
84
+
85
+ ## Who is using prek?
86
+
87
+ prek is pretty new, but it is already being used or recommend by some projects and organizations:
88
+
89
+ - [Airflow](https://github.com/apache/airflow/issues/44995)
90
+ - [PDM](https://github.com/pdm-project/pdm/pull/3593)
91
+ - [basedpyright](https://github.com/DetachHead/basedpyright/pull/1413)
92
+ - [OpenLineage](https://github.com/OpenLineage/OpenLineage/pull/3965)
93
+ - [Authlib](https://github.com/authlib/authlib/pull/804)
94
+ - [pre-commit-crocodile](https://radiandevcore.gitlab.io/tools/pre-commit-crocodile/)
95
+ - [PaperQA2](https://github.com/Future-House/paper-qa/pull/1098)
96
+ - [requests-cache](https://github.com/requests-cache/requests-cache/pull/1116)
97
+
98
+ <!-- why:end -->
99
+
100
+ ## Installation
101
+
102
+ <details>
103
+ <summary>Standalone installer</summary>
104
+
105
+ prek provides a standalone installer script to download and install the tool,
106
+
107
+ On Linux and macOS:
108
+
109
+ <!-- linux-standalone-install:start -->
110
+ ```bash
111
+ curl --proto '=https' --tlsv1.2 -LsSf https://github.com/j178/prek/releases/download/v0.2.5/prek-installer.sh | sh
112
+ ```
113
+ <!-- linux-standalone-install:end -->
114
+
115
+ On Windows:
116
+
117
+ <!-- windows-standalone-install:start -->
118
+ ```powershell
119
+ powershell -ExecutionPolicy ByPass -c "irm https://github.com/j178/prek/releases/download/v0.2.5/prek-installer.ps1 | iex"
120
+ ```
121
+ <!-- windows-standalone-install:end -->
122
+
123
+ </details>
124
+
125
+ <details>
126
+ <summary>PyPI</summary>
127
+
128
+ prek is published as Python binary wheel to PyPI, you can install it using `pip`, `uv` (recommended), or `pipx`:
129
+
130
+ <!-- pypi-install:start -->
131
+ ```bash
132
+ # Using uv (recommended)
133
+ uv tool install prek
134
+
135
+ # Using pip
136
+ pip install prek
137
+
138
+ # Using pipx
139
+ pipx install prek
140
+ ```
141
+ <!-- pypi-install:end -->
142
+
143
+ </details>
144
+
145
+ <details>
146
+ <summary>Homebrew</summary>
147
+
148
+ <!-- homebrew-install:start -->
149
+ ```bash
150
+ brew install prek
151
+ ```
152
+ <!-- homebrew-install:end -->
153
+
154
+ </details>
155
+
156
+ <details>
157
+ <summary>mise</summary>
158
+
159
+ <!-- mise-install:start -->
160
+ To use prek with [mise](https://mise.jdx.dev):
161
+
162
+ ```bash
163
+ mise use prek
164
+ ```
165
+ <!-- mise-install:end -->
166
+
167
+ </details>
168
+
169
+ <details>
170
+ <summary>Cargo</summary>
171
+
172
+ <!-- cargo-install:start -->
173
+ Build from source using Cargo (Rust 1.89+ is required):
174
+
175
+ ```bash
176
+ cargo install --locked --git https://github.com/j178/prek
177
+ ```
178
+ <!-- cargo-install:end -->
179
+
180
+ </details>
181
+
182
+ <details>
183
+ <summary>npmjs</summary>
184
+
185
+ prek is published as a Node.js package, you can install it using `npm`, `pnpm`, or `npx`:
186
+
187
+ <!-- npmjs-install:start -->
188
+ ```bash
189
+ # Using npm
190
+ npm add -D prek-cli
191
+
192
+ # Using pnpm
193
+ pnpm add -D prek-cli
194
+
195
+ # Using npx
196
+ npx prek-cli --version
197
+
198
+ # or install globally
199
+ npm install -g prek-cli
200
+
201
+ # then use `prek` command
202
+ prek --version
203
+ ```
204
+ <!-- npmjs-install:end -->
205
+
206
+ </details>
207
+
208
+ <details>
209
+ <summary>GitHub Releases</summary>
210
+
211
+ <!-- pre-built-binaries:start -->
212
+ Pre-built binaries are available for download from the [GitHub releases](https://github.com/j178/prek/releases) page.
213
+ <!-- pre-built-binaries:end -->
214
+
215
+ </details>
216
+
217
+ <details>
218
+ <summary>GitHub Actions</summary>
219
+
220
+ <!-- github-actions:start -->
221
+ prek can be used in GitHub Actions via the [j178/prek-action](https://github.com/j178/prek-action) repository.
222
+
223
+ Example workflow:
224
+
225
+ ```yaml
226
+ name: Prek checks
227
+ on: [push, pull_request]
228
+
229
+ jobs:
230
+ prek:
231
+ runs-on: ubuntu-latest
232
+ steps:
233
+ - uses: actions/checkout@v5
234
+ - uses: j178/prek-action@v1
235
+ ```
236
+
237
+ This action installs prek and runs `prek run --all-files` on your repository.
238
+ <!-- github-actions:end -->
239
+ </details>
240
+
241
+ <!-- self-update:start -->
242
+ If installed via the standalone installer, prek can update itself to the latest version:
243
+
244
+ ```bash
245
+ prek self update
246
+ ```
247
+ <!-- self-update:end -->
248
+
249
+ ## Acknowledgements
250
+
251
+ This project is heavily inspired by the original [pre-commit](https://pre-commit.com/) tool, and it wouldn't be possible without the hard work
252
+ of the maintainers and contributors of that project.
253
+
254
+ And a special thanks to the [Astral](https://github.com/astral-sh) team for their remarkable projects, particularly [uv](https://github.com/astral-sh/uv),
255
+ from which I've learned a lot on how to write efficient and idiomatic Rust code.
package/install.js ADDED
@@ -0,0 +1,125 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * prek postinstall script
5
+ *
6
+ * This script ensures that the correct binary for the current platform and
7
+ * architecture is downloaded and available.
8
+ */
9
+
10
+ const fs = require("fs");
11
+ const path = require("path");
12
+ const https = require("https");
13
+ const packageJson = require("./package.json");
14
+ const process = require("process");
15
+ const { getPlatformPackage } = require("./utils");
16
+
17
+ const VERSION = packageJson.version;
18
+
19
+ // Extract all files from a tarball to a destination directory
20
+ async function extractTarball(tarballStream, destDir) {
21
+ const { extract } = require("tar");
22
+
23
+ if (!fs.existsSync(destDir)) {
24
+ fs.mkdirSync(destDir, { recursive: true });
25
+ }
26
+
27
+ return new Promise((resolve, reject) => {
28
+ tarballStream
29
+ .pipe(extract({ cwd: destDir, strip: 1 }))
30
+ .on("error", (err) => reject(err))
31
+ .on("end", () => resolve());
32
+ });
33
+ }
34
+
35
+ // Extract all files from a zip to a destination directory
36
+ async function extractZip(zipStream, destDir) {
37
+ const unzipper = require("unzipper");
38
+
39
+ if (!fs.existsSync(destDir)) {
40
+ fs.mkdirSync(destDir, { recursive: true });
41
+ }
42
+
43
+ return new Promise((resolve, reject) => {
44
+ zipStream
45
+ .pipe(unzipper.Extract({ path: destDir }))
46
+ .on("error", (err) => reject(err))
47
+ .on("close", () => resolve());
48
+ });
49
+ }
50
+
51
+ async function downloadAndExtractBinary(packageName, extension) {
52
+ let packageUrl = `https://github.com/j178/prek/releases/download/v${VERSION}/${packageName}.${extension}`;
53
+ process.stdout.write(`Downloading ${packageUrl}...\n`);
54
+
55
+ return new Promise((resolve) => {
56
+ https
57
+ .get(packageUrl, (response) => {
58
+ if (response.statusCode === 302 || response.statusCode === 301) {
59
+ // Handle redirects
60
+ https
61
+ .get(response.headers.location, handleResponse)
62
+ .on("error", (err) => {
63
+ console.error("Download error:", err);
64
+ resolve();
65
+ });
66
+ return;
67
+ }
68
+
69
+ handleResponse(response);
70
+
71
+ async function handleResponse(response) {
72
+ try {
73
+ if (response.statusCode !== 200) {
74
+ throw new Error(
75
+ `Download failed with status code: ${response.statusCode}`
76
+ );
77
+ }
78
+
79
+ const destDir = path.join(__dirname, "node_modules", packageName);
80
+ if (extension === "tar.gz") {
81
+ await extractTarball(response, destDir);
82
+ } else if (extension === "zip") {
83
+ await extractZip(response, destDir);
84
+ } else {
85
+ throw new Error(`Unsupported archive format: ${extension}`);
86
+ }
87
+
88
+ process.stdout.write(
89
+ `Successfully downloaded and installed ${packageName}\n`
90
+ );
91
+ } catch (error) {
92
+ console.error("Error during extraction:", error);
93
+ resolve();
94
+ } finally {
95
+ resolve();
96
+ }
97
+ }
98
+ })
99
+ .on("error", (err) => {
100
+ console.error("Download error:", err);
101
+ resolve();
102
+ });
103
+ });
104
+ }
105
+
106
+ async function main() {
107
+ try {
108
+ let package = getPlatformPackage();
109
+ if (package === null) {
110
+ console.error(
111
+ `Unsupported platform and/or architecture ${process.platform} ${process.arch} for prek binary on npm.`
112
+ );
113
+ process.exit(1);
114
+ } else {
115
+ const { name, ext } = package;
116
+ await downloadAndExtractBinary(name, ext);
117
+ process.exit(0);
118
+ }
119
+ } catch (error) {
120
+ console.error(error);
121
+ return;
122
+ }
123
+ }
124
+
125
+ main();
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@j178/prek",
3
+ "version": "0.2.5",
4
+ "description": "Better `pre-commit`, re-engineered in Rust",
5
+ "keywords": [
6
+ "pre-commit",
7
+ "git",
8
+ "hooks"
9
+ ],
10
+ "readme": "README.md",
11
+ "homepage": "https://github.com/j178/prek",
12
+ "bugs": {
13
+ "url": "https://github.com/j178/prek/issues"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+ssh://git@github.com/j178/prek.git"
18
+ },
19
+ "funding": [
20
+ {
21
+ "type": "github",
22
+ "url": "https://github.com/sponsors/j178"
23
+ }
24
+ ],
25
+ "author": {
26
+ "name": "j178",
27
+ "email": "hi@j178.dev"
28
+ },
29
+ "scripts": {
30
+ "prepare": "cpy '../../README.md' './'",
31
+ "postinstall": "node ./install.js"
32
+ },
33
+ "dependencies": {
34
+ "detect-libc": "2.1.2",
35
+ "tar": "7.5.1",
36
+ "unzipper": "0.12.3"
37
+ },
38
+ "files": [
39
+ "install.js",
40
+ "prek.js",
41
+ "utils.js",
42
+ "README.md"
43
+ ],
44
+ "type": "commonjs",
45
+ "bin": {
46
+ "prek": "./prek.js"
47
+ },
48
+ "devDependencies": {
49
+ "cpy-cli": "6.0.0"
50
+ }
51
+ }
package/prek.js ADDED
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env node
2
+
3
+ const { spawn } = require("child_process");
4
+ const path = require("path");
5
+ const fs = require("fs");
6
+ const process = require("process");
7
+ const { getPlatformPackage } = require("./utils");
8
+
9
+ const isWin = process.platform === "win32";
10
+ const binName = isWin ? "prek.exe" : "prek";
11
+ const package = getPlatformPackage();
12
+ if (package === null) {
13
+ console.error(
14
+ `Unsupported platform and/or architecture ${process.platform} ${process.arch} for prek binary on npm.`
15
+ );
16
+ process.exit(1);
17
+ }
18
+ const binPath = path.resolve(
19
+ __dirname,
20
+ `./node_modules/${package.name}/`,
21
+ binName
22
+ );
23
+
24
+ if (!fs.existsSync(binPath)) {
25
+ console.error(`Error: prek not found at ${binPath}`);
26
+ process.exit(1);
27
+ }
28
+
29
+ const child = spawn(binPath, process.argv.slice(2), {
30
+ stdio: "inherit",
31
+ });
32
+
33
+ child.on("close", (code) => process.exit(code));
34
+ child.on("error", (err) => {
35
+ console.error(err);
36
+ process.exit(1);
37
+ });
package/utils.js ADDED
@@ -0,0 +1,87 @@
1
+ const detectLibc = require("detect-libc");
2
+
3
+ function getPlatformPackage() {
4
+ let platform = process.platform;
5
+ let arch = process.arch;
6
+
7
+ let libc = "";
8
+ if (platform === "linux") {
9
+ libc = detectLibc.isNonGlibcLinuxSync() ? "musl" : "gnu";
10
+ }
11
+
12
+ // Map to our package naming conventions
13
+ if (platform === "darwin") {
14
+ if (arch === "arm64") {
15
+ return {
16
+ name: "prek-aarch64-apple-darwin",
17
+ ext: "tar.gz",
18
+ };
19
+ } else if (arch === "x64") {
20
+ return {
21
+ name: "prek-x86_64-apple-darwin",
22
+ ext: "tar.gz",
23
+ };
24
+ }
25
+ } else if (platform === "win32") {
26
+ if (arch === "arm64") {
27
+ return {
28
+ name: "prek-aarch64-pc-windows-msvc",
29
+ ext: "zip",
30
+ };
31
+ } else if (arch === "ia32") {
32
+ return {
33
+ name: "prek-i686-pc-windows-msvc",
34
+ ext: "zip",
35
+ };
36
+ } else if (arch === "x64") {
37
+ return {
38
+ name: "prek-x86_64-pc-windows-msvc",
39
+ ext: "zip",
40
+ };
41
+ }
42
+ } else if (platform === "linux") {
43
+ if (arch === "x64") {
44
+ return {
45
+ name: `prek-x86_64-unknown-linux-${libc}`,
46
+ ext: "tar.gz",
47
+ };
48
+ } else if (arch === "ia32") {
49
+ return {
50
+ name: `prek-i686-unknown-linux-${libc}`,
51
+ ext: "tar.gz",
52
+ };
53
+ } else if (arch === "arm64") {
54
+ return {
55
+ name: `prek-aarch64-unknown-linux-${libc}`,
56
+ ext: "tar.gz",
57
+ };
58
+ } else if (arch === "arm") {
59
+ return {
60
+ name: `prek-armv7-unknown-linux-${libc}eabihf`,
61
+ ext: "tar.gz",
62
+ };
63
+ } else if (arch === "ppc64") {
64
+ return {
65
+ name: "prek-powerpc64-unknown-linux-gnu",
66
+ ext: "tar.gz",
67
+ };
68
+ } else if (arch === "ppc64le") {
69
+ return {
70
+ name: "prek-powerpc64le-unknown-linux-gnu",
71
+ ext: "tar.gz",
72
+ };
73
+ } else if (arch === "riscv64") {
74
+ return {
75
+ name: "prek-riscv64gc-unknown-linux-gnu",
76
+ ext: "tar.gz",
77
+ };
78
+ } else if (arch === "s390x") {
79
+ return {
80
+ name: "prek-s390x-unknown-linux-gnu",
81
+ ext: "tar.gz",
82
+ };
83
+ }
84
+ }
85
+ }
86
+
87
+ module.exports = { getPlatformPackage };