createShims.cjs 2.99 KB
/*
  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'));
    candidates.push(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();