createShims.cjs
2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
Create global shims for the local @venus/cli build so that `venus` is available
immediately after `pnpm install`. This is primarily to handle Windows environments
where pnpm's global link may not produce an executable in PATH-accessible locations.
Behavior:
- Determine the absolute path to the CLI entry: packages/cli/dist/index.cjs.js
- Detect global bin directories and write shims there.
* On Windows: write venus.cmd and venus.ps1
* On POSIX: write venus (bash) with executable permission
*/
const cp = require("node:child_process");
const fs = require("node:fs");
const os = require("node:os");
const path = require("node:path");
function log(msg) {
console.log(`[createShims] ${msg}`);
}
function safeExec(cmd) {
try {
const out = cp
.execSync(cmd, { stdio: ["ignore", "pipe", "pipe"] })
.toString()
.trim();
return out;
} catch {
return "";
}
}
function ensureDir(dir) {
try {
fs.mkdirSync(dir, { recursive: true });
} catch {
// ignore
}
}
function writeFile(filePath, content, mode) {
ensureDir(path.dirname(filePath));
fs.writeFileSync(filePath, content, "utf8");
if (mode) {
try {
fs.chmodSync(filePath, mode);
} catch {}
}
log(`wrote shim: ${filePath}`);
}
function main() {
const repoRoot = process.cwd();
const entry = path.resolve(repoRoot, "packages/cli/dist/index.cjs.js");
const isWin = process.platform === "win32";
if (!fs.existsSync(entry)) {
log(`entry not found: ${entry}`);
log("skip creating shims (CLI not built yet).");
return;
}
// Prefer pnpm reported global bin, then common fallback dirs
const pnpmGlobalBin = safeExec("pnpm bin -g");
const candidates = [];
if (pnpmGlobalBin) candidates.push(pnpmGlobalBin);
if (isWin) {
// Common Node.js global bin location added to PATH by many installers
candidates.push(path.join(os.homedir(), "AppData", "Roaming", "npm"));
} else {
// Common pnpm homes on POSIX
candidates.push(
path.join(os.homedir(), ".local", "share", "pnpm"),
path.join(os.homedir(), "Library", "pnpm"), // macOS
);
}
// De-duplicate and keep only existing dirs
const dirs = [...new Set(candidates)].filter((d) => {
try {
return fs.existsSync(d);
} catch {
return false;
}
});
if (dirs.length === 0) {
log("no global bin directories found.");
log("you may need to run `pnpm setup` to add PNPM_HOME to PATH.");
return;
}
for (const dir of dirs) {
try {
if (isWin) {
const cmdBody = `@echo off\r\nnode "${entry}" %*`;
writeFile(path.join(dir, "venus.cmd"), cmdBody);
const ps1Body = `& node "${entry}" $args`;
writeFile(path.join(dir, "venus.ps1"), ps1Body);
} else {
const bashBody = `#!/usr/bin/env bash\nnode "${entry}" "$@"`;
writeFile(path.join(dir, "venus"), bashBody, 0o755);
}
} catch (error) {
log(`failed to write into ${dir}: ${error && error.message}`);
}
}
log("done.");
}
main();