php 使用 Composer 在生产环境中安装 npm 和 bower 包(即没有 devDependencies)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25122187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Composer to install npm and bower packages on production (i.e. no devDependencies)
提问by Dan B
In my composer.json file I have the following in the scripts section:
在我的 composer.json 文件中,我的脚本部分包含以下内容:
"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')
运行“composer install”时,这将导致 npm 和 bower 安装它们的所有依赖项,默认情况下包括 devDependencies。在进行生产部署时(例如“composer install --no-dev”,我想启动“npm install --production”和“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.
据我所知,似乎没有一种方法可以根据传递的标志更改为“post-install-command”指定的列表,也没有一种方法可以设置变量,然后可以将这些变量传递给 post 中的命令-安装-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.
我错过了什么吗?似乎不可能使用 Composer 仅使用配置来进行开发和生产安装。我真的必须composer install --no-scripts
在生产中使用,然后自己手动运行所有四个命令吗?这似乎有点笨拙。
回答by kfriend
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.
您可以随时使用 PHP 为您进行环境检测,然后从同一脚本安装其他依赖项。这不是很好也很干净,就像在 post-install-cmd 中包含 npm 和 bower 一样,但它会给你你想要的东西。
"post-install-cmd": [
"php artisan clear-compiled",
"php artisan optimize",
"php path/to/installer.php"
]
Example installer.php:
示例 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.
您可以使这个示例更加健壮,甚至可以为它创建一个 Artisan 命令。
回答by Laurence
This would work;
这行得通;
"post-update-cmd": [
"php artisan clear-compiled",
"php artisan optimize",
"npm install",
"bower install"
],
"post-install-cmd": [
"php artisan clear-compiled",
"php artisan optimize",
"npm install --production",
"bower install --production"
]
i.e. you should be running 'update' on your dev environment, and only ever run 'install' on your production environment.
即您应该在您的开发环境中运行“更新”,并且只在您的生产环境中运行“安装”。
回答by dave1010
It's a bit of hack but you can get the parent command's PID with $PPID
in bash. From that you can get the commandline arguments.
这有点小技巧,但您可以$PPID
在 bash 中获取父命令的 PID 。从中您可以获得命令行参数。
"post-install-cmd": [
"ps -ocommand= -p $PPID | grep no-dev > /dev/null && echo called with no-dev || echo called without no-dev",
],
If you were to do this, I'd put it in a bash script and run it like this: run-if-env-is-production.sh "bower install --production"
如果你要这样做,我会把它放在一个 bash 脚本中并像这样运行它: run-if-env-is-production.sh "bower install --production"
I would recommend @kwoodfriend's solution over this though, as this is less portable as it requires bash, ps & grep.
不过,我会推荐 @kwoodfriend 的解决方案,因为它需要 bash、ps 和 grep,因此便携性较差。
回答by loranger
Because I use npm, bower and gulp to build my assets, I don't need any of them on production, so here is the composer.json I use:
因为我使用 npm、bower 和 gulp 来构建我的资产,所以我在生产中不需要它们中的任何一个,所以这里是我使用的 composer.json:
"post-install-cmd": [
"php artisan clear-compiled",
"php artisan optimize",
"(ps -ocommand= -p $PPID | grep -q no-dev && true) || npm --loglevel silent install",
"(ps -ocommand= -p $PPID | grep -q no-dev && true) || npm --loglevel silent install --global bower",
"(ps -ocommand= -p $PPID | grep -q no-dev && true) || bower --silent install"
],
"post-update-cmd": [
"php artisan clear-compiled",
"php artisan optimize",
"(ps -ocommand= -p $PPID | grep -q no-dev && true) || npm --loglevel silent update",
"(ps -ocommand= -p $PPID | grep -q no-dev && true) || bower --silent update"
]
... But it looks strange to me to use such a tricky command in order to detect dev environment, or not...
......但我觉得使用这样一个棘手的命令来检测开发环境很奇怪,或者不......
回答by pliashkou
The whole block example from Composer.org
来自Composer.org的整个块示例
{
"scripts": {
"post-update-cmd": "MyVendor\MyClass::postUpdate",
"post-package-install": [
"MyVendor\MyClass::postPackageInstall"
],
"post-install-cmd": [
"MyVendor\MyClass::warmCache",
"phpunit -c app/"
],
"post-create-project-cmd" : [
"php -r \"copy('config/local-example.php', 'config/local.php');\""
]
}
}