fixLocale.js   A
last analyzed

Complexity

Total Complexity 11
Complexity/F 3.67

Size

Lines of Code 40
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
wmc 11
nc 3
mnd 3
bc 10
fnc 3
dl 0
loc 40
rs 10
bpm 3.3333
cpm 3.6666
noi 3
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A fs.writeFile 0 5 2
A ➔ walkSync 0 13 1
1
var fs = require('fs');
2
var en = JSON.parse(fs.readFileSync('_locales/en/messages.json', 'utf8'));
3
4
var walkSync = function (dir, filelist) {
5
    var files = fs.readdirSync(dir);
6
    filelist = filelist || [];
7
    files.forEach(function (file) {
8
        if (fs.statSync(dir + file).isDirectory()) {
9
            filelist = walkSync(dir + file + '/', filelist);
10
        }
11
        else {
12
            filelist.push(dir + file);
13
        }
14
    });
15
    return filelist;
16
};
17
18
var language_files = [];
19
language_files = walkSync('_locales/', language_files);
20
21
for (var i = 0; i < language_files.length; i++) {
22
    var file = language_files[i];
23
    if(file == '_locales/en/messages.json'){
24
        continue;
25
    }
26
    var json_data = JSON.parse(fs.readFileSync(file, 'utf8'));
27
    for (var translate_key in json_data) {
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
28
        if (en[translate_key] && !translate_key.hasOwnProperty('placeholders') && en[translate_key].hasOwnProperty('placeholders')) {
29
            console.log('Fixed ' + file + ' translate key: ' + translate_key);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
30
            json_data[translate_key].placeholders = en[translate_key].placeholders;
31
        }
32
    }
33
    var options = { flag : 'w' };
34
    fs.writeFile(file, JSON.stringify(json_data, null, 4), function (err) {
35
        if(err){
36
           console.log('Error during saving');
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
37
        }
38
    });
39
40
}