use std::path::{Path, PathBuf};
use std::process::Command;
use anyhow::{Context, Result, bail};
use crate::project::Project;
const CONFIGS: [&str; 4] = ["postcss.config.mjs", "postcss.config.js", "postcss.config.cjs", ".postcssrc.json"];
const RUNNER: &str = r#"import { readFileSync, writeFileSync } from "node:fs";
import { createRequire } from "node:module";
import { pathToFileURL } from "node:url";
const [config, input, output, skipTailwind] = process.argv.slice(2);
const require = createRequire(pathToFileURL(process.cwd() + "/package.json"));
const load = async (name) => { const m = await import(pathToFileURL(require.resolve(name)).href); return m.default ?? m; };
const postcss = await load("postcss");
let options = config.endsWith(".json") ? JSON.parse(readFileSync(config, "utf8")) : (await import(pathToFileURL(config).href)).default;
if (typeof options === "function") options = await options({ env: process.env.NODE_ENV });
const tailwind = (name) => skipTailwind === "1" && /(^|\/)(@tailwindcss\/postcss|tailwindcss)$/.test(name);
let plugins = [];
const entries = options?.plugins ?? [];
if (Array.isArray(entries)) {
for (const entry of entries) {
if (typeof entry === "string") { if (!tailwind(entry)) plugins.push((await load(entry))()); }
else if (Array.isArray(entry)) { if (!tailwind(entry[0])) plugins.push((await load(entry[0]))(entry[1])); }
else if (entry && !tailwind(entry.postcssPlugin ?? "")) plugins.push(entry);
}
} else {
for (const [name, value] of Object.entries(entries)) {
if (value === false || tailwind(name)) continue;
plugins.push((await load(name))(value === true ? {} : value));
}
}
const css = readFileSync(input, "utf8");
if (plugins.length === 0) { writeFileSync(output, css); process.exit(0); }
const result = await postcss(plugins).process(css, { from: input, to: output, map: false });
writeFileSync(output, result.css);
"#;
pub struct PostCss<'a> {
project: &'a Project,
}
impl<'a> PostCss<'a> {
pub fn new(project: &'a Project) -> Self {
Self { project }
}
fn config(&self) -> Option<PathBuf> {
CONFIGS.iter().map(|name| self.project.root.join(name)).find(|path| path.is_file())
}
pub fn enabled(&self) -> bool {
self.config().is_some() && self.project.node_modules().join("postcss/package.json").is_file()
}
pub fn process(&self, css: String, tailwind_done: bool) -> Result<String> {
if !self.enabled() {
return Ok(css);
}
let input = self.dir()?.join("input.css");
std::fs::write(&input, css)?;
self.process_file(&input, tailwind_done)
}
fn dir(&self) -> Result<PathBuf> {
let dir = self.project.cache_dir().join("postcss");
std::fs::create_dir_all(&dir)?;
Ok(dir)
}
pub fn has_tailwind(&self) -> bool {
self.enabled()
&& self
.config()
.and_then(|path| std::fs::read_to_string(path).ok())
.is_some_and(|c| c.contains("tailwindcss"))
}
pub fn process_file(&self, input: &Path, tailwind_done: bool) -> Result<String> {
let Some(config) = self.config() else { bail!("no PostCSS config found") };
let dir = self.dir()?;
let (runner, output) = (dir.join("run.mjs"), dir.join("output.css"));
std::fs::write(&runner, RUNNER)?;
let result = Command::new("node")
.arg(&runner)
.arg(&config)
.arg(input)
.arg(&output)
.arg(if tailwind_done { "1" } else { "0" })
.current_dir(&self.project.root)
.output()
.context("cannot run node for PostCSS; is Node.js installed?")?;
if !result.status.success() {
bail!("postcss failed:\n{}", String::from_utf8_lossy(&result.stderr));
}
std::fs::read_to_string(&output).context("postcss produced no output")
}
}