问题描述
问:是否可以更改npm运行脚本的上下文?
Q: Is it possible to change the the context in which npm runs scripts?
我想要的是以下内容:
"scripts": {
"test": "gulp mocha",
"pre-install": "./deps/2.7/cpython/configure --prefix=$(pwd)/build --exec-prefix=$(pwd)/build && make -C deps/2.7/cpython && make -C deps/2.7/cpython install",
"install": "node-gyp rebuild"
},
很显然,cd deps/2.7/cpython/ && ./configure
可以在类似UNIX的系统上运行,但不能在Windows上运行.
Obviously cd deps/2.7/cpython/ && ./configure
would work on UNIX-like systems but not on windows.
原因:问题的根源是python repo的configure
命令将文件输出到调用它的目录中.但是,这些文件的构建与make
和make install
有关,它们在存储库目录中查找文件.
Why: The root of the problem is, that the configure
command of the python repo outputs files into the directory where it is called. The files however are build relevant for make
and make install
which look for the files in the directory of the repo.
在这种情况下,我无法更改Makefile
,因为可以理解的是Python的构建过程很复杂.
In this case I can't change the Makefile
since the build process of Python is understandably complex.
替代方法:替代方法可能是编写一些install.js
并使用节点的操作系统独立API和某些child_process.exec()
,我可能会这样做.但是,不离开 npm 真的很好.
Alternative: The alternative is probably to write some install.js
and use node's OS independent API and some child_process.exec()
, which I am probably gonna do. However, not leaving npm would be really nice.
推荐答案
npm
仅允许执行cd dir && command -args
,它也将在Windows上运行.
npm
allows only to do cd dir && command -args
, which will also run on Windows.
在PR https://github.com/npm中进行了更改,以使用node
的生成功能. /npm/pull/10958 ,但由于上述解决方案而被拒绝.
A change to use node
's spawn functionality has been made in PR https://github.com/npm/npm/pull/10958, but was rejected, due to the above solution.
这篇关于更改npm脚本的工作目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!