Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/Service/NodePackageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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...');
Expand Down Expand Up @@ -74,7 +81,7 @@ public function isNodeModulesInSync(string $path): bool
}

if (!$this->fileDriver->isExists($path . '/package-lock.json')) {
return true;
return false;
}

$currentDir = getcwd();
Expand Down
54 changes: 13 additions & 41 deletions src/Service/ThemeBuilder/MagentoStandard/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
) {
}

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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...');
Expand Down
Loading