Browse Source

Refactor test server code

Ben Hsieh 8 years ago
parent
commit
3a001d447a
1 changed files with 51 additions and 63 deletions
  1. 51
    63
      test-server/server.js

+ 51
- 63
test-server/server.js View File

@@ -3,87 +3,44 @@
3 3
  * @description react-native-fetch-blob test & dev server
4 4
  */
5 5
 
6
-var express = require('express');
7
-var bodyParser = require('body-parser');
8
-var chokidar = require('chokidar');
9
-var multer = require('multer');
10
-var upload = multer({dest : 'uploads/'});
11
-var chalk = require('chalk');
12
-var mkdirp = require('mkdirp');
13
-var dirname = require('path').dirname;
14
-var app = express();
15
-var fs = require('fs');
6
+var express = require('express')
7
+var bodyParser = require('body-parser')
8
+var chokidar = require('chokidar')
9
+var multer = require('multer')
10
+var upload = multer({dest : 'uploads/'})
11
+var chalk = require('chalk')
12
+var mkdirp = require('mkdirp')
13
+var dirname = require('path').dirname
14
+var app = express()
15
+var fs = require('fs')
16 16
 
17 17
 var JS_SOURCE_PATH = '../test/',
18
-    APP_SOURCE_PATH = '../RNFetchBlobTest/';
18
+    LIB_SOURCE_PATH = '../src/',
19
+    NODE_MODULE_MODULE_PATH = '../RNFetchBlobTest/node_modules/react-native-fetch-blob/',
20
+    APP_SOURCE_PATH = '../RNFetchBlobTest/'
19 21
 
20
-// watch index.js in src folder
21
-chokidar
22
-  .watch('../src/index.js')
23
-  .on('change', function(path) {
24
-    console.log(chalk.green('js file changed'), path);
25
-    var targetPath = String(path).replace('../src/', '../RNFetchBlobTest/node_modules/react-native-fetch-blob/')
26
-    mkdirp(dirname(targetPath), function (err) {
27
-    if (err) return cb(err);
28
-      fs.writeFileSync(targetPath, fs.readFileSync(path));
29
-    });
30
-  })
31
-
32
-// watch files in  test folder
33
-chokidar
34
-  .watch(JS_SOURCE_PATH)
35
-  .on('add', function(path) {
36
-    console.log(chalk.magenta('file created'), path);
37
-    var targetPath = String(path).replace(JS_SOURCE_PATH, APP_SOURCE_PATH)
38
-    mkdirp(dirname(targetPath), function (err) {
39
-    if (err) return cb(err);
40
-      fs.writeFileSync(targetPath, fs.readFileSync(path));
41
-    });
42
-  })
43
-  .on('change', function(path) {
44
-    console.log(chalk.green('file changed'), path);
45
-    var targetPath = String(path).replace(JS_SOURCE_PATH, APP_SOURCE_PATH)
46
-    mkdirp(dirname(targetPath), function (err) {
47
-    if (err) return cb(err);
48
-      fs.writeFileSync(targetPath, fs.readFileSync(path));
49
-    });
50
-  })
51
-  .on('unlink', function(path) {
52
-    console.log(chalk.red('file removed'), path);
53
-    var targetPath = String(path).replace(JS_SOURCE_PATH, APP_SOURCE_PATH)
54
-    mkdirp(dirname(targetPath), function (err) {
55
-    if (err) return cb(err);
56
-      fs.unlinkSync(targetPath);
57
-    });
58
-  })
59
-  .on('error', function(err){
60
-    console.log(err);
61
-  });
22
+// watch test app source
23
+watch(JS_SOURCE_PATH, APP_SOURCE_PATH)
24
+// watch lib js source
25
+watch(LIB_SOURCE_PATH, NODE_MODULE_MODULE_PATH, {ignored: /\.\.\/src\/(android|ios)\//})
62 26
 
63 27
 app.listen(8123, function(err){
64
-
65 28
   if(!err)
66 29
     console.log('test server running at port ',8123)
67
-
68 30
 })
69 31
 
70 32
 
71 33
 app.use(function(req,res,next){
72
-
73
-  console.log(chalk.green('request url=') + chalk.magenta(req.url));
74
-
34
+  console.log(chalk.green('request url=') + chalk.magenta(req.url))
75 35
   next()
76 36
 })
77 37
 
78 38
 app.use(upload.any())
79
-
80 39
 app.use('/public', express.static('./public'))
81
-
82 40
 // for redirect test
83 41
 app.get('/redirect', function(req, res) {
84 42
   res.redirect('/public/github.png')
85 43
 })
86
-
87 44
 // handle octet-stream request
88 45
 app.post('/upload', function(req, res){
89 46
 
@@ -97,12 +54,10 @@ app.post('/upload', function(req, res){
97 54
   })
98 55
 
99 56
 })
100
-
101 57
 // return an empty response
102 58
 app.all('/empty', function(req, res) {
103 59
   res.send('')
104 60
 })
105
-
106 61
 // handle multipart/form-data request
107 62
 app.post('/upload-form', function(req, res) {
108 63
   console.log(req.headers)
@@ -119,3 +74,36 @@ app.post('/upload-form', function(req, res) {
119 74
     files : req.files
120 75
   })
121 76
 })
77
+
78
+function watch(source, dest, ignore) {
79
+  // watch files in  test folder
80
+  chokidar
81
+    .watch(source, ignore)
82
+    .on('add', function(path) {
83
+      console.log(chalk.magenta('file created'), path)
84
+      var targetPath = String(path).replace(source, dest)
85
+      mkdirp(dirname(targetPath), function (err) {
86
+      if (err) return cb(err)
87
+        fs.writeFileSync(targetPath, fs.readFileSync(path))
88
+      })
89
+    })
90
+    .on('change', function(path) {
91
+      console.log(chalk.green('file changed'), path)
92
+      var targetPath = String(path).replace(source, dest)
93
+      mkdirp(dirname(targetPath), function (err) {
94
+      if (err) return cb(err)
95
+        fs.writeFileSync(targetPath, fs.readFileSync(path))
96
+      })
97
+    })
98
+    .on('unlink', function(path) {
99
+      console.log(chalk.red('file removed'), path)
100
+      var targetPath = String(path).replace(source, dest)
101
+      mkdirp(dirname(targetPath), function (err) {
102
+      if (err) return cb(err)
103
+        fs.unlinkSync(targetPath)
104
+      })
105
+    })
106
+    .on('error', function(err){
107
+      console.log(err)
108
+    })
109
+}