-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimageImporter.ts
54 lines (43 loc) · 1.33 KB
/
imageImporter.ts
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
import fs from "fs";
import Sqids from "sqids";
/**
* List all images in the src/assets/images/original directory
*/
function listImages() {
return fs.readdirSync("./src/assets/images/original");
}
/**
* Returns a hash code from a string
*/
function simpleHash(input: string) {
let hash = 0;
for (let i = 0; i < input.length; i++) {
const char = input.charCodeAt(i);
hash = (hash << 5) - hash + char;
hash >>>= 0; // Convert to an unsigned 32bit integer
}
return hash;
}
const sqids = new Sqids({
minLength: 10,
alphabet: "abcdefghijklmnopqrstuvwxyz",
});
const targetFile = "./src/assets/images/imageList.ts";
const esmImports: string[] = [];
const imageMap: Record<string, string> = {};
for (const image of listImages()) {
if (!/\.(png|jpg|jpeg|webp)$/.test(image)) continue;
const imageHash = simpleHash(image);
const readableId = sqids.encode([imageHash]);
esmImports.push(`import ${readableId} from "./original/${image}";`);
imageMap[readableId] = readableId;
}
const imageListContent = `// This file is auto-generated by imageImporter.ts
// Do not edit this file directly
${esmImports.join("\n")}
const imageList: Record<string, ImageMetadata> = {\n${Object.entries(imageMap)
.map(([key, value]) => `\t${key}: ${value},`)
.join("\n")}\n};
export default imageList;
`;
fs.writeFileSync(targetFile, imageListContent);