diff --git a/src/App.php b/src/App.php index 94539b3..45b819f 100644 --- a/src/App.php +++ b/src/App.php @@ -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; } } diff --git a/src/NodePackageManagers/NodePackageManagerInterface.php b/src/NodePackageManagers/NodePackageManagerInterface.php index ee65d4c..3c3f6bf 100644 --- a/src/NodePackageManagers/NodePackageManagerInterface.php +++ b/src/NodePackageManagers/NodePackageManagerInterface.php @@ -2,6 +2,8 @@ namespace WPEmerge\Cli\NodePackageManagers; +use Symfony\Component\Console\Output\OutputInterface; + interface NodePackageManagerInterface { /** * Check if a package is already installed @@ -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 ); } diff --git a/src/NodePackageManagers/Npm.php b/src/NodePackageManagers/Npm.php index db84d2b..0f79f28 100644 --- a/src/NodePackageManagers/Npm.php +++ b/src/NodePackageManagers/Npm.php @@ -2,6 +2,7 @@ namespace WPEmerge\Cli\NodePackageManagers; +use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Process\Exception\RuntimeException; use WPEmerge\Cli\App; @@ -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 ); } } diff --git a/src/NodePackageManagers/Proxy.php b/src/NodePackageManagers/Proxy.php index 233b6df..d09312d 100644 --- a/src/NodePackageManagers/Proxy.php +++ b/src/NodePackageManagers/Proxy.php @@ -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; @@ -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() ); } /** diff --git a/src/NodePackageManagers/Yarn.php b/src/NodePackageManagers/Yarn.php index 0f256ff..c6bbcc8 100644 --- a/src/NodePackageManagers/Yarn.php +++ b/src/NodePackageManagers/Yarn.php @@ -2,6 +2,7 @@ namespace WPEmerge\Cli\NodePackageManagers; +use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Process\Exception\RuntimeException; use WPEmerge\Cli\App; @@ -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 ); } } diff --git a/src/Presets/Bootstrap.php b/src/Presets/Bootstrap.php index e455573..b9a8815 100644 --- a/src/Presets/Bootstrap.php +++ b/src/Presets/Bootstrap.php @@ -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' ); } } diff --git a/src/Presets/Bulma.php b/src/Presets/Bulma.php index 0b49994..141111d 100644 --- a/src/Presets/Bulma.php +++ b/src/Presets/Bulma.php @@ -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' ); } } diff --git a/src/Presets/FontAwesome.php b/src/Presets/FontAwesome.php index fc05237..6071bd4 100644 --- a/src/Presets/FontAwesome.php +++ b/src/Presets/FontAwesome.php @@ -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' ); } } diff --git a/src/Presets/Foundation.php b/src/Presets/Foundation.php index 41ba184..802aab0 100644 --- a/src/Presets/Foundation.php +++ b/src/Presets/Foundation.php @@ -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' ); } } diff --git a/src/Presets/FrontEndPresetTrait.php b/src/Presets/FrontEndPresetTrait.php index eef2e79..3c33659 100644 --- a/src/Presets/FrontEndPresetTrait.php +++ b/src/Presets/FrontEndPresetTrait.php @@ -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; @@ -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 ); } /** diff --git a/src/Presets/Tachyons.php b/src/Presets/Tachyons.php index d054df9..14b4989 100644 --- a/src/Presets/Tachyons.php +++ b/src/Presets/Tachyons.php @@ -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' ); } }