Create build script to build React icons

- Create automated build script to go through all icons and
  automatically generate corresponding React components
This commit is contained in:
Daniel Martin 2021-05-18 00:03:45 +01:00
parent a0abf62f72
commit ff280f55ed
4 changed files with 1469 additions and 0 deletions

3
packages/iconoir-react/.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
node_modules
build
dist

View file

@ -0,0 +1,110 @@
const path = require('path');
const fs = require('fs');
const upperCamelCase = require('uppercamelcase');
const format = require('prettier-eslint');
const rootDir = path.join(__dirname, '..');
const iconoirRootDir = path.join(rootDir, '../..');
const iconoirIconsDir = path.join(iconoirRootDir, 'icons');
const builtIconsDir = path.join(rootDir, 'src/icons');
const initialTypeDefinitions = `/// <reference types="react" />
import { FC } from 'react';
export interface IconProps {
color?: string;
size?: string | number;
};
export type Icon = FC<IconProps>;
`;
if (!fs.existsSync(builtIconsDir)) {
fs.mkdirSync(builtIconsDir);
};
fs.writeFileSync(path.join(rootDir, 'src', 'index.js'), '', 'utf-8');
fs.writeFileSync(
path.join(rootDir, 'src', 'index.d.ts'),
initialTypeDefinitions,
'utf-8',
);
const incompatibleNames = {
'1st-medal': 'Medal1st',
'4k-display': 'Display4k',
'4x4-cell': 'Cell4x4',
'github': 'GitHub',
'github-outline': 'GitHubOutline',
'gitlab-full': 'GitLabFull',
'linkedin': 'LinkedIn',
'tiktok': 'TikTok',
'youtube': 'YouTube',
};
fs.readdir(iconoirIconsDir, (err, files) => {
if (err) {
return console.error('Unable to find root icons directory');
};
files.forEach((file) => {
const iconName = file.split('.')[0];
const location = path.join(builtIconsDir, `${iconName}.jsx`);
const componentName = (iconName in incompatibleNames) ? incompatibleNames[iconName] : upperCamelCase(iconName);
const fileData = fs.readFileSync(path.join(iconoirIconsDir, file));
const element = `import React, { forwardRef } from 'react';
import PropTypes from 'prop-types';
export const ${componentName} = forwardRef(({color = 'currentColor', size = 24}, ref) => {
return (
<svg ref={ref} width={size} height={size} viewBox="0 0 24 24" fill="none" color={color} xmlns="http://www.w3.org/2000/svg">
${fileData}
</svg>
);
});
${componentName}.propTypes = {
color: PropTypes.string,
size: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
]),
};
${componentName}.displayName = '${componentName}';
export default ${componentName};
`;
const component = format({
text: element,
prettierOptions: {
singleQuote: true,
parser: 'babel',
},
});
fs.writeFileSync(location, component, 'utf-8');
console.log('Built', componentName);
const exportString = `export { default as ${componentName} } from './icons/${iconName}';\n`;
fs.appendFileSync(
path.join(rootDir, 'src', 'index.js'),
exportString,
'utf-8',
);
const typeString = `export const ${componentName}: Icon;\n`;
fs.appendFileSync(
path.join(rootDir, 'src', 'index.d.ts'),
typeString,
'utf-8',
);
});
});

1324
packages/iconoir-react/package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,32 @@
{
"name": "iconoir-react",
"version": "1.0.0",
"description": "React library for Iconoir icon set",
"main": "build/index.js",
"module": "dist/index.js",
"scripts": {
"test": "echo Tests not currently set up",
"build": "rm -rf src/icons && node bin/build.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/lucaburgio/iconoir.git"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/lucaburgio/iconoir/issues"
},
"homepage": "https://github.com/lucaburgio/iconoir#readme",
"dependencies": {
"prop-types": "^15.7.2"
},
"peerDependencies": {
"react": "^16.8.6 || ^17"
},
"devDependencies": {
"prettier-eslint": "^9.0.0",
"react": "^17.0.2",
"rollup": "^2.48.0",
"uppercamelcase": "^3.0.0"
}
}