|
| 1 | +var fs = require("fs"); |
| 2 | +var tmp = require("tmp"); |
| 3 | +var path = require("path"); |
| 4 | +var compileElm = require("node-elm-compiler").compile; |
| 5 | + |
| 6 | +function generateCss(opts) { |
| 7 | + assertOptionsPresent(opts); |
| 8 | + |
| 9 | + return withTmpDir().then(tmpDirPath => { |
| 10 | + var emitterFile = path.join(tmpDirPath, "StyleElementsEmitter.elm"); |
| 11 | + var destFile = path.join(tmpDirPath, "style-elements-emitter.js"); |
| 12 | + var emitterTemplate = buildEmitterTemplate(opts.stylesheetModule, opts.stylesheetFunction); |
| 13 | + |
| 14 | + return writeFile(emitterFile, emitterTemplate) |
| 15 | + .then(() => compile(emitterFile, { output: destFile, yes: true })) |
| 16 | + .then(() => extractCssResults(destFile)); |
| 17 | + }); |
| 18 | +} |
| 19 | + |
| 20 | +function assertOptionsPresent(opts = {}) { |
| 21 | + var requiredOptions = ["stylesheetModule", "stylesheetFunction"]; |
| 22 | + var providedOptions = Object.keys(opts); |
| 23 | + var missingOptions = requiredOptions.filter(o => !providedOptions.includes(o) || providedOptions[o] === ""); |
| 24 | + |
| 25 | + if (missingOptions.length > 0) { |
| 26 | + throw new Error(`Missing options: ${missingOptions.join(', ')}`); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +function withTmpDir() { |
| 31 | + return new Promise(function(resolve, reject) { |
| 32 | + tmp.dir({ unsafeCleanup: true }, function(err, tmpDirPath) { |
| 33 | + if (err) { |
| 34 | + reject(err); |
| 35 | + } else { |
| 36 | + resolve(tmpDirPath); |
| 37 | + } |
| 38 | + }); |
| 39 | + }); |
| 40 | +} |
| 41 | + |
| 42 | +function buildEmitterTemplate(stylesheetModule, stylesheetFunction) { |
| 43 | + return unindent( |
| 44 | + 4, |
| 45 | + ` |
| 46 | + port module StyleElementsEmitter exposing (..) |
| 47 | +
|
| 48 | + import ${stylesheetModule} |
| 49 | +
|
| 50 | +
|
| 51 | + port result : String -> Cmd msg |
| 52 | +
|
| 53 | +
|
| 54 | + stylesheet = |
| 55 | + ${stylesheetModule}.${stylesheetFunction} |
| 56 | +
|
| 57 | +
|
| 58 | + main : Program Never () Never |
| 59 | + main = |
| 60 | + Platform.program |
| 61 | + { init = ( (), result stylesheet.css ) |
| 62 | + , update = \\_ _ -> ( (), Cmd.none ) |
| 63 | + , subscriptions = \\_ -> Sub.none |
| 64 | + } |
| 65 | + ` |
| 66 | + ); |
| 67 | +} |
| 68 | + |
| 69 | +function unindent(count, text) { |
| 70 | + var spaces = new Array(count + 1).join(" "); |
| 71 | + return text.replace(new RegExp(`^${spaces}`, "gm"), ""); |
| 72 | +} |
| 73 | + |
| 74 | +function writeFile(...args) { |
| 75 | + return new Promise((resolve, reject) => { |
| 76 | + return fs.writeFile(...args, err => err ? reject(err) : resolve()); |
| 77 | + }) |
| 78 | +} |
| 79 | + |
| 80 | +function compile(src, options) { |
| 81 | + return new Promise(function(resolve, reject) { |
| 82 | + compileElm(src, options).on("close", function(exitCode) { |
| 83 | + if (exitCode === 0) { |
| 84 | + resolve(); |
| 85 | + } else { |
| 86 | + reject("Errored with exit code " + exitCode); |
| 87 | + } |
| 88 | + }); |
| 89 | + }); |
| 90 | +} |
| 91 | + |
| 92 | +function extractCssResults(destFile) { |
| 93 | + var emitter = require(destFile).StyleElementsEmitter; |
| 94 | + var worker = emitter.worker(); |
| 95 | + |
| 96 | + return new Promise(resolve => worker.ports.result.subscribe(resolve)); |
| 97 | +} |
| 98 | + |
| 99 | +module.exports = generateCss; |
0 commit comments