Completed
Push — master ( 98b2f1...a418ff )
by Dimas
08:55
created

libs/bin/syncjs/src/classes/Watcher.ts   A

Complexity

Total Complexity 9
Complexity/F 1.29

Size

Lines of Code 145
Function Count 7

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 107
dl 0
loc 145
rs 10
c 0
b 0
f 0
wmc 9
mnd 2
bc 2
fnc 7
bpm 0.2857
cpm 1.2857
noi 0

7 Functions

Rating   Name   Duplication   Size   Complexity  
A Watcher.add 0 8 1
A Watcher.all 0 6 2
A Watcher.handler 0 17 2
A Watcher.unlink 0 8 1
A Watcher.unlinkDir 0 8 1
A Watcher.ready 0 4 1
A Watcher.change 0 9 1
1
import * as chokidar from 'chokidar';
2
import * as upath from 'upath';
3
import * as chalk from 'chalk';
4
import * as process from 'process';
5
import { FSWatcher } from 'fs';
6
import Uploader from './Uploader';
7
import Config from './Config';
8
import CLI from './CLI';
9
const observatory = require('observatory');
10
11
export default class Watcher {
12
  files: FSWatcher;
13
  private tasks = {};
14
15
  constructor(
16
    private uploader: Uploader,
17
    private config: Config,
18
    private cli: CLI,
19
    private base: string = config.localPath
20
  ) {
21
    let defaultIgnores: Array<string | RegExp> = [
22
      /node_modules/,
23
      /.git/,
24
      /.svn/,
25
      /bower_components/,
26
      /vendor/,
27
      /tmp/,
28
    ];
29
30
    /*setInterval(function () {
31
            console.log(base);
32
        }, 5000);*/
33
34
    this.files = chokidar.watch(base, {
35
      ignored: defaultIgnores.concat(this.config.ignores),
36
      ignoreInitial: true,
37
      followSymlinks: true,
38
      disableGlobbing: false,
39
      usePolling: false,
40
      interval: 100,
41
      binaryInterval: 300,
42
      alwaysStat: false,
43
      depth: 99,
44
      awaitWriteFinish: {
45
        stabilityThreshold: 2000,
46
        pollInterval: 100,
47
      },
48
      ignorePermissionErrors: false,
49
      persistent: true,
50
    });
51
    //const log = console.log.bind(console);
52
53
    // Attach events
54
    ['all', 'add', 'change', 'unlink', 'unlinkDir'].forEach((method) => {
55
      this.files.on(method, this.handler(method));
56
    });
57
  }
58
59
  ready(): Promise<void> {
60
    return new Promise<void>((resolve) => {
61
      this.files.on('ready', resolve);
62
    });
63
  }
64
65
  eventToWord = {
66
    add: chalk.green('ADDED'),
67
    change: chalk.green('CHANGED'),
68
    unlink: chalk.red('DELETED'),
69
    unlinkDir: chalk.red('DELETED'),
70
  };
71
72
  private handler(method: string): Function {
73
    return (...args: string[]) => {
74
      let path: string,
75
        event = method;
76
77
      // Handle argument difference
78
      if (method === 'all') {
79
        path = args[1];
80
        event = args[0];
81
      } else {
82
        path = args[0];
83
      }
84
      //console.log(path);
85
86
      // If not, continue as ususal
87
      this[method](...args);
88
    };
89
  }
90
91
  private all = (event: string, path: string) => {
92
    if (event in this.eventToWord) {
93
      this.tasks[path] = observatory.add(
94
        this.eventToWord[event] + ' ' + path.replace(this.config.localPath, '')
95
      );
96
      this.tasks[path].status('Uploading');
97
    }
98
  };
99
100
  private add = (path: string) => {
101
    this.uploader
102
      .uploadFile(path)
103
      .then((remote) => {
104
        this.tasks[path].done('Done');
105
      })
106
      .catch((err) => {
107
        this.tasks[path].fail('Fail').details(err.message);
108
      });
109
  };
110
111
  private change = (path: string) => {
112
    this.uploader
113
      .uploadFile(path)
114
      .then((remote) => {
115
        //console.log('remote' + remote);
116
        this.tasks[path].done('Done');
117
      })
118
      .catch((err) => {
119
        this.tasks[path].fail('Fail').details(err.message);
120
      });
121
  };
122
123
  private unlink = (path: string) => {
124
    this.uploader
125
      .unlinkFile(path)
126
      .then((remote) => {
127
        this.tasks[path].done('Done');
128
      })
129
      .catch((err) => {
130
        this.tasks[path].fail('Fail').details(`Error deleting file ${err}`);
131
      });
132
  };
133
134
  private unlinkDir = (path: string) => {
135
    this.uploader
136
      .unlinkFolder(path)
137
      .then((remote) => {
138
        this.tasks[path].done('Done');
139
      })
140
      .catch((err) => {
141
        this.tasks[path].fail('Fail').details(`Error deleting folder ${err}`);
142
      });
143
  };
144
}
145