forked from polkadot-js/apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobohash.cjs
executable file
·88 lines (65 loc) · 2.5 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
// Copyright 2017-2021 @polkadot/react-components authors & contributors
// SPDX-License-Identifier: Apache-2.0
const fs = require('fs');
const path = require('path');
const HEADER = `// Copyright 2017-2021 @polkadot/react-components authors & contributors
// SPDX-License-Identifier: Apache-2.0
// Automatically generated, do not edit`;
const PATH = 'packages/react-components/src/IdentityIcon/RoboHash';
function getCounter (index) {
return `000${index}`.slice(-3);
}
function getFiles (dir) {
return fs
.readdirSync(dir)
.filter((entry) => !['.', '..', 'index.ts'].includes(entry))
.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) => {
return (a.includes('-') && b.includes('-'))
? a.split('-')[1].localeCompare(b.split('-')[1])
: 0;
});
}
function extractBg () {
const root = path.join(__dirname, '..', PATH, 'backgrounds');
const files = [];
getFiles(root).forEach((sub) => {
getFiles(path.join(root, sub)).forEach((entry) => files.push(`./${sub}/${entry}`));
});
const imports = files.map((file, index) => `import b${getCounter(index)} from '${file}';`);
const list = `const backgrounds: any[] = [${files.map((_, index) => `b${getCounter(index)}`).join(', ')}];`;
fs.writeFileSync(path.join(root, 'index.ts'), `${HEADER}\n\n${imports.join('\n')}\n\n${list}\n\nexport default backgrounds;\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}/${entry}`)
)
);
const imports = [];
let list = 'const sets: any[][][] = [';
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}';`);
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\n${list}\n\nexport default sets;\n`);
}
extractBg();
extractSets();