Skip to content

Commit

Permalink
Implement progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
limzykenneth committed Jan 2, 2025
1 parent 2890646 commit 02a1eb7
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 16 deletions.
119 changes: 119 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"html-minifier-terser": "^7.2.0",
"is-absolute-url": "^4.0.1",
"jsdom": "^24.0.0",
"log-update": "^6.1.0",
"marked": "^4.0.10",
"msw": "^2.2.9",
"path": "^0.12.7",
Expand Down
87 changes: 71 additions & 16 deletions src/scripts/fast-compress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import sharp from "sharp";
import { writeFile, readFile } from 'node:fs/promises';
import { minify } from 'html-minifier-terser';
import { optimize } from 'svgo';
import logUpdate from 'log-update';

export default () => {
return {
Expand All @@ -12,47 +13,85 @@ export default () => {
"astro:build:done": async ({dir}: any) => {
const dirPath = dir.pathname;
const files = await fg(join(dirPath, "**/*.(webp|gif|jpg|jpeg|png|html|svg)"));
let processing = 0;
let writing = 0;
let total = 0;


function logMessage(){
const processingProgress = 1 - (processing/total);
const writingProgress = 1 - (writing/total);
const processingBar = `[${'='.repeat(Math.round(processingProgress * 20))}${' '.repeat(20 - Math.round(processingProgress * 20))}]`;
const writingBar = `[${'='.repeat(Math.round(writingProgress * 20))}${' '.repeat(20 - Math.round(writingProgress * 20))}]`;

logUpdate(
`
Processing ${processingBar} ${Math.floor(processingProgress * 100)}% (${total - processing}/${total})
Writing ${writingBar} ${Math.floor(writingProgress * 100)}% (${total - writing}/${total})
`
);
}

const promises = files.map(async (file) => {
if(extname(file) === ".webp"){
const data = await sharp(file, {animated: true})
.webp({
effort: 6
})
.toBuffer()
console.log(`Processed: ${file}`);
.toBuffer();

processing--;
logMessage();
// console.log(`Processed: ${file}`);
await writeFile(file, data);
console.log(`Written: ${file}`);
// console.log(`Written: ${file}`);
writing--;
logMessage();

} else if(extname(file) === ".gif") {
const data = await sharp(file, {animated: true})
.gif({
effort: 10
})
.toBuffer()
console.log(`Processed: ${file}`);
.toBuffer();

processing--;
logMessage();
// console.log(`Processed: ${file}`);
await writeFile(file, data);
console.log(`Written: ${file}`);
// console.log(`Written: ${file}`);
writing--;
logMessage();

} else if(extname(file) === ".jpg" || extname(file) === ".jpeg") {
const data = await sharp(file)
.jpeg({
})
.toBuffer()
console.log(`Processed: ${file}`);
.toBuffer();

processing--;
logMessage();
// console.log(`Processed: ${file}`);
await writeFile(file, data);
console.log(`Written: ${file}`);
// console.log(`Written: ${file}`);
writing--;
logMessage();

} else if(extname(file) === ".png") {
const data = await sharp(file)
.png({
compressionLevel: 9,
effort: 10
})
.toBuffer()
console.log(`Processed: ${file}`);
.toBuffer();

processing--;
logMessage();
// console.log(`Processed: ${file}`);
await writeFile(file, data);
console.log(`Written: ${file}`);
// console.log(`Written: ${file}`);
writing--;
logMessage();

} else if(extname(file) === ".html") {
const text = await readFile(file, {encoding: "utf8"});
Expand Down Expand Up @@ -142,9 +181,14 @@ export default () => {
useShortDoctype: false,
});

console.log(`Processed: ${file}`);
processing--;
logMessage();
// console.log(`Processed: ${file}`);
await writeFile(file, data);
console.log(`Written: ${file}`);
// console.log(`Written: ${file}`);
writing--;
logMessage();

}catch(err){
console.warn(err);
}
Expand All @@ -161,13 +205,24 @@ export default () => {
},
plugins: ["preset-default"],
})
processing--;
logMessage();

console.log(`Processed: ${file}`);
// console.log(`Processed: ${file}`);
await writeFile(file, data);
console.log(`Written: ${file}`);
// console.log(`Written: ${file}`);

writing--;
logMessage();
}
});

processing = promises.length;
writing = promises.length;
total = promises.length;

logMessage();

await Promise.all(promises);
}
}
Expand Down

0 comments on commit 02a1eb7

Please sign in to comment.