【问题标题】:How can I have my npx script run commands within the same package/repo?如何让我的 npx 脚本在同一个包/存储库中运行命令?
【发布时间】:2019-01-18 21:59:41
【问题描述】:

假设我创建了一个 my-npx-hello-world 存储库

它有以下文件

my-npx-hello-world/
      foo.sh
      index.sh
      package.json

package.json 有

  "bin": "./index.sh"

index.sh 调用 ./foo.sh 即

#!/bin/sh

echo "running foo"
./foo.sh

现在,当我尝试运行它时

# npx my-npx-hello-world
running foo
line 4: ./foo.sh: No such file or directory

那是因为它在我的 CWD 中而不是在 my-npx-hello-world 存储库中寻找 foo.sh。

我需要做什么才能让 index.sh 在同一个 repo 中执行 foo.sh 脚本。

【问题讨论】:

    标签: npx


    【解决方案1】:

    回答我自己的问题,我通过找到如下 CWD 解决了这个问题

    BASEDIR=$(dirname "$0")/../lib/node_modules/my-npx-hello-worl
    

    然后

    cd $BASEDIR
    ./foo.sh
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-13
      • 1970-01-01
      • 2014-06-14
      • 2019-11-30
      • 2014-03-29
      • 2014-03-08
      • 2022-08-08
      相关资源
      最近更新 更多