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

src/cli/cms/operations/remove.js   A

Complexity

Total Complexity 10
Complexity/F 2

Size

Lines of Code 54
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
wmc 10
c 1
b 0
f 0
nc 1
mnd 2
bc 9
fnc 5
dl 0
loc 54
rs 10
bpm 1.8
cpm 2
noi 3

3 Functions

Rating   Name   Duplication   Size   Complexity  
A remove.js ➔ removeFile 0 9 3
A remove.js ➔ remove 0 12 1
A remove.js ➔ olderRevisionByType 0 19 2
1
import path from 'path'
2
import fse from 'fs-extra'
3
4
import {
5
  Hooks,
6
  cmsData,
7
  coreUtils,
8
  cmsOperations,
9
  Manager
10
} from '../../'
11
12
13
export function remove(filePath) {
14
  filePath = coreUtils.slug.clean(filePath)
15
  filePath = Hooks.instance.trigger('beforeDeleteFile', filePath)
16
17
  var revisions = cmsData.revision.getVersions(filePath)
18
19
  Array.prototype.forEach.call(revisions, (revision) => {
20
    cmsOperations.remove.removeFile(revision.path, revision.htmlPath)
21
  })
22
23
  Manager.instance.updateList()
24
}
25
26
export function removeFile(file, json) {
27
  if(coreUtils.file.exist(file)) {
28
    fse.removeSync(file)
29
  }
30
31
  if(coreUtils.file.exist(json)) {
32
    fse.removeSync(json)
33
  }
34
}
35
36
export function olderRevisionByType(fileName, type) {
37
  var folder = fileName.split('/')
38
  var file = folder.pop()
39
  var extension = file.replace(/.*?\./, '')
40
  folder = folder.join('/')
41
  var stat = fse.statSync(folder)
42
  if(stat){
43
    var files = FileParser.getFiles(folder, true, 1, new RegExp('\\.' + extension))
0 ignored issues
show
Bug introduced by
The variable FileParser seems to be never declared. If this is a global, consider adding a /** global: FileParser */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
44
    files.forEach(function (fileItem) {
45
      var fname = cmsData.fileAttr.delete(fileItem.cleanPath)
46
      var ftype = cmsData.fileAttr.get(fileItem.cleanPath).s
47
      if(fname === file && ftype === type){
48
        var fileDraft = fileItem.path.replace(/-abe-./, '-abe-d')
49
        cmsOperations.remove.removeFile(fileItem.path, FileParser.changePathEnv(fileItem.path, config.data.url).replace(new RegExp('\\.' + extension), '.json'))
0 ignored issues
show
Bug introduced by
The variable config seems to be never declared. If this is a global, consider adding a /** global: config */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Bug introduced by
The variable FileParser seems to be never declared. If this is a global, consider adding a /** global: FileParser */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
50
        cmsOperations.remove.removeFile(fileDraft, FileParser.changePathEnv(fileDraft, config.data.url).replace(new RegExp('\\.' + extension), '.json'))
51
      }
52
    })
53
  }
54
}