Created
May 24, 2021 14:21
-
-
Save waldekmastykarz/34365f8ac5271f84f9754c20aac663e8 to your computer and use it in GitHub Desktop.
Convert images modified in the last hour to cwebp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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