-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.ts
More file actions
55 lines (46 loc) · 1.67 KB
/
sort.ts
File metadata and controls
55 lines (46 loc) · 1.67 KB
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
import fs from 'fs';
import _ from 'lodash';
import { getVariants } from './utils.js';
const getDirectories = (source: string): string[] => {
return fs
.readdirSync(source, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name);
};
const getFiles = (source: string): string[] => {
return fs.readdirSync(source);
};
const iconFolders = getDirectories('./icons');
for (const slug of iconFolders) {
const iconNames = getFiles(`./icons/${slug}`);
const variants = getVariants(slug).map((variant) => _.snakeCase(variant));
const sortedIconTable: {
[key: string]: string[];
} = {};
for (const iconName of iconNames) {
const rawName = ('_' + _.snakeCase(iconName.replace('.svg', '')) + '_')
.replace(new RegExp(`_(?:${variants.join('|')})_`, 'g'), '_')
.replace(/^_+|_+$/g, '');
if (sortedIconTable[rawName] === undefined) {
sortedIconTable[rawName] = [];
}
sortedIconTable[rawName].push(iconName);
}
for (const rawName in sortedIconTable) {
if (sortedIconTable[rawName].length <= 1) continue;
const folderName = _.upperFirst(_.camelCase(rawName));
const newDirPath = `./icons/${slug}/${folderName}`;
if (!fs.existsSync(newDirPath)) {
fs.mkdirSync(newDirPath);
}
for (const svgFileName of sortedIconTable[rawName]) {
fs.rename(
`./icons/${slug}/${svgFileName}`,
`./icons/${slug}/${folderName}/${svgFileName}`,
(err) => {
if (err) throw err;
}
);
}
}
}