Commit b3145a7c by liimin

feat(安装脚本): 添加创建全局命令快捷方式的功能

在 postinstall 脚本中添加 createShims.cjs 的执行,用于在安装后自动创建 Windows 和 POSIX 系统的全局命令快捷方式。同时更新了 CLI 的 README 文档,添加了关于不同终端使用的说明。

主要修改:
- 新增 createShims.cjs 脚本处理跨平台命令快捷方式创建
- 更新 package.json 的 postinstall 脚本
- 补充 CLI 文档中的终端使用说明
1 parent 3a0b9f28
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
"prepare": "husky", "prepare": "husky",
"precommit": "lint-staged", "precommit": "lint-staged",
"build": "venus build", "build": "venus build",
"postinstall": "node scripts/build.js && pnpm -C packages/cli link --global", "postinstall": "node scripts/build.js && pnpm -C packages/cli link --global && node scripts/createShims.cjs",
"venus": "venus" "venus": "venus"
}, },
"config": { "config": {
......
...@@ -183,6 +183,11 @@ pnpm exec venus create demo --no-install ...@@ -183,6 +183,11 @@ pnpm exec venus create demo --no-install
- `pnpm setup` 后运行 `pnpm -C packages/cli link --global` - `pnpm setup` 后运行 `pnpm -C packages/cli link --global`
- 新开一个终端后直接:`venus --help` - 新开一个终端后直接:`venus --help`
### 终端提示(Windows / Git Bash)
- Windows(PowerShell/CMD):`venus` 可直接运行;`where venus` 可查看命令位置。
- Git Bash:不解析 Windows 的 `.cmd/.ps1`,请使用 `pnpm exec venus`,或在 `~/.bashrc` 中加入 `alias venus='pnpm exec venus'`。
## 常见问题(FAQ) ## 常见问题(FAQ)
- 执行 `venus` 提示找不到命令? - 执行 `venus` 提示找不到命令?
......
/*
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 fs = require('fs');
const path = require('path');
const os = require('os');
const cp = require('child_process');
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 (e) {
return '';
}
}
function ensureDir(dir) {
try {
fs.mkdirSync(dir, { recursive: true });
} catch (e) {
// 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'));
candidates.push(path.join(os.homedir(), 'Library', 'pnpm')); // macOS
}
// De-duplicate and keep only existing dirs
const dirs = Array.from(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 (e) {
log(`failed to write into ${dir}: ${e && e.message}`);
}
}
log('done.');
}
main();
\ No newline at end of file \ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!