Skip to content

Commit 2fbdaf1

Browse files
committed
fix: fix binary installation
1 parent 3e9608b commit 2fbdaf1

7 files changed

Lines changed: 133 additions & 67 deletions

File tree

.github/workflows/release.yml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,28 +88,28 @@ jobs:
8888
run: |
8989
if [[ "${{ matrix.platform }}" == "ubuntu-latest" ]]; then
9090
if [[ "${{ matrix.arch }}" == "x64" ]]; then
91-
mkdir -p ./build/linux/x64
92-
cp target/x86_64-unknown-linux-gnu/release/todoctor ./build/linux/x64/todoctor
91+
mkdir -p ./bin/linux/x64
92+
cp target/x86_64-unknown-linux-gnu/release/todoctor ./bin/linux/x64/todoctor
9393
elif [[ "${{ matrix.arch }}" == "arm64" ]]; then
94-
mkdir -p ./build/linux/arm64
95-
cp target/aarch64-unknown-linux-gnu/release/todoctor ./build/linux/arm64/todoctor
94+
mkdir -p ./bin/linux/arm64
95+
cp target/aarch64-unknown-linux-gnu/release/todoctor ./bin/linux/arm64/todoctor
9696
fi
9797
elif [[ "${{ matrix.platform }}" == "macos-latest" ]]; then
9898
if [[ "${{ matrix.arch }}" == "x64" ]]; then
99-
mkdir -p ./build/macos/x64
100-
cp target/x86_64-apple-darwin/release/todoctor ./build/macos/x64/todoctor
99+
mkdir -p ./bin/macos/x64
100+
cp target/x86_64-apple-darwin/release/todoctor ./bin/macos/x64/todoctor
101101
elif [[ "${{ matrix.arch }}" == "arm64" ]]; then
102-
mkdir -p ./build/macos/arm64
103-
cp target/aarch64-apple-darwin/release/todoctor ./build/macos/arm64/todoctor
102+
mkdir -p ./bin/macos/arm64
103+
cp target/aarch64-apple-darwin/release/todoctor ./bin/macos/arm64/todoctor
104104
fi
105105
fi
106106
shell: bash
107107

108108
- name: Move Binaries to Bin Folder (Windows)
109109
if: runner.os == 'Windows'
110110
run: |
111-
mkdir build\windows\x64
112-
copy target\x86_64-pc-windows-msvc\release\todoctor.exe build\windows\x64\todoctor.exe
111+
mkdir bin\windows\x64
112+
copy target\x86_64-pc-windows-msvc\release\todoctor.exe bin\windows\x64\todoctor.exe
113113
shell: cmd
114114

115115
- name: Upload Binaries

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ node_modules/
66
target/
77

88
# Build
9-
build/
9+
!bin/todoctor.js
1010
dist/
1111
bin/
1212

bin/todoctor.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env node
2+
3+
import { spawn } from 'node:child_process'
4+
import { fileURLToPath } from 'node:url'
5+
import path from 'node:path'
6+
import os from 'node:os'
7+
import fs from 'node:fs'
8+
9+
let filename = fileURLToPath(import.meta.url)
10+
let dirname = path.dirname(filename)
11+
12+
let platform = os.platform()
13+
let arch = os.arch()
14+
15+
let binaries = {
16+
'win32:x64': 'windows/x64/todoctor.exe',
17+
'darwin:arm64': 'macos/arm64/todoctor',
18+
'linux:arm64': 'linux/arm64/todoctor',
19+
'darwin:x64': 'macos/x64/todoctor',
20+
'linux:x64': 'linux/x64/todoctor',
21+
}
22+
23+
let key = `${platform}:${arch}`
24+
let relativePath = binaries[key]
25+
26+
if (!relativePath) {
27+
console.error(`Unsupported platform or architecture: ${platform}, ${arch}`)
28+
process.exit(1)
29+
}
30+
31+
let binaryPath = path.join(dirname, relativePath)
32+
33+
if (!fs.existsSync(binaryPath)) {
34+
console.error(`Binary not found: ${binaryPath}`)
35+
console.error(
36+
'Please ensure that the postinstall script has run successfully.',
37+
)
38+
process.exit(1)
39+
}
40+
41+
let args = process.argv.slice(2)
42+
let child = spawn(binaryPath, args, { stdio: 'inherit' })
43+
44+
child.on('exit', code => {
45+
process.exit(code)
46+
})

package.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@
2323
"start": "pnpm run /^start:/",
2424
"start:preview": "vite",
2525
"build": "pnpm run /^build:/",
26-
"build:lib": "cargo build --release && cp target/release/todoctor ./bin/",
26+
"build:lib": "cargo build --release && node ./scripts/build",
2727
"build:preview": "vite build",
28-
"postinstall": "node ./scripts/install.js",
2928
"release": "pnpm release:check && pnpm release:version",
3029
"release:check": "pnpm test && pnpm run build",
3130
"release:version": "changelogen --output changelog.md --release --push",
@@ -42,6 +41,15 @@
4241
"test:types": "tsc --noEmit --pretty",
4342
"test:unit": "cargo test"
4443
},
44+
"os": [
45+
"darwin",
46+
"linux",
47+
"win32"
48+
],
49+
"cpu": [
50+
"x64",
51+
"arm64"
52+
],
4553
"publishConfig": {
4654
"access": "public"
4755
},

scripts/build.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import path from 'node:path'
2+
import fs from 'node:fs'
3+
import os from 'node:os'
4+
5+
let platform = os.platform()
6+
let arch = os.arch()
7+
8+
let platformMap = {
9+
win32: 'windows',
10+
darwin: 'macos',
11+
linux: 'linux',
12+
}
13+
14+
let archMap = {
15+
arm64: 'arm64',
16+
x64: 'x64',
17+
}
18+
19+
let platformName = platformMap[platform]
20+
let archName = archMap[arch]
21+
22+
if (!platformName || !archName) {
23+
console.error(`Unsupported platform or architecture: ${platform}, ${arch}`)
24+
process.exit(1)
25+
}
26+
27+
let binaryName = platform === 'win32' ? 'todoctor.exe' : 'todoctor'
28+
29+
let sourcePath = path.join(
30+
import.meta.dirname,
31+
'..',
32+
'target',
33+
'release',
34+
binaryName,
35+
)
36+
let destDir = path.join(
37+
import.meta.dirname,
38+
'..',
39+
'bin',
40+
platformName,
41+
archName,
42+
)
43+
let destPath = path.join(destDir, binaryName)
44+
45+
if (!fs.existsSync(destDir)) {
46+
fs.mkdirSync(destDir, { recursive: true })
47+
}
48+
49+
try {
50+
fs.copyFileSync(sourcePath, destPath)
51+
52+
if (platform !== 'win32') {
53+
fs.chmodSync(destPath, 0o755)
54+
}
55+
} catch (error) {
56+
console.error(`Error copying file: ${error.message}`)
57+
process.exit(1)
58+
}

scripts/install.js

Lines changed: 0 additions & 48 deletions
This file was deleted.

src/get_dist_path.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@ pub fn get_dist_path() -> Option<PathBuf> {
1010
return None;
1111
}
1212
};
13+
let real_exe_path = fs::canonicalize(&exe_path).unwrap_or(exe_path);
1314

14-
if let Ok(real_path) = fs::read_link(&exe_path) {
15-
let project_root = real_path.parent()?.parent()?.to_path_buf();
16-
let dist_path = project_root.join("dist");
17-
if dist_path.exists() {
18-
return Some(dist_path);
15+
fn ascend_path(mut path: PathBuf, levels: usize) -> Option<PathBuf> {
16+
for _ in 0..levels {
17+
path = path.parent()?.to_path_buf();
1918
}
19+
Some(path)
2020
}
2121

22-
let project_root = exe_path.parent()?.parent()?.to_path_buf();
22+
let levels_up = 4;
23+
24+
let project_root = ascend_path(real_exe_path, levels_up)?;
2325
let dist_path = project_root.join("dist");
2426
if dist_path.exists() {
2527
return Some(dist_path);

0 commit comments

Comments
 (0)