dprint 0.52.0 → 0.53.1

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.
@@ -10,7 +10,7 @@ const exePath = path.join(__dirname, os.platform() === "win32" ? "dprint.exe" :
10
10
 
11
11
  if (!fs.existsSync(exePath)) {
12
12
  try {
13
- const resolvedExePath = require("./install_api").runInstall();
13
+ const resolvedExePath = require("./install_api.cjs").runInstall();
14
14
  runDprintExe(resolvedExePath);
15
15
  } catch (err) {
16
16
  if (err !== undefined && typeof err.message === "string") {
@@ -32,16 +32,14 @@ function runDprintExe(exePath) {
32
32
  { stdio: "inherit" },
33
33
  );
34
34
  if (result.error) {
35
- throw result.error;
36
- }
37
-
38
- throwIfNoExePath();
39
-
40
- process.exitCode = result.status;
41
-
42
- function throwIfNoExePath() {
43
35
  if (!fs.existsSync(exePath)) {
44
- throw new Error("Could not find exe at path '" + exePath + "'. Maybe try running dprint again.");
36
+ throw new Error(
37
+ "Could not find exe at path '" + exePath
38
+ + "'. Maybe try installing dprint again.",
39
+ );
45
40
  }
41
+ throw result.error;
46
42
  }
43
+
44
+ process.exit(result.status ?? 1);
47
45
  }
package/install.cjs ADDED
@@ -0,0 +1,10 @@
1
+ // @ts-check
2
+ "use strict";
3
+
4
+ const api = require("./install_api.cjs");
5
+ const exePath = api.runInstall();
6
+ try {
7
+ api.replaceBinEntry(exePath);
8
+ } catch (_err) {
9
+ // ignore - falls back to bin.cjs
10
+ }
@@ -8,6 +8,7 @@ const path = require("path");
8
8
  let cachedIsMusl = undefined;
9
9
 
10
10
  module.exports = {
11
+ replaceBinEntry,
11
12
  runInstall() {
12
13
  const dprintFileName = os.platform() === "win32" ? "dprint.exe" : "dprint";
13
14
  const targetExecutablePath = path.join(
@@ -38,7 +39,7 @@ module.exports = {
38
39
  // into the dprint package folder
39
40
  hardLinkOrCopy(sourceExecutablePath, targetExecutablePath);
40
41
  if (os.platform() !== "win32") {
41
- // chomd +x
42
+ // chmod +x
42
43
  chmodX(targetExecutablePath);
43
44
  }
44
45
  return targetExecutablePath;
@@ -53,12 +54,6 @@ module.exports = {
53
54
  err,
54
55
  );
55
56
  }
56
- // use the path found in the specific package
57
- try {
58
- chmodX(sourceExecutablePath);
59
- } catch (_err) {
60
- // ignore
61
- }
62
57
  return sourceExecutablePath;
63
58
  }
64
59
  },
@@ -66,8 +61,13 @@ module.exports = {
66
61
 
67
62
  /** @filePath {string} */
68
63
  function chmodX(filePath) {
69
- const perms = fs.statSync(filePath).mode;
70
- fs.chmodSync(filePath, perms | 0o111);
64
+ const fd = fs.openSync(filePath, "r");
65
+ try {
66
+ const perms = fs.fstatSync(fd).mode;
67
+ fs.fchmodSync(fd, perms | 0o111);
68
+ } finally {
69
+ fs.closeSync(fd);
70
+ }
71
71
  }
72
72
 
73
73
  function getTarget() {
@@ -140,6 +140,89 @@ function getLinuxFamily() {
140
140
  }
141
141
  }
142
142
 
143
+ /**
144
+ * Replaces the bin entry in node_modules/.bin to point directly at the
145
+ * native binary, avoiding Node.js startup overhead on each invocation.
146
+ * @param exePath {string}
147
+ */
148
+ function replaceBinEntry(exePath) {
149
+ const binDir = findBinDir();
150
+ if (binDir === undefined) return;
151
+
152
+ const relative = path.relative(binDir, exePath);
153
+ if (os.platform() === "win32") {
154
+ // rewrite .cmd and .ps1 wrappers to invoke the native binary directly
155
+ fs.writeFileSync(
156
+ path.join(binDir, "dprint.cmd"),
157
+ "@\"%~dp0" + relative + "\" %*\r\n",
158
+ );
159
+ fs.writeFileSync(
160
+ path.join(binDir, "dprint.ps1"),
161
+ "& \"$PSScriptRoot/" + relative.replace(/\\/g, "/")
162
+ + "\" $args\r\nexit $LASTEXITCODE\r\n",
163
+ );
164
+ } else {
165
+ // replace symlink to point directly at the native binary
166
+ const binDprint = path.join(binDir, "dprint");
167
+ fs.unlinkSync(binDprint);
168
+ fs.symlinkSync(relative, binDprint);
169
+ }
170
+ }
171
+
172
+ function findBinDir() {
173
+ // For global installs, npm sets npm_config_global=true and npm_config_prefix
174
+ // to the install prefix. The bin dir is {prefix}/bin on Linux/Mac or
175
+ // {prefix} on Windows (e.g. %APPDATA%\npm).
176
+ if (process.env.npm_config_global === "true") {
177
+ const prefix = process.env.npm_config_prefix;
178
+ if (prefix) {
179
+ const binDir = os.platform() === "win32"
180
+ ? prefix
181
+ : path.join(prefix, "bin");
182
+ if (isBinDirForThisPackage(binDir)) {
183
+ return binDir;
184
+ }
185
+ }
186
+ }
187
+
188
+ // For local installs, walk up looking for node_modules/.bin
189
+ let dir = __dirname;
190
+ for (let i = 0; i < 64; i++) {
191
+ const parent = path.dirname(dir);
192
+ if (parent === dir) {
193
+ break;
194
+ }
195
+ if (path.basename(parent) === "node_modules") {
196
+ const binDir = path.join(parent, ".bin");
197
+ if (isBinDirForThisPackage(binDir)) {
198
+ return binDir;
199
+ }
200
+ }
201
+ dir = parent;
202
+ }
203
+ return undefined;
204
+ }
205
+
206
+ function isBinDirForThisPackage(binDir) {
207
+ try {
208
+ if (os.platform() === "win32") {
209
+ // verify the .cmd wrapper references our bin.cjs
210
+ const content = fs.readFileSync(
211
+ path.join(binDir, "dprint.cmd"),
212
+ "utf8",
213
+ );
214
+ return content.includes("bin.cjs");
215
+ } else {
216
+ // verify the symlink points into our package directory
217
+ const linkTarget = fs.readlinkSync(path.join(binDir, "dprint"));
218
+ const resolved = path.resolve(binDir, linkTarget);
219
+ return resolved.endsWith("bin.cjs");
220
+ }
221
+ } catch (_err) {
222
+ return false;
223
+ }
224
+ }
225
+
143
226
  /**
144
227
  * @param sourcePath {string}
145
228
  * @param destinationPath {string}
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "dprint",
3
- "version": "0.52.0",
3
+ "version": "0.53.1",
4
4
  "description": "Pluggable and configurable code formatting platform written in Rust.",
5
- "bin": "bin.js",
5
+ "bin": "bin.cjs",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "git+https://github.com/dprint/dprint.git"
@@ -19,19 +19,19 @@
19
19
  "homepage": "https://github.com/dprint/dprint#readme",
20
20
  "preferUnplugged": true,
21
21
  "scripts": {
22
- "postinstall": "node ./install.js"
22
+ "postinstall": "node ./install.cjs"
23
23
  },
24
24
  "optionalDependencies": {
25
- "@dprint/win32-x64": "0.52.0",
26
- "@dprint/win32-arm64": "0.52.0",
27
- "@dprint/darwin-x64": "0.52.0",
28
- "@dprint/darwin-arm64": "0.52.0",
29
- "@dprint/linux-x64-glibc": "0.52.0",
30
- "@dprint/linux-x64-musl": "0.52.0",
31
- "@dprint/linux-arm64-glibc": "0.52.0",
32
- "@dprint/linux-arm64-musl": "0.52.0",
33
- "@dprint/linux-riscv64-glibc": "0.52.0",
34
- "@dprint/linux-loong64-glibc": "0.52.0",
35
- "@dprint/linux-loong64-musl": "0.52.0"
25
+ "@dprint/win32-x64": "0.53.1",
26
+ "@dprint/win32-arm64": "0.53.1",
27
+ "@dprint/darwin-x64": "0.53.1",
28
+ "@dprint/darwin-arm64": "0.53.1",
29
+ "@dprint/linux-x64-glibc": "0.53.1",
30
+ "@dprint/linux-x64-musl": "0.53.1",
31
+ "@dprint/linux-arm64-glibc": "0.53.1",
32
+ "@dprint/linux-arm64-musl": "0.53.1",
33
+ "@dprint/linux-riscv64-glibc": "0.53.1",
34
+ "@dprint/linux-loong64-glibc": "0.53.1",
35
+ "@dprint/linux-loong64-musl": "0.53.1"
36
36
  }
37
37
  }
package/install.js DELETED
@@ -1,4 +0,0 @@
1
- // @ts-check
2
- "use strict";
3
-
4
- require("./install_api").runInstall();