diff --git a/src/node/index.ts b/src/node/index.ts index 8e90fe0..8c6dcf9 100644 --- a/src/node/index.ts +++ b/src/node/index.ts @@ -1,6 +1,6 @@ import { logger } from '@/lib/logger'; import { execSync } from "child_process"; -import fs from 'fs' +import fs from 'fs'; type InstallType = 'nvm'; @@ -13,18 +13,16 @@ export class NodeVenv { } set changeNvmrcFile(file: string) { - if(!file) throw new Error('File path is required'); - if(!file.includes(".")) throw new Error('Invalid file path'); + if (!file) throw new Error('File path is required'); + if (!file.includes(".")) throw new Error('Invalid file path'); this.nvmrcPath = file; } private isManagerInstalled(manager: InstallType): boolean { + const command = this.isWindows ? `powershell.exe -Command "Get-Command ${manager}"` : `command -v ${manager}`; + logger.info(`Checking if ${manager} is installed...`); try { - if (this.isWindows) { - execSync(`powershell.exe -Command "Get-Command ${manager}"`, { stdio: 'ignore' }); - } else { - execSync(`command -v ${manager}`, { stdio: 'ignore' }); - } + execSync(command, { stdio: 'ignore' }); return true; } catch (e) { return false; @@ -54,55 +52,27 @@ export class NodeVenv { } private runInstallNodeCommand(version: string, type: InstallType) { - if(this.isWindows) { - try { - logger.info(`Installing Node.js version ${version} using ${type} on Windows...`); - execSync(`powershell -Command "${type} install ${version}"`, { stdio: 'inherit', windowsHide: true }); - execSync(`powershell -Command "${type} use ${version}"`, { stdio: 'inherit', windowsHide: true }); - } catch (error) { - logger.error(`Failed to install Node.js with ${type} on Windows. Make sure ${type} is installed and the version is valid.`); - process.exit(1); - } - } - else { - try { - logger.info(`Installing Node.js version ${version} using ${type}...`); - execSync(`${type} install ${version}`, { stdio: 'inherit' }); - execSync(`${type} use ${version}`, { stdio: 'inherit' }); - } catch (error) { - logger.error(`Failed to install Node.js with ${type}. Make sure ${type} is installed and the version is valid.`); - process.exit(1); - } - } - } - - public updateNvmrcFile(version: string, type:InstallType = 'nvm'): void { - const nvmrcPath = this.nvmrcPath; + const installCommand = this.isWindows ? `powershell -Command "${type} install ${version}"` : `${type} install ${version}`; + const useCommand = this.isWindows ? `powershell -Command "${type} use ${version}"` : `${type} use ${version}`; try { - fs.writeFileSync(nvmrcPath, `v${version}`, 'utf-8'); - logger.info(`āœ” Updated ${nvmrcPath} file with version ${version}.`); - - this.runInstallNodeCommand(version, type); - - logger.info(`\nšŸŽ‰ Node.js Version Managed Successfully with ${type}!`); - + logger.info(`Installing Node.js version ${version} using ${type}...`); + execSync(installCommand, { stdio: 'inherit', windowsHide: true }); + execSync(useCommand, { stdio: 'inherit', windowsHide: true }); } catch (error) { - logger.error(`Failed to update ${nvmrcPath} file. Make sure you have the necessary permissions.`); + logger.error(`Failed to install Node.js with ${type}. Make sure ${type} is installed and the version is valid.`); + process.exit(1); } } - public installNode(version: string, type:InstallType = 'nvm') { - - if(!this.isManagerInstalled(type)) { + private manageNodeVersion(version: string, type: InstallType) { + if (!this.isManagerInstalled(type)) { logger.error(`${type} is not installed. Please install ${type} and try again.`); return process.exit(1); } if (!this.checkNvmrcFile(version)) return process.exit(1); - const envPath = this.nvmrcPath; - - const nodeVersion = fs.readFileSync(envPath, 'utf-8').trim(); + const nodeVersion = fs.readFileSync(this.nvmrcPath, 'utf-8').trim(); logger.info(`āœ” Found Node.js version ${nodeVersion} using ${type}...`); this.runInstallNodeCommand(version, type); @@ -110,24 +80,36 @@ export class NodeVenv { logger.info(`\nšŸŽ‰ Node.js Version Managed Successfully with ${type}!`); } + public updateNvmrcFile(version: string, type: InstallType = 'nvm'): void { + try { + fs.writeFileSync(this.nvmrcPath, `v${version}`, 'utf-8'); + logger.info(`āœ” Updated ${this.nvmrcPath} file with version ${version}.`); + this.runInstallNodeCommand(version, type); + logger.info(`\nšŸŽ‰ Node.js Version Managed Successfully with ${type}!`); + } catch (error) { + logger.error(`Failed to update ${this.nvmrcPath} file. Make sure you have the necessary permissions.`); + } + } + + public installNode(version: string, type: InstallType = 'nvm') { + this.manageNodeVersion(version, type); + } + public autoManageNode() { - const nvmrcPath = this.nvmrcPath; try { - if (!fs.existsSync(nvmrcPath)) { - logger.error(`No ${nvmrcPath} file found in the current directory. Please create one with a Node.js version.`); + if (!fs.existsSync(this.nvmrcPath)) { + logger.error(`No ${this.nvmrcPath} file found in the current directory. Please create one with a Node.js version.`); process.exit(1); } - const nodeVersion = fs.readFileSync(nvmrcPath, 'utf-8').trim(); + const nodeVersion = fs.readFileSync(this.nvmrcPath, 'utf-8').trim(); if (!nodeVersion) { - logger.error(`No version found in ${nvmrcPath} file. Please specify a Node.js version.`); + logger.error(`No version found in ${this.nvmrcPath} file. Please specify a Node.js version.`); process.exit(1); } this.runInstallNodeCommand(nodeVersion, 'nvm'); - logger.info(`\nšŸŽ‰ Node.js Version Managed Successfully with nvm!`); - } catch (error) { logger.error(`Failed to manage Node.js version with nvm. Make sure you have the necessary permissions.`); }