Skip to content

Commit

Permalink
Update build.js
Browse files Browse the repository at this point in the history
  • Loading branch information
resetsix authored Jul 19, 2024
1 parent 60a3cfe commit a9f10d3
Showing 1 changed file with 55 additions and 58 deletions.
113 changes: 55 additions & 58 deletions bin/build.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
/* eslint-disable import/no-extraneous-dependencies */
/* eslint-disable prefer-template */
const path = require('path');
const fs = require('fs');
const format = require('prettier-eslint');
const processSvg = require('./processSvg');
const { parseName } = require('./utils');
const defaultStyle = process.env.npm_package_config_style || 'stroke';
const { getAttrs, getElementCode } = require('./template');
const icons = require('../src/data.json');
const rootDir = path.join(__dirname, '..');
const path = require('path')
const fs = require('fs')
const format = require('prettier-eslint')
const processSvg = require('./processSvg')
const { parseName } = require('./utils')
const defaultStyle = process.env.npm_package_config_style || 'stroke'
const { getAttrs, getElementCode } = require('./template')
const icons = require('../src/data.json')
const rootDir = path.join(__dirname, '..')
// where icons code in
const srcDir = path.join(rootDir, 'src');
const iconsDir = path.join(rootDir, 'src/icons');
const srcDir = path.join(rootDir, 'src')
const iconsDir = path.join(rootDir, 'src/icons')

// generate icons.js and icons.d.ts file
const generateIconsIndex = () => {
if (!fs.existsSync(srcDir)) {
fs.mkdirSync(srcDir);
fs.mkdirSync(iconsDir);
fs.mkdirSync(srcDir)
fs.mkdirSync(iconsDir)
} else if (!fs.existsSync(iconsDir)) {
fs.mkdirSync(iconsDir);
fs.mkdirSync(iconsDir)
}
const initialTypeDefinitions = `/// <reference types="react" />
import { ComponentType, SVGAttributes } from 'react';
interface Props extends SVGAttributes<SVGElement> {
color?: string;
size?: string | number;
}
type Icon = ComponentType<Props>;
`;
import { ComponentType, SVGAttributes } from 'react';
interface Props extends SVGAttributes<SVGElement> {
color?: string;
size?: string | number;
}
type Icon = ComponentType<Props>;
`;
fs.writeFileSync(path.join(rootDir, 'src', 'icons.js'), '', 'utf-8');
fs.writeFileSync(
path.join(rootDir, 'src', 'icons.d.ts'),
initialTypeDefinitions,
'utf-8',
);
};
}

// generate attributes code
const attrsToString = (attrs, style) => {
console.log('style: ', style);
console.log('style: ', style)
return Object.keys(attrs).map((key) => {
// should distinguish fill or stroke
if (key === 'width' || key === 'height' || key === style) {
Expand All @@ -53,23 +53,23 @@ const attrsToString = (attrs, style) => {
};

// generate icon code separately
const generateIconCode = async ({ name }) => {
const names = parseName(name, defaultStyle);
console.log(names);
const fileName = names.name.replace(/[^a-zA-Z0-9]/g, ''); // Remove non-alphanumeric characters
const location = path.join(rootDir, 'src/svg', `${fileName}.svg`);
const destination = path.join(rootDir, 'src/icons', `${fileName}.js`);
const generateIconCode = async ({name}) => {
const names = parseName(name, defaultStyle)
console.log(names)
const fileName = names.name.replace(/[^a-zA-Z0-9-_]/g, '_') // Replace non-alphanumeric characters (except - and _) with underscore
const location = path.join(rootDir, 'src/svg', `${fileName}.svg`)
const destination = path.join(rootDir, 'src/icons', `${fileName}.js`)

try {
if (!fs.existsSync(location)) {
console.error(`SVG file not found: ${location}`);
return null;
console.error(`SVG file not found: ${location}`)
return null
}

const code = fs.readFileSync(location, 'utf8');
const svgCode = await processSvg(code);
const ComponentName = names.componentName;
const element = getElementCode(ComponentName, attrsToString(getAttrs(names.style), names.style), svgCode);
const code = fs.readFileSync(location, 'utf-8')
const svgCode = await processSvg(code)
const ComponentName = names.componentName
const element = getElementCode(ComponentName, attrsToString(getAttrs(names.style), names.style), svgCode)
const component = format({
text: element,
eslintConfig: {
Expand All @@ -83,15 +83,15 @@ const generateIconCode = async ({ name }) => {
});
fs.writeFileSync(destination, component, 'utf-8');
console.log('Successfully built', ComponentName);
return { ComponentName, name: fileName };
return {ComponentName, name: fileName}
} catch (error) {
console.error(`Error generating code for icon: ${name}`, error);
return null;
console.error(`Error generating code for icon: ${name}`, error)
return null
}
};
}

// append export code to icons.js
const appendToIconsIndex = ({ ComponentName, name }) => {
const appendToIconsIndex = ({ComponentName, name}) => {
const exportString = `export { default as ${ComponentName} } from './icons/${name}';\r\n`;
fs.appendFileSync(
path.join(rootDir, 'src', 'icons.js'),
Expand All @@ -104,24 +104,21 @@ const appendToIconsIndex = ({ ComponentName, name }) => {
exportTypeString,
'utf-8',
);
};
}

generateIconsIndex();
generateIconsIndex()

Promise.all(
Object
.keys(icons)
.map(key => icons[key])
.map(({ name }) => generateIconCode({ name }))
)
.then(results => {
results.filter(Boolean).forEach(result => {
if (result) {
appendToIconsIndex(result);
}
});
console.log('Icon generation completed.');
Object
.keys(icons)
.map(key => icons[key])
.forEach(({name}) => {
generateIconCode({name})
.then((result) => {
if (result) {
appendToIconsIndex(result)
}
})
.catch((error) => {
console.error(`Error processing icon: ${name}`, error)
})
})
.catch(error => {
console.error('Error during icon generation:', error);
});

0 comments on commit a9f10d3

Please sign in to comment.