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.
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.
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')
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 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With