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
|
import fs from 'node:fs/promises';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
const assetFolders = ['icons', 'images', 'svgs/brands', 'svgs/ext/Extras', 'svgs/ext/Code'];
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
await Promise.all(
assetFolders.map(async (folder) => {
const indexFilePath = join(__dirname, '..', folder, 'index.ts');
const assetsFolderPath = join(__dirname, '..', folder);
if (
await fs.access(indexFilePath).then(
() => true,
() => false,
)
)
await fs.unlink(indexFilePath);
const fileNames = await fs.readdir(assetsFolderPath);
const assetImports = fileNames
.filter(fileName => fileName !== 'index.ts' && !/(^|\/)\.[^\/\.]/g.test(fileName))
.map((fileName) => {
const variableName = fileName.split('.')[0].replace(/-/g, '');
if (folder.startsWith('svgs'))
return `import { ReactComponent as ${variableName} } from './${fileName}';`;
return `import ${variableName} from './${fileName}';`;
})
.join('\n');
const assetExports = fileNames
.filter(fileName => fileName !== 'index.ts' && !/(^|\/)\.[^\/\.]/g.test(fileName))
.map(fileName => `${fileName.split('.')[0].replace(/-/g, '')}`)
.join(',\n');
const indexFileContent
= `
/*
* This file was automatically generated by a script.
* To regenerate this file, run: pnpm assets gen
*/
${assetImports}
export {
${assetExports}
};`
;
await fs.writeFile(indexFilePath, indexFileContent);
}),
);
|