Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Composer to install npm and bower packages on production (i.e. no devDependencies)

In my composer.json file I have the following in the scripts section:

    "post-install-cmd": [
        "php artisan clear-compiled",
        "php artisan optimize",
        "npm install",
        "bower install"
    ]

When running 'composer install' this will cause npm and bower to install all their dependencies, which by default include devDependencies. When it comes to doing a production rollout (e.g. 'composer install --no-dev' I want to fire up 'npm install --production' and 'bower install --production')

As far as I can tell, there doesn't seem to be a way to either change the list specified for 'post-install-command' depending on flags passed, or a way of setting variables that can then be passed to commands in post-install-cmd.

Am I missing something? It doesn't seem possible to use composer to do both a dev and production install using just the config. Do I really have to use composer install --no-scripts on production and then manually run all four of the commands myself? That seems a little clunky.

like image 519
Dan B Avatar asked Aug 04 '14 15:08

Dan B


People also ask

How to manage Bower and npm packages using composer?

Let's check another option - how to manage Bower and npm packages using Composer. In a lot of cases CSS/JavaScript libraries use Bower as a package manager. If you use composer + robloach/component-installer, then you will need to register those libraries in your composer.json as package. You will get enourmous list in the end.

What does Composer install-no-Dev do?

Show activity on this post. When running 'composer install' this will cause npm and bower to install all their dependencies, which by default include devDependencies. When it comes to doing a production rollout (e.g. 'composer install --no-dev' I want to fire up 'npm install --production' and 'bower install --production')

What does'Composer install--no-Dev'do?

When running 'composer install' this will cause npm and bower to install all their dependencies, which by default include devDependencies. When it comes to doing a production rollout (e.g. 'composer install --no-dev' I want to fire up 'npm install --production' and 'bower install --production')

How do I deploy composer to production server?

As the most accepted way to deploy is to push the composer.lock (that holds your current composer setup) and then do an composer install on the production server, this will also install the development stuff.


1 Answers

You could always make use of PHP to do environment detection for you, then install other dependencies from the same script. This isn't nice and clean, like including npm and bower in post-install-cmd, but it will get you what you're looking for.

"post-install-cmd": [
     "php artisan clear-compiled",
     "php artisan optimize",
     "php path/to/installer.php"
 ]

Example installer.php:

// Logic to determine the environment. This could be determined many ways, and depends on how your
// application's environment is determined. If you're making use of Laravel's environment
// capabilities, you could do the following:
$env = trim(exec('php artisan env'));

// Clean up response to get the value we actually want
$env = substr($env, strrpos($env, ' ') + 1);

$envFlag = ($env === 'production')
    ? '--production'
    : '';

// Install npm
passthru("npm install {$envFlag}");
// Install bower
passthru("bower install {$envFlag}");

You could make this example more robust, and even create an Artisan command for it.

like image 179
kfriend Avatar answered Oct 16 '22 01:10

kfriend