Completed
Push — master ( 1fc2c9...675e71 )
by greg
01:46
created

src/cli/cms/data/file.js   A

Complexity

Total Complexity 35
Complexity/F 7

Size

Lines of Code 129
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
wmc 35
c 1
b 0
f 0
nc 1
mnd 3
bc 13
fnc 5
dl 0
loc 129
rs 9
bpm 2.6
cpm 7
noi 2

3 Functions

Rating   Name   Duplication   Size   Complexity  
A file.js ➔ fromUrl 0 56 3
A file.js ➔ get 0 15 3
B file.js ➔ getAllWithKeys 0 46 1
1
import path from 'path'
2
import fse from 'fs-extra'
3
4
import {
5
  Hooks,
6
  FileParser,
7
  cmsData,
8
  config
9
} from '../../'
10
11
export function getAllWithKeys(withKeys) {
12
  var files = FileParser.getFiles(path.join(config.root, config.data.url), true, 99, /\.json/)
13
  var filesArr = []
14
15
  var i = 0
0 ignored issues
show
Unused Code introduced by
The variable i seems to be never used. Consider removing it.
Loading history...
16
17
  files.forEach(function (file) {
18
    var cleanFile = file
19
    var json = cmsData.file.get(file.path)
20
21
    if(typeof json.abe_meta.latest !== 'undefined' && json.abe_meta.latest !== null
22
      && typeof json.abe_meta.latest !== 'undefined' && json.abe_meta.latest !== null
23
      && typeof json.abe_meta.latest.date !== 'undefined' && json.abe_meta.latest.date !== null) {
24
      file.date = json.abe_meta.latest.date
25
    }
26
27
    if(typeof json.abe_meta !== 'undefined' && json.abe_meta !== null) {
28
      var date = null
29
      if (typeof json.abe_meta.latest !== 'undefined' && json.abe_meta.latest !== null
30
        && typeof json.abe_meta.latest.date !== 'undefined' && json.abe_meta.latest.date !== null) {
31
        date = json.abe_meta.latest.date
32
      }else if (typeof json.abe_meta.date !== 'undefined' && json.abe_meta.date !== null) {
33
        date = json.abe_meta.date
34
      }
35
      cleanFile.abe_meta = {
36
        date: date
37
        , type: (typeof json.abe_meta.type !== 'undefined' && json.abe_meta.type !== null) ? json.abe_meta.type : null
38
        , link: (typeof json.abe_meta.link !== 'undefined' && json.abe_meta.link !== null) ? json.abe_meta.link : null
39
        , template: (typeof json.abe_meta.template !== 'undefined' && json.abe_meta.template !== null) ? json.abe_meta.template : null
40
        , status: (typeof json.abe_meta.status !== 'undefined' && json.abe_meta.status !== null) ? json.abe_meta.status : null
41
        , cleanName: (typeof json.abe_meta.cleanName !== 'undefined' && json.abe_meta.cleanName !== null) ? json.abe_meta.cleanName : null
42
        , cleanFilename: (typeof json.abe_meta.cleanFilename !== 'undefined' && json.abe_meta.cleanFilename !== null) ? json.abe_meta.cleanFilename : null
43
      }
44
    }
45
    Array.prototype.forEach.call(withKeys, (key) => {
46
      var keyFirst = key.split('.')[0]
47
      cleanFile[keyFirst] = json[keyFirst]
48
    })
49
    filesArr.push(cleanFile)
50
  })
51
52
  var merged = cmsData.revision.getFilesMerged(filesArr)
53
54
  Hooks.instance.trigger('afterGetAllFiles', merged)
55
  return merged
56
}
57
58
export function get(pathJson) {
59
  var json = {}
60
  pathJson = Hooks.instance.trigger('beforeGetJson', pathJson)
61
  
62
  try {
63
    var stat = fse.statSync(pathJson)
64
    if (stat) {
65
      json = fse.readJsonSync(pathJson)
66
    }
67
  }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...
68
  }
69
70
  json = Hooks.instance.trigger('afterGetJson', json)
71
  return json
72
}
73
74
export function fromUrl(url) {
75
  var res = {
76
    root: '',
77
    draft: {
78
      dir: '',
79
      file: '',
80
      path: ''
81
    },
82
    publish: {
83
      dir: '',
84
      file: '',
85
      link: '',
86
      path: '',
87
      json: ''
88
    },
89
    json: {
90
      path: '',
91
      file: ''
92
    }
93
  }
94
95
  if(typeof url !== 'undefined' && url !== null) {
96
97
    var dir = path.dirname(url).replace(config.root, '')
98
    var filename = path.basename(url)
99
    var link = url.replace(config.root, '')
100
    link = link.replace(/^\//, '').split('/')
101
    link.shift()
102
    link = cmsData.fileAttr.delete('/' + link.join('/').replace(/\/$/, ''))
103
104
    let draft = config.draft.url
105
    let publish = config.publish.url
106
    let data = config.data.url
107
108
    res.root = config.root
109
110
    // set dir path draft/json
111
    res.draft.dir = FileParser.changePathEnv(path.join(config.root, dir), draft)
112
    res.json.dir = FileParser.changePathEnv(path.join(config.root, dir), data)
113
    res.publish.dir = FileParser.changePathEnv(path.join(config.root, dir), publish)
114
    res.publish.json = res.json.dir
115
116
    // set filename draft/json
117
    res.draft.file = filename
118
    res.publish.file = cmsData.fileAttr.delete(filename)
119
    res.publish.link = link
120
    res.json.file = filename.replace(`.${config.files.templates.extension}`, '.json')
121
    res.publish.json = path.join(res.json.dir, cmsData.fileAttr.delete(res.json.file))
122
123
    // set filename draft/json
124
    res.draft.path = path.join(res.draft.dir, res.draft.file)
125
    res.publish.path = path.join(res.publish.dir, res.publish.file)
126
    res.json.path = path.join(res.json.dir, res.json.file)
127
  }
128
  return res
129
}