UNPKG

nodesync-cli

Version:

A smart CLI tool to auto-detect, install, and manage the right Node.js version for your project.

92 lines (79 loc) • 3.33 kB
import fs from 'fs'; import prompts from 'prompts'; import { ensureNVMInstalled, installNodeAndDeps } from '../core/installNode.js'; import { detectVersion } from '../core/detectVersion.js'; import { suggestVersion } from '../core/suggestVersion.js'; import { initLogger, log, logError } from '../utils/logUtils.js'; export default async function runSmartCommand(options) { console.log('\n🧠 Running nodesync smart...\n'); initLogger(options.log); log('Started nodesync smart command.'); try { // Step 1: NVM installation console.log('šŸ” Checking for NVM installation...'); log('Checking for NVM...'); if (options.dryRun) { console.log('[dry-run] Would check and install NVM if needed'); log('[dry-run] Skipped actual NVM install'); } else { const nvmInstalled = await ensureNVMInstalled(options.verbose); if (!nvmInstalled) { console.error('āŒ NVM installation failed or unsupported.'); log('NVM installation failed or unsupported.'); return; } } // Step 2: Version detection console.log('šŸ”Ž Detecting Node.js version...'); log('Detecting Node.js version...'); const detected = await detectVersion(); let nodeVersion = null; if (detected) { console.log(`šŸ“Œ Found version: ${detected.version} (from ${detected.source})`); log(`Detected version from ${detected.source}: ${detected.version}`); nodeVersion = detected.version; } else { console.log('āš ļø No version found. Suggesting LTS version...'); log('No version detected. Suggesting...'); const suggested = await suggestVersion(); nodeVersion = suggested.version; console.log(`šŸ“Œ Suggested version: ${nodeVersion}`); log(`Suggested Node.js version: ${nodeVersion}`); if (options.dryRun) { console.log(`[dry-run] Would write .nvmrc with version ${nodeVersion}`); log(`[dry-run] Would write .nvmrc with version ${nodeVersion}`); } else { const response = await prompts({ type: 'confirm', name: 'confirmWrite', message: `Do you want to use Node.js ${nodeVersion} and save it to .nvmrc?`, initial: true }); if (response.confirmWrite) { fs.writeFileSync('.nvmrc', nodeVersion, 'utf-8'); console.log('šŸ“ .nvmrc created.'); log('.nvmrc created with suggested version.'); } else { console.log('āŒ Skipped writing .nvmrc.'); log('User declined to write .nvmrc.'); } } } // Step 3: Install Node.js and deps console.log('āš™ļø Installing Node.js and project dependencies...'); log(`Preparing to install Node.js ${nodeVersion}...`); if (options.dryRun) { console.log(`[dry-run] Would run: nvm install ${nodeVersion} && nvm use ${nodeVersion}`); if (!options.noInstall) console.log(`[dry-run] Would run: npm install`); if (options.fixNatives) console.log(`[dry-run] Would run: npm rebuild`); log('[dry-run] Skipped actual install steps.'); } else { installNodeAndDeps(nodeVersion, options); } console.log('\nāœ… nodesync smart completed.\n'); log('nodesync smart completed.'); } catch (err) { console.error('āŒ Error:', err.message); logError(err); } }