forked from flyover/three.ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
glsl.js
40 lines (37 loc) · 1.05 KB
/
glsl.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
const fs = require('fs');
const path = require('path');
function walk(dir, callback) {
fs.readdir(dir, (error, list) => {
if (error) throw error;
list.forEach((file) => {
fs.stat(path.join(dir, file), (error, stat) => {
if (error) throw error;
if (stat && stat.isDirectory()) {
walk(path.join(dir, file), callback);
} else {
callback(dir, file);
}
});
});
});
}
function transform(data) {
const lines = [];
lines.push("export default [");
data.split('\n').forEach((line) => {
lines.push("\"" + line.replace(/\"/g, "\\\"") + "\",");
});
lines.push("].join('\\n');")
return lines.join('\n');
}
walk(process.argv[2] || "src/renderers/shaders", (dir, file) => {
if (path.extname(file) === ".glsl") {
fs.readFile(path.join(dir, file), 'utf-8', (error, data) => {
if (error) throw error;
fs.writeFile(path.join(dir, file + ".ts"), transform(data), (error) => {
if (error) throw error;
console.log(path.join(dir, file));
});
});
}
});