Completed
Push — master ( 652c21...99e276 )
by greg
10:52 queued 08:59
created

abe-locales.js ➔ ???   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
nc 1
nop 1
dl 0
loc 5
cc 2
rs 9.4285
1
import fse from 'fs-extra'
2
import clc from 'cli-color'
3
import path from 'path'
4
5
import {
6
  config,
7
  fileUtils,
8
  FileParser
9
} from '../../'
10
11
let singleton = Symbol()
12
let singletonEnforcer = Symbol()
13
14
class Locales {
15
16
  constructor(enforcer) {
17
    if(enforcer != singletonEnforcer) throw 'Cannot construct Json singleton'
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
18
19
    this.i18n = this._getFiles()
20
  }
21
22
  static get instance() {
23
    if(!this[singleton]) {
24
      this[singleton] = new Locales(singletonEnforcer)
25
    }
26
    return this[singleton]
27
  }
28
29
  _reloadLocales() {
30
    this.i18n = this._getFiles()
31
  }
32
  
33
  _getFiles() {
34
    var loc = {}
35
    var website = config.root
36
37
    try{
38
      var localesFolder = path.join(website, 'locales')
39
      var stat = fse.statSync(localesFolder)
40
      if (stat && stat.isDirectory()) {
41
        var files = FileParser.read(fileUtils.cleanPath(localesFolder), fileUtils.cleanPath(localesFolder), 'files', true, /\.json/, 0)
42
        Array.prototype.forEach.call(files, (file) => {
43
          var json = fse.readJsonSync(file.path)
44
          loc[file.name.replace(/\.json/, '')] = json
45
        })
46
      }
47
    }catch(e){}
0 ignored issues
show
Coding Style Comprehensibility Best Practice introduced by
Empty catch clauses should be used with caution; consider adding a comment why this is needed.
Loading history...
48
49
    return loc
50
  }
51
}
52
53
export default Locales