Completed
Push — master ( b940ae...2e93c1 )
by Dimas
223:30 queued 207:23
created

libs/src/compiler/filemanager.ts   A

Complexity

Total Complexity 15
Complexity/F 3.75

Size

Lines of Code 103
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 62
dl 0
loc 103
rs 10
c 0
b 0
f 0
wmc 15
mnd 11
bc 11
fnc 4
bpm 2.75
cpm 3.75
noi 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
B filemanager.unlink 0 34 6
A filemanager.mkfile 0 13 2
A filemanager.mkdir 0 13 3
A filemanager.empty 0 22 4
1
import rimraf from "rimraf";
2
import * as fs from "fs";
3
import * as path from "path";
4
import log from "./log";
5
//import { core } from "./core";
6
import core from "./core";
7
8
class filemanager {
9
  /**
10
   * Delete file or directory recursive
11
   * @param filedir
12
   * @param async
13
   * @returns null = file/dir not exists, false = delete filedir failed, true = success
14
   */
15
  static unlink(filedir: string, async?: boolean) {
16
    const deleteNow = function () {
17
      if (fs.existsSync(filedir)) {
18
        if (async) {
19
          rimraf(filedir, function (err) {
20
            if (!err) {
21
              log.log(log.success("done"));
22
            } else {
23
              log.log(log.error(`cannot delete ${core.filelog(filedir)}`));
24
            }
25
          });
26
        } else {
27
          rimraf.sync(filedir);
28
        }
29
        return true;
30
      } else {
31
        return false;
32
      }
33
    };
34
    try {
35
      var exists = fs.existsSync(filedir);
36
      if (exists) {
37
        return deleteNow();
38
      } else {
39
        return null;
40
      }
41
    } catch (error) {
42
      return false;
43
    }
44
  }
45
46
  /**
47
   * create file recursive
48
   * @param file
49
   * @param content
50
   */
51
  static mkfile(file: string, content: any) {
52
    this.mkdir(path.dirname(file));
53
    if (typeof content == "object" || Array.isArray(content)) {
54
      content = JSON.stringify(content, null, 4);
55
    }
56
    fs.writeFileSync(file, content, { encoding: "utf-8" });
57
    return file;
58
  }
59
60
  /**
61
   * create directory recursive
62
   * @param dir
63
   */
64
  static mkdir(dir: string) {
65
    if (!fs.existsSync(path.dirname(dir))) {
66
      this.mkdir(path.dirname(dir));
67
    }
68
    if (!fs.existsSync(dir)) {
69
      fs.mkdirSync(dir);
70
    }
71
    return dir;
72
  }
73
74
  /**
75
   * remove all files/folders except matches regex
76
   * @param folder
77
   * @param exclude
78
   */
79
  static empty(folder: string, exclude: RegExp | null) {
80
    fs.readdir(folder, (err, files) => {
81
      if (err) {
82
        log.log(log.error(err.message));
83
      } else {
84
        files.forEach((file) => {
85
          const fileDir = path.join(folder, file);
86
87
          if (exclude) {
88
            if (!exclude.test(file)) {
89
              filemanager.unlink(fileDir, true);
90
            } else {
91
              log.log(log.error(`${fileDir} in excluded lists`));
92
            }
93
          } else {
94
            filemanager.unlink(fileDir, true);
95
          }
96
        });
97
      }
98
    });
99
  }
100
}
101
102
export = filemanager;
103