在npm安装上编译coffeescript [英] Compiling coffeescript on npm install
问题描述
我正在建立一个由私人npm回馈组成的应用程式内置CoffeeScript。为了保持部署语言无关性,并允许每个应用程序指定其版本的CoffeeScript,我将CoffeeScript作为每个库中的依赖项,并在npm安装时构建为JavaScript。
npm安装适用于独立的repos,但是当我尝试安装一个依赖于正在构建的另一个repo的repo时失败。
所以如果我有 repo-a
,其 package.json
包括:
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 安装
钩子上调用安装
任务:
{print} = requireutil
{spawn} = requirechild_process
coffee =./node_modules / coffee-script / bin / coffee
echo =(child) - >
child.stdout.ondata,(data) - > print data.toString()
child.stderr.ondata,(data) - > print data.toString()
child
install =(cb) - >
console.logBuilding ...
echo child = spawn coffee,[-c,-o,lib,src]
child.on退出,(状态) - > cb?()如果status为0
任务install,安装,构建和测试repo,安装
npm install
适用于repo-a
code> repo-b :sh:./node_modules/coffee -script / bin / cake:没有这样的文件或目录
此时未完成的
___ coffee-script.npm
目录存在于node_modules
。
将更容易使用 app.js包装器,但我需要部署JavaScript,而不是CoffeeScript。
解决方案有两件事。
- 如果你从npm命令运行蛋糕,你可以指定
cake install
或build
作为scripts.install字段。这将在本地安装咖啡脚本后运行,并且其bin被正确链接(在窗口上有一个垫片),并且将使用PATH环境运行,以使本地安装的
- 如果您不是从npm命令运行此命令,但是系统路径中使用了你仍然希望咖啡脚本已经通过npm(它看起来像)安装在本地,那么你可能应该击中
./ node_modules / .bin / cake
或者./ node_modules / .bin / coffee
而不是深入包装内部。
如果你不是用npm安装咖啡脚本,而是使用一些git子模块或其他东西,那么你自己:)
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 installation works fine for standalone repos, but fails when I try to install a repo that depends on another repo being built.
So if I have
repo-a
, whosepackage.json
includes this:"dependencies": { "coffee-script": "~1.2.0" }, "scripts": { "install": "./node_modules/coffee-script/bin/cake install" }
and
repo-b
, whosepackage.json
includes this:"dependencies": { "coffee-script": "~1.2.0", "repo-a": "git+ssh://git@mydomain.com:myrepo.git" }, "scripts": { "install": "./node_modules/coffee-script/bin/cake install" }
where both have a
Cakefile
that looks like this, with aninstall
task called on an npminstall
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
works for forrepo-a
, but fails forrepo-b
with this message:sh: ./node_modules/coffee-script/bin/cake: No such file or directory
at which point an unfinished
___coffee-script.npm
directory exists innode_modules
.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?
解决方案Two things.
- 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.If you are not installing coffee-script with npm, but instead using some git submodules or something, then you're on your own :)
这篇关于在npm安装上编译coffeescript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!