利用Node的chokidar 监听文件改变的文件。

最近维护一个项目。每次改完东西,都要上传到服务器。然后有时候就忘记一些东西,于是就想有没有可以方法能监听文件的改变。然后我再利用程序把更改的文件一键上传到服务器。

于是就找到了nodejs 的chokidar模块。 然后利用redis的set集合。因为我们频繁更改。如果用普通的字符 会增加比较。set是一个集合,里面的元素都是不重复的。正好可以利用这个特性。帮我们记录更改的记录文件。删除的时候,然后删掉set中的文件。目前只做了增加或修改的文件提交,如果本地删除的 不会同步到服务器。

监听本地文件的代码如下

const chokidar = require('chokidar')
const redis = require("redis"),
    client = redis.createClient();

let watcher = null
let ready = false
const key = "modifyFiles";
let watch = function (filepath) {
  // 文件新增时
  function addFileListener(path_) {
    if (ready) {
      client.sadd(key,path_);
      console.log('文件', path_, 'has been added')
    }
  }
  function addDirecotryListener(path_) {
    if (ready) {
      console.log('目录', path_, 'has been added')
    }
  }

  // 文件内容改变时
  function fileChangeListener(path_) {
      client.sadd(key,path_);
      console.log('文件', path_, '已经修改')
  }

  // 删除文件
  function fileRemovedListener(path_) {
      client.srem(key,path_)
      console.log('文件', path_, '被删除了')
  }

  // 删除目录时
  function directoryRemovedListener(path_) {
    console.info('目录', path_, '被删除了')
  }

  if (!watcher) {
    watcher = chokidar.watch(filepath)
  }
  watcher
    .on('add', addFileListener)
    .on('addDir', addDirecotryListener)
    .on('change', fileChangeListener)
    .on('unlink', fileRemovedListener)
    .on('unlinkDir', directoryRemovedListener)
    .on('error', function (error) {
      console.info('发生了错误:', error);
    })
    .on('ready', function () {
      console.info('准备监听');
      ready = true
    })
}
watch("E:\work\www.cccc.com\")

上传到服务器

上传到服务器:我们需要读取set集合中的数据,然后将本地的目录和远程目录映射,利用sftp传输到指定的文件。代码如下

 1 var path, fs, redis, client, node_ssh, ssh
 2 node_ssh = require('node-ssh')
 3 ssh = new node_ssh()
 4 fs = require('fs')
 5 path = require('path')
 6 redis = require("redis")
 7 client = redis.createClient()
 8 
 9 //redis 集合key
10 const key = "modifyFiles";
11 
12 //远程目录
13 const remote_path = "/home/wwwroot/www.ccc.com/";
14 
15 //本地目录
16 const local_path = "E:/work/www.ccc.com/"
17 
18 let uploadFiles = [{
19   "local": "",
20   "remote": ""
21 }];
22 
23 //远程server信息
24 const serverConfig = {
25   host: 'x.x.x.x',
26   username: 'admin',
27   password: 'admin',
28   port: 22
29 }
30 //读取redis中的set
31 client.smembers(key,  (error, data)=> {
32   if (error == true) {
33     return
34   }
35 
36   //链接ssh
37   ssh.connect(serverConfig)
38   .then(() => {
39 
40     if(data.length == 0) {
41       return 
42     }
43 
44     // 组装数据
45     data.map(function(item, index){
46       let path = item.replace(/\/g, "/");
47       let temp = {
48         local: path,
49         remote: path.replace(local_path, remote_path)
50       }
51       uploadFiles[index] = temp;
52     })
53     
54     //上传文件
55     ssh.putFiles(uploadFiles).then(function () {
56         console.log("文档上传成功了!!!!")
57         client.srem(key,data)
58       }, function (error) {
59         console.log(error)
60       })
61 
62     })
63   
64 })
原文地址:https://www.cnblogs.com/tl542475736/p/8718335.html