-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-asset-types.js
executable file
·155 lines (146 loc) · 3.55 KB
/
generate-asset-types.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env node
/*
* Generates .d.ts files for all of our assets to provide stricter type
* checking and nicer autocomplete when importing them.
*/
const yargs = require("yargs");
const glob = require("glob");
const fs = require("fs");
const path = require("path");
const camelcase = require("camelcase");
const extensions = [
// images
"bmp",
"gif",
"jpg",
"png",
"svg",
// fonts
"ttf",
// audio
"flac",
"mp3",
"ogg",
"wav",
// shaders
"frag",
"vert",
//models
"gltf",
"glb",
//misc
"txt",
];
function log(...args) {
// Just so this stops showing up in searches for console dot log
console["log"](...args);
}
// The contents of the generated file
function getContent(filename) {
const varName = camelcase(path.basename(filename).split(".")[0]);
return `// This file autogenerated by generate-asset-types.js
declare const ${varName}: string;
export default ${varName};
`;
}
// Removes old types
function cleanTypes(assetsFolder, silent = false) {
if (!silent) {
log(`removing old asset .d.ts files from "${assetsFolder}" . . .`);
}
const pattern = `${assetsFolder}/**/*.@(${extensions.join("|")}).d.ts`;
const fileNames = glob.sync(pattern, {});
fileNames.forEach((fileName) => {
if (!silent) {
log(" - removed " + fileName);
}
fs.unlinkSync(fileName);
});
if (fileNames.length === 0 && !silent) {
log("No files to remove");
}
return fileNames;
}
// Generates the new type files
function generateTypes(assetsFolder, silent = false) {
if (!silent) {
log(`generating new asset .d.ts files in "${assetsFolder}" . . .`);
}
const pattern = `${assetsFolder}/**/*.@(${extensions.join("|")})`;
const fileNames = glob.sync(pattern, {}).map((f) => f + ".d.ts");
fileNames.forEach((fileName) => {
fs.writeFileSync(fileName, getContent(fileName));
if (!silent) {
log(" - created " + fileName);
}
});
if (fileNames.length === 0 && !silent) {
log("No files to generate");
}
return fileNames;
}
function cleanAndGenerate(assetsFolder) {
log(`Updating asset types in "${assetsFolder}" . . .`);
const removed = new Set(cleanTypes(assetsFolder, true));
const generated = new Set(generateTypes(assetsFolder, true));
const newFiles = [];
const deletedFiles = [];
for (const fileName of removed) {
if (!generated.has(fileName)) {
deletedFiles.push(fileName);
}
}
for (const fileName of generated) {
if (!removed.has(fileName)) {
newFiles.push(fileName);
}
}
if (deletedFiles.length) {
log(deletedFiles.map((f) => ` - removed ${f}`).join("\n"));
}
if (newFiles.length) {
log(newFiles.map((f) => ` - created ${f}`).join("\n"));
}
if (newFiles.length === 0 && deletedFiles.length === 0) {
log("Nothing has changed");
}
}
yargs
.scriptName("generate-asset-types")
.usage(
"Generates .d.ts files for assets to provide stricter type checking and nicer autocomplete when importing them."
)
.help()
.option("directory", {
alias: "d",
describe: "Directory containing assets",
default: "./src/assets",
coerce: (d) => path.resolve(d),
type: "string",
normalize: true,
global: true,
})
.command(
"$0",
"cleans and generates",
() => {},
(argv) => {
cleanAndGenerate(argv.directory);
}
)
.command(
"clean",
"removes all asset types",
() => {},
(argv) => {
cleanTypes(argv.directory);
}
)
.command(
"generate",
"generates new .d.ts files",
() => {},
(argv) => {
generateTypes(argv.directory);
}
).argv;