有没有当文件时改变自动make的middleware
发布于 3年前 作者 guilin 1439 次浏览

connect-compiler 满足不了我的需求, 编译过程是Makefile写的, 希望当某个文件夹被改变后, 自动执行某个命令. 是否有这样的工具?

感谢 [[[[[[[@jiyinyiyong](/user/jiyinyiyong)](/user/jiyinyiyong)](/user/jiyinyiyong)](/user/jiyinyiyong)](/user/jiyinyiyong)](/user/jiyinyiyong)](/user/jiyinyiyong)

var watch = require('watch')
  , spawn = require('child_process').spawn
  ;

watch.createMonitor('.', { ignoreDotFiles: true }, function (monitor) {
    monitor.on("created", function (f, stat) {
        // Handle file changes
    })
    monitor.on("changed", function (f, curr, prev) {
        if(f == 'Makefile' || /\.jade$/.test(f)) make();
    })
    monitor.on("removed", function (f, stat) {
        // Handle removed files
    })
});

function make() {
  console.log('start make')

  var makeprg = spawn('make')

  makeprg.stdout.on('data', function(data) {
      console.log(data.toString());
  });

  makeprg.stderr.on('data', function(data) {
      console.log(data.toString());
  });

  makeprg.on('exit', function(code) {
      console.log('make done ' + code);
  })
}
10 回复

可以自己写一个的,用fs.watch来监视目录变化,然后执行你写的脚本就好了

fs.watch无法监视文件夹

搜到 3 个监视树的
node-inotify-plusplus

directive = {
    all_events: function (ev) {
      console.log("some things happened: " + ev.masks.toString())
    },
    moved_from: true
}

node-watch-tree

watcher = require('watch-tree').watchTree(path, {'sample-rate': 5});
    watcher.on('fileDeleted', function(path) {
        console.log("Quoth the walrus: Noooo, they're deleting mah " + path + "!");
    });

watch

watch.watchTree('/home/mikeal', function (f, curr, prev) {
  if (typeof f == "object" && prev === null && curr === null) {
    // Finished walking the tree
  } else if (prev === null) {
    // f is a new file
  } else if (curr.nlink === 0) {
    // f was removed
  } else {
    // f was changed
  }
})

Monitoring directory for changes - potential high memory

node_inotify 在mac下npm install 失败, 不解

试试 watch, mikeal 这位作者有很多优秀的作品.

@guilin 我也只是搜到网页, npm search 里面只有 inotify-plusplus
页面的文档只提到编译, 怀疑只能编译

对于这类问题,我一般是用 ruby 的 guard 来解决的,定义规则非常方便,而且已有大量的第三方资源可供使用。它应当是目前监控类应用中最为成熟的,强烈推荐。不过使用前提是得熟悉一下 ruby 的基本语法。

ruby在CI等方面的确是非常领先. 收藏.

回到顶部