Passed
Pull Request — master (#3)
by Tito
03:27
created

gulp/utils.js   A

Complexity

Total Complexity 9
Complexity/F 1.5

Size

Lines of Code 77
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 33
mnd 3
bc 3
fnc 6
dl 0
loc 77
rs 10
bpm 0.5
cpm 1.5
noi 0
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A utils.js ➔ readJSON 0 7 2
A utils.js ➔ getFilesbyType 0 12 3
A utils.js ➔ cloneObject 0 4 1
A utils.js ➔ getManifestVersion 0 12 3
1
const fs = require("fs");
2
const xml2js = require("xml2js");
3
const xmlParser = new xml2js.Parser();
4
5
/**
6
 * Reads a JSON file and returns the parsed JSON data
7
 *
8
 * @param {string}  jsonFile   Path to the json file
9
 *
10
 * @returns {string}  Empty json object if file does not exist
11
 */
12
function readJSON(jsonFile) {
13
    "use strict";
14
    if (!fs.existsSync(jsonFile)) {
15
        return "{}";
16
    }
17
    return JSON.parse(fs.readFileSync(jsonFile).toString());
18
}
19
20
/**
21
 * Clones an object
22
 *
23
 * @param object
24
 *
25
 * @returns object
26
 */
27
function cloneObject(object) {
28
    "use strict";
29
    return JSON.parse(JSON.stringify(object));
30
}
31
32
/**
33
 * Get a list of files over a parent directory
34
 *
35
 * @param {string}  dir   Parent directory
36
 * @param {string}  type  Type of files: "directories" or "files"
37
 *
38
 * @returns {Array}
39
 */
40
function getFilesbyType(dir, type) {
41
    "use strict";
42
    const files = fs.readdirSync(dir);
43
    const dirs = [];
44
    files.forEach(function (file) {
45
        const isDirectory = fs.statSync(dir + "/" + file).isDirectory();
46
        if ((type === "directory" && isDirectory) || (type !== "directory" && !isDirectory)) {
47
            dirs.push(file);
48
        }
49
    });
50
    return dirs;
51
}
52
53
/**
54
 * Gets the version of a Joomla xml manifest file
55
 *
56
 * @param {string}  xmlFile  Input XML manifest file (Joomla format)
57
 *
58
 * @returns {string}
59
 */
60
function getManifestVersion(xmlFile) {
61
    "use strict";
62
    let version = "";
63
    xmlParser.parseString(fs.readFileSync(xmlFile), function (err, data) {
64
        if (err) {
65
            return "";
66
        }
67
        version = data.extension.version;
68
        return version;
69
    });
70
    return version;
71
}
72
73
74
exports.readJSON = readJSON;
75
exports.cloneObject = cloneObject;
76
exports.getFilesByType = getFilesbyType;
77
exports.getManifestVersion = getManifestVersion;