提问者:小点点

使用Composer在生产环境中安装npm和bower软件包(即无devDependencies)


在我的作曲家。json文件我在脚本部分有以下内容:

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

运行“composer install”时,这将导致npm和bower安装其所有依赖项,默认情况下包括devdependency。当涉及到制作卷展时(例如“composer install--no dev”,我想启动“npm install--production”和“bower install--production”)

据我所知,似乎没有一种方法可以根据传递的标志来更改为“安装后命令”指定的列表,也没有一种方法可以设置变量,然后这些变量可以传递给安装后命令中的命令。

我错过什么了吗?似乎不可能使用composer仅使用配置就同时进行开发和生产安装。我真的必须在生产环境中使用composer-install--no-scripts,然后自己手动运行所有四个命令吗?那似乎有点笨重。


共3个答案

匿名用户

您可以始终使用PHP为您执行环境检测,然后从同一脚本安装其他依赖项。这不是很好和干净,就像在安装后的cmd中包含npm和bower一样,但它会让你得到你想要的东西。

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

示例安装程序。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命令。

匿名用户

这是可行的;

"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"
]

也就是说,您应该在您的开发环境上运行更新,并且只在正式生产环境上运行安装。

匿名用户

这有点像黑客,但是您可以在bash中使用$PPID获取父命令的PID。从中可以得到命令行参数。

"post-install-cmd": [
    "ps -ocommand= -p $PPID | grep no-dev > /dev/null && echo called with no-dev || echo called without no-dev",
],

如果要执行此操作,我会将其放入bash脚本中,并按如下方式运行:runif env是production。sh“bower安装--生产”

不过,我还是推荐@kwoodfriend的解决方案,因为它需要bash,ps,所以移植性较差