61 lines
No EOL
1.8 KiB
JavaScript
Executable file
61 lines
No EOL
1.8 KiB
JavaScript
Executable file
#!/usr/bin/env -S node
|
|
|
|
import { $, argv, path } from "zx";
|
|
import { strict as assert } from "node:assert";
|
|
import fs from "node:fs/promises";
|
|
|
|
/**Catches any p Promise throws and instead returns those in a tuple*/
|
|
async function ptry<TRet, TError = Error>(
|
|
p: Promise<TRet>
|
|
): Promise<[TError, undefined] | [undefined, TRet]> {
|
|
try {
|
|
const result = await p;
|
|
return [undefined, result];
|
|
} catch (err) {
|
|
return [err as TError, undefined];
|
|
}
|
|
}
|
|
|
|
$.verbose = true;
|
|
|
|
const scriptDir = path.dirname(new URL(import.meta.url).pathname);
|
|
const scrubJq = path.join(scriptDir, "scrub.jq");
|
|
|
|
const targetDir = argv._[0];
|
|
|
|
assert(targetDir, "Usage: ./scrub.ts <directory>");
|
|
|
|
const targetPath = path.resolve(targetDir);
|
|
|
|
// const stat = await fs.stat(targetPath);
|
|
// assert(stat.isDirectory(), "");
|
|
|
|
const [notADir] = await ptry($`test -d ${targetPath}`);
|
|
assert(!notADir, `Error: '${targetPath}' is not a directory`);
|
|
|
|
const [noScrubJq] = await ptry($`test -f ${scrubJq}`);
|
|
assert(!noScrubJq, `Error: scrub.jq not found at ${scrubJq}`);
|
|
|
|
console.log(`Scrubbing JSON files in: ${targetPath}`);
|
|
console.log(`Using scrub.jq from: ${scrubJq}`);
|
|
console.log();
|
|
|
|
const [findErr, files] = await ptry($`fdfind -t f '\\.json$' ${targetPath} -0`.quiet());
|
|
assert(!findErr, `Error finding JSON files: ${findErr}`);
|
|
|
|
const filePaths = files.stdout.split("\0").filter(Boolean);
|
|
console.log("filePaths", filePaths);
|
|
|
|
for (const file of filePaths) {
|
|
console.log(`Processing: ${file}`);
|
|
const tmpFile = `${file}.tmp`;
|
|
|
|
const [jqErr] = await ptry($`jq -f ${scrubJq} ${file} > ${tmpFile}`);
|
|
assert(!jqErr, `Error processing ${file}: ${jqErr}`);
|
|
|
|
const [mvErr] = await ptry($`mv ${tmpFile} ${file}`);
|
|
assert(!mvErr, `Error moving ${tmpFile} to ${file}: ${mvErr}`);
|
|
}
|
|
|
|
console.log();
|
|
console.log("Done!"); |