在 npm install 上编译咖啡脚本 [英] Compiling coffeescript on npm install
问题描述
我正在构建一个由 private 组成的应用程序内置在 CoffeeScript 中的 npm repos.为了保持部署语言不可知,并允许每个应用程序指定其 CoffeeScript 版本,我将 CoffeeScript 作为依赖项包含在每个库中,并在安装 npm 时构建到 JavaScript 中.
I'm building an app consisting of private npm repos built in CoffeeScript. To keep deployment language-agnostic, and allow each app to specify its version of CoffeeScript, I'm including CoffeeScript as a dependency in each library, and building into JavaScript upon npm installation.
npm 安装适用于独立存储库,但当我尝试安装依赖于正在构建的另一个存储库的存储库时失败.
npm installation works fine for standalone repos, but fails when I try to install a repo that depends on another repo being built.
所以如果我有 repo-a
,它的 package.json
包括这个:
So if I have repo-a
, whose package.json
includes this:
"dependencies": {
"coffee-script": "~1.2.0"
},
"scripts": {
"install": "./node_modules/coffee-script/bin/cake install"
}
和repo-b
,其package.json
包括:
"dependencies": {
"coffee-script": "~1.2.0",
"repo-a": "git+ssh://git@mydomain.com:myrepo.git"
},
"scripts": {
"install": "./node_modules/coffee-script/bin/cake install"
}
两者都有一个看起来像这样的 Cakefile
,在 npm install
钩子上调用了一个 install
任务:
where both have a Cakefile
that looks like this, with an install
task called on an npm install
hook:
{print} = require "util"
{spawn} = require "child_process"
coffee = "./node_modules/coffee-script/bin/coffee"
echo = (child) ->
child.stdout.on "data", (data) -> print data.toString()
child.stderr.on "data", (data) -> print data.toString()
child
install = (cb) ->
console.log "Building..."
echo child = spawn coffee, ["-c", "-o", "lib", "src"]
child.on "exit", (status) -> cb?() if status is 0
task "install", "Install, build, and test repo", install
npm install
适用于 repo-a
,但对于 repo-b
失败并显示以下消息:
npm install
works for for repo-a
, but fails for repo-b
with this message:
sh: ./node_modules/coffee-script/bin/cake: No such file or directory
此时 node_modules
中存在未完成的 ___coffee-script.npm
目录.
at which point an unfinished ___coffee-script.npm
directory exists in node_modules
.
当然,使用 app.js 会容易得多wrapper,但我需要部署 JavaScript,而不是 CoffeeScript.谁能告诉我如何让它工作?
Of course it would be much easier to use a app.js wrapper, but I need to deploy JavaScript, not CoffeeScript. Can anyone tell me how I could get this to work?
推荐答案
两件事.
- 如果您从 npm 命令运行 cake,您可以指定
cake install
或cake build
作为 scripts.install 字段.这将在本地安装咖啡脚本后运行,并且它的 bin 正确链接(在 Windows 上使用 shim),并将使用 PATH 环境运行,以便本地安装的cake使用 code> 而不是系统路径中的任何其他内容.
- 如果你不是从 npm 命令运行它,但你仍然期望咖啡脚本已经通过 npm 安装在本地(看起来像这样),那么你应该是点击
./node_modules/.bin/cake
或./node_modules/.bin/coffee
而不是深入了解包内部.
- If you're running cake from an npm command, you can just specify
cake install
orcake build
as the scripts.install field. This will run after coffee-script has been installed locally, and its bin linked appropriately (with a shim on windows), and will run with a PATH environ such that the locally installedcake
is used rather than anything else in the system path. - If you're not running this from an npm command, but you are nonetheless expecting that coffee-script has already been installed locally via npm (which it looks like), then you should probably be hitting
./node_modules/.bin/cake
or./node_modules/.bin/coffee
rather than diving into the package internals.
如果你不使用 npm 安装咖啡脚本,而是使用一些 git 子模块或其他东西,那么你就靠自己了 :)
If you are not installing coffee-script with npm, but instead using some git submodules or something, then you're on your own :)
这篇关于在 npm install 上编译咖啡脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!