咕噜咕噜的表变化并重新启动咖

时间:2014-07-03 08:57:57

标签: coffeescript gruntjs grunt-contrib-watch grunt-contrib-coffee

我正在设置一个我正在尝试的Grunt文件:

  1. 某些 coffeescript编译为客户端的javascript。
  2. 关注要为客户端编译为javascript的coffeescript的更改。
  3. 观察后端(服务器)coffeescript文件的更改,并在找到更改后重新启动咖啡应用程序。
  4. 我有前两个步骤使用它:

    module.exports = (grunt) ->
      grunt.initConfig
        pkg: grunt.file.readJSON 'package.json'
        coffee:
          compile:
            expand: true
            flatten: true
            cwd: 'public/src'
            src: ['*.coffee']
            dest: 'public/dist'
            ext: '.js'
        watch:
          coffee:
            files: ['public/src/*.coffee']
            tasks: ['coffee']
    
      grunt.loadNpmTasks 'grunt-contrib-coffee'
      grunt.loadNpmTasks 'grunt-contrib-watch'
      grunt.registerTask 'default', ['coffee', 'watch']
    

    但我不确定如何做第三步。

    目录结构目前如下所示:

    app
      lib.coffee
      routes.coffee
    public/
      dist/
        client.js
      src/
        client.coffee
    Gruntfile.coffee
    package.json
    server.coffee
    

    我如何观察app目录或server.coffee文件中的任何内容的更改,并使用grunt自动启动服务器(例如'coffee server.coffee')?

    此外,服务器使用快递 - 重新启动应用程序需要在启动之前查看该端口是否再次可用?

1 个答案:

答案 0 :(得分:1)

管理以最终实现这一目标:

module.exports = (grunt) ->
  grunt.initConfig
    pkg: grunt.file.readJSON 'package.json'
    coffee:
      compile:
        expand: true
        flatten: true
        cwd: 'public/src'
        src: ['*.coffee']
        dest: 'public/dist'
        ext: '.js'
    watch:
      coffee:
        files: ['public/src/*.coffee']
        tasks: ['coffee']
      express:
        files: ['server.coffee']
        tasks: ['express:dev']
        options:
          spawn: false
    express:
      dev:
        options:
          script: 'server.coffee'
          opts: ['/path/to/coffee']
          #port: 8080

  grunt.loadNpmTasks 'grunt-contrib-coffee'
  grunt.loadNpmTasks 'grunt-contrib-watch'
  grunt.loadNpmTasks 'grunt-express-server'
  grunt.registerTask 'default', ['coffee', 'express:dev', 'watch']