diff --git a/src/Service/NodePackageManager.php b/src/Service/NodePackageManager.php index 5e1aa2c9..8966c706 100644 --- a/src/Service/NodePackageManager.php +++ b/src/Service/NodePackageManager.php @@ -35,7 +35,14 @@ public function installNodeModules(string $path, SymfonyStyle $io, bool $isVerbo try { if ($this->fileDriver->isExists($path . '/package-lock.json')) { - $this->shell->execute('npm ci --quiet'); + try { + $this->shell->execute('npm ci --quiet'); + } catch (\Exception $e) { + if ($isVerbose) { + $io->warning('npm ci failed, falling back to npm install...'); + } + $this->shell->execute('npm install --quiet'); + } } else { if ($isVerbose) { $io->warning('No package-lock.json found, running npm install...'); @@ -74,7 +81,7 @@ public function isNodeModulesInSync(string $path): bool } if (!$this->fileDriver->isExists($path . '/package-lock.json')) { - return true; + return false; } $currentDir = getcwd(); diff --git a/src/Service/ThemeBuilder/MagentoStandard/Builder.php b/src/Service/ThemeBuilder/MagentoStandard/Builder.php index 0ac63fe4..7a2cd7c0 100644 --- a/src/Service/ThemeBuilder/MagentoStandard/Builder.php +++ b/src/Service/ThemeBuilder/MagentoStandard/Builder.php @@ -7,6 +7,7 @@ use Magento\Framework\Filesystem\Driver\File; use Magento\Framework\Shell; use OpenForgeProject\MageForge\Service\CacheCleaner; +use OpenForgeProject\MageForge\Service\NodePackageManager; use OpenForgeProject\MageForge\Service\StaticContentCleaner; use OpenForgeProject\MageForge\Service\StaticContentDeployer; use OpenForgeProject\MageForge\Service\SymlinkCleaner; @@ -24,7 +25,8 @@ public function __construct( private readonly StaticContentDeployer $staticContentDeployer, private readonly StaticContentCleaner $staticContentCleaner, private readonly CacheCleaner $cacheCleaner, - private readonly SymlinkCleaner $symlinkCleaner + private readonly SymlinkCleaner $symlinkCleaner, + private readonly NodePackageManager $nodePackageManager ) { } @@ -98,9 +100,16 @@ public function build(string $themeCode, string $themePath, SymfonyStyle $io, Ou public function autoRepair(string $themePath, SymfonyStyle $io, OutputInterface $output, bool $isVerbose): bool { - // Check for node_modules in root - if (!$this->installNodeModulesIfMissing($io, $isVerbose)) { - return false; + $rootPath = '.'; + + // Check if node_modules is in sync with package-lock.json + if (!$this->nodePackageManager->isNodeModulesInSync($rootPath)) { + if ($isVerbose) { + $io->warning('Node modules out of sync, missing, or no lock file found. Installing...'); + } + if (!$this->nodePackageManager->installNodeModules($rootPath, $io, $isVerbose)) { + return false; + } } // Check for grunt @@ -116,39 +125,7 @@ public function autoRepair(string $themePath, SymfonyStyle $io, OutputInterface return true; } - /** - * Install Node modules if they're missing - */ - private function installNodeModulesIfMissing(SymfonyStyle $io, bool $isVerbose): bool - { - if (!$this->fileDriver->isDirectory('node_modules')) { - if ($isVerbose) { - $io->warning('Node modules not found in root directory. Running npm ci...'); - } - - try { - if ($this->fileDriver->isExists('package-lock.json')) { - $this->shell->execute('npm ci --quiet'); - } else { - if ($isVerbose) { - $io->warning('No package-lock.json found, running npm install...'); - } - $this->shell->execute('npm install --quiet'); - } - - if ($isVerbose) { - $io->success('Node modules installed successfully.'); - } - - return true; - } catch (\Exception $e) { - $io->error('Failed to install node modules: ' . $e->getMessage()); - return false; - } - } - return true; - } /** * Install Grunt if it's missing @@ -209,11 +186,6 @@ public function watch(string $themeCode, string $themePath, SymfonyStyle $io, Ou return false; } - // Check for node_modules in root - if (!$this->installNodeModulesIfMissing($io, $isVerbose)) { - return false; - } - try { if ($isVerbose) { $io->text('Starting watch mode with verbose output...');