Completed
Push — master ( de94f8...830b48 )
by greg
01:45
created

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

Complexity

Total Complexity 27
Complexity/F 3

Size

Lines of Code 202
Function Count 9

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 0
wmc 27
c 2
b 0
f 0
nc 1
mnd 3
bc 22
fnc 9
dl 0
loc 202
rs 10
bpm 2.4444
cpm 3
noi 2

6 Functions

Rating   Name   Duplication   Size   Complexity  
A file.js ➔ fromUrl 0 56 2
A file.js ➔ get 0 15 3
A file.js ➔ getFilesByType 0 21 3
B file.js ➔ getAllWithKeys 0 26 1
F file.js ➔ getAbeMeta 0 26 11
B file.js ➔ getFileObject 0 41 2
1
import path from 'path'
2
import mkdirp from 'mkdirp'
3
import fse from 'fs-extra'
4
import moment from 'moment'
5
6
import {
7
  abeExtend,
8
  coreUtils,
9
  cmsData,
10
  config
11
} from '../../'
12
13
export function getAbeMeta(fileObject, json) {
14
  if(json.abe_meta.latest.date != null) {
15
    fileObject.date = json.abe_meta.latest.date
16
    fileObject.cleanDate = moment(json.abe_meta.latest.date).format('YYYY/MM/DD HH:MM:ss')
17
  }
18
19
  if(json.abe_meta != null) {
20
    var date = null
21
    if (json.abe_meta.latest.date !== null) {
22
      date = json.abe_meta.latest.date
23
    } else if (json.abe_meta.date !== null) {
24
      date = json.abe_meta.date
25
    }
26
    fileObject.abe_meta = {
27
      date: date,
28
      type: (json.abe_meta.type != null) ? json.abe_meta.type : null,
29
      link: (json.abe_meta.link != null) ? json.abe_meta.link : null,
30
      template: (json.abe_meta.template != null) ? json.abe_meta.template : null,
31
      status: (json.abe_meta.status != null) ? json.abe_meta.status : null,
32
      cleanName: (json.abe_meta.cleanName != null) ? json.abe_meta.cleanName : null,
33
      cleanFilename: (json.abe_meta.cleanFilename != null) ? json.abe_meta.cleanFilename : null
34
    }
35
  }
36
37
  return fileObject
38
}
39
40
export function getAllWithKeys(withKeys) {
41
  const pathData = path.join(config.root, config.data.url)
42
  const extension = '.json'
43
  const files = coreUtils.file.getFilesSync(pathData, true, extension)
44
45
  let filesArr = []
46
47
  Array.prototype.forEach.call(files, (pathFile) => {
48
    let fileObject = cmsData.file.getFileObject(pathFile)
49
    const json = cmsData.file.get(pathFile)
50
51
    fileObject = cmsData.file.getAbeMeta(fileObject, json)
52
    
53
    Array.prototype.forEach.call(withKeys, (key) => {
54
      var keyFirst = key.split('.')[0]
55
      fileObject[keyFirst] = json[keyFirst]
56
    })
57
58
    filesArr.push(fileObject)
59
  })
60
61
  var merged = cmsData.revision.getFilesMerged(filesArr)
62
  abeExtend.hooks.instance.trigger('afterGetAllFiles', merged)
63
64
  return merged
65
}
66
67
export function get(pathJson) {
68
  var json = {}
69
  pathJson = abeExtend.hooks.instance.trigger('beforeGetJson', pathJson)
70
  
71
  try {
72
    var stat = fse.statSync(pathJson)
73
    if (stat) {
74
      json = fse.readJsonSync(pathJson)
75
    }
76
  }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...
77
  }
78
79
  json = abeExtend.hooks.instance.trigger('afterGetJson', json)
80
  return json
81
}
82
83
export function fromUrl(url) {
84
  var res = {
85
    root: '',
86
    draft: {
87
      dir: '',
88
      file: '',
89
      path: ''
90
    },
91
    publish: {
92
      dir: '',
93
      file: '',
94
      link: '',
95
      path: '',
96
      json: ''
97
    },
98
    json: {
99
      path: '',
100
      file: ''
101
    }
102
  }
103
104
  if(url != null) {
105
106
    var dir = path.dirname(url).replace(config.root, '')
107
    var filename = path.basename(url)
108
    var link = url.replace(config.root, '')
109
    link = link.replace(/^\//, '').split('/')
110
    link.shift()
111
    link = cmsData.fileAttr.delete('/' + link.join('/').replace(/\/$/, ''))
112
113
    let draft = config.draft.url
114
    let publish = config.publish.url
115
    let data = config.data.url
116
117
    res.root = config.root
118
119
    // set dir path draft/json
120
    res.draft.dir = coreUtils.file.changePath(dir, draft)
121
    res.json.dir = coreUtils.file.changePath(dir, data)
122
    res.publish.dir = coreUtils.file.changePath(dir, publish)
123
    res.publish.json = res.json.dir
124
125
    // set filename draft/json
126
    res.draft.file = filename
127
    res.publish.file = cmsData.fileAttr.delete(filename)
128
    res.publish.link = link
129
    res.json.file = filename.replace(`.${config.files.templates.extension}`, '.json')
130
    res.publish.json = path.join(res.json.dir, cmsData.fileAttr.delete(res.json.file))
131
132
    // set filename draft/json
133
    res.draft.path = path.join(res.draft.dir, res.draft.file)
134
    res.publish.path = path.join(res.publish.dir, res.publish.file)
135
    res.json.path = path.join(res.json.dir, res.json.file)
136
  }
137
  return res
138
}
139
140
export function getFilesByType(pathFile, type = null) {
141
  const extension = '.' + config.files.templates.extension
142
  let result = []
143
  
144
  try {
145
    var directory = fse.lstatSync(pathFile)
146
    if (!directory.isDirectory()) {
147
      mkdirp.sync(pathFile)
148
    }
149
  } catch (e) {
150
    mkdirp.sync(pathFile)
151
  }
152
  const files = coreUtils.file.getFilesSync(pathFile, true, extension)
153
154
  Array.prototype.forEach.call(files, (file) => {
155
    var val = cmsData.fileAttr.get(file).s
156
    if(type === null || val === type) result.push(file)
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...
157
  })
158
159
  return result
160
}
161
162
export function getFileObject(pathFile) {
163
  const pathData = path.join(config.root, config.data.url)
164
  const extension = '.json'
165
166
  const name = path.basename(pathFile)
167
  const nameNoExt = path.basename(pathFile, extension)
168
  const relativePath = pathFile.replace(pathData + '/', '')
169
170
  const parentName = cmsData.fileAttr.delete(name)
171
  const parentPath = cmsData.fileAttr.delete(pathFile)
172
  const parentRelativePath = cmsData.fileAttr.delete(pathFile).replace(config.root, '').replace(/^\/?.+?\//, '')
173
174
  const fileData = cmsData.fileAttr.get(name)
175
176
  let date
177
  if (fileData.d) {
178
    date = fileData.d
179
  }else {
180
    const stat = fse.statSync(pathFile)
181
    date = stat.mtime
182
  }
183
184
  const fileDate = moment(date)
185
  const duration = moment.duration(moment(fileDate).diff(new Date())).humanize(true)
186
  
187
  let fileObject = {
188
    'name': name,
189
    'cleanNameNoExt': nameNoExt,
190
    'path': pathFile,
191
    'cleanPath': relativePath,
192
    'filePath': relativePath,
193
    'date': date,
194
    'cleanDate': fileDate.format('YYYY/MM/DD HH:MM:ss'),
195
    'duration': duration,
196
    'cleanName': parentName,
197
    'cleanPathName': parentPath,
198
    'cleanFilePath': parentRelativePath
199
  }
200
201
  return fileObject
202
}
203