| Conditions | 16 |
| Paths | 4608 |
| Total Lines | 115 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like save.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 fse from 'fs-extra' |
||
| 62 | var p = new Promise((resolve) => { |
||
| 63 | var isRejectedDoc = false |
||
| 64 | if(type === 'reject'){ |
||
| 65 | isRejectedDoc = true |
||
| 66 | url = Hooks.instance.trigger('beforeReject', url) |
||
| 67 | type = 'draft' |
||
| 68 | realType = 'draft' |
||
| 69 | url = Hooks.instance.trigger('afterReject', url) |
||
| 70 | } |
||
| 71 | var tplUrl = FileParser.getFileDataFromUrl(url) |
||
| 72 | type = type || FileParser.getType(url) |
||
| 73 | var pathIso = dateIso(tplUrl, type) |
||
| 74 | if(typeof previousSave !== 'undefined' && previousSave !== null){ |
||
| 75 | pathIso.jsonPath = path.join(config.root, previousSave.jsonPath.replace(config.root, '')).replace(/-abe-d/, `-abe-${realType[0]}`) |
||
| 76 | pathIso.htmlPath = path.join(config.root, previousSave.htmlPath.replace(config.root, '')).replace(/-abe-d/, `-abe-${realType[0]}`) |
||
| 77 | } |
||
| 78 | |||
| 79 | if (tplPath.indexOf('.') > -1) { |
||
| 80 | tplPath = fileUtils.removeExtension(tplPath) |
||
| 81 | } |
||
| 82 | var tpl = tplPath.replace(config.root, '') |
||
| 83 | |||
| 84 | var fullTpl = path.join(config.root, config.templates.url, tpl) + '.' + config.files.templates.extension |
||
| 85 | |||
| 86 | if(typeof json === 'undefined' || json === null) { |
||
| 87 | json = FileParser.getJson(tplUrl.json.path) |
||
| 88 | } |
||
| 89 | |||
| 90 | var ext = { |
||
| 91 | template: tpl.replace(/^\/+/, ''), |
||
| 92 | link: tplUrl.publish.link, |
||
| 93 | complete: 0, |
||
| 94 | type: type |
||
| 95 | } |
||
| 96 | |||
| 97 | let meta = config.meta.name |
||
| 98 | json[meta] = extend(json[meta], ext) |
||
| 99 | var date = fileAttr.get(pathIso.jsonPath).d |
||
| 100 | |||
| 101 | if (publishAll) { |
||
| 102 | if(typeof json[meta].publish !== 'undefined' && json[meta].publish !== null) { |
||
| 103 | date = json[meta].publish.date |
||
| 104 | } |
||
| 105 | }else { |
||
| 106 | if(typeof date === 'undefined' || date === null || date === '') { |
||
| 107 | date = new Date() |
||
| 108 | }else { |
||
| 109 | date = new Date(date) |
||
| 110 | } |
||
| 111 | } |
||
| 112 | Util.addMetas(tpl, json, type, {}, date, realType) |
||
| 113 | |||
| 114 | if(typeof text === 'undefined' || text === null || text === '') { |
||
| 115 | text = getTemplate(fullTpl) |
||
| 116 | } |
||
| 117 | |||
| 118 | Util.getDataList(fileUtils.removeLast(tplUrl.publish.link), text, json) |
||
| 119 | .then(() => { |
||
| 120 | |||
| 121 | json = Hooks.instance.trigger('afterGetDataListOnSave', json) |
||
| 122 | for(var prop in json){ |
||
| 123 | if(typeof json[prop] === 'object' && Array.isArray(json[prop]) && json[prop].length === 1){ |
||
| 124 | var valuesAreEmpty = true |
||
| 125 | json[prop].forEach(function (element) { |
||
| 126 | for(var p in element) { |
||
| 127 | if(element[p] !== ''){ |
||
| 128 | valuesAreEmpty = false |
||
| 129 | } |
||
| 130 | } |
||
| 131 | }) |
||
| 132 | if(valuesAreEmpty){ |
||
| 133 | delete json[prop] |
||
| 134 | } |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | var obj = { |
||
| 139 | publishAll:publishAll, |
||
| 140 | type:type, |
||
| 141 | template:{ |
||
| 142 | path: fullTpl |
||
| 143 | }, |
||
| 144 | html: { |
||
| 145 | path:pathIso.htmlPath |
||
| 146 | }, |
||
| 147 | json: { |
||
| 148 | content: json, |
||
| 149 | path: pathIso.jsonPath |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | obj = Hooks.instance.trigger('beforeSave', obj) |
||
| 154 | |||
| 155 | obj.json.content[meta].complete = checkRequired(text, obj.json.content) |
||
| 156 | |||
| 157 | var res = saveJsonAndHtml(tpl.replace(/^\/+/, ''), obj, text) |
||
| 158 | if (isRejectedDoc) { |
||
| 159 | res.reject = fileAttr.delete(url).replace(path.join(config.root, config.draft.url), '') |
||
| 160 | } |
||
| 161 | |||
| 162 | Hooks.instance.trigger('afterSave', obj) |
||
| 163 | |||
| 164 | FileParser.copySiteAssets() |
||
| 165 | |||
| 166 | if(typeof config.publishAll !== 'undefined' && config.publishAll !== null && config.publishAll === true) { |
||
| 167 | if(!publishAll && type === 'publish') { |
||
| 168 | abeProcess('publish-all', [`FILEPATH=${json.abe_meta.link}`]) |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | resolve(res) |
||
| 173 | }).catch(function(e) { |
||
| 174 | console.error('Save.js', e) |
||
| 175 | }) |
||
| 176 | }) |
||
| 177 | |||
| 259 | } |