Skip to content

Instantly share code, notes, and snippets.

@Lucas-Geitner
Last active May 12, 2021 12:21
Show Gist options
  • Save Lucas-Geitner/54e61314930dcb2a4138546890fcad9a to your computer and use it in GitHub Desktop.
Save Lucas-Geitner/54e61314930dcb2a4138546890fcad9a to your computer and use it in GitHub Desktop.
#!/usr/bin/env zx
// await $`npm install --global @squoosh/cli`
// await $`npm install --global slug`
// await $`npm install --global escape-path-with-spaces`
import slug from 'slug'
import escape from 'escape-path-with-spaces'
const updateFileName = async (old_folder_name, new_folder_name) => {
await $`rm ./${old_folder_name}.txt`
await $`ls ./${old_folder_name} >> ${old_folder_name}.txt`
let filesString = await fs.readFile(`./${old_folder_name}.txt`, "utf8")
const files = filesString.split("\n").filter(u => u !== "")
// Import in new file
for await (const filePath of files) {
const [pathname, extention] = filePath.split(".")
const slugifyFile = await slug(pathname, {remove: null})
const new_name = slugifyFile + "." + extention
const escaped_filePath = escape(filePath)
const oldPathName = `./${old_folder_name}/${escaped_filePath}`
const new_pathname = `./${new_folder_name}/${new_name}`
await fs.copyFile(oldPathName, new_pathname);
try{
await $`squoosh-cli --mozjpeg '{"quality":60,"baseline":false,"arithmetic":false,"progressive":true,"optimize_coding":true,"smoothing":0,"color_space":3,"quant_table":3,"trellis_multipass":false,"trellis_opt_zero":false,"trellis_opt_table":false,"trellis_loops":1,"auto_subsample":true,"chroma_subsample":2,"separate_chroma_quality":false,"chroma_quality":75}' ${new_pathname} `
}
catch(err){
console.log(err)
}
}
}
await $`rm ./allFolder.txt`
await $`ls >> allFolder.txt`
let filesString = await fs.readFile('./allFolder.txt', "utf8")
const files = filesString.split("\n").filter(u => u !== "").filter(u => !u.includes("new")).filter(u => !u.includes("."))
for await (const filePath of files) {
const slugifyFolder = await slug(filePath, {remove: null})
const old_folder_name = filePath
const new_folder_name = "new-" + slugifyFolder
$`mkdir ${new_folder_name}`
await updateFileName(old_folder_name, new_folder_name )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment