nodesync-cli
Version:
A smart CLI tool to auto-detect, install, and manage the right Node.js version for your project.
79 lines (67 loc) • 2.72 kB
JavaScript
import { execSync } from 'child_process';
import fs from 'fs';
import os from 'os';
import { log } from '../utils/logUtils.js';
export async function ensureNVMInstalled(verbose = false) {
const isWindows = process.platform === 'win32';
try {
execSync('nvm --version', { stdio: 'ignore' });
log('✅ nvm is already installed.');
return true;
} catch (e) {
log('❌ nvm not found. Attempting installation...');
if (isWindows) {
console.log('🪟 Windows detected.');
console.log('⚠️ Please install nvm-windows manually from:');
console.log(' 👉 https://github.com/coreybutler/nvm-windows/releases');
console.log(' Then re-run: nodesync smart');
log('Aborted: User must install nvm-windows manually.');
return false;
} else {
console.log('🐧 Unix-based system detected. Installing NVM...');
try {
execSync('curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash', {
stdio: 'inherit',
shell: '/bin/bash'
});
console.log('✅ nvm installed. Please restart your terminal or reload your shell config.');
log('nvm installed successfully.');
return true;
} catch (installErr) {
console.error('❌ Failed to install nvm:', installErr.message);
log('Failed to install nvm:', installErr.message);
return false;
}
}
}
}
export function installNodeAndDeps(version, options = {}) {
const isWindows = process.platform === 'win32';
try {
if (isWindows) {
console.log(`🪟 Running: nvm install ${version}`);
execSync(`nvm install ${version}`, { stdio: 'inherit' });
console.log(`🪟 Running: nvm use ${version}`);
execSync(`nvm use ${version}`, { stdio: 'inherit' });
} else {
console.log(`🐧 Running: nvm install ${version}`);
execSync(`. ~/.nvm/nvm.sh && nvm install ${version}`, { stdio: 'inherit', shell: '/bin/bash' });
console.log(`🐧 Running: nvm use ${version}`);
execSync(`. ~/.nvm/nvm.sh && nvm use ${version}`, { stdio: 'inherit', shell: '/bin/bash' });
}
if (!options.noInstall) {
console.log('📦 Running: npm install');
execSync('npm install', { stdio: 'inherit' });
} else {
console.log('⏭️ Skipped npm install (per --no-install)');
}
if (options.fixNatives) {
console.log('🔧 Rebuilding native modules...');
execSync('npm rebuild', { stdio: 'inherit' });
}
log(`Node.js ${version} installed and dependencies handled.`);
} catch (err) {
console.error('❌ Failed to install Node or dependencies:', err.message);
log('Install failed:', err.message);
}
}