| Total Complexity | 42 |
| Complexity/F | 1.91 |
| Lines of Code | 199 |
| Function Count | 22 |
| Duplicated Lines | 128 |
| Ratio | 64.32 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like src/cli/cms/operations/post.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import path from 'path' |
||
| 2 | |||
| 3 | import { |
||
| 4 | cmsData, |
||
| 5 | cmsOperations, |
||
| 6 | coreUtils, |
||
| 7 | config, |
||
| 8 | abeExtend, |
||
| 9 | Manager |
||
| 10 | } from '../../' |
||
| 11 | |||
| 12 | View Code Duplication | export function draft(filePath, tplPath, json, workflow = 'draft', type = 'draft') { |
|
|
|
|||
| 13 | var p = new Promise((resolve, reject) => { |
||
| 14 | abeExtend.hooks.instance.trigger('beforeDraft', json, filePath, tplPath) |
||
| 15 | cmsOperations.save.save( |
||
| 16 | path.join(config.root, config.draft.url, filePath.replace(config.root)), |
||
| 17 | tplPath, |
||
| 18 | json, |
||
| 19 | '', |
||
| 20 | workflow, |
||
| 21 | null, |
||
| 22 | type) |
||
| 23 | .then((resSave) => { |
||
| 24 | var result |
||
| 25 | if(typeof resSave.error !== 'undefined' && resSave.error !== null ){ |
||
| 26 | result = {error: resSave.error} |
||
| 27 | }else if(typeof resSave.reject !== 'undefined' && resSave.reject !== null){ |
||
| 28 | result = resSave |
||
| 29 | }else if(typeof resSave.json !== 'undefined' && resSave.json !== null){ |
||
| 30 | Manager.instance.updateList() |
||
| 31 | result = { |
||
| 32 | success: 1, |
||
| 33 | json: resSave.json |
||
| 34 | } |
||
| 35 | } |
||
| 36 | resolve(result) |
||
| 37 | }) |
||
| 38 | }) |
||
| 39 | |||
| 40 | return p |
||
| 41 | } |
||
| 42 | |||
| 43 | export function publish(filePath, tplPath, json) { |
||
| 44 | var p = new Promise((resolve, reject) => { |
||
| 45 | abeExtend.hooks.instance.trigger('beforePublish', json, filePath, tplPath) |
||
| 46 | var p1 = new Promise((resolve) => { |
||
| 47 | cmsOperations.save.save( |
||
| 48 | path.join(config.root, config.draft.url, filePath.replace(config.root)), |
||
| 49 | tplPath, |
||
| 50 | json, |
||
| 51 | '', |
||
| 52 | 'draft', |
||
| 53 | null, |
||
| 54 | 'publish') |
||
| 55 | .then(() => { |
||
| 56 | resolve() |
||
| 57 | }).catch(function(e) { |
||
| 58 | console.error(e) |
||
| 59 | }) |
||
| 60 | }) |
||
| 61 | |||
| 62 | p1.then((resSave) => { |
||
| 63 | cmsOperations.save.save( |
||
| 64 | path.join(config.root, config.draft.url, filePath.replace(config.root)), |
||
| 65 | tplPath, |
||
| 66 | json, |
||
| 67 | '', |
||
| 68 | 'publish', |
||
| 69 | resSave, |
||
| 70 | 'publish') |
||
| 71 | .then((resSave) => { |
||
| 72 | var result |
||
| 73 | if(typeof resSave.error !== 'undefined' && resSave.error !== null ){ |
||
| 74 | result = { |
||
| 75 | success: 0, |
||
| 76 | error: resSave.error |
||
| 77 | } |
||
| 78 | } else if(typeof resSave.reject !== 'undefined' && resSave.reject !== null){ |
||
| 79 | result = resSave |
||
| 80 | } else if(typeof resSave.json !== 'undefined' && resSave.json !== null){ |
||
| 81 | result = { |
||
| 82 | success: 1, |
||
| 83 | json: resSave.json |
||
| 84 | } |
||
| 85 | } |
||
| 86 | abeExtend.hooks.instance.trigger('afterPublish', result) |
||
| 87 | Manager.instance.updateList() |
||
| 88 | resolve(result) |
||
| 89 | }).catch(function(e) { |
||
| 90 | console.error('post.js', e) |
||
| 91 | var result = { |
||
| 92 | success: 0, |
||
| 93 | error: 'publish error' |
||
| 94 | } |
||
| 95 | abeExtend.hooks.instance.trigger('afterPublish', result) |
||
| 96 | resolve(result) |
||
| 97 | }) |
||
| 98 | }).catch(function(e) { |
||
| 99 | console.error('post.js', e) |
||
| 100 | var result = { |
||
| 101 | success: 0, |
||
| 102 | error: 'publish error' |
||
| 103 | } |
||
| 104 | abeExtend.hooks.instance.trigger('afterPublish', result) |
||
| 105 | resolve(result) |
||
| 106 | }) |
||
| 107 | }) |
||
| 108 | View Code Duplication | ||
| 109 | return p |
||
| 110 | } |
||
| 111 | |||
| 112 | export function unpublish(filePath) { |
||
| 113 | abeExtend.hooks.instance.trigger('beforeUnpublish', filePath) |
||
| 114 | filePath = coreUtils.slug.clean(filePath) |
||
| 115 | var tplUrl = cmsData.file.fromUrl(path.join(config.publish.url, filePath)) |
||
| 116 | if(coreUtils.file.exist(tplUrl.json.path)) { |
||
| 117 | var json = JSON.parse(JSON.stringify(cmsData.file.get(tplUrl.json.path))) |
||
| 118 | if(json.abe_meta.publish != null) { |
||
| 119 | delete json.abe_meta.publish |
||
| 120 | } |
||
| 121 | |||
| 122 | cmsOperations.save.save( |
||
| 123 | path.join(config.root, config.draft.url, json.abe_meta.link.replace(config.root)), |
||
| 124 | json.abe_meta.template, |
||
| 125 | json, |
||
| 126 | '', |
||
| 127 | 'reject', |
||
| 128 | null, |
||
| 129 | 'reject' |
||
| 130 | ) |
||
| 131 | .then(() => { |
||
| 132 | cmsOperations.remove.removeFile(tplUrl.publish.path, tplUrl.publish.json) |
||
| 133 | abeExtend.hooks.instance.trigger('afterUnpublish', tplUrl.publish.path, tplUrl.publish.json) |
||
| 134 | Manager.instance.updateList() |
||
| 135 | }) |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | export function reject(filePath, tplPath, json) { |
||
| 140 | abeExtend.hooks.instance.trigger('beforeReject', filePath) |
||
| 141 | var p = new Promise((resolve, reject) => { |
||
| 142 | var p1 = new Promise((resolve) => { |
||
| 143 | cmsOperations.save.save( |
||
| 144 | path.join(config.root, config.draft.url, filePath.replace(config.root)), |
||
| 145 | tplPath, |
||
| 146 | json, |
||
| 147 | '', |
||
| 148 | 'draft', |
||
| 149 | null, |
||
| 150 | 'reject') |
||
| 151 | .then((resSave) => { |
||
| 152 | resolve() |
||
| 153 | }).catch(function(e) { |
||
| 154 | console.error(e) |
||
| 155 | }) |
||
| 156 | }) |
||
| 157 | |||
| 158 | p1.then((resSave) => { |
||
| 159 | cmsOperations.save.save( |
||
| 160 | path.join(config.root, config.draft.url, filePath.replace(config.root)), |
||
| 161 | tplPath, |
||
| 162 | json, |
||
| 163 | '', |
||
| 164 | 'reject', |
||
| 165 | resSave, |
||
| 166 | 'reject') |
||
| 167 | .then((resSave) => { |
||
| 168 | var result |
||
| 169 | if(typeof resSave.error !== 'undefined' && resSave.error !== null ){ |
||
| 170 | result = { |
||
| 171 | success: 0, |
||
| 172 | error: resSave.error |
||
| 173 | } |
||
| 174 | } else if(typeof resSave.reject !== 'undefined' && resSave.reject !== null){ |
||
| 175 | resSave.success = 1 |
||
| 176 | result = resSave |
||
| 177 | } else if(typeof resSave.json !== 'undefined' && resSave.json !== null){ |
||
| 178 | result = { |
||
| 179 | success: 1, |
||
| 180 | json: resSave.json |
||
| 181 | } |
||
| 182 | } |
||
| 183 | abeExtend.hooks.instance.trigger('afterReject', result) |
||
| 184 | Manager.instance.updateList() |
||
| 185 | resolve(result) |
||
| 186 | }) |
||
| 187 | }).catch(function(e) { |
||
| 188 | console.error(e) |
||
| 189 | var result = { |
||
| 190 | success: 0, |
||
| 191 | error: 'reject error' |
||
| 192 | } |
||
| 193 | abeExtend.hooks.instance.trigger('afterReject', result) |
||
| 194 | resolve(result) |
||
| 195 | }) |
||
| 196 | }) |
||
| 197 | |||
| 198 | return p |
||
| 199 | } |