diff --git a/composer.json b/composer.json index 8733be2..a4be81a 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ ], "require": { "php": ">=5.3.0", - "composer-plugin-api": "^1.0.0", + "composer-plugin-api": "^1.0 || ^2.0", "ext-openssl": "*" }, "require-dev": { diff --git a/src/NodeJsInstaller.php b/src/NodeJsInstaller.php index a0e30d6..f40e2e2 100644 --- a/src/NodeJsInstaller.php +++ b/src/NodeJsInstaller.php @@ -1,6 +1,7 @@ io = $io; - $this->rfs = new RemoteFilesystem($io); + $this->rfs = new RemoteFilesystem($io, $composer->getConfig()); } /** @@ -199,7 +200,6 @@ public function install($version, $targetDirectory) $this->io->write(" Downloading from $url"); $cwd = getcwd(); - chdir(__DIR__.'/../../../../'); $fileName = 'vendor/'.pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_BASENAME); @@ -291,9 +291,6 @@ private function extractTo($tarGzFile, $targetDir) public function createBinScripts($binDir, $targetDir, $isLocal) { - $cwd = getcwd(); - chdir(__DIR__.'/../../../../'); - if (!file_exists($binDir)) { $result = mkdir($binDir, 0775, true); if ($result === false) { @@ -311,8 +308,6 @@ public function createBinScripts($binDir, $targetDir, $isLocal) $this->createBinScript($binDir, $fullTargetDir, 'node.bat', 'node', $isLocal); $this->createBinScript($binDir, $fullTargetDir, 'npm.bat', 'npm', $isLocal); } - - chdir($cwd); } /** diff --git a/src/NodeJsPlugin.php b/src/NodeJsPlugin.php index 704fb19..f54da3d 100644 --- a/src/NodeJsPlugin.php +++ b/src/NodeJsPlugin.php @@ -19,11 +19,14 @@ */ class NodeJsPlugin implements PluginInterface, EventSubscriberInterface { + const NODEJS_TARGET_DIR = 'vendor/nodejs/nodejs'; + const DOWNLOAD_NODEJS_EVENT = 'download-nodejs'; + /** + * @var Composer + */ protected $composer; - const DOWNLOAD_NODEJS_EVENT = 'download-nodejs'; - /** * @var IOInterface */ @@ -35,6 +38,19 @@ public function activate(Composer $composer, IOInterface $io) $this->io = $io; } + public function deactivate(Composer $composer, IOInterface $io) + { + $binDir = $composer->getConfig()->get('bin-dir'); + $this->onDeactivate($binDir); + } + + public function uninstall(Composer $composer, IOInterface $io) + { + $binDir = $composer->getConfig()->get('bin-dir'); + $targetDir = self::NODEJS_TARGET_DIR; + $this->onUninstall($binDir, $targetDir); + } + /** * Let's register the harmony dependencies update events. * @@ -61,7 +77,7 @@ public static function getSubscribedEvents() public function onPostUpdateInstall(Event $event) { $settings = array( - 'targetDir' => 'vendor/nodejs/nodejs', + 'targetDir' => self::NODEJS_TARGET_DIR, 'forceLocal' => false, 'includeBinInPath' => false, ); @@ -90,7 +106,7 @@ public function onPostUpdateInstall(Event $event) $this->verboseLog("NodeJS installer:"); $this->verboseLog(" - Requested version: ".$versionConstraint); - $nodeJsInstaller = new NodeJsInstaller($this->io); + $nodeJsInstaller = new NodeJsInstaller($this->io, $this->composer); $isLocal = false; @@ -181,7 +197,7 @@ private function installLocalVersion($binDir, NodeJsInstaller $nodeJsInstaller, */ private function installBestPossibleLocalVersion(NodeJsInstaller $nodeJsInstaller, $versionConstraint, $targetDir) { - $nodeJsVersionsLister = new NodeJsVersionsLister($this->io); + $nodeJsVersionsLister = new NodeJsVersionsLister($this->io, $this->composer); $allNodeJsVersions = $nodeJsVersionsLister->getList(); $nodeJsVersionMatcher = new NodeJsVersionMatcher(); @@ -245,7 +261,17 @@ private function onUninstall($binDir, $targetDir) } } - // Now, let's remove the links + $this->onDeactivate($binDir); + } + + /** + * Deactivates NodeJS links. + */ + private function onDeactivate($binDir) + { + $fileSystem = new Filesystem(); + + // Remove the links. $this->verboseLog("Removing NodeJS and NPM links from Composer bin directory"); foreach (array("node", "npm", "node.bat", "npm.bat") as $file) { $realFile = $binDir.DIRECTORY_SEPARATOR.$file; @@ -254,4 +280,5 @@ private function onUninstall($binDir, $targetDir) } } } + } diff --git a/src/NodeJsVersionsLister.php b/src/NodeJsVersionsLister.php index 6b31e03..809ce73 100644 --- a/src/NodeJsVersionsLister.php +++ b/src/NodeJsVersionsLister.php @@ -1,6 +1,7 @@ io = $io; - $this->rfs = new RemoteFilesystem($io); + $this->rfs = new RemoteFilesystem($io, $composer->getConfig()); } public function getList()