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 Apr 1, 2022
2 parents 98b55fa + 47e45d2 commit e630425
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 23 deletions.
15 changes: 10 additions & 5 deletions lib/apputil.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@ const { createReadStream, createWriteStream } = require("fs");
const { promisify } = require("util");
const pipe = promisify(pipeline);

const isBlank = function (obj) {
return (!obj || obj.trim() === "");
};

const getAllFiles = function(dirPath, arrayOfFiles) {
files = fs.readdirSync(dirPath);

arrayOfFiles = arrayOfFiles || [];

files.forEach(function(file) {
if (fs.statSync(dirPath + "/" + file).isDirectory()) {
arrayOfFiles = getAllFiles(dirPath + "/" + file, arrayOfFiles);
if (fs.statSync(dirPath + path.sep + file).isDirectory()) {
arrayOfFiles = getAllFiles(dirPath + path.sep + file, arrayOfFiles);
}
else {
arrayOfFiles.push(path.join(dirPath, "/", file));
arrayOfFiles.push(path.join(dirPath, path.sep, file));
}
})

Expand All @@ -35,6 +39,7 @@ async function doGzip(input, output) {
}

module.exports = {
"doGzip": doGzip,
"getAllFiles": getAllFiles
"isBlank": isBlank,
"doGzip": doGzip,
"getAllFiles": getAllFiles
};
32 changes: 15 additions & 17 deletions lib/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module.exports = class MinifyAllCLI {

constructor(strSourceDirectory, strDestinationDirectory, options) {
var me = this;

me.SourceDirectory = strSourceDirectory;
me.DestinationDirectory = strDestinationDirectory;
me.options = _.extend({}, me.defaultOptions, options);
Expand All @@ -41,17 +42,18 @@ module.exports = class MinifyAllCLI {
if (!fs.existsSync(me.SourceDirectory)) {
throw "SourceDirectory doesn't exists!";
}

// Validate Destination Directory and delete if already exists
if (fs.existsSync(me.DestinationDirectory)) {
fs.rmSync(me.DestinationDirectory, { recursive: true });
}


this.logger.info("me.SourceDirectory: " + me.SourceDirectory);
this.logger.info("me.DestinationDirectory: " + me.DestinationDirectory);

// Collect all the files to loop through!
// TODO: May use globby or fast-glob to manage better file-masking in future!
let arrayAllFiles = AppUtil.getAllFiles(me.SourceDirectory);

let totalFiles = arrayAllFiles.length * (arrayAllFiles.length + 1) / 2;

let processedFiles = 0;
peach(arrayAllFiles, async (strFilePath, index) => {
Expand All @@ -67,10 +69,10 @@ module.exports = class MinifyAllCLI {
else {
await this.gzip(strFilePath, index);
}

processedFiles += index + 1;
processedFiles++;
}, me.options.processCount).then(() => {
this.logger.info("Total Files: " + totalFiles + "; Processed Files: " + processedFiles);
this.logger.info("Total Files: " + arrayAllFiles.length + "; Processed Files: " + processedFiles);
});
}

Expand Down Expand Up @@ -127,7 +129,7 @@ module.exports = class MinifyAllCLI {

setTimeout(() => {
var strPartialFilePath = strFilePath.replace(me.SourceDirectory, "");
var strDestinationFile = path.join(me.DestinationDirectory, "/", strPartialFilePath);
var strDestinationFile = path.join(me.DestinationDirectory, path.sep, strPartialFilePath);

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

Expand All @@ -148,7 +150,7 @@ module.exports = class MinifyAllCLI {

setTimeout(() => {
var strPartialFilePath = strFilePath.replace(me.SourceDirectory, "");
var strDestinationFile = path.join(me.DestinationDirectory, "/", strPartialFilePath);
var strDestinationFile = path.join(me.DestinationDirectory, path.sep, strPartialFilePath);

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

Expand All @@ -175,19 +177,15 @@ module.exports = class MinifyAllCLI {
var me = this;

setTimeout(() => {
this.logger.info("me.SourceDirectory: " + me.SourceDirectory);
this.logger.info("me.DestinationDirectory: " + me.DestinationDirectory);
this.logger.info("strFilePath: " + strFilePath);

var strPartialFilePath = strFilePath.replace(me.SourceDirectory, "");
var strDestinationFile = path.join(me.DestinationDirectory, "/", strPartialFilePath); //strFilePath.replace(me.SourceDirectory, me.DestinationDirectory);
var strDestinationFile = path.join(me.DestinationDirectory, path.sep, strPartialFilePath); //strFilePath.replace(me.SourceDirectory, me.DestinationDirectory);
var strFileDirectory = path.dirname(strDestinationFile);

fs.mkdirSync(strFileDirectory, { recursive: true });

if (me.options.doGzip) {
AppUtil.doGzip(strFilePath, strDestinationFile).then(function () {
me.logger.info("Gzip. Path: " + strDestinationFile);
me.logger.info("Gzip. Source: " + strFilePath + "; Destination: " + strDestinationFile);
resolve();
})
.catch((err) => {
Expand All @@ -196,7 +194,7 @@ module.exports = class MinifyAllCLI {
fs.copyFile(strFilePath, strDestinationFile, (err) => {
if (err) me.logger.error(err); //throw err;

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

me.logger.warn("Skip. Path: " + strDestinationFile);
me.logger.warn("Skip. Source: " + strFilePath + "; Destination: " + strDestinationFile);
resolve();
});
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "minify-all-cli",
"version": "1.0.5",
"version": "1.0.6",
"description": "Minify All JS, CSS and HTML files in a folder by using UglifyJS, CSSNano and HTMLMinifier",
"main": "bin/minify-all-cli.js",
"bin": {
Expand Down

0 comments on commit e630425

Please sign in to comment.