Skip to content

Commit

Permalink
Merge branch 'develop' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
jadiagaurang committed May 8, 2022
2 parents 4863539 + 571c9b0 commit b88dc3c
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 21 deletions.
88 changes: 67 additions & 21 deletions lib/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const fs = require("fs");
const path = require("path");
const _ = require("underscore");
const peach = require("parallel-each");
const zlib = require("zlib");
const winston = require("./logger").winston;
const AppUtil = require("./apputil.js");
const UglifyJS = require("uglify-js");
Expand Down Expand Up @@ -45,7 +46,7 @@ module.exports = class MinifyAllCLI {
}
// Validate Source Directory
if (!fs.existsSync(me.SourceDirectory)) {
throw "SourceDirectory doesn't exists!";
throw new Error("SourceDirectory doesn't exists!");
}

// Check Destination Directory is Absolute; if not then convert to absolute
Expand Down Expand Up @@ -118,18 +119,33 @@ module.exports = class MinifyAllCLI {
var result = UglifyJS.minify(code, uglifyOptions);

if (!result.error) {
fs.writeFile(strDestinationFile, result.code, (err) => {
if (err) me.logger.error(err); //throw err;

me.logger.info("JS. Path: " + strDestinationFile);
resolve();
});
let strCode = result.code;

if (me.options.doGzip) {
const bufJS = new Buffer.from(strCode, "utf-8");
zlib.gzip(bufJS, function (_, strGzipResult) {
fs.writeFile(strDestinationFile, strGzipResult, (err) => {
if (err) me.logger.error(err);

me.logger.info("JS. Path: " + strDestinationFile);
resolve();
});
});
}
else {
fs.writeFile(strDestinationFile, strCode, (err) => {
if (err) me.logger.error(err);

me.logger.info("JS. Path: " + strDestinationFile);
resolve();
});
}
}
else {
me.logger.error(result.error);

fs.writeFile(strDestinationFile, code, (err) => {
if (err) me.logger.error(err); //throw err;
if (err) me.logger.error(err);

me.logger.warn("Skip. Path: " + strDestinationFile);
resolve();
Expand All @@ -154,11 +170,27 @@ module.exports = class MinifyAllCLI {
PostCSS([CSSNano({ preset: "cssnano-preset-default" })])
.process(css, { from: undefined, to: undefined })
.then(result => {
fs.writeFile(strDestinationFile, result.css, (err) => {
if (err) me.logger.error(err); //throw err;

me.logger.info("CSS. Path: " + strDestinationFile);
});
let strCode = result.css;

if (me.options.doGzip) {
const bufJS = new Buffer.from(strCode, "utf-8");
zlib.gzip(bufJS, function (_, strGzipResult) {
fs.writeFile(strDestinationFile, strGzipResult, (err) => {
if (err) me.logger.error(err);

me.logger.info("CSS. Path: " + strDestinationFile);
resolve();
});
});
}
else {
fs.writeFile(strDestinationFile, strCode, (err) => {
if (err) me.logger.error(err);

me.logger.info("CSS. Path: " + strDestinationFile);
resolve();
});
}
});
}, 100);
});
Expand All @@ -175,7 +207,7 @@ module.exports = class MinifyAllCLI {

var html = fs.readFileSync(strFilePath, "utf8");

var result = HTMLMinifier(html, {
let strCode = HTMLMinifier(html, {
collapseWhitespace: true,
removeAttributeQuotes: true,
removeComments: true,
Expand All @@ -185,12 +217,26 @@ module.exports = class MinifyAllCLI {
minifyCSS: true,
useShortDoctype: true
});

if (me.options.doGzip) {
const bufJS = new Buffer.from(strCode, "utf-8");
zlib.gzip(bufJS, function (_, strGzipResult) {
fs.writeFile(strDestinationFile, strGzipResult, (err) => {
if (err) me.logger.error(err);

fs.writeFile(strDestinationFile, result, (err) => {
if (err) me.logger.error(err); //throw err;
me.logger.info("HTML. Path: " + strDestinationFile);
resolve();
});
});
}
else {
fs.writeFile(strDestinationFile, strCode, (err) => {
if (err) me.logger.error(err);

me.logger.info("HTML. Path: " + strDestinationFile);
});
me.logger.info("HTML. Path: " + strDestinationFile);
resolve();
});
}
}, 100);
});

Expand All @@ -211,10 +257,10 @@ module.exports = class MinifyAllCLI {
resolve();
})
.catch((err) => {
if (err) me.logger.error(err); //throw err;
if (err) me.logger.error(err);

fs.copyFile(strFilePath, strDestinationFile, (err) => {
if (err) me.logger.error(err); //throw err;
if (err) me.logger.error(err);

me.logger.warn("Skip. Source: " + strFilePath + "; Destination: " + strDestinationFile);
resolve();
Expand All @@ -223,7 +269,7 @@ module.exports = class MinifyAllCLI {
}
else {
fs.copyFile(strFilePath, strDestinationFile, (err) => {
if (err) me.logger.error(err); //throw err;
if (err) me.logger.error(err);

me.logger.warn("Skip. Source: " + strFilePath + "; Destination: " + strDestinationFile);
resolve();
Expand Down
22 changes: 22 additions & 0 deletions tests/cli.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,26 @@ describe("minify-all-cli", () => {

expect(arrayAllFiles.length).to.equal(arrayProcessedFiles.length);
});

it("minify-all-cli with optimum options", async () => {
const response = await cmd.execute(
"./bin/minify-all-cli.js", [
"-s", "./tests/asserts",
"-d", "./tests/asserts_compressed",
"--skipJS=true", "--skipCSS=true", "--skipHTML=true",
"--doGzip=true",
"--skipFileExtensions=.mp3", "--skipFileExtensions=.mp4",
"--processCount=10",
"--logLevel=info"
]
);

// Print the standard output stream response
console.log(response);

var arrayAllFiles = AppUtil.getAllFiles("./tests/asserts");
var arrayProcessedFiles = AppUtil.getAllFiles("./tests/asserts_compressed");

expect(arrayAllFiles.length).to.equal(arrayProcessedFiles.length);
});
});

0 comments on commit b88dc3c

Please sign in to comment.