Skip to content

Instantly share code, notes, and snippets.

@waldekmastykarz
Created May 24, 2021 14:21
Show Gist options
  • Save waldekmastykarz/34365f8ac5271f84f9754c20aac663e8 to your computer and use it in GitHub Desktop.
Save waldekmastykarz/34365f8ac5271f84f9754c20aac663e8 to your computer and use it in GitHub Desktop.
Convert images modified in the last hour to cwebp
#!/usr/bin/env zx
const fs = require('fs'),
path = require('path');
const nonWebPFiles = fs
.readdirSync(process.cwd())
.filter(file => !file.endsWith('.webp'));
const lastHour = new Date();
lastHour.setHours(lastHour.getHours() - 1);
nonWebPFiles.forEach(file => {
fs.stat(file, (err, stats) => {
if (err) {
throw err;
}
if (stats.isDirectory() ||
stats.mtime < lastHour) {
return;
}
const fileNameWithoutExtension = path.basename(file, path.extname(file));
$`cwebp ${file} -o ${fileNameWithoutExtension}.webp`;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment