forked from florango/cazzaran
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sass-compile.js
42 lines (36 loc) · 1.2 KB
/
sass-compile.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
import sass from 'sass';
import fs from 'fs';
import path from 'path';
import { readdir } from 'fs/promises';
import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const compileAndSave = async (sassFile) => {
const dest = sassFile.replace(path.extname(sassFile), ".css");
fs.writeFile(dest, sass.compile(sassFile).css, (err) => {
if (err) console.log(err);
console.log(`Compiled ${sassFile} to ${dest}`);
});
}
const processFiles = async (parent) => {
let files = await readdir(parent, { withFileTypes: true});
for (const file of files) {
if (file.isDirectory()) {
await processFiles(path.join(parent, file.name));
}
if (path.extname(file.name) === '.scss') {
await compileAndSave(path.join(parent, file.name));
}
}
}
for (const folder of ["styles","blocks"]) {
try {
await processFiles(path.join(__dirname, folder));
} catch (err) {
console.error(err);
}
}
fs.watch('.', {recursive: true}, (eventType, fileName) => {
if (path.extname(fileName) === ".scss" && eventType === "change") {
compileAndSave(path.join(__dirname, fileName));
}
})