Skip to content
Permalink
Browse files

⚡️ Better prebuild script output

  • Loading branch information...
GitSquared committed Feb 6, 2019
1 parent e68b06a commit 7a87c5fd7045209687de8fe2003ef3bd4aba8cde
Showing with 39 additions and 30 deletions.
  1. +39 −30 prebuild-minify.js
@@ -1,22 +1,26 @@
const fs = require("fs");
const path = require("path");
const stdout = process.stdout;
const UglifyJS = require("uglify-es");
const CleanCSS = require("clean-css");
JSON.minify = require("node-json-minify");

const writeMinified = (path, data) => {
fs.writeFile(path, data, (err) => {
if (err) {
console.log(path+" - ❌");
console.log("");
console.log("");
throw err;
}
console.log(path+" - ✓");
function writeMinified(path, data) {
return new Promise((res, rej) => {
fs.writeFile(path, data, (err) => {
if (err) {
stdout.write(" - ❌\n\n\n", () => {
rej(err);
});
}
stdout.write(" - ✓\n", () => {
res();
});
});
});
};
}

const recursiveMinify = (dirPath) => {
async function recursiveMinify(dirPath) {
try { var files = fs.readdirSync(dirPath); }
catch(e) { return; }
if (files.length > 0) {
@@ -25,7 +29,8 @@ const recursiveMinify = (dirPath) => {
if (fs.statSync(filePath).isFile()) {

// Do not process grid.json because it's heavy and pre-minified, and themes and keyboard files to leave them in a human-readable state
if (filePath.endsWith(".json")) return;
if (filePath.endsWith(".json") && !filePath.endsWith("icons.json")) return;
await stdout.write(filePath);

switch (filePath.split(".").pop()) {
case "js":
@@ -41,42 +46,46 @@ const recursiveMinify = (dirPath) => {
}
});
if (!minified.error) {
writeMinified(filePath, minified.code);
break;
}
else {
console.log(filePath+" - ❌");
console.log("");
console.log("");
await writeMinified(filePath, minified.code).catch(e => {
throw e;
});
} else {
stdout.write(" - ❌\n\n\n");
throw minified.error;
}
break;
case "css":
let output = new CleanCSS({level:2}).minify(fs.readFileSync(filePath, {encoding:"utf-8"}));
if (output.errors.length >= 1) {
console.log(filePath+" - ❌");
console.log("");
console.log("");
stdout.write(" - ❌\n\n\n");
throw output.errors;
} else {
writeMinified(filePath, output.styles);
break;
await writeMinified(filePath, output.styles).catch(e => {
throw e;
});
}
break;
case "json":
let out;
try {
writeMinified(filePath, JSON.minify(fs.readFileSync(filePath, {encoding:"utf-8"})));
out = JSON.minify(fs.readFileSync(filePath, {encoding:"utf-8"}));
break;
} catch(err) {
console.log(filePath+" - ❌");
console.log("");
console.log("");
stdout.write(" - ❌\n\n\n");
throw err;
}
await writeMinified(filePath, out).catch(e => {
throw e;
});
break;
default:
stdout.write("\n");
}
} else {
recursiveMinify(filePath);
await recursiveMinify(filePath);
}
}
}
};
}

recursiveMinify(path.join(__dirname, "prebuild-src"));

0 comments on commit 7a87c5f

Please sign in to comment.
You can’t perform that action at this time.