为任何本地依赖项运行安装后挂钩 [英] Run postinstall hook for any local dependency

查看:69
本文介绍了为任何本地依赖项运行安装后挂钩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们有这个:

{
  "scripts":{
    "postinstall":"./scripts/postinstall.sh"
  }
}

然后这个 postinstall 钩子会在我们执行任何操作时运行

Then this postinstall hook will run whenever we do an

$ npm install

在命令行

然而,我想知道的是,当我们安装这样的依赖项时,是否有办法运行 postinstall 钩子

What I am wondering however, is if there is a way to run a postinstall hook when we install a dependency like this

$ npm install x

是否有一些我们可以使用的 NPM 钩子?

Is there some NPM hook that we can use for that?

推荐答案

简短回答 npm 没有内置功能来提供我所知道的这种钩子.

Short answer There's no functionality built-in to npm which provides this kind of hook that I'm aware of.

可能的解决方案,虽然是一个 bash 解决方案,但将使用您自己的自定义逻辑完全覆盖 npm install x 命令.例如:

Possible solution, albeit a bash one, would be to completely override the npm install x command with your own custom logic. For example:

  1. 创建一个 .sh 脚本如下.让我们将文件命名为 custom-npm-install.sh:

  1. Create a .sh script as follows. Lets name the file custom-npm-install.sh:

#!/usr/bin/env bash

npm() {
  if [[ $* == "install "* || $* == "i "* ]]; then

    # When running `$ npm install <name>` (i.e. `$ npm install ...` followed
    # by a space char and some other chars such as a package name - run
    # the command provided.
    command npm "$@"

    # Then run a pseudo `postinstall` command, such as another shell script.
    command path/to/postinstall.sh
  else
    # Run the `$ npm install` command and all others as per normal.
    command npm "$@"
  fi
}

  • 将以下代码段添加到您的 .bash_profile 文件 (注意:您需要定义 custom-npm-install.sh 的实际路径代码>):

  • Add the following snippet to your .bash_profile file ( Note: you'll need to define the actual path to custom-npm-install.sh):

    # Custom logic applied when the `npm install <name>` or the
    # shorthand equivalent `npm i <name>` command is run.
    . path/to/custom-npm-install.sh
    

  • <小时>

    注意事项

    1. 按照上述第二点配置您的 .bash_proile 后,您需要创建一个新的终端会话/窗口才能使其生效.该逻辑将在此后的所有终端会话中有效.

    1. After configuring your .bash_proile as per point two above, you'll need to create a new terminal session/window for it to be effective. The logic will be effective in all terminal sessions thereafter.

    现在,每当您运行 npm install 或许多其他变体,例如:

    Now, whenever you run npm install <name> or the many other variations such as:

    • npm install @
    • npm install <名称><名称>--save-dev
    • npm i -D
    • 等等等等...

    custom-npm-install.sh 将按照正常方式运行命令,然后运行命令 ./scripts/postinstall.sh(即无论随后给出的命令设置为).

    custom-npm-install.sh will run the command as per normal and then run the command ./scripts/postinstall.sh (i.e. whatever the subsequently given command is set to).

    所有其他 npm 命令将正常运行,例如npm install

    All other npm commands will be run as normal, e.g. npm install

    给定 custom-npm-install.sh 当前逻辑 ./scripts/postinstall.sh 将在 npm install 时运行;... 通过 CLI 输入.但是,如果您希望它在安装特定包时ONLY 运行,那么您需要更改 if 语句中的条件逻辑.例如,如果您希望 ./scripts/postinstall.sh 在安装 ONLY 运行rel="nofollow noreferrer">shelljs 然后将 if 语句改为:

    Given custom-npm-install.sh current logic the ./scripts/postinstall.sh will be run whenever npm install <name> ... is entered via the CLI. However, if you wanted it to run ONLY when a specific package is installed then you'll need to change the conditional logic in the if statement. For example if you want ./scripts/postinstall.sh to run ONLY when installing shelljs then change the if statement to:

    if [[ $* == "install "*" shelljs"* || $* == "i "*" shelljs"* ]];
    

    这篇关于为任何本地依赖项运行安装后挂钩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆