mirror of
https://github.com/iconoir-icons/iconoir
synced 2024-06-07 16:32:26 +02:00
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:
parent
a0abf62f72
commit
ff280f55ed
3
packages/iconoir-react/.gitignore
vendored
Normal file
3
packages/iconoir-react/.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
node_modules
|
||||
build
|
||||
dist
|
110
packages/iconoir-react/bin/build.js
Normal file
110
packages/iconoir-react/bin/build.js
Normal 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
1324
packages/iconoir-react/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
32
packages/iconoir-react/package.json
Normal file
32
packages/iconoir-react/package.json
Normal 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"
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue