@j178/prek 0.2.5 → 0.2.9

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 j178
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -30,7 +30,7 @@ while also providing some additional long-requested features.
30
30
  ## Features
31
31
 
32
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.
33
+ - ⚡ [Faster](https://prek.j178.dev/benchmark/) than `pre-commit` and uses only half the disk space.
34
34
  - 🔄 Fully compatible with the original pre-commit configurations and hooks.
35
35
  - 🏗️ Built-in support for monorepos (i.e. [workspace mode](https://prek.j178.dev/workspace/)).
36
36
  - 🐍 Integration with [`uv`](https://github.com/astral-sh/uv) for managing Python virtual environments and dependencies.
@@ -60,9 +60,9 @@ For configuring `.pre-commit-config.yaml` and writing hooks, you can refer to th
60
60
  <!-- why:start -->
61
61
  ## Why prek?
62
62
 
63
- ### prek is way faster
63
+ ### prek is faster
64
64
 
65
- - It is about [10x faster](https://prek.j178.dev/benchmark/) than `pre-commit` and uses only half the disk space.
65
+ - It is [multiple times faster](https://prek.j178.dev/benchmark/) than `pre-commit` and takes up half the disk space.
66
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
67
  - Repositories are cloned in parallel, and hooks are installed in parallel if their dependencies are disjoint.
68
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.
@@ -108,7 +108,7 @@ On Linux and macOS:
108
108
 
109
109
  <!-- linux-standalone-install:start -->
110
110
  ```bash
111
- curl --proto '=https' --tlsv1.2 -LsSf https://github.com/j178/prek/releases/download/v0.2.5/prek-installer.sh | sh
111
+ curl --proto '=https' --tlsv1.2 -LsSf https://github.com/j178/prek/releases/download/v0.2.9/prek-installer.sh | sh
112
112
  ```
113
113
  <!-- linux-standalone-install:end -->
114
114
 
@@ -116,7 +116,7 @@ On Windows:
116
116
 
117
117
  <!-- windows-standalone-install:start -->
118
118
  ```powershell
119
- powershell -ExecutionPolicy ByPass -c "irm https://github.com/j178/prek/releases/download/v0.2.5/prek-installer.ps1 | iex"
119
+ powershell -ExecutionPolicy ByPass -c "irm https://github.com/j178/prek/releases/download/v0.2.9/prek-installer.ps1 | iex"
120
120
  ```
121
121
  <!-- windows-standalone-install:end -->
122
122
 
@@ -125,9 +125,9 @@ powershell -ExecutionPolicy ByPass -c "irm https://github.com/j178/prek/releases
125
125
  <details>
126
126
  <summary>PyPI</summary>
127
127
 
128
+ <!-- pypi-install:start -->
128
129
  prek is published as Python binary wheel to PyPI, you can install it using `pip`, `uv` (recommended), or `pipx`:
129
130
 
130
- <!-- pypi-install:start -->
131
131
  ```bash
132
132
  # Using uv (recommended)
133
133
  uv tool install prek
@@ -166,6 +166,19 @@ mise use prek
166
166
 
167
167
  </details>
168
168
 
169
+ <details>
170
+ <summary>Cargo binstall</summary>
171
+
172
+ <!-- cargo-binstall:start -->
173
+ Install pre-compiled binaries from GitHub using Cargo binstall (Rust 1.89+ is required):
174
+
175
+ ```bash
176
+ cargo binstall prek --git https://github.com/j178/prek
177
+ ```
178
+ <!-- cargo-binstall:end -->
179
+
180
+ </details>
181
+
169
182
  <details>
170
183
  <summary>Cargo</summary>
171
184
 
@@ -182,21 +195,21 @@ cargo install --locked --git https://github.com/j178/prek
182
195
  <details>
183
196
  <summary>npmjs</summary>
184
197
 
198
+ <!-- npmjs-install:start -->
185
199
  prek is published as a Node.js package, you can install it using `npm`, `pnpm`, or `npx`:
186
200
 
187
- <!-- npmjs-install:start -->
188
201
  ```bash
189
202
  # Using npm
190
- npm add -D prek-cli
203
+ npm add -D @j178/prek
191
204
 
192
205
  # Using pnpm
193
- pnpm add -D prek-cli
206
+ pnpm add -D @j178/prek
194
207
 
195
208
  # Using npx
196
- npx prek-cli --version
209
+ npx @j178/prek --version
197
210
 
198
211
  # or install globally
199
- npm install -g prek-cli
212
+ npm install -g @j178/prek
200
213
 
201
214
  # then use `prek` command
202
215
  prek --version
@@ -205,6 +218,38 @@ prek --version
205
218
 
206
219
  </details>
207
220
 
221
+ <details>
222
+ <summary>Nix</summary>
223
+
224
+ <!-- nix-install:start -->
225
+ prek is [available in Nix as `prek`](https://search.nixos.org/packages?channel=unstable&show=prek&query=prek).
226
+
227
+ ```shell
228
+ # Choose what's appropriate for your use case.
229
+ # One-off in a shell:
230
+ nix-shell -p prek
231
+ # NixOS or non-NixOS without flakes:
232
+ nix-env -iA nixos.prek
233
+ # Non-NixOS with flakes:
234
+ nix profile install nixpkgs#prek
235
+ ```
236
+ <!-- nix-install:end -->
237
+
238
+ </details>
239
+
240
+ <details>
241
+ <summary>Conda</summary>
242
+
243
+ <!-- conda-forge-install:start -->
244
+ prek is [available as `prek` via conda-forge](https://anaconda.org/conda-forge/prek).
245
+
246
+ ```shell
247
+ conda install conda-forge::prek
248
+ ```
249
+ <!-- conda-forge-install:end -->
250
+
251
+ </details>
252
+
208
253
  <details>
209
254
  <summary>GitHub Releases</summary>
210
255
 
@@ -0,0 +1,212 @@
1
+ const { createWriteStream, existsSync, mkdirSync, mkdtemp } = require("fs");
2
+ const { join, sep } = require("path");
3
+ const { spawnSync } = require("child_process");
4
+ const { tmpdir } = require("os");
5
+
6
+ const axios = require("axios");
7
+ const rimraf = require("rimraf");
8
+ const tmpDir = tmpdir();
9
+
10
+ const error = (msg) => {
11
+ console.error(msg);
12
+ process.exit(1);
13
+ };
14
+
15
+ class Package {
16
+ constructor(platform, name, url, filename, zipExt, binaries) {
17
+ let errors = [];
18
+ if (typeof url !== "string") {
19
+ errors.push("url must be a string");
20
+ } else {
21
+ try {
22
+ new URL(url);
23
+ } catch (e) {
24
+ errors.push(e);
25
+ }
26
+ }
27
+ if (name && typeof name !== "string") {
28
+ errors.push("package name must be a string");
29
+ }
30
+ if (!name) {
31
+ errors.push("You must specify the name of your package");
32
+ }
33
+ if (binaries && typeof binaries !== "object") {
34
+ errors.push("binaries must be a string => string map");
35
+ }
36
+ if (!binaries) {
37
+ errors.push("You must specify the binaries in the package");
38
+ }
39
+
40
+ if (errors.length > 0) {
41
+ let errorMsg =
42
+ "One or more of the parameters you passed to the Binary constructor are invalid:\n";
43
+ errors.forEach((error) => {
44
+ errorMsg += error;
45
+ });
46
+ errorMsg +=
47
+ '\n\nCorrect usage: new Package("my-binary", "https://example.com/binary/download.tar.gz", {"my-binary": "my-binary"})';
48
+ error(errorMsg);
49
+ }
50
+
51
+ this.platform = platform;
52
+ this.url = url;
53
+ this.name = name;
54
+ this.filename = filename;
55
+ this.zipExt = zipExt;
56
+ this.installDirectory = join(__dirname, "node_modules", ".bin_real");
57
+ this.binaries = binaries;
58
+
59
+ if (!existsSync(this.installDirectory)) {
60
+ mkdirSync(this.installDirectory, { recursive: true });
61
+ }
62
+ }
63
+
64
+ exists() {
65
+ for (const binaryName in this.binaries) {
66
+ const binRelPath = this.binaries[binaryName];
67
+ const binPath = join(this.installDirectory, binRelPath);
68
+ if (!existsSync(binPath)) {
69
+ return false;
70
+ }
71
+ }
72
+ return true;
73
+ }
74
+
75
+ install(fetchOptions, suppressLogs = false) {
76
+ if (this.exists()) {
77
+ if (!suppressLogs) {
78
+ console.error(
79
+ `${this.name} is already installed, skipping installation.`,
80
+ );
81
+ }
82
+ return Promise.resolve();
83
+ }
84
+
85
+ if (existsSync(this.installDirectory)) {
86
+ rimraf.sync(this.installDirectory);
87
+ }
88
+
89
+ mkdirSync(this.installDirectory, { recursive: true });
90
+
91
+ if (!suppressLogs) {
92
+ console.error(`Downloading release from ${this.url}`);
93
+ }
94
+
95
+ return axios({ ...fetchOptions, url: this.url, responseType: "stream" })
96
+ .then((res) => {
97
+ return new Promise((resolve, reject) => {
98
+ mkdtemp(`${tmpDir}${sep}`, (err, directory) => {
99
+ let tempFile = join(directory, this.filename);
100
+ const sink = res.data.pipe(createWriteStream(tempFile));
101
+ sink.on("error", (err) => reject(err));
102
+ sink.on("close", () => {
103
+ if (/\.tar\.*/.test(this.zipExt)) {
104
+ const result = spawnSync("tar", [
105
+ "xf",
106
+ tempFile,
107
+ // The tarballs are stored with a leading directory
108
+ // component; we strip one component in the
109
+ // shell installers too.
110
+ "--strip-components",
111
+ "1",
112
+ "-C",
113
+ this.installDirectory,
114
+ ]);
115
+ if (result.status == 0) {
116
+ resolve();
117
+ } else if (result.error) {
118
+ reject(result.error);
119
+ } else {
120
+ reject(
121
+ new Error(
122
+ `An error occurred untarring the artifact: stdout: ${result.stdout}; stderr: ${result.stderr}`,
123
+ ),
124
+ );
125
+ }
126
+ } else if (this.zipExt == ".zip") {
127
+ let result;
128
+ if (this.platform.artifactName.includes("windows")) {
129
+ // Windows does not have "unzip" by default on many installations, instead
130
+ // we use Expand-Archive from powershell
131
+ result = spawnSync("powershell.exe", [
132
+ "-NoProfile",
133
+ "-NonInteractive",
134
+ "-Command",
135
+ `& {
136
+ param([string]$LiteralPath, [string]$DestinationPath)
137
+ Expand-Archive -LiteralPath $LiteralPath -DestinationPath $DestinationPath -Force
138
+ }`,
139
+ tempFile,
140
+ this.installDirectory,
141
+ ]);
142
+ } else {
143
+ result = spawnSync("unzip", [
144
+ "-q",
145
+ tempFile,
146
+ "-d",
147
+ this.installDirectory,
148
+ ]);
149
+ }
150
+
151
+ if (result.status == 0) {
152
+ resolve();
153
+ } else if (result.error) {
154
+ reject(result.error);
155
+ } else {
156
+ reject(
157
+ new Error(
158
+ `An error occurred unzipping the artifact: stdout: ${result.stdout}; stderr: ${result.stderr}`,
159
+ ),
160
+ );
161
+ }
162
+ } else {
163
+ reject(
164
+ new Error(`Unrecognized file extension: ${this.zipExt}`),
165
+ );
166
+ }
167
+ });
168
+ });
169
+ });
170
+ })
171
+ .then(() => {
172
+ if (!suppressLogs) {
173
+ console.error(`${this.name} has been installed!`);
174
+ }
175
+ })
176
+ .catch((e) => {
177
+ error(`Error fetching release: ${e.message}`);
178
+ });
179
+ }
180
+
181
+ run(binaryName, fetchOptions) {
182
+ const promise = !this.exists()
183
+ ? this.install(fetchOptions, true)
184
+ : Promise.resolve();
185
+
186
+ promise
187
+ .then(() => {
188
+ const [, , ...args] = process.argv;
189
+
190
+ const options = { cwd: process.cwd(), stdio: "inherit" };
191
+
192
+ const binRelPath = this.binaries[binaryName];
193
+ if (!binRelPath) {
194
+ error(`${binaryName} is not a known binary in ${this.name}`);
195
+ }
196
+ const binPath = join(this.installDirectory, binRelPath);
197
+ const result = spawnSync(binPath, args, options);
198
+
199
+ if (result.error) {
200
+ error(result.error);
201
+ }
202
+
203
+ process.exit(result.status);
204
+ })
205
+ .catch((e) => {
206
+ error(e.message);
207
+ process.exit(1);
208
+ });
209
+ }
210
+ }
211
+
212
+ module.exports.Package = Package;
package/binary.js ADDED
@@ -0,0 +1,126 @@
1
+ const { Package } = require("./binary-install");
2
+ const os = require("os");
3
+ const cTable = require("console.table");
4
+ const libc = require("detect-libc");
5
+ const { configureProxy } = require("axios-proxy-builder");
6
+
7
+ const error = (msg) => {
8
+ console.error(msg);
9
+ process.exit(1);
10
+ };
11
+
12
+ const {
13
+ name,
14
+ artifactDownloadUrl,
15
+ supportedPlatforms,
16
+ glibcMinimum,
17
+ } = require("./package.json");
18
+
19
+ const builderGlibcMajorVersion = glibcMinimum.major;
20
+ const builderGlibcMInorVersion = glibcMinimum.series;
21
+
22
+ const getPlatform = () => {
23
+ const rawOsType = os.type();
24
+ const rawArchitecture = os.arch();
25
+
26
+ // We want to use rust-style target triples as the canonical key
27
+ // for a platform, so translate the "os" library's concepts into rust ones
28
+ let osType = "";
29
+ switch (rawOsType) {
30
+ case "Windows_NT":
31
+ osType = "pc-windows-msvc";
32
+ break;
33
+ case "Darwin":
34
+ osType = "apple-darwin";
35
+ break;
36
+ case "Linux":
37
+ osType = "unknown-linux-gnu";
38
+ break;
39
+ }
40
+
41
+ let arch = "";
42
+ switch (rawArchitecture) {
43
+ case "x64":
44
+ arch = "x86_64";
45
+ break;
46
+ case "arm64":
47
+ arch = "aarch64";
48
+ break;
49
+ }
50
+
51
+ if (rawOsType === "Linux") {
52
+ if (libc.familySync() == "musl") {
53
+ osType = "unknown-linux-musl-dynamic";
54
+ } else if (libc.isNonGlibcLinuxSync()) {
55
+ console.warn(
56
+ "Your libc is neither glibc nor musl; trying static musl binary instead",
57
+ );
58
+ osType = "unknown-linux-musl-static";
59
+ } else {
60
+ let libcVersion = libc.versionSync();
61
+ let splitLibcVersion = libcVersion.split(".");
62
+ let libcMajorVersion = splitLibcVersion[0];
63
+ let libcMinorVersion = splitLibcVersion[1];
64
+ if (
65
+ libcMajorVersion != builderGlibcMajorVersion ||
66
+ libcMinorVersion < builderGlibcMInorVersion
67
+ ) {
68
+ // We can't run the glibc binaries, but we can run the static musl ones
69
+ // if they exist
70
+ console.warn(
71
+ "Your glibc isn't compatible; trying static musl binary instead",
72
+ );
73
+ osType = "unknown-linux-musl-static";
74
+ }
75
+ }
76
+ }
77
+
78
+ // Assume the above succeeded and build a target triple to look things up with.
79
+ // If any of it failed, this lookup will fail and we'll handle it like normal.
80
+ let targetTriple = `${arch}-${osType}`;
81
+ let platform = supportedPlatforms[targetTriple];
82
+
83
+ if (!platform) {
84
+ error(
85
+ `Platform with type "${rawOsType}" and architecture "${rawArchitecture}" is not supported by ${name}.\nYour system must be one of the following:\n\n${Object.keys(
86
+ supportedPlatforms,
87
+ ).join(",")}`,
88
+ );
89
+ }
90
+
91
+ return platform;
92
+ };
93
+
94
+ const getPackage = () => {
95
+ const platform = getPlatform();
96
+ const url = `${artifactDownloadUrl}/${platform.artifactName}`;
97
+ let filename = platform.artifactName;
98
+ let ext = platform.zipExt;
99
+ let binary = new Package(platform, name, url, filename, ext, platform.bins);
100
+
101
+ return binary;
102
+ };
103
+
104
+ const install = (suppressLogs) => {
105
+ if (!artifactDownloadUrl || artifactDownloadUrl.length === 0) {
106
+ console.warn("in demo mode, not installing binaries");
107
+ return;
108
+ }
109
+ const package = getPackage();
110
+ const proxy = configureProxy(package.url);
111
+
112
+ return package.install(proxy, suppressLogs);
113
+ };
114
+
115
+ const run = (binaryName) => {
116
+ const package = getPackage();
117
+ const proxy = configureProxy(package.url);
118
+
119
+ package.run(binaryName, proxy);
120
+ };
121
+
122
+ module.exports = {
123
+ install,
124
+ run,
125
+ getPackage,
126
+ };
package/install.js CHANGED
@@ -1,125 +1,4 @@
1
1
  #!/usr/bin/env node
2
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();
3
+ const { install } = require("./binary");
4
+ install(false);