refactoring: use Promise.all (#42219)

* chore: use Promise.all

* del type

* fix
This commit is contained in:
lijianan 2023-05-08 19:48:24 +08:00 committed by GitHub
parent 4271ff0976
commit a4669ace4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,15 +1,14 @@
import { globSync } from 'glob';
import path from 'path';
import type { FC } from 'react';
import React from 'react';
type StyleFn = (prefix?: string) => void;
type GenCssinjs = (options: {
interface GenCssinjsOptions {
key: string;
render: (component: FC) => void;
render: (component: React.FC) => void;
beforeRender?: (componentName: string) => void;
}) => Promise<void>;
}
export const styleFiles = globSync(
path.join(
@ -18,29 +17,27 @@ export const styleFiles = globSync(
),
);
export const generateCssinjs: GenCssinjs = async ({ key, beforeRender, render }) => {
// eslint-disable-next-line no-restricted-syntax
for (const file of styleFiles) {
const pathArr = file.split('/');
const styleIndex = pathArr.lastIndexOf('style');
const componentName = pathArr[styleIndex - 1];
let useStyle: StyleFn = () => {};
if (file.includes('grid')) {
// eslint-disable-next-line no-await-in-loop
const { useColStyle, useRowStyle } = await import(file);
useStyle = (prefixCls: string) => {
useRowStyle(prefixCls);
useColStyle(prefixCls);
export const generateCssinjs = ({ key, beforeRender, render }: GenCssinjsOptions) =>
Promise.all(
styleFiles.map(async (file) => {
const pathArr = file.split('/');
const styleIndex = pathArr.lastIndexOf('style');
const componentName = pathArr[styleIndex - 1];
let useStyle: StyleFn = () => {};
if (file.includes('grid')) {
const { useColStyle, useRowStyle } = await import(file);
useStyle = (prefixCls: string) => {
useRowStyle(prefixCls);
useColStyle(prefixCls);
};
} else {
useStyle = (await import(file)).default;
}
const Demo: React.FC = () => {
useStyle(`${key}-${componentName}`);
return React.createElement('div');
};
} else {
// eslint-disable-next-line no-await-in-loop
useStyle = (await import(file)).default;
}
const Demo: FC = () => {
useStyle(`${key}-${componentName}`);
return React.createElement('div');
};
beforeRender?.(componentName);
render?.(Demo);
}
};
beforeRender?.(componentName);
render?.(Demo);
}),
);