forked from availproject/avail-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
robohash.cjs
executable file
·119 lines (90 loc) · 3.19 KB
/
robohash.cjs
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2017-2024 @polkadot/react-components authors & contributors
// SPDX-License-Identifier: Apache-2.0
const fs = require('node:fs');
const path = require('node:path');
const HEADER = `// Copyright 2017-2024 @polkadot/react-components authors & contributors
// SPDX-License-Identifier: Apache-2.0
// Automatically generated, do not edit
/* eslint-disable simple-import-sort/imports */`;
const PATH = 'packages/react-components/src/IdentityIcon/RoboHash';
/**
* @param {number} index
* @returns {string}
*/
function getCounter (index) {
return `000${index}`.slice(-3);
}
/**
* @param {string} dir
* @returns {string[]}
*/
function getFiles (dir) {
const genpath = path.join(dir, 'generated');
if (!fs.existsSync(genpath)) {
fs.mkdirSync(genpath, { recursive: true });
}
const all = fs
.readdirSync(dir)
.filter((entry) => {
if (entry.endsWith('.ts')) {
fs.rmSync(path.join(dir, entry), { force: true });
return false;
}
return !entry.startsWith('.') && entry !== 'generated';
})
.map((entry) => {
if (entry.includes('#')) {
const newName = entry.replace(/#/g, '-');
fs.renameSync(path.join(dir, entry), path.join(dir, newName));
return newName;
}
return entry;
})
.sort((a, b) =>
(a.includes('-') && b.includes('-'))
? a.split('-')[1].localeCompare(b.split('-')[1])
: 0
);
for (const f of all) {
if (f.endsWith('.png')) {
fs.writeFileSync(path.join(dir, `generated/${f}`).replace('.png', '.ts'), `${HEADER}\n\nexport default 'data:image/png;base64,${fs.readFileSync(path.join(dir, f)).toString('base64')}';\n`);
}
}
return all;
}
function extractBg () {
const root = path.join(__dirname, '..', PATH, 'backgrounds');
/** @type {string[]} */
const files = [];
getFiles(root).forEach((sub) => {
getFiles(path.join(root, sub)).forEach((entry) => files.push(`./${sub}/generated/${entry}`));
});
fs.writeFileSync(path.join(root, 'index.ts'), `${HEADER}\n\n${files.map((file, index) => `import b${getCounter(index)} from '${file.replace('.png', '')}';`).join('\n')}\n\nexport default [${files.map((_, index) => `b${getCounter(index)}`).join(', ')}];\n`);
}
function extractSets () {
const root = path.join(__dirname, '..', PATH, 'sets');
const sets = getFiles(root).map((sub) =>
getFiles(path.join(root, sub)).map((dir) =>
getFiles(path.join(root, sub, dir)).map((entry) => `./${sub}/${dir}/generated/${entry}`)
)
);
/** @type {string[]} */
const imports = [];
let list = '[';
sets.forEach((areas, sindex) => {
list = `${list}${sindex ? ',' : ''}\n [`;
areas.forEach((files, aindex) => {
const indexes = files.map((file, findex) => {
const index = `s${getCounter(sindex)}${getCounter(aindex)}${getCounter(findex)}`;
imports.push(`import ${index} from '${file.replace('.png', '')}';`);
return index;
});
list = `${list}${aindex ? ',' : ''}\n [${indexes.join(', ')}]`;
});
list = `${list}\n ]`;
});
list = `${list}\n];`;
fs.writeFileSync(path.join(root, 'index.ts'), `${HEADER}\n\n${imports.join('\n')}\n\nexport default ${list}\n`);
}
extractBg();
extractSets();