CNode

有没有当文件时改变自动make的middleware

Gguilin发布于14 年前最后回复14 年前10 回复5903 浏览0 收藏

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

感谢 @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)

S
sumory#1·14 年前

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

J
jiyinyiyong#3·14 年前

搜到 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

J
j4cnodejs#4·14 年前

mark

J
jiyinyiyong#8·14 年前
引用 jiyinyiyong搜到 3 个监视树的 node inotify plusplus directive = { all events: function (ev) { console.log("some thi...

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

X
xqunix#9·14 年前

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

参与回复
登录后即可参与回复。登录