Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate the AddItems icon #2099

Merged
merged 2 commits into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sweet-mice-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sumup/icons': minor
---

Deprecated the AddItems icon. Use the Add or Items icons instead.
56 changes: 19 additions & 37 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/icons/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
{
"name": "add_items",
"category": "Action",
"size": "24"
"size": "24",
"deprecation": "Use the `Add` or `Items` icons instead."
},
{
"name": "chevron_down",
Expand Down
6 changes: 2 additions & 4 deletions packages/icons/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,9 @@
"@babel/preset-env": "^7.21.5",
"@babel/preset-react": "^7.18.6",
"@types/babel__core": "^7.20.0",
"@types/dedent": "^0.7.0",
"@types/lodash": "^4.14.194",
"@types/prettier": "^2.7.2",
"babel-plugin-inline-react-svg": "^2.0.2",
"dedent": "^0.7.0",
"lodash": "^4.17.21",
"prettier": "^2.8.8",
"ts-node": "^10.9.1",
"ts-node-dev": "^2.0.0",
"typescript": "^5.0.4"
Expand Down
63 changes: 38 additions & 25 deletions packages/icons/scripts/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
import fs from 'fs';
import path from 'path';

import dedent from 'dedent';
import { entries, flow, groupBy, map } from 'lodash/fp';
connor-baer marked this conversation as resolved.
Show resolved Hide resolved
import prettier from 'prettier';
import { transformSync } from '@babel/core';

import manifest from '../manifest.json';
Expand All @@ -26,23 +25,29 @@ const BASE_DIR = path.join(__dirname, '..');
const ICON_DIR = path.join(BASE_DIR, './web/v2');
const DIST_DIR = path.join(BASE_DIR, 'dist');

enum IconSize {
SIZE_16 = '16',
SIZE_24 = '24',
SIZE_32 = '32',
}

type Icon = {
name: string;
category: string;
size: IconSize;
size: '16' | '24' | '32';
deprecation?: string;
};

type Component = {
name: string;
icons: Icon[];
deprecation?: string;
};

function createDeprecationComment(deprecation?: string) {
if (!deprecation) {
return '';
}
return `
/**
* @deprecated ${deprecation}
*/`;
}

function getComponentName(name: string): string {
// Split on non-word characters
const words = name.split(/[^a-z0-9]/i);
Expand Down Expand Up @@ -78,17 +83,16 @@ function buildComponentFile(component: Component): string {
/**
* TODO look into whether we still need the React import here
*/
return dedent`
return `
import React from 'react';
${iconImports.join('\n')}

const sizeMap = {
${sizeMap.join('\n')}
}

export const ${
component.name
} = ({ size = '${defaultSize}', ...props }) => {
${createDeprecationComment(component.deprecation)}
export function ${component.name}({ size = '${defaultSize}', ...props }) {
const Icon = sizeMap[size] || sizeMap['${defaultSize}'];

if (
Expand All @@ -114,11 +118,13 @@ function buildDeclarationFile(components: Component[]): string {
const declarationStatements = components.map((component) => {
const sizes = component.icons.map(({ size }) => `'${size}'`).sort();
const SizesType = sizes.join(' | ');
return `declare const ${component.name}: FC<IconProps<${SizesType}>>;`;
return `
${createDeprecationComment(component.deprecation)}
declare const ${component.name}: FC<IconProps<${SizesType}>>;`;
});
const exportNames = components.map((file) => file.name);
return dedent`
import { FC, SVGProps } from 'react';
return `
import type { FC, SVGProps } from 'react';

export interface IconProps<Sizes = '16' | '24' | '32'> extends SVGProps<SVGSVGElement> {
/**
Expand All @@ -136,6 +142,7 @@ function buildDeclarationFile(components: Component[]): string {
name: string;
category: string;
size: '16' | '24' | '32';
deprecation?: boolean;
}[];
};
`;
Expand Down Expand Up @@ -166,25 +173,31 @@ function transpileModule(fileName: string, code: string): void {
function writeFile(dir: string, fileName: string, fileContent: string): void {
const filePath = path.join(dir, fileName);
const directory = path.dirname(filePath);
const formattedContent = prettier.format(fileContent, { filepath: filePath });
if (directory && directory !== '.') {
fs.mkdirSync(directory, { recursive: true });
}
return fs.writeFile(filePath, fileContent, { flag: 'w' }, (err) => {
return fs.writeFile(filePath, formattedContent, { flag: 'w' }, (err) => {
if (err) {
throw err;
}
});
}

function main(): void {
const components = flow(
groupBy('name'),
entries,
map((group: [string, Icon[]]) => ({
name: getComponentName(group[0]),
icons: group[1],
})),
)(manifest.icons) as Component[];
const icons = manifest.icons as Icon[];
const iconsByName = icons.reduce((acc, icon) => {
acc[icon.name] = acc[icon.name] || [];
acc[icon.name].push(icon);
return acc;
}, {} as Record<string, Icon[]>);
const components = Object.entries(iconsByName).map(
([name, icons]): Component => ({
name: getComponentName(name),
icons,
deprecation: icons.find((icon) => icon.deprecation)?.deprecation,
}),
);

const indexRaw = buildIndexFile(components);
const declarationFile = buildDeclarationFile(components);
Expand Down