forked from DotCamp/ultimate-blocks-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recompile.js
65 lines (62 loc) · 1.51 KB
/
recompile.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
const sass = require("sass");
const { writeFile, writeFileSync } = require("fs");
const { transformFileSync } = require("@babel/core");
const { resolve } = require("path");
const { readdir } = require("fs").promises;
(async () => {
let newEditorStyle = "";
let newFrontendStyle = "";
for await (const f of getFiles(__dirname + "/src")) {
if (f.endsWith("editor.scss") || f.endsWith("style.scss")) {
try {
const result = sass.renderSync({
file: f,
outputStyle: "compressed"
});
writeFileSync(f.replace(/\.scss$/, ".css"), result.css.toString());
if (f.endsWith("editor.scss")) {
newEditorStyle += result.css.toString();
} else {
newFrontendStyle += result.css.toString();
}
} catch (err) {
console.log(err);
}
} else if (f.endsWith("front.js")) {
try {
writeFileSync(
`${f.replace(/front\.js$/, "front.build.js")}`,
transformFileSync(f).code
);
} catch (err) {
console.log(err);
}
}
}
writeFile(
`${__dirname}/dist/blocks.editor.build.css`,
newEditorStyle,
err => {
if (err) throw err;
}
);
writeFile(
`${__dirname}/dist/blocks.style.build.css`,
newFrontendStyle,
err => {
if (err) throw err;
}
);
})();
//taken from qwtel at https://stackoverflow.com/a/45130990
async function* getFiles(dir) {
const dirents = await readdir(dir, { withFileTypes: true });
for (const dirent of dirents) {
const res = resolve(dir, dirent.name);
if (dirent.isDirectory()) {
yield* getFiles(res);
} else {
yield res;
}
}
}