/now
projects
ramblings
smol projects

zx is jank af

05.11.2023 1 min read

I came across a thread on Lemmy where someone asked for A Good Language to handle complex shell scripts. Someone mentioned “zx”. So. I took a look.

zx is (apparently unofficially) made by Google. TLDR: you use javascript to write bash. Here’s a simple script to change file extensions:

const ls = $`ls`;
let files = "";

for await (const chunk of ls.stdout) {
    files += chunk.toString();
}

files = files.split("\n").filter(el => !!el)

for (const item of files) {
    // the script we're running gets recognized as a .mjs file by the zx "engine", even if we don't add an extension
    // so we need this block for the script to ignore itself
    if (item === "main.mjs") { 
        continue;
    }
    if (item.split(".").length === 2) {
        const [filename] = item.split(".")
        try {
            await $`mv ${item} ${filename}.txt`
        } catch { 
            // do stuff
        }
    }
}

So… I don’t know how I’m supposed to feel about this. I mean… on one hand, this was far easier to write than its bash equivalent. On the other, something about this just. feels. so. wrong.

Built with Astro and Tailwind 🚀