Ver código fonte

Add file watch function to test server

Ben Hsieh 8 anos atrás
pai
commit
f23c4260c6
1 arquivos alterados com 48 adições e 1 exclusões
  1. 48
    1
      test-server/server.js

+ 48
- 1
test-server/server.js Ver arquivo

@@ -1,10 +1,58 @@
1 1
 var express = require('express');
2 2
 var bodyParser = require('body-parser');
3
+var chokidar = require('chokidar');
3 4
 var multer = require('multer');
4 5
 var upload = multer({dest : 'uploads/'});
6
+var chalk = require('chalk');
7
+var mkdirp = require('mkdirp');
8
+var dirname = require('path').dirname;
5 9
 var app = express();
6 10
 var fs = require('fs');
7 11
 
12
+var JS_SOURCE_PATH = '../test/',
13
+    APP_SOURCE_PATH = '../RNFetchBlobTest/';
14
+
15
+chokidar
16
+  .watch('../src/index.js')
17
+  .on('change', function(path) {
18
+    console.log(chalk.green('js file changed'), path);
19
+    var targetPath = String(path).replace('../src/', '../RNFetchBlobTest/node_modules/react-native-fetch-blob/')
20
+    mkdirp(dirname(targetPath), function (err) {
21
+    if (err) return cb(err);
22
+      fs.writeFileSync(targetPath, fs.readFileSync(path));
23
+    });
24
+  })
25
+
26
+chokidar
27
+  .watch(JS_SOURCE_PATH)
28
+  .on('add', function(path) {
29
+    console.log(chalk.magenta('file created'), path);
30
+    var targetPath = String(path).replace(JS_SOURCE_PATH, APP_SOURCE_PATH)
31
+    mkdirp(dirname(targetPath), function (err) {
32
+    if (err) return cb(err);
33
+      fs.writeFileSync(targetPath, fs.readFileSync(path));
34
+    });
35
+  })
36
+  .on('change', function(path) {
37
+    console.log(chalk.green('file changed'), path);
38
+    var targetPath = String(path).replace(JS_SOURCE_PATH, APP_SOURCE_PATH)
39
+    mkdirp(dirname(targetPath), function (err) {
40
+    if (err) return cb(err);
41
+      fs.writeFileSync(targetPath, fs.readFileSync(path));
42
+    });
43
+  })
44
+  .on('unlink', function(path) {
45
+    console.log(chalk.red('file removed'), path);
46
+    var targetPath = String(path).replace(JS_SOURCE_PATH, APP_SOURCE_PATH)
47
+    mkdirp(dirname(targetPath), function (err) {
48
+    if (err) return cb(err);
49
+      fs.unlinkSync(targetPath);
50
+    });
51
+  })
52
+  .on('error', function(err){
53
+    console.log(err);
54
+  });
55
+
8 56
 app.listen(8123, function(err){
9 57
 
10 58
   if(!err)
@@ -12,7 +60,6 @@ app.listen(8123, function(err){
12 60
 
13 61
 })
14 62
 
15
-// app.use(bodyParser.raw())
16 63
 
17 64
 app.use(function(req,res,next){
18 65