2023-04-29 10:43:06 +08:00
|
|
|
import fs from 'fs-extra';
|
|
|
|
import type { DeclarationReflection } from 'typedoc';
|
|
|
|
import { Application, TSConfigReader, TypeDocReader } from 'typedoc';
|
2022-11-25 14:23:11 +08:00
|
|
|
|
2023-08-27 22:52:43 +08:00
|
|
|
interface TokenMeta {
|
2023-07-05 17:49:59 +08:00
|
|
|
seed: ReturnType<typeof getTokenList>;
|
|
|
|
map: ReturnType<typeof getTokenList>;
|
|
|
|
alias: ReturnType<typeof getTokenList>;
|
|
|
|
components: Record<string, ReturnType<typeof getTokenList>>;
|
2023-08-27 22:52:43 +08:00
|
|
|
}
|
2023-07-05 17:49:59 +08:00
|
|
|
|
2023-04-29 10:43:06 +08:00
|
|
|
function getTokenList(list?: DeclarationReflection[], source?: string) {
|
|
|
|
return (list || [])
|
2022-12-26 10:54:53 +08:00
|
|
|
.filter(
|
|
|
|
(item) =>
|
2023-07-05 17:49:59 +08:00
|
|
|
!item.comment?.blockTags.some(
|
|
|
|
(tag) => tag.tag === '@internal' || tag.tag === '@private' || tag.tag === '@deprecated',
|
|
|
|
),
|
2022-12-26 10:54:53 +08:00
|
|
|
)
|
2022-11-25 14:23:11 +08:00
|
|
|
.map((item) => ({
|
|
|
|
source,
|
2022-11-30 11:48:47 +08:00
|
|
|
token: item.name,
|
2023-04-29 10:43:06 +08:00
|
|
|
type: item?.type?.toString(),
|
2022-12-02 14:18:14 +08:00
|
|
|
desc:
|
|
|
|
item.comment?.blockTags
|
|
|
|
?.find((tag) => tag.tag === '@desc')
|
2022-12-07 20:22:15 +08:00
|
|
|
?.content.reduce((result, str) => result.concat(str.text), '') || '',
|
2022-11-25 14:23:11 +08:00
|
|
|
descEn:
|
2022-12-02 14:18:14 +08:00
|
|
|
item.comment?.blockTags
|
|
|
|
?.find((tag) => tag.tag === '@descEN')
|
2022-12-07 20:22:15 +08:00
|
|
|
?.content.reduce((result, str) => result.concat(str.text), '') || '',
|
2022-12-02 14:18:14 +08:00
|
|
|
name:
|
|
|
|
item.comment?.blockTags
|
|
|
|
?.find((tag) => tag.tag === '@nameZH')
|
2022-12-07 20:22:15 +08:00
|
|
|
?.content.reduce((result, str) => result.concat(str.text), '') || '',
|
2022-11-30 11:48:47 +08:00
|
|
|
nameEn:
|
2022-12-02 14:18:14 +08:00
|
|
|
item.comment?.blockTags
|
|
|
|
?.find((tag) => tag.tag === '@nameEN')
|
2022-12-07 20:22:15 +08:00
|
|
|
?.content.reduce((result, str) => result.concat(str.text), '') || '',
|
2022-11-25 14:23:11 +08:00
|
|
|
}));
|
2023-04-29 10:43:06 +08:00
|
|
|
}
|
2022-11-25 14:23:11 +08:00
|
|
|
|
2023-08-27 22:52:43 +08:00
|
|
|
const main = async () => {
|
|
|
|
const app = await (Application as any).bootstrap(
|
|
|
|
{
|
|
|
|
// typedoc options here
|
|
|
|
entryPoints: ['components/theme/interface/index.ts', 'components/*/style/index.{ts,tsx}'],
|
|
|
|
skipErrorChecking: true,
|
2024-06-28 15:02:59 +08:00
|
|
|
logLevel: 'Error',
|
2023-08-27 22:52:43 +08:00
|
|
|
},
|
|
|
|
[new TSConfigReader(), new TypeDocReader()],
|
|
|
|
);
|
2022-11-25 14:23:11 +08:00
|
|
|
|
2023-08-27 22:52:43 +08:00
|
|
|
const project = await app.convert();
|
2022-11-25 14:23:11 +08:00
|
|
|
|
|
|
|
if (project) {
|
|
|
|
// Project may not have converted correctly
|
|
|
|
const output = 'components/version/token-meta.json';
|
2023-07-05 17:49:59 +08:00
|
|
|
const tokenMeta: TokenMeta = {
|
|
|
|
seed: [],
|
|
|
|
map: [],
|
|
|
|
alias: [],
|
|
|
|
components: {},
|
|
|
|
};
|
2022-11-25 14:23:11 +08:00
|
|
|
|
2023-08-27 22:52:43 +08:00
|
|
|
project?.children?.forEach((file: any) => {
|
2023-07-05 17:49:59 +08:00
|
|
|
// Global Token
|
|
|
|
if (file.name === 'theme/interface') {
|
|
|
|
let presetColors: string[] = [];
|
2023-08-27 22:52:43 +08:00
|
|
|
file.children?.forEach((type: any) => {
|
2023-07-05 17:49:59 +08:00
|
|
|
if (type.name === 'SeedToken') {
|
|
|
|
tokenMeta.seed = getTokenList(type.children, 'seed');
|
|
|
|
} else if (type.name === 'MapToken') {
|
|
|
|
tokenMeta.map = getTokenList(type.children, 'map');
|
|
|
|
} else if (type.name === 'AliasToken') {
|
|
|
|
tokenMeta.alias = getTokenList(type.children, 'alias');
|
|
|
|
} else if (type.name === 'PresetColors') {
|
|
|
|
presetColors = (type?.type as any)?.target?.elements?.map((item: any) => item.value);
|
|
|
|
}
|
|
|
|
});
|
2022-11-25 14:23:11 +08:00
|
|
|
|
2023-07-05 17:49:59 +08:00
|
|
|
// Exclude preset colors
|
|
|
|
tokenMeta.seed = tokenMeta.seed.filter(
|
|
|
|
(item) => !presetColors.some((color) => item.token.startsWith(color)),
|
|
|
|
);
|
|
|
|
tokenMeta.map = tokenMeta.map.filter(
|
|
|
|
(item) => !presetColors.some((color) => item.token.startsWith(color)),
|
|
|
|
);
|
|
|
|
tokenMeta.alias = tokenMeta.alias.filter(
|
|
|
|
(item) => !presetColors.some((color) => item.token.startsWith(color)),
|
|
|
|
);
|
|
|
|
|
|
|
|
tokenMeta.alias = tokenMeta.alias.filter(
|
|
|
|
(item) => !tokenMeta.map.some((mapItem) => mapItem.token === item.token),
|
|
|
|
);
|
|
|
|
tokenMeta.map = tokenMeta.map.filter(
|
|
|
|
(item) => !tokenMeta.seed.some((seedItem) => seedItem.token === item.token),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
const component = file.name
|
|
|
|
.slice(0, file.name.indexOf('/'))
|
2023-08-27 22:52:43 +08:00
|
|
|
.replace(/(^(.)|-(.))/g, (match: string) => match.replace('-', '').toUpperCase());
|
|
|
|
const componentToken = file.children?.find((item: any) => item?.name === 'ComponentToken');
|
2023-07-05 17:49:59 +08:00
|
|
|
if (componentToken) {
|
|
|
|
tokenMeta.components[component] = getTokenList(componentToken.children, component);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2022-11-25 14:23:11 +08:00
|
|
|
|
2024-06-22 23:57:39 +08:00
|
|
|
const finalMeta = Object.entries(tokenMeta).reduce<any>((acc, [key, value]) => {
|
2023-07-05 17:49:59 +08:00
|
|
|
if (key !== 'components') {
|
|
|
|
(value as any[]).forEach((item) => {
|
|
|
|
acc.global = acc.global || {};
|
|
|
|
acc.global[item.token] = {
|
|
|
|
name: item.name,
|
|
|
|
nameEn: item.nameEn,
|
|
|
|
desc: item.desc,
|
|
|
|
descEn: item.descEn,
|
|
|
|
type: item.type,
|
|
|
|
source: key,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
acc.components = value;
|
|
|
|
}
|
2022-12-01 20:52:35 +08:00
|
|
|
return acc;
|
2024-06-22 23:57:39 +08:00
|
|
|
}, {});
|
2022-12-01 20:52:35 +08:00
|
|
|
|
|
|
|
fs.writeJsonSync(output, finalMeta, 'utf8');
|
2024-09-19 03:30:19 +08:00
|
|
|
|
2022-11-25 14:23:11 +08:00
|
|
|
console.log(`✅ Token Meta has been written to ${output}`);
|
|
|
|
}
|
2023-04-29 10:43:06 +08:00
|
|
|
};
|
2022-11-25 14:23:11 +08:00
|
|
|
|
|
|
|
main();
|