bin/postcss in jekyll-postcss-0.3.0 vs bin/postcss in jekyll-postcss-0.3.1

- old
+ new

@@ -1,31 +1,50 @@ #!/usr/bin/env node -const postcss = require(require.resolve("postcss", {paths: [process.cwd()]})); +const postcss = require(require.resolve("postcss", { paths: [process.cwd()] })); const config = require(`${process.cwd()}/postcss.config.js`); const net = require("net"); -const server = net.createServer((c) => { - c.on('data', (data) => { +class PostCSS { + static process(data, write) { postcss(config.plugins) - .process(JSON.parse(data).raw_content, {from: "stdin"}) - .then(result => { - const packet = { compiled_css: result.toString() }; - c.write(`${JSON.stringify(packet)}\n`); + .process(JSON.parse(data).raw_content, { from: "stdin" }) + .then((result) => write(result)) + .catch((error) => { + console.error("PostCSS Error!\n"); + console.error(error.toString()); + }); + } - return; - }) - .catch(error => { - console.log("PostCSS Error!\n"); - console.log(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.log("PostCSS Server Error!\n"); - throw err; -}); + server.on("error", (err) => { + console.error("PostCSS Server Error!\n"); + throw err; + }); -server.listen(8124, () => { - console.log("PostCSS Server listening on port 8124..."); -}); + 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)}`); + }); +}