在npm install上编译coffeescript

Jed*_*idt 12 node.js coffeescript npm

我正在构建一个由使用CoffeeScript构建的私有npm repos组成的应用程序.为了保持与部署语言无关,并允许每个应用程序指定其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"
}
Run Code Online (Sandbox Code Playgroud)

而且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"
}
Run Code Online (Sandbox Code Playgroud)

两者都有Cakefile这样的东西,install在npm install钩子上调用一个任务:

{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
Run Code Online (Sandbox Code Playgroud)

npm install适用于for repo-a,但对于repo-b此消息失败:

sh: ./node_modules/coffee-script/bin/cake: No such file or directory
Run Code Online (Sandbox Code Playgroud)

此时___coffee-script.npm存在未完成的目录node_modules.

当然,使用app.js包装器要容易得多,但我需要部署JavaScript,而不是CoffeeScript.谁能告诉我如何才能让它发挥作用?

isa*_*acs 9

两件事情.

  1. 如果您从npm命令运行cake,则可以指定cake installcake build作为scripts.install字段.这将本地安装coffee-script并且其bin已正确链接(在Windows上使用垫片)之后运行,并且将以PATH环境运行,以便使用本地安装cake而不是系统路径中的任何其他内容.
  2. 如果你没有从npm命令运行它,但是你仍然期望咖啡脚本已经通过npm本地安装(它看起来像),那么你应该打击./node_modules/.bin/cake或者./node_modules/.bin/coffee不要潜入包内部.

如果您没有使用npm安装coffee-script,而是使用一些git子模块或其他东西,那么你就是自己的:)