Skip to content
This repository has been archived by the owner on Jul 8, 2023. It is now read-only.

Commit

Permalink
Merge branch 'release/2.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
ezzatron committed Feb 12, 2014
2 parents ebec812 + b8c18fa commit 69d6034
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 29 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Composer NPM bridge changelog

## 2.1.0 (2014-02-12)

- **[IMPROVED]** Composer dev/production modes are now honored when installing
NPM dependencies
- **[IMPROVED]** Can now be utilized as a dev-only dependency of the root
package

## 2.0.0 (2014-01-29)

- **[BC BREAK]** Completely re-written as a Composer plugin
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

*NPM integration for Composer packages.*

[![The most recent stable version is 2.0.0][version-image]][Semantic versioning]
[![The most recent stable version is 2.1.0][version-image]][Semantic versioning]
[![Current build status image][build-image]][Current build status]
[![Current coverage status image][coverage-image]][Current coverage status]

Expand Down Expand Up @@ -67,4 +67,4 @@ ignore the `node_modules` directory in order to avoid this.
[Current coverage status]: https://coveralls.io/r/eloquent/composer-npm-bridge
[eloquent/composer-npm-bridge]: https://packagist.org/packages/eloquent/composer-npm-bridge
[Semantic versioning]: http://semver.org/
[version-image]: http://img.shields.io/:semver-2.0.0-brightgreen.svg "This project uses semantic versioning"
[version-image]: http://img.shields.io/:semver-2.1.0-brightgreen.svg "This project uses semantic versioning"
40 changes: 31 additions & 9 deletions src/NpmBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,24 @@ public function client()
/**
* Install NPM dependencies for a Composer project and its dependencies.
*
* @param Composer $composer The main Composer object.
* @param Composer $composer The main Composer object.
* @param boolean|null $isDevMode True if dev mode is enabled.
*
* @throws Exception\NpmNotFoundException If the npm executable cannot be located.
* @throws Exception\NpmCommandFailedException If the operation fails.
*/
public function install(Composer $composer)
public function install(Composer $composer, $isDevMode = null)
{
if (null === $isDevMode) {
$isDevMode = true;
}

$this->io()->write(
'<info>Installing NPM dependencies for root project</info>'
);

if ($this->isDependantPackage($composer->getPackage())) {
$this->client()->install();
if ($this->isDependantPackage($composer->getPackage(), $isDevMode)) {
$this->client()->install(null, $isDevMode);
} else {
$this->io()->write('Nothing to install');
}
Expand All @@ -120,8 +125,9 @@ public function update(Composer $composer)
'<info>Updating NPM dependencies for root project</info>'
);

if ($this->isDependantPackage($composer->getPackage())) {
if ($this->isDependantPackage($composer->getPackage(), true)) {
$this->client()->update();
$this->client()->install(null, true);
$this->client()->shrinkwrap();
} else {
$this->io()->write('Nothing to update');
Expand All @@ -133,18 +139,33 @@ public function update(Composer $composer)
/**
* Returns true if the supplied package requires the Composer NPM bridge.
*
* @param PackageInterface $package The package to inspect.
* @param PackageInterface $package The package to inspect.
* @param boolean|null $includeDevDependencies True if the dev dependencies should also be inspected.
*
* @return boolean True if the package requires the bridge.
*/
public function isDependantPackage(PackageInterface $package)
{
public function isDependantPackage(
PackageInterface $package,
$includeDevDependencies = null
) {
if (null === $includeDevDependencies) {
$includeDevDependencies = false;
}

foreach ($package->getRequires() as $link) {
if ('eloquent/composer-npm-bridge' === $link->getTarget()) {
return true;
}
}

if ($includeDevDependencies) {
foreach ($package->getDevRequires() as $link) {
if ('eloquent/composer-npm-bridge' === $link->getTarget()) {
return true;
}
}
}

return false;
}

Expand Down Expand Up @@ -176,7 +197,8 @@ protected function installForVendors(Composer $composer)
);

$this->client()->install(
sprintf('%s/%s', $vendorDir, $package->getName())
sprintf('%s/%s', $vendorDir, $package->getName()),
false
);
}
} else {
Expand Down
13 changes: 9 additions & 4 deletions src/NpmBridgeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ interface NpmBridgeInterface
/**
* Install NPM dependencies for a Composer project and its dependencies.
*
* @param Composer $composer The main Composer object.
* @param Composer $composer The main Composer object.
* @param boolean|null $isDevMode True if dev mode is enabled.
*
* @throws Exception\NpmNotFoundException If the npm executable cannot be located.
* @throws Exception\NpmCommandFailedException If the operation fails.
*/
public function install(Composer $composer);
public function install(Composer $composer, $isDevMode = null);

/**
* Update NPM dependencies for a Composer project and its dependencies.
Expand All @@ -46,9 +47,13 @@ public function update(Composer $composer);
/**
* Returns true if the supplied package requires the Composer NPM bridge.
*
* @param PackageInterface $package The package to inspect.
* @param PackageInterface $package The package to inspect.
* @param boolean|null $includeDevDependencies True if the dev dependencies should also be inspected.
*
* @return boolean True if the package requires the bridge.
*/
public function isDependantPackage(PackageInterface $package);
public function isDependantPackage(
PackageInterface $package,
$includeDevDependencies = null
);
}
2 changes: 1 addition & 1 deletion src/NpmBridgePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function onPostInstallCmd(Event $event)
{
$this->bridgeFactory()
->create($event->getIO())
->install($event->getComposer());
->install($event->getComposer(), $event->isDevMode());
}

/**
Expand Down
17 changes: 14 additions & 3 deletions src/NpmClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,25 @@ public function executableFinder()
/**
* Install NPM dependencies for the project at the supplied path.
*
* @param string|null $path The path to the NPM project, or null to use the current working directory.
* @param string|null $path The path to the NPM project, or null to use the current working directory.
* @param boolean|null $isDevMode True if dev dependencies should be included.
*
* @throws Exception\NpmNotFoundException If the npm executable cannot be located.
* @throws Exception\NpmCommandFailedException If the operation fails.
*/
public function install($path = null)
public function install($path = null, $isDevMode = null)
{
$this->executeNpm(array('install'), $path);
if (null === $isDevMode) {
$isDevMode = true;
}

if ($isDevMode) {
$arguments = array('install');
} else {
$arguments = array('install', '--production');
}

$this->executeNpm($arguments, $path);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/NpmClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ interface NpmClientInterface
/**
* Install NPM dependencies for the project at the supplied path.
*
* @param string|null $path The path to the NPM project, or null to use the current working directory.
* @param string|null $path The path to the NPM project, or null to use the current working directory.
* @param boolean|null $isDevMode True if dev dependencies should be included.
*
* @throws Exception\NpmNotFoundException If the npm executable cannot be located.
* @throws Exception\NpmCommandFailedException If the operation fails.
*/
public function install($path = null);
public function install($path = null, $isDevMode = null);

/**
* Update NPM dependencies for the project at the supplied path.
Expand Down
2 changes: 1 addition & 1 deletion src/NpmVendorFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function find(Composer $composer, NpmBridgeInterface $bridge)

$dependantPackages = array();
foreach ($packages as $package) {
if ($bridge->isDependantPackage($package)) {
if ($bridge->isDependantPackage($package, false)) {
$dependantPackages[] = $package;
}
}
Expand Down
14 changes: 12 additions & 2 deletions test/suite/NpmBridgePluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,21 @@ public function testGetSubscribedEvents()

public function testOnPostInstallCmd()
{
$this->plugin->onPostInstallCmd(new Event(ScriptEvents::POST_INSTALL_CMD, $this->composer, $this->io));
$this->plugin->onPostInstallCmd(new Event(ScriptEvents::POST_INSTALL_CMD, $this->composer, $this->io, true));

Phake::inOrder(
Phake::verify($this->bridgeFactory)->create($this->io),
Phake::verify($this->bridge)->install($this->composer)
Phake::verify($this->bridge)->install($this->composer, true)
);
}

public function testOnPostInstallCmdProductionMode()
{
$this->plugin->onPostInstallCmd(new Event(ScriptEvents::POST_INSTALL_CMD, $this->composer, $this->io, false));

Phake::inOrder(
Phake::verify($this->bridgeFactory)->create($this->io),
Phake::verify($this->bridge)->install($this->composer, false)
);
}

Expand Down
60 changes: 55 additions & 5 deletions test/suite/NpmBridgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,51 @@ public function testInstall()

Phake::inOrder(
Phake::verify($this->io)->write('<info>Installing NPM dependencies for root project</info>'),
Phake::verify($this->client)->install(),
Phake::verify($this->client)->install(null, true),
Phake::verify($this->io)->write('<info>Installing NPM dependencies for Composer dependencies</info>'),
Phake::verify($this->io)->write('<info>Installing NPM dependencies for vendorA/packageA</info>'),
Phake::verify($this->client)->install('path/to/vendor/vendora/packagea'),
Phake::verify($this->client)->install('path/to/vendor/vendora/packagea', false),
Phake::verify($this->io)->write('<info>Installing NPM dependencies for vendorB/packageB</info>'),
Phake::verify($this->client)->install('path/to/vendor/vendorb/packageb')
Phake::verify($this->client)->install('path/to/vendor/vendorb/packageb', false)
);
}

public function testInstallProductionMode()
{
$this->rootPackage->setRequires(array($this->linkRoot1, $this->linkRoot2, $this->linkRoot3));
Phake::when($this->vendorFinder)->find($this->composer, $this->bridge)
->thenReturn(array($this->packageA, $this->packageB));
$this->bridge->install($this->composer, false);

Phake::inOrder(
Phake::verify($this->io)->write('<info>Installing NPM dependencies for root project</info>'),
Phake::verify($this->client)->install(null, false),
Phake::verify($this->io)->write('<info>Installing NPM dependencies for Composer dependencies</info>'),
Phake::verify($this->io)->write('<info>Installing NPM dependencies for vendorA/packageA</info>'),
Phake::verify($this->client)->install('path/to/vendor/vendora/packagea', false),
Phake::verify($this->io)->write('<info>Installing NPM dependencies for vendorB/packageB</info>'),
Phake::verify($this->client)->install('path/to/vendor/vendorb/packageb', false)
);
}

public function testInstallRootDevDependenciesInDevMode()
{
$this->rootPackage->setDevRequires(array($this->linkRoot3));
Phake::when($this->vendorFinder)->find($this->composer, $this->bridge)->thenReturn(array());
$this->bridge->install($this->composer, true);

Phake::verify($this->client)->install(null, true);
}

public function testInstallRootDevDependenciesInProductionMode()
{
$this->rootPackage->setDevRequires(array($this->linkRoot3));
Phake::when($this->vendorFinder)->find($this->composer, $this->bridge)->thenReturn(array());
$this->bridge->install($this->composer, false);

Phake::verify($this->client, Phake::never())->install(Phake::anyParameters());
}

public function testInstallNothing()
{
$this->rootPackage->setRequires(array($this->linkRoot1, $this->linkRoot2));
Expand All @@ -108,12 +144,13 @@ public function testUpdate()
Phake::inOrder(
Phake::verify($this->io)->write('<info>Updating NPM dependencies for root project</info>'),
Phake::verify($this->client)->update(),
Phake::verify($this->client)->install(null, true),
Phake::verify($this->client)->shrinkwrap(),
Phake::verify($this->io)->write('<info>Installing NPM dependencies for Composer dependencies</info>'),
Phake::verify($this->io)->write('<info>Installing NPM dependencies for vendorA/packageA</info>'),
Phake::verify($this->client)->install('path/to/vendor/vendora/packagea'),
Phake::verify($this->client)->install('path/to/vendor/vendora/packagea', false),
Phake::verify($this->io)->write('<info>Installing NPM dependencies for vendorB/packageB</info>'),
Phake::verify($this->client)->install('path/to/vendor/vendorb/packageb')
Phake::verify($this->client)->install('path/to/vendor/vendorb/packageb', false)
);
}

Expand All @@ -130,4 +167,17 @@ public function testUpdateNothing()
Phake::verify($this->io)->write('Nothing to install')
);
}

public function testIsDependantPackage()
{
$this->packageA->setRequires(array($this->linkRoot3));
$this->packageB->setDevRequires(array($this->linkRoot3));

$this->assertTrue($this->bridge->isDependantPackage($this->packageA));
$this->assertFalse($this->bridge->isDependantPackage($this->packageB));
$this->assertTrue($this->bridge->isDependantPackage($this->packageA, false));
$this->assertFalse($this->bridge->isDependantPackage($this->packageB, false));
$this->assertTrue($this->bridge->isDependantPackage($this->packageA, true));
$this->assertTrue($this->bridge->isDependantPackage($this->packageB, true));
}
}
11 changes: 11 additions & 0 deletions test/suite/NpmClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ public function testInstall()
);
}

public function testInstallProductionMode()
{
$this->assertNull($this->client->install('/path/to/project', false));
Phake::inOrder(
Phake::verify($this->executableFinder)->find('npm'),
Phake::verify($this->isolator)->chdir('/path/to/project'),
Phake::verify($this->processExecutor)->execute("'/path/to/npm' 'install' '--production'"),
Phake::verify($this->isolator)->chdir('/path/to/cwd')
);
}

public function testInstallFailureNpmNotFound()
{
Phake::when($this->executableFinder)->find('npm')->thenReturn(null);
Expand Down

0 comments on commit 69d6034

Please sign in to comment.