|
| 1 | +const glob = require('glob'); |
| 2 | +const path = require('path'); |
| 3 | +const fs = require('fs'); |
| 4 | + |
| 5 | +const camelCased = (string) => string.replace(/-([a-z])/g, (g) => g[1].toUpperCase()).replace(/^./, (g) => g.toUpperCase()); |
| 6 | + |
| 7 | +const availableMappers = [ |
| 8 | + { title: 'MUI', mapper: 'mui' }, |
| 9 | + { title: 'PF4', mapper: 'pf4' }, |
| 10 | + { title: 'PF3', mapper: 'pf3' }, |
| 11 | + { title: 'BJS', mapper: 'blueprint' }, |
| 12 | + { title: 'SUIR', mapper: 'suir' } |
| 13 | +]; |
| 14 | + |
| 15 | +const targetDirectory = path.resolve(__dirname, '../src/doc-components'); |
| 16 | + |
| 17 | +const mdSources = availableMappers.reduce( |
| 18 | + (acc, curr) => ({ |
| 19 | + ...acc, |
| 20 | + [curr.mapper]: glob.sync(path.resolve(__dirname, `../src/doc-components/examples-texts/${curr.mapper}/*.md`)).map((path) => path.split('/').pop()) |
| 21 | + }), |
| 22 | + {} |
| 23 | +); |
| 24 | + |
| 25 | +const filesToGenerate = glob.sync(path.resolve(__dirname, '../pages/component-example/*.js')).map((path) => |
| 26 | + path |
| 27 | + .split('/') |
| 28 | + .pop() |
| 29 | + .replace('.js', '') |
| 30 | +); |
| 31 | + |
| 32 | +const fileTemplate = `import React from 'react'; |
| 33 | +import PropTypes from 'prop-types'; |
| 34 | +{{imports}} |
| 35 | +import GenericMuiComponent from '../helpers/generic-mui-component'; |
| 36 | +
|
| 37 | +const {{componentName}} = ({ activeMapper }) => { |
| 38 | +{{renders}} |
| 39 | + return <GenericMuiComponent activeMapper={activeMapper} component="{{file}}" />; |
| 40 | +}; |
| 41 | +
|
| 42 | +{{componentName}}.propTypes = { |
| 43 | + activeMapper: PropTypes.string.isRequired |
| 44 | +}; |
| 45 | +
|
| 46 | +export default {{componentName}}; |
| 47 | +`; |
| 48 | + |
| 49 | +filesToGenerate.forEach((file) => { |
| 50 | + const componentName = camelCased(file); |
| 51 | + let markup = fileTemplate.replace(/\{\{componentName\}\}/g, componentName); |
| 52 | + const imports = `${availableMappers |
| 53 | + .filter(({ mapper }) => mdSources[mapper].find((name) => name === `${mapper}-${file}.md`)) |
| 54 | + .map( |
| 55 | + ({ mapper }) => |
| 56 | + `import ${mapper.replace(/^./, (g) => g.toUpperCase())}${camelCased(file).replace(/^./, (g) => |
| 57 | + g.toUpperCase() |
| 58 | + )} from './examples-texts/${mapper}/${mapper}-${file}.md';` |
| 59 | + ) |
| 60 | + .join('\n')}`; |
| 61 | + markup = markup.replace('{{imports}}', imports); |
| 62 | + const renders = `${availableMappers |
| 63 | + .filter(({ mapper }) => mdSources[mapper].find((name) => name === `${mapper}-${file}.md`)) |
| 64 | + .map( |
| 65 | + ({ mapper }) => |
| 66 | + ` if (activeMapper === '${mapper}') { |
| 67 | + return <${mapper.replace(/^./, (g) => g.toUpperCase())}${camelCased(file).replace(/^./, (g) => g.toUpperCase())} />; |
| 68 | + }\n` |
| 69 | + ) |
| 70 | + .join('\n')}`; |
| 71 | + markup = markup.replace('{{file}}', file); |
| 72 | + markup = markup.replace('{{renders}}', renders); |
| 73 | + const targetFile = path.resolve(targetDirectory, file); |
| 74 | + fs.writeFileSync(`${targetFile}.js`, markup); |
| 75 | +}); |
0 commit comments