Skip to content

Commit 1e54d39

Browse files
committed
fix: streamline npm command execution in NodePackageManager
1 parent fd02e2f commit 1e54d39

1 file changed

Lines changed: 8 additions & 34 deletions

File tree

src/Service/NodePackageManager.php

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -35,40 +35,30 @@ public function __construct(
3535
*/
3636
public function installNodeModules(string $path, SymfonyStyle $io, bool $isVerbose): bool
3737
{
38-
$currentDir = getcwd();
39-
if ($currentDir === false) {
40-
$io->error('Cannot determine current directory');
41-
return false;
42-
}
43-
44-
chdir($path);
45-
4638
try {
4739
if ($this->fileDriver->isExists($path . '/package-lock.json')) {
4840
try {
49-
$this->shell->execute('npm ci --quiet');
41+
$this->shell->execute('cd %s && npm ci --quiet', [$path]);
5042
} catch (\Exception $e) {
5143
if ($isVerbose) {
5244
$io->warning('npm ci failed, falling back to npm install...');
5345
}
54-
$this->shell->execute('npm install --quiet');
46+
$this->shell->execute('cd %s && npm install --quiet', [$path]);
5547
}
5648
} else {
5749
if ($isVerbose) {
5850
$io->warning('No package-lock.json found, running npm install...');
5951
}
60-
$this->shell->execute('npm install --quiet');
52+
$this->shell->execute('cd %s && npm install --quiet', [$path]);
6153
}
6254

6355
if ($isVerbose) {
6456
$io->success('Node modules installed successfully.');
6557
}
6658

67-
chdir($currentDir);
6859
return true;
6960
} catch (\Exception $e) {
7061
$io->error('Failed to install node modules: ' . $e->getMessage());
71-
chdir($currentDir);
7262
return false;
7363
}
7464
}
@@ -94,19 +84,10 @@ public function isNodeModulesInSync(string $path): bool
9484
return false;
9585
}
9686

97-
$currentDir = getcwd();
98-
if ($currentDir === false) {
99-
return false;
100-
}
101-
102-
chdir($path);
103-
10487
try {
105-
$this->shell->execute('npm ls --depth=0 --json > /dev/null 2>&1');
106-
chdir($currentDir);
88+
$this->shell->execute('cd %s && npm ls --depth=0 --json > /dev/null 2>&1', [$path]);
10789
return true;
10890
} catch (\Exception $e) {
109-
chdir($currentDir);
11091
return false;
11192
}
11293
}
@@ -120,23 +101,16 @@ public function isNodeModulesInSync(string $path): bool
120101
*/
121102
public function checkOutdatedPackages(string $path, SymfonyStyle $io): void
122103
{
123-
$currentDir = getcwd();
124-
if ($currentDir === false) {
125-
return;
126-
}
127-
128-
chdir($path);
129-
130104
try {
131-
$outdated = $this->shell->execute('npm outdated --json');
105+
$outdated = $this->shell->execute('cd %s && npm outdated --json', [$path]);
132106
if ($outdated) {
133107
$io->warning('Outdated packages found:');
134108
$io->writeln($outdated);
135109
}
136110
} catch (\Exception $e) {
137-
// npm outdated returns non-zero exit code when packages are outdated
111+
if ($io->isVerbose()) {
112+
$io->warning('Failed to check outdated packages: ' . $e->getMessage());
113+
}
138114
}
139-
140-
chdir($currentDir);
141115
}
142116
}

0 commit comments

Comments
 (0)