generated from adobe/aem-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvite.helpers.ts
58 lines (49 loc) · 1.99 KB
/
vite.helpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { parse, resolve } from 'path';
import { existsSync, readdirSync, writeFileSync } from 'fs';
import { execSync } from 'child_process';
import { config } from './config.ts';
const getBlockEntry = (blockName: string, fileType: string): string | null => {
const filePath = resolve(__dirname, `src/blocks/${blockName}/${blockName}.${fileType}`);
return existsSync(filePath) ? filePath : null;
};
const getTsEntry = (blockName: string): Record<string, string> | null => {
const tsPath = getBlockEntry(blockName, 'ts');
return tsPath !== null ? { [blockName]: tsPath } : null;
};
const getBlockNamesFromSrcFolder = (): string[] => {
const blocksPath = resolve(__dirname, 'src/blocks');
try {
return readdirSync(blocksPath);
} catch (error) {
console.error(`Failed to read directory at ${blocksPath}`, error);
return [];
}
};
export const generateBlockEntries = () => {
const blockNames = getBlockNamesFromSrcFolder();
let entries = {};
blockNames.forEach((blockName) => {
const tsEntry = getTsEntry(blockName);
entries = { ...entries, ...tsEntry };
});
return entries;
};
export const generateIconNameType = () => {
try {
const { iconsDirPath, iconsTypesPath } = config;
const iconFiles = readdirSync(iconsDirPath).filter((file) => file.endsWith('.svg'));
const icons = iconFiles.map((file) => parse(file).name.replace(/[^a-zA-Z0-9-]/g, ''));
const typeComment = `/*
* DO NOT EDIT THIS FILE DIRECTLY.
* This file is auto-generated by the vite.helpers.ts script.
*/`;
const typeDefinition = `${typeComment}
export type IconName = '${icons.join("' | \n'")}';\n`;
writeFileSync(iconsTypesPath, typeDefinition);
const prettierConfigPath = './.prettierrc';
execSync(`npx prettier --write ${iconsTypesPath} --config ${prettierConfigPath}`);
console.log('Icon type definitions generated and formatted successfully.');
} catch (error) {
console.error('Error generating and formatting icon type definitions:', error);
}
};