如何在 npm 脚本中以跨平台兼容的方式使用 ~ (home dir)?
How can I use ~ (home dir) in a cross-platform compatible way in an npm script?
我正在尝试将 ~/.aws
复制到我的 docker 容器中。但是我在 Windows 机器 运行ning git-bash
上,这似乎导致了一些问题。
到目前为止我尝试了两件事:
"d-aws-copy": "docker cp ~/.aws constraint-listener:/usr/src/app"
结果:GetFileAttributesEx C:\work\project-name\~: The system cannot find the file specified.
"d-aws-copy": "docker cp /c/Users/user-name-here/.aws constraint-listener:/usr/src/app",
结果:GetFileAttributesEx C:\c: The system cannot find the file specified.
我见过的所有涉及波浪号扩展的解决方案都是针对节点代码的,而不是针对命令行 npm 脚本的。
如果我从 bash 运行 docker cp ~/.aws constraint-listener:/usr/src/app
它工作正常。但是在 npm 脚本内部它会崩溃,如上所示。
如何让 ~
在 Windows 机器上的 npm 脚本中工作?
解决方法是直接调用bash
:
"d-copy-aws": "bash -c \"docker cp ~/.aws constraint-listener:/root\" && echo aws credentials copied",
从 Node 调用时,您使用的是标准 shell,可能是 Windows 上的 CMD。它不会对波浪号进行字符串插值。使用平台自变量代替或检测平台并使用 process.env 查找 $HOME 或 $WINHOME 的值 - 取决于 OS.
编辑:没有看到它是一个 NPM 脚本...错过了标签。以为题中提到了Node.
我正在尝试将 ~/.aws
复制到我的 docker 容器中。但是我在 Windows 机器 运行ning git-bash
上,这似乎导致了一些问题。
到目前为止我尝试了两件事:
"d-aws-copy": "docker cp ~/.aws constraint-listener:/usr/src/app"
结果:GetFileAttributesEx C:\work\project-name\~: The system cannot find the file specified.
"d-aws-copy": "docker cp /c/Users/user-name-here/.aws constraint-listener:/usr/src/app",
结果:GetFileAttributesEx C:\c: The system cannot find the file specified.
我见过的所有涉及波浪号扩展的解决方案都是针对节点代码的,而不是针对命令行 npm 脚本的。
如果我从 bash 运行 docker cp ~/.aws constraint-listener:/usr/src/app
它工作正常。但是在 npm 脚本内部它会崩溃,如上所示。
如何让 ~
在 Windows 机器上的 npm 脚本中工作?
解决方法是直接调用bash
:
"d-copy-aws": "bash -c \"docker cp ~/.aws constraint-listener:/root\" && echo aws credentials copied",
从 Node 调用时,您使用的是标准 shell,可能是 Windows 上的 CMD。它不会对波浪号进行字符串插值。使用平台自变量代替或检测平台并使用 process.env 查找 $HOME 或 $WINHOME 的值 - 取决于 OS.
编辑:没有看到它是一个 NPM 脚本...错过了标签。以为题中提到了Node.