|
| 1 | +import fs from 'fs-extra' |
| 2 | +import lodash from 'lodash' |
| 3 | +import semverUtils from 'semver-utils' |
| 4 | + |
| 5 | +const { without } = lodash |
| 6 | + |
| 7 | +const classItemKeys = [ |
| 8 | + 'access', |
| 9 | + 'category', |
| 10 | + 'chainable', |
| 11 | + 'class', |
| 12 | + 'default', |
| 13 | + 'deprecated', |
| 14 | + 'deprecationMessage', |
| 15 | + 'description', |
| 16 | + 'file', |
| 17 | + 'filetype', |
| 18 | + 'final', |
| 19 | + 'itemtype', |
| 20 | + 'line', |
| 21 | + 'module', |
| 22 | + 'name', |
| 23 | + 'namespace', |
| 24 | + 'params', |
| 25 | + 'return', |
| 26 | + 'see', |
| 27 | + 'since', |
| 28 | + 'static', |
| 29 | + 'tagname', |
| 30 | + 'throws', |
| 31 | + 'type', |
| 32 | +] |
| 33 | + |
| 34 | +const normalizeItem = item => { |
| 35 | + let normalizedItem = {} |
| 36 | + |
| 37 | + let encounteredDescription = false |
| 38 | + let finishedNormalization = false |
| 39 | + let newDescription = '' |
| 40 | + |
| 41 | + Object.keys(item).forEach(key => { |
| 42 | + if (key === 'description') { |
| 43 | + encounteredDescription = true |
| 44 | + } |
| 45 | + |
| 46 | + if (!encounteredDescription || finishedNormalization) { |
| 47 | + normalizedItem[key] = item[key] |
| 48 | + } else { |
| 49 | + if (key === 'description' || !classItemKeys.includes(key)) { |
| 50 | + let content = item[key] |
| 51 | + |
| 52 | + if (key === 'description') { |
| 53 | + newDescription = content + '\n' |
| 54 | + |
| 55 | + if (content.endsWith('}')) { |
| 56 | + newDescription += '\n' |
| 57 | + } |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + // For cases where we have @decorator(args), other cases are @decorator x |
| 62 | + if (!content.startsWith('(')) { |
| 63 | + content = ' ' + content |
| 64 | + } |
| 65 | + |
| 66 | + newDescription += ' @' + key + content + '\n' |
| 67 | + } else { |
| 68 | + normalizedItem[key] = item[key] |
| 69 | + finishedNormalization = true |
| 70 | + normalizedItem.description = newDescription |
| 71 | + } |
| 72 | + } |
| 73 | + }) |
| 74 | + |
| 75 | + const keysToNormalize = without(Object.keys(item), ...Object.keys(normalizedItem)) |
| 76 | + |
| 77 | + console.log( |
| 78 | + `File name: ${item.file} | Line number: ${item.line} | Faulty keys: ${keysToNormalize}` |
| 79 | + ) |
| 80 | + |
| 81 | + return normalizedItem |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * In ember 3.10 and above we introduced decorators. |
| 86 | + * Unfortunately YUIDoc freaks out when descriptions contain lines |
| 87 | + * starting with decorators. See https://github.com/yui/yuidoc/issues/347 |
| 88 | + * |
| 89 | + * Instead of having authors use encoded characters in source code. we're |
| 90 | + * fixing it here |
| 91 | + * @param {*} file |
| 92 | + */ |
| 93 | +export default async function fixBorkedYuidocFiles(file) { |
| 94 | + const version = semverUtils.parse( |
| 95 | + file |
| 96 | + .replace('tmp/s3-docs/v', '') |
| 97 | + .replace('/ember-docs.json', '') |
| 98 | + .replace('/ember-docs.json', '') |
| 99 | + ) |
| 100 | + |
| 101 | + if ( |
| 102 | + parseInt(version['major']) < 3 || |
| 103 | + !(parseInt(version['major']) === 3 && parseInt(version['minor']) >= 10) |
| 104 | + ) { |
| 105 | + return file |
| 106 | + } |
| 107 | + const originalFileBackup = file.replace('s3-docs', 's3-original-docs') |
| 108 | + |
| 109 | + if (fs.existsSync(originalFileBackup)) { |
| 110 | + console.log(`${file} was already processed`) |
| 111 | + return file |
| 112 | + } |
| 113 | + |
| 114 | + console.log(`\n\n\nProcessing ${file}`) |
| 115 | + |
| 116 | + const doc = await fs.readJson(file) |
| 117 | + |
| 118 | + let normalizedClassItems = doc.classitems.map(item => { |
| 119 | + let keys = Object.keys(item) |
| 120 | + let locationOfDescriptionField = keys.indexOf('description') |
| 121 | + |
| 122 | + if ( |
| 123 | + locationOfDescriptionField === -1 || |
| 124 | + classItemKeys.includes(keys[locationOfDescriptionField + 1]) |
| 125 | + ) { |
| 126 | + return item |
| 127 | + } else { |
| 128 | + return normalizeItem(item) |
| 129 | + } |
| 130 | + }) |
| 131 | + |
| 132 | + let newDoc = {} |
| 133 | + |
| 134 | + Object.keys(doc).forEach(key => { |
| 135 | + newDoc[key] = key === 'classitems' ? normalizedClassItems : doc[key] |
| 136 | + }) |
| 137 | + |
| 138 | + await fs.move(file, originalFileBackup) |
| 139 | + console.log(`Moved the original file ${file} to ${originalFileBackup}`) |
| 140 | + await fs.writeJson(file, newDoc, { spaces: 2 }) |
| 141 | +} |
0 commit comments