Skip to content
This repository has been archived by the owner on Oct 22, 2021. It is now read-only.

Commit

Permalink
⚡ Better prebuild script output
Browse files Browse the repository at this point in the history
  • Loading branch information
GitSquared committed Feb 6, 2019
1 parent e68b06a commit 7a87c5f
Showing 1 changed file with 39 additions and 30 deletions.
69 changes: 39 additions & 30 deletions prebuild-minify.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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":
Expand All @@ -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.