Skip to content

Commit

Permalink
provide better feedback when installing front-end presets
Browse files Browse the repository at this point in the history
  • Loading branch information
atanas-dev committed Jul 27, 2018
1 parent 43e0942 commit da81488
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 71 deletions.
27 changes: 25 additions & 2 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,35 @@ public static function execute( $command, $directory = null, $timeout = 120 ) {

$process = new Process( $command, null, null, null, $timeout );
$process->setWorkingDirectory( $directory );
$process->run();
$process->mustRun();

return $process->getOutput();
}

/**
* Run a shell command and return the output as it comes in
*
* @param string $command
* @param OutputInterface $command
* @param string|null $directory
* @param integer $timeout
* @return Process
*/
public static function liveExecute( $command, OutputInterface $output, $directory = null, $timeout = 120 ) {
$directory = $directory !== null ? $directory : getcwd();

$process = new Process( $command, null, null, null, $timeout );
$process->setWorkingDirectory( $directory );
$process->start();

$process->wait( function( $type, $buffer ) use ( $output ) {
$output->writeln( $buffer );
} );

if ( ! $process->isSuccessful() ) {
throw new ProcessFailedException( $process );
}

return $process->getOutput();
return $process;
}
}
26 changes: 15 additions & 11 deletions src/NodePackageManagers/NodePackageManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace WPEmerge\Cli\NodePackageManagers;

use Symfony\Component\Console\Output\OutputInterface;

interface NodePackageManagerInterface {
/**
* Check if a package is already installed
Expand All @@ -15,21 +17,23 @@ public function installed( $directory, $package );
/**
* Install a package
*
* @param string $directory
* @param string $package
* @param string|null $version
* @param boolean $dev
* @return string
* @param string $directory
* @param OutputInterface $output
* @param string $package
* @param string|null $version
* @param boolean $dev
* @return void
*/
public function install( $directory, $package, $version = null, $dev = false );
public function install( $directory, OutputInterface $output, $package, $version = null, $dev = false );

/**
* Uninstall a package
*
* @param string $directory
* @param string $package
* @param boolean $dev
* @return string
* @param string $directory
* @param OutputInterface $output
* @param string $package
* @param boolean $dev
* @return void
*/
public function uninstall( $directory, $package, $dev = false );
public function uninstall( $directory, OutputInterface $output, $package, $dev = false );
}
13 changes: 5 additions & 8 deletions src/NodePackageManagers/Npm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace WPEmerge\Cli\NodePackageManagers;

use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Exception\RuntimeException;
use WPEmerge\Cli\App;

Expand Down Expand Up @@ -29,26 +30,22 @@ public function installed( $directory, $package ) {
/**
* {@inheritDoc}
*/
public function install( $directory, $package, $version = null, $dev = false ) {
public function install( $directory, OutputInterface $output, $package, $version = null, $dev = false ) {
$command = 'npm install ' .
'"' . $package .( $version !== null ? '@' . $version : '' ) . '"' .
( $dev ? ' --only=dev' : '' );

$output = App::execute( $command, $directory );

return trim( $output );
App::liveExecute( $command, $output, $directory );
}

/**
* {@inheritDoc}
*/
public function uninstall( $directory, $package, $dev = false ) {
public function uninstall( $directory, OutputInterface $output, $package, $dev = false ) {
$command = 'npm uninstall ' .
$package .
( $dev ? ' --only=dev' : '' );

$output = App::execute( $command, $directory );

return trim( $output );
App::liveExecute( $command, $output, $directory );
}
}
10 changes: 5 additions & 5 deletions src/NodePackageManagers/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace WPEmerge\Cli\NodePackageManagers;

use Exception;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Exception\RuntimeException;
use WPEmerge\Cli\App;
Expand All @@ -18,15 +18,15 @@ public function installed( $directory, $package ) {
/**
* {@inheritDoc}
*/
public function install( $directory, $package, $version = null, $dev = false ) {
return call_user_func_array( [$this->getNodePackageManager(), 'install'], func_get_args() );
public function install( $directory, OutputInterface $output, $package, $version = null, $dev = false ) {
call_user_func_array( [$this->getNodePackageManager(), 'install'], func_get_args() );
}

/**
* {@inheritDoc}
*/
public function uninstall( $directory, $package, $dev = false ) {
return call_user_func_array( [$this->getNodePackageManager(), 'uninstall'], func_get_args() );
public function uninstall( $directory, OutputInterface $output, $package, $dev = false ) {
call_user_func_array( [$this->getNodePackageManager(), 'uninstall'], func_get_args() );
}

/**
Expand Down
13 changes: 5 additions & 8 deletions src/NodePackageManagers/Yarn.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace WPEmerge\Cli\NodePackageManagers;

use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Exception\RuntimeException;
use WPEmerge\Cli\App;

Expand Down Expand Up @@ -29,26 +30,22 @@ public function installed( $directory, $package ) {
/**
* {@inheritDoc}
*/
public function install( $directory, $package, $version = null, $dev = false ) {
public function install( $directory, OutputInterface $output, $package, $version = null, $dev = false ) {
$command = 'yarn add ' .
'"' . $package .( $version !== null ? '@' . $version : '' ) . '"' .
( $dev ? ' --dev' : '' );

$output = App::execute( $command, $directory );

return trim( $output );
App::liveExecute( $command, $output, $directory );
}

/**
* {@inheritDoc}
*/
public function uninstall( $directory, $package, $dev = false ) {
public function uninstall( $directory, OutputInterface $output, $package, $dev = false ) {
$command = 'yarn remove ' .
$package .
( $dev ? ' --dev' : '' );

$output = App::execute( $command, $directory );

return trim( $output );
App::liveExecute( $command, $output, $directory );
}
}
7 changes: 1 addition & 6 deletions src/Presets/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@ public function getName() {
* {@inheritDoc}
*/
public function execute( $directory, OutputInterface $output ) {
$install_output = $this->installNodePackage( $directory, 'bootstrap', '^4.0', true );

if ( $output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE ) {
$output->writeln( $install_output );
}

$this->installNodePackage( $directory, $output, 'bootstrap', '^4.0', true );
$this->addCssVendorImport( $directory, 'bootstrap/dist/css/bootstrap.css' );
}
}
7 changes: 1 addition & 6 deletions src/Presets/Bulma.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@ public function getName() {
* {@inheritDoc}
*/
public function execute( $directory, OutputInterface $output ) {
$install_output = $this->installNodePackage( $directory, 'bulma', '^0.6', true );

if ( $output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE ) {
$output->writeln( $install_output );
}

$this->installNodePackage( $directory, $output, 'bulma', '^0.6', true );
$this->addCssVendorImport( $directory, 'bulma/css/bulma.css' );
}
}
7 changes: 1 addition & 6 deletions src/Presets/FontAwesome.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@ public function getName() {
* {@inheritDoc}
*/
public function execute( $directory, OutputInterface $output ) {
$install_output = $this->installNodePackage( $directory, 'font-awesome', '^4.7', true );

if ( $output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE ) {
$output->writeln( $install_output );
}

$this->installNodePackage( $directory, $output, 'font-awesome', '^4.7', true );
$this->addCssVendorImport( $directory, 'font-awesome/css/font-awesome.css' );
}
}
7 changes: 1 addition & 6 deletions src/Presets/Foundation.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@ public function getName() {
* {@inheritDoc}
*/
public function execute( $directory, OutputInterface $output ) {
$install_output = $this->installNodePackage( $directory, 'foundation-sites', '^6.4', true );

if ( $output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE ) {
$output->writeln( $install_output );
}

$this->installNodePackage( $directory, $output, 'foundation-sites', '^6.4', true );
$this->addCssVendorImport( $directory, 'foundation-sites/dist/css/foundation.css' );
}
}
16 changes: 9 additions & 7 deletions src/Presets/FrontEndPresetTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace WPEmerge\Cli\Presets;

use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Exception\RuntimeException;
use WPEmerge\Cli\NodePackageManagers\Proxy;

Expand All @@ -11,20 +12,21 @@ trait FrontEndPresetTrait {
/**
* Install a node package
*
* @param string $directory
* @param string $package
* @param string|null $version
* @param boolean $dev
* @return string
* @param string $directory
* @param OutputInterface $directory
* @param string $package
* @param string|null $version
* @param boolean $dev
* @return void
*/
protected function installNodePackage( $directory, $package, $version = null, $dev = false ) {
protected function installNodePackage( $directory, OutputInterface $output, $package, $version = null, $dev = false ) {
$package_manager = new Proxy();

if ( $package_manager->installed( $directory, $package ) ) {
throw new RuntimeException( 'Package is already installed.' );
}

return $package_manager->install( $directory, $package, $version, $dev );
$package_manager->install( $directory, $output, $package, $version, $dev );
}

/**
Expand Down
7 changes: 1 addition & 6 deletions src/Presets/Tachyons.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@ public function getName() {
* {@inheritDoc}
*/
public function execute( $directory, OutputInterface $output ) {
$install_output = $this->installNodePackage( $directory, 'tachyons', '^4.9', true );

if ( $output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE ) {
$output->writeln( $install_output );
}

$this->installNodePackage( $directory, $output, 'tachyons', '^4.9', true );
$this->addCssVendorImport( $directory, 'tachyons/css/tachyons.css' );
}
}

0 comments on commit da81488

Please sign in to comment.