#!/usr/bin/env node
"use strict";

const fs = require("node:fs").promises;
const path = require("node:path");
const pkg = require("../package.json");

const fileName = path.join(process.cwd(), pkg.main);

function cleanup(css) {
  return css
    // Perfectionist adds comments to the end of the previous line...
    // }/* comment */ => }\n\n  /* comment */
    .replace(/}\/\*(([\s\S])+?)\*\/\s*/g, "}\n\n  /*$1*/\n  ")
    .replace(/,\s\/\*/g, ",\n  /*")
    // .replace(/\s+regexp\(/g, "\nregexp(")
    // Remove extra carriage returns between definitions
    .replace(/\n+/g, "\n");
}

async function postPerfectionist() {
  const css = await fs.readFile(fileName, "utf8");
  await fs.writeFile(fileName, cleanup(css));
  console.info("\u001B[32m%s\u001B[0m", `${pkg.title} usercss cleanup completed`);
}

postPerfectionist();
