简体   繁体   English

通过gulp-nodemon传递CLI参数

[英]Passing CLI arguments through gulp-nodemon

I am using the npm package gulp-nodemon to start my webserver. 我正在使用npm包gulp-nodemon来启动我的web服务器。

The problem I am having is that I cannot pass CLI arguments to my server script. 我遇到的问题是我无法将CLI参数传递给我的服务器脚本。

I would like to write something like this: 我想写这样的东西:

gulp --argument1 value1

And the nodemon then should call coffee server.coffee --argument1 value1 然后nodemon应该调用coffee server.coffee --argument1 value1

My current nodemon task: 我当前的nodemon任务:

# nodemon development server
gulp.task 'nodemonServer',  () ->
  nodemon({
    script: 'server.coffee'
    ext: 'coffee'
    watch: ['server/', 'server.coffee']
    env: { 'NODE_ENV': 'development' }
  })
  .on 'crash', () ->
    notifyServerError()

I tried writing script: 'server.coffee --argument1 value1' but I am getting a strange error: File not found: C:\\Users\\user\\projectRoot\\"server.coffee 我尝试编写script: 'server.coffee --argument1 value1'但我收到一个奇怪的错误: File not found: C:\\Users\\user\\projectRoot\\"server.coffee

Yes, with the " in the file path. Here is the log: 是的,使用“在文件路径中。这是日志:

[13:13:10] [nodemon] starting `coffee.cmd "server.coffee --argument1 value1"`
File not found: C:\Users\user\projectRoot\"server.coffee

Thanks. 谢谢。

Use args to pass any arguments for coffee command. 使用args传递coffee命令的任何参数。 This are the agruments passed AFTER the script filename. 这是脚本文件名后传递的agruments。

Use exec to pass any coffee options along with the coffee executable. 使用exec传递任何咖啡选项以及coffee可执行文件。 These are passed BEFORE script filename. 这些是通过BEFORE脚本文件名传递的。

var gulp = require('gulp');
var nodemon = require('gulp-nodemon');
gulp.task('nodemonServer', function () {
  nodemon({
    script: 'server.coffee',
    exec: 'coffee -p',
    args: ['--argument','value1'],
    ext: 'coffee'
  , env: { 'NODE_ENV': 'development' }
  })
})

To see the actual command used by nodemon, use DEBUG=nodemon 要查看nodemon使用的实际命令,请使用DEBUG=nodemon

DEBUG=nodemon gulp nodemonServer

which logs: 哪些日志:

  nodemon bus new listener: reset (0) +0ms
  nodemon bus new listener: reset (0) +6ms
  ...
[13:47:20] [nodemon] starting `coffee -p server.coffee --arguments value1`
  nodemon spawning +0ms coffee -p server.coffee --arguments value1

According to some samples in the gulp-nodemon repository, you can use: 根据gulp-nodemon存储库中的一些示例,您可以使用:

nodeArgs: ['--some-arg']

For example: 例如:

# nodemon development server
gulp.task 'nodemonServer',  () ->

  nodemon({
    script: 'server.coffee'
    ext: 'coffee'
    watch: ['server/', 'server.coffee']
    env: { 'NODE_ENV': 'development' }
    nodeArgs: ['--argument1', 'value1']
  })

  .on 'crash', () ->
    notifyServerError()

Source: Example gulpfile from gulp-nodemon 来源: gulp-nodemon的示例gulpfile

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM