|
| 1 | +const path = require(`path`) |
| 2 | +const fs = require(`fs`) |
| 3 | +const appRootDir = require(`app-root-dir`).get() |
| 4 | + |
| 5 | +const componentPageTemplate = path.resolve( |
| 6 | + `src/templates/ComponentPage/index.js` |
| 7 | +) |
| 8 | +const tableOfContentsTemplate = path.resolve(`src/templates/TOC/index.js`) |
| 9 | + |
| 10 | +exports.createPages = ({ graphql, boundActionCreators }) => { |
| 11 | + const { createPage } = boundActionCreators |
| 12 | + |
| 13 | + return new Promise((resolve, reject) => { |
| 14 | + resolve( |
| 15 | + Promise.all([ |
| 16 | + graphql(` |
| 17 | + { |
| 18 | + allComponentMetadata { |
| 19 | + edges { |
| 20 | + node { |
| 21 | + id |
| 22 | + displayName |
| 23 | + description { |
| 24 | + text |
| 25 | + } |
| 26 | + props { |
| 27 | + name |
| 28 | + type { |
| 29 | + value |
| 30 | + raw |
| 31 | + name |
| 32 | + } |
| 33 | + description { |
| 34 | + text |
| 35 | + } |
| 36 | + required |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + `), |
| 43 | + graphql(` |
| 44 | + { |
| 45 | + allMarkdownRemark( |
| 46 | + filter: { fileAbsolutePath: { regex: "/README.md/" } } |
| 47 | + ) { |
| 48 | + edges { |
| 49 | + node { |
| 50 | + fileAbsolutePath |
| 51 | + html |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + `), |
| 57 | + ]) |
| 58 | + .then(([docgenResult, markdownResult]) => { |
| 59 | + const errors = docgenResult.errors || markdownResult.errors |
| 60 | + if (errors) { |
| 61 | + reject(new Error(errors)) |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + const allComponents = docgenResult.data.allComponentMetadata.edges.map( |
| 66 | + (edge, i) => |
| 67 | + Object.assign({}, edge.node, { |
| 68 | + path: `/components/${edge.node.displayName.toLowerCase()}/`, |
| 69 | + html: markdownResult.data.allMarkdownRemark.edges[i].node.html, |
| 70 | + }) |
| 71 | + ) |
| 72 | + |
| 73 | + const exportFileContents = |
| 74 | + allComponents |
| 75 | + .reduce((accumulator, { id, displayName }) => { |
| 76 | + const absolutePath = id.replace(/ absPath of.*$/, ``) |
| 77 | + accumulator.push( |
| 78 | + `export { default as ${displayName} } from "${absolutePath}"` |
| 79 | + ) |
| 80 | + return accumulator |
| 81 | + }, []) |
| 82 | + .join(`\n`) + `\n` |
| 83 | + |
| 84 | + fs.writeFileSync( |
| 85 | + path.join(appRootDir, `.cache/components.js`), |
| 86 | + exportFileContents |
| 87 | + ) |
| 88 | + |
| 89 | + allComponents.forEach(data => { |
| 90 | + const { path } = data |
| 91 | + const context = Object.assign({}, data, { |
| 92 | + allComponents, |
| 93 | + }) |
| 94 | + createPage({ |
| 95 | + path, |
| 96 | + component: componentPageTemplate, |
| 97 | + context, |
| 98 | + }) |
| 99 | + }) |
| 100 | + |
| 101 | + createPage({ |
| 102 | + path: `/components/`, |
| 103 | + component: tableOfContentsTemplate, |
| 104 | + context: { |
| 105 | + allComponents, |
| 106 | + }, |
| 107 | + }) |
| 108 | + }) |
| 109 | + .catch(err => { |
| 110 | + console.log(err) |
| 111 | + throw new Error(err) |
| 112 | + }) |
| 113 | + ) |
| 114 | + }) |
| 115 | +} |
0 commit comments