问题描述
在我的composer.json文件中,脚本部分中包含以下内容:
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"
]
运行 composer install时,这将导致npm和bower安装其所有依赖项,默认情况下包括devDependencies。在进行生产部署时(例如, composer install --no-dev,我想启动 npm install --production和 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')
据我所知,似乎没有办法根据传递的标志来更改为 post-install-command指定的列表,也没有办法设置可以传递给post-install-cmd中的命令。
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.
我错过了什么吗?似乎不可能仅使用config来使用composer进行开发和生产安装。我真的必须在生产中使用 composer install --no-scripts
然后自己亲自运行所有四个命令吗?似乎有点笨拙。
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.
推荐答案
您总是可以使用PHP为您进行环境检测,然后从中安装其他依赖项相同的脚本。这不是很好,也不干净,例如在post-install-cmd中包含npm和bower,但是它将为您提供所需的内容。
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"
]
示例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}");
您可以使此示例更强大,甚至为其创建Artisan命令。
You could make this example more robust, and even create an Artisan command for it.
这篇关于使用Composer在生产环境上安装npm和bower软件包(即没有devDependencies)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!