Skip to content

Commit 327a074

Browse files
committed
refactor: Always use helper file
1 parent a784a84 commit 327a074

File tree

14 files changed

+80
-45
lines changed

14 files changed

+80
-45
lines changed

snapshots/all-in-one-js/Icon.js

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import React from 'react';
44
import { Svg, Path } from 'react-native-svg';
5+
import { getIconColor } from './helper';
56

67

78
// If you don't want to make all icons in one file,
@@ -94,20 +95,4 @@ Icon.defaultProps = {
9495
size: 14,
9596
};
9697

97-
/**
98-
* @param {string | string[] | undefined} color
99-
* @param {number} index
100-
* @param {string} defaultColor
101-
* @return {string}
102-
*/
103-
const getIconColor = (color, index, defaultColor) => {
104-
return color
105-
? (
106-
typeof color === 'string'
107-
? color
108-
: color[index] || defaultColor
109-
)
110-
: defaultColor;
111-
};
112-
11398
export default Icon;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/* eslint-disable */
2+
3+
export const getIconColor: (color: string | string[] | undefined, index: number, defaultColor: string) => string;

snapshots/all-in-one-js/helper.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* eslint-disable */
2+
3+
/**
4+
* @param {string | string[] | undefined} color
5+
* @param {number} index
6+
* @param {string} defaultColor
7+
* @return {string}
8+
*/
9+
export const getIconColor = (color, index, defaultColor) => {
10+
return color
11+
? (
12+
typeof color === 'string'
13+
? color
14+
: color[index] || defaultColor
15+
)
16+
: defaultColor;
17+
};

snapshots/all-in-one-ts/Icon.tsx

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import React, { FunctionComponent } from 'react';
55
import { ViewProps } from 'react-native';
66
import { Svg, GProps, Path } from 'react-native-svg';
7+
import { getIconColor } from './helper';
78

89

910
export type IconNames = 'alipay' | 'user' | 'setup';
@@ -104,14 +105,4 @@ Icon.defaultProps = {
104105
size: 16,
105106
};
106107

107-
const getIconColor = (color: string | string[] | undefined, index: number, defaultColor: string) => {
108-
return color
109-
? (
110-
typeof color === 'string'
111-
? color
112-
: color[index] || defaultColor
113-
)
114-
: defaultColor;
115-
};
116-
117108
export default Icon;

snapshots/all-in-one-ts/helper.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* tslint:disable */
2+
/* eslint-disable */
3+
4+
export const getIconColor = (color: string | string[] | undefined, index: number, defaultColor: string) => {
5+
return color
6+
? (
7+
typeof color === 'string'
8+
? color
9+
: color[index] || defaultColor
10+
)
11+
: defaultColor;
12+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/* eslint-disable */
2+
3+
export const getIconColor: (color: string | string[] | undefined, index: number, defaultColor: string) => string;

src/libs/copyTemplate.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
4+
export const copyTemplate = (fromFile: string, toFile: string) => {
5+
return fs.copyFileSync(
6+
path.join(__dirname, `../templates/${fromFile}.template`),
7+
toFile,
8+
);
9+
};

src/libs/generateComponent.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,22 @@ import { XmlData } from 'iconfont-parser';
88
import { Config } from './getConfig';
99
import { getTemplate } from './getTemplate';
1010
import {
11-
replaceCases, replaceColorFunc,
11+
replaceCases,
1212
replaceComponentName,
1313
replaceImports,
1414
replaceNames,
15-
replaceNamesArray, replaceNoColor,
15+
replaceNamesArray,
1616
replaceSingleIconContent,
1717
replaceSize, replaceSummaryIcon,
1818
replaceSvgComponents,
1919
replaceToDependsComments,
2020
replaceToOneComments,
21+
replaceNoHelper,
22+
replaceHelper,
2123
} from './replace';
2224
import { whitespace } from './whitespace';
2325
import { GENERATE_MODE } from './generateMode';
26+
import { copyTemplate } from './copyTemplate';
2427

2528
const SVG_MAP = {
2629
path: 'Path',
@@ -48,11 +51,9 @@ export const generateComponent = (data: XmlData, config: Config) => {
4851
svgComponents.add('GProps');
4952
}
5053

51-
if (config.generate_mode === GENERATE_MODE.dependsOn) {
52-
fs.copyFileSync(
53-
path.join(__dirname, '..', 'templates', `helper${jsExtension}.template`),
54-
path.join(saveDir, `helper${jsExtension}`),
55-
);
54+
copyTemplate(`helper${jsExtension}`, path.join(saveDir, `helper${jsExtension}`));
55+
if (!config.use_typescript) {
56+
copyTemplate('helper.d.ts', path.join(saveDir, 'helper.d.ts'));
5657
}
5758

5859
data.svg.symbol.forEach((item) => {
@@ -104,6 +105,7 @@ export const generateComponent = (data: XmlData, config: Config) => {
104105
singleFile = replaceComponentName(singleFile, componentName);
105106
singleFile = replaceSingleIconContent(singleFile, generateCase(item, 4));
106107
singleFile = replaceToOneComments(singleFile);
108+
singleFile = replaceHelper(singleFile);
107109

108110
fs.writeFileSync(path.join(saveDir, componentName + jsxExtension), singleFile);
109111

@@ -119,6 +121,12 @@ export const generateComponent = (data: XmlData, config: Config) => {
119121

120122
let iconFile = getTemplate('Icon' + jsxExtension);
121123

124+
if (config.generate_mode === GENERATE_MODE.dependsOn) {
125+
iconFile = replaceNoHelper(iconFile);
126+
} else {
127+
iconFile = replaceHelper(iconFile);
128+
}
129+
122130
iconFile = replaceSize(iconFile, config.default_icon_size);
123131
iconFile = replaceCases(iconFile, cases);
124132
iconFile = replaceSvgComponents(iconFile, svgComponents);
@@ -138,10 +146,8 @@ export const generateComponent = (data: XmlData, config: Config) => {
138146

139147
if (config.generate_mode === GENERATE_MODE.allInOne) {
140148
iconFile = replaceToDependsComments(iconFile);
141-
iconFile = replaceColorFunc(iconFile, jsExtension);
142149
} else {
143150
iconFile = replaceToOneComments(iconFile);
144-
iconFile = replaceNoColor(iconFile);
145151
}
146152

147153
iconFile = replaceSummaryIcon(iconFile, config.summary_component_name);

src/libs/replace.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { GENERATE_MODE } from './generateMode';
2-
import { getTemplate } from './getTemplate';
32

43
export const replaceSize = (content: string, size: number) => {
54
return content.replace(/#size#/g, String(size));
@@ -61,12 +60,17 @@ export const replaceToDependsComments = (content: string) => {
6160
);
6261
};
6362

64-
export const replaceColorFunc = (content: string, extension: string) => {
63+
export const replaceHelper = (content: string) => {
6564
return content.replace(
66-
/#colorFunc#/,
67-
getTemplate(`helper${extension}`)
68-
.replace(/export\s+/g, '')
69-
.replace(/\/\*\s.+?\s\*\/\n/g, '')
65+
/#helper#/g,
66+
'import { getIconColor } from \'./helper\';'
67+
);
68+
};
69+
70+
export const replaceNoHelper = (content: string) => {
71+
return content.replace(
72+
/#helper#\n/g,
73+
''
7074
);
7175
};
7276

src/templates/Icon.js.template

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import React from 'react';
44
#svgComponents#
5+
#helper#
56
#imports#
67

78
#comments#
@@ -16,5 +17,5 @@ export const #SummaryIcon# = ({ color, name, size, ...rest }) => {
1617
#SummaryIcon#.defaultProps = {
1718
size: #size#,
1819
};
19-
#colorFunc#
20+
2021
export default #SummaryIcon#;

0 commit comments

Comments
 (0)