-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.js
45 lines (36 loc) · 1.13 KB
/
tree.js
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
const fs = require('fs');
const util = require('util');
const dirTree = require('directory-tree');
(async function() {
try {
async function convertFiles(files) {
if (!files) {
return [];
}
const items = [];
for (const file of files) {
const content = await readFile(file.path);
items.push({
content,
name: getFileName(file.name),
});
}
return items;
}
const readFile = (fileName) => {
return util.promisify(fs.readFile)(fileName, 'utf8');
};
const getFileName = (fileName) => {
return fileName.replace(/\.svg/g, '');
};
const { children } = dirTree('./src/assets/icons');
const icons = {
items: await convertFiles(children),
};
fs.writeFile('icons.json', JSON.stringify(icons), 'utf8', function() {
console.log('JSON with icons has been successfully created');
});
} catch (error) {
throw new Error(error);
}
})();