|
| 1 | +import { join, resolve } from 'path' |
| 2 | + |
| 3 | +const DATA_DIR = resolve(__dirname, '../../data/libraries') |
| 4 | +const AVATAR_BASE_URL = |
| 5 | + 'https://raw.githubusercontent.com/sustained/vue-land-bot/feat-library/assets/images/avatars/' |
| 6 | + |
| 7 | +/* |
| 8 | + TODO: We should probably just scan the directory but not sure how to handle |
| 9 | + exporting things which are loaded in a callback or a promise then handler? |
| 10 | +*/ |
| 11 | +const LIBRARY_NAMES = [ |
| 12 | + 'gridsome', |
| 13 | + 'nuxt', |
| 14 | + 'quasar', |
| 15 | + 'saber', |
| 16 | + 'vuepress', |
| 17 | + 'vuetify', |
| 18 | +] |
| 19 | + |
| 20 | +const libraries = {} |
| 21 | + |
| 22 | +for (const libraryName of LIBRARY_NAMES) { |
| 23 | + try { |
| 24 | + const library = require(join(DATA_DIR, `${libraryName}.json`)) |
| 25 | + libraries[libraryName] = _validateLibrary(library) |
| 26 | + } catch (error) { |
| 27 | + console.warn( |
| 28 | + `[LibraryService] Something went wrong when requiring or validating "${libraryName}.json":` |
| 29 | + ) |
| 30 | + console.error(error) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +export default libraries |
| 35 | + |
| 36 | +console.log(libraries['saber']) |
| 37 | + |
| 38 | +/** |
| 39 | + * @typedef {Object} LibraryDefinition |
| 40 | + * @property {string} name The name of the library. |
| 41 | + * @property {string} [icon] An optional icon path. |
| 42 | + * @property {Array<string>} [tags=[]] An array of tags. |
| 43 | + * @property {string} tagline The library's tagline or description. |
| 44 | + * @property {Array<{name: string, value: string}>} fields An array of field objects. |
| 45 | + * @property {{name: string, url: string, avatar: string}} [author] The author of the library. |
| 46 | + * @property {{site: string, docs: string, repo: string, bugs: string}} url Various URLs relating to the library. |
| 47 | + */ |
| 48 | + |
| 49 | +/** |
| 50 | + * |
| 51 | + * @param {string} name The name of the library. |
| 52 | + * @returns {LibraryDefinition} The library object. |
| 53 | + */ |
| 54 | +export function getLibrary(name) { |
| 55 | + if (libraries[name]) { |
| 56 | + return libraries[name] |
| 57 | + } |
| 58 | + |
| 59 | + // TODO: Check aliases and/or use an algorithm such as the |
| 60 | + // Levenshtein distance to find the nearest match. |
| 61 | + |
| 62 | + throw new Error(`[LibraryService] Could not find library: ${name}`) |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Ensure that the structure of the JSON is as we expect it to be, |
| 67 | + * including required fields etc. |
| 68 | + * |
| 69 | + * @param {Object} library The parsed library JSON file. |
| 70 | + * @returns {LibraryDefinition} The validated library object. |
| 71 | + * @throws If certain fields are missing or of the wrong type. |
| 72 | + */ |
| 73 | +function _validateLibrary(library) { |
| 74 | + if (typeof library !== 'object') { |
| 75 | + throw new TypeError('Object expected') |
| 76 | + } |
| 77 | + |
| 78 | + if (typeof library.name === 'undefined') { |
| 79 | + throw new Error('Field "name" required') |
| 80 | + } |
| 81 | + |
| 82 | + if (typeof library.url === 'undefined') { |
| 83 | + throw new Error('Field "url" required') |
| 84 | + } |
| 85 | + |
| 86 | + if (typeof library.fields === 'undefined') { |
| 87 | + throw new Error('Field "fields" required') |
| 88 | + } |
| 89 | + |
| 90 | + if (!Array.isArray(library.fields)) { |
| 91 | + throw new TypeError('Field "fields" must be of type "Array"') |
| 92 | + } |
| 93 | + |
| 94 | + if (!Array.isArray(library.tags)) { |
| 95 | + library.tags = [] |
| 96 | + } |
| 97 | + |
| 98 | + if (typeof library.colour === 'undefined') { |
| 99 | + library.colour = 'RANDOM' |
| 100 | + } |
| 101 | + |
| 102 | + if (library.author && library.author.avatar) { |
| 103 | + library.author.avatar = AVATAR_BASE_URL + library.author.avatar |
| 104 | + } |
| 105 | + |
| 106 | + return library |
| 107 | +} |
0 commit comments