#!/usr/bin/env node const postcss = require(require.resolve("postcss", { paths: [process.cwd()] })); const config = require(`${process.cwd()}/postcss.config.js`); const net = require("net"); class PostCSS { static process(data, write) { postcss(config.plugins) .process(JSON.parse(data).raw_content, { from: "stdin" }) .then((result) => write(result)) .catch((error) => { console.error("PostCSS Error!\n"); console.error(error.toString()); }); } static startServer() { const server = net.createServer((c) => { c.on("data", (data) => { this.process(data, function (result) { const packet = { compiled_css: result.toString() }; c.write(`${JSON.stringify(packet)}\n`); }); }); }); server.on("error", (err) => { console.error("PostCSS Server Error!\n"); throw err; }); server.listen(8124, () => { console.log("PostCSS Server listening on port 8124..."); }); } } switch (process.argv[2]) { case "--development": PostCSS.startServer(); break; default: PostCSS.process(process.argv[2], function (result) { const packet = { compiled_css: result.toString() }; process.stdout.write(`${JSON.stringify(packet)}`); }); }