Skip to content
This repository was archived by the owner on Sep 17, 2023. It is now read-only.

Commit ae0bd52

Browse files
committed
fix: include a default bin/monorepo script
This dummy acts as a target for `npm` to link into `node_modules/.bin` and `chmod` when run in such a way that the `monorepo` executable has not been downloaded yet. Closes #19
1 parent 4ff33d8 commit ae0bd52

File tree

4 files changed

+47
-8
lines changed

4 files changed

+47
-8
lines changed

bin/monorepo

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/sh
2+
3+
cat <<EOF
4+
This is not the "monorepo" binary, which is usually downloaded via the npm "postinstall"
5+
hook. Was the "npm install" run with the "--ignore-scripts" flag?
6+
7+
Please try reinstalling "@typescript-tools/rust-implementation" or opening a ticket with
8+
details about your use case.
9+
EOF

npm/install.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,31 @@
11
const fs = require('fs')
22
const path = require('path')
3+
const tmp = require('tmp')
34
const getBinary = require('./get-binary')
45

56
const binaryName = 'monorepo'
67
const repoDir = path.dirname(__dirname)
78
const downloadDir = path.resolve(repoDir, 'bin')
89
const scopedPackageDir = path.dirname(repoDir)
910
const nodeModulesDir = path.dirname(scopedPackageDir)
11+
const nodeModulesBinDir = path.resolve(nodeModulesDir, '.bin')
1012

11-
// Link the downloaded binary into the node_modules/.bin directory
13+
// Link the downloaded binary into the node_modules/.bin directory.
14+
// Inspiration from https://github.com/evanw/esbuild/releases/tag/v0.13.4
1215
const linkBinaryIntoBin = () => {
16+
const tempPath = tmp.tmpNameSync({ tmpdir: nodeModulesBinDir })
1317
const existingPath = path.resolve(downloadDir, binaryName)
14-
const desiredPath = path.resolve(nodeModulesDir, '.bin', binaryName)
18+
const desiredPath = path.resolve(nodeModulesBinDir, binaryName)
1519

16-
// Only symlink the path when it does not yet exist
17-
if (!fs.existsSync(desiredPath)) {
18-
fs.linkSync(existingPath, desiredPath)
19-
}
20+
// First, link the binary with a temporary file. If this fails and throw an error,
21+
// then we'll end up doing nothing. This uses a hard link to avoid taking up
22+
// additional space on the file system.
23+
fs.linkSync(existingPath, tempPath)
24+
25+
// Then use rename to atomically replace the target file with the temporary file.
26+
// If this fails and throws an error, we'll end up leaving the temporary file there,
27+
// which is harmless.
28+
fs.renameSync(tempPath, desiredPath)
2029
}
2130

2231
// Downloaded binary to /bin/{ name }

package-lock.json

Lines changed: 21 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"dependencies": {
3333
"axios": "0.24.0",
3434
"rimraf": "3.0.2",
35-
"tar": "6.1.11"
35+
"tar": "6.1.11",
36+
"tmp": "^0.2.1"
3637
}
3738
}

0 commit comments

Comments
 (0)