Skip to content

Commit

Permalink
Add cli - generates css and writes to a file
Browse files Browse the repository at this point in the history
  • Loading branch information
opsb committed Jul 15, 2017
1 parent 5c8196c commit eb48218
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ elm-stuff/
repl-temp-*
.DS_Store
*/index.html
index.html
index.html
node_modules

99 changes: 99 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
var fs = require("fs");
var tmp = require("tmp");
var path = require("path");
var compileElm = require("node-elm-compiler").compile;

function generateCss(opts) {
assertOptionsPresent(opts);

return withTmpDir().then(tmpDirPath => {
var emitterFile = path.join(tmpDirPath, "StyleElementsEmitter.elm");
var destFile = path.join(tmpDirPath, "style-elements-emitter.js");
var emitterTemplate = buildEmitterTemplate(opts.stylesheetModule, opts.stylesheetFunction);

return writeFile(emitterFile, emitterTemplate)
.then(() => compile(emitterFile, { output: destFile, yes: true }))
.then(() => extractCssResults(destFile));
});
}

function assertOptionsPresent(opts = {}) {
var requiredOptions = ["stylesheetModule", "stylesheetFunction"];
var providedOptions = Object.keys(opts);
var missingOptions = requiredOptions.filter(o => !providedOptions.includes(o) || providedOptions[o] === "");

if (missingOptions.length > 0) {
throw new Error(`Missing options: ${missingOptions.join(', ')}`);
}
}

function withTmpDir() {
return new Promise(function(resolve, reject) {
tmp.dir({ unsafeCleanup: true }, function(err, tmpDirPath) {
if (err) {
reject(err);
} else {
resolve(tmpDirPath);
}
});
});
}

function buildEmitterTemplate(stylesheetModule, stylesheetFunction) {
return unindent(
4,
`
port module StyleElementsEmitter exposing (..)
import ${stylesheetModule}
port result : String -> Cmd msg
stylesheet =
${stylesheetModule}.${stylesheetFunction}
main : Program Never () Never
main =
Platform.program
{ init = ( (), result stylesheet.css )
, update = \\_ _ -> ( (), Cmd.none )
, subscriptions = \\_ -> Sub.none
}
`
);
}

function unindent(count, text) {
var spaces = new Array(count + 1).join(" ");
return text.replace(new RegExp(`^${spaces}`, "gm"), "");
}

function writeFile(...args) {
return new Promise((resolve, reject) => {
return fs.writeFile(...args, err => err ? reject(err) : resolve());
})
}

function compile(src, options) {
return new Promise(function(resolve, reject) {
compileElm(src, options).on("close", function(exitCode) {
if (exitCode === 0) {
resolve();
} else {
reject("Errored with exit code " + exitCode);
}
});
});
}

function extractCssResults(destFile) {
var emitter = require(destFile).StyleElementsEmitter;
var worker = emitter.worker();

return new Promise(resolve => worker.ports.result.subscribe(resolve));
}

module.exports = generateCss;
29 changes: 29 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "style-elements",
"version": "3.2.3",
"description": "CSS Preprocoessor for Elm's style-elements",
"main": "index.js",
"bin": {
"style-elements": "./style-elements.js"
},
"directories": {
"example": "examples",
"test": "tests"
},
"dependencies": {
"chalk": "^2.0.1",
"commander": "^2.11.0",
"postcss": "^6.0.6",
"tmp": "0.0.31"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "http://package.elm-lang.org/packages/mdgriffith/style-elements"
},
"author": "Oliver Searle-Barnes",
"license": "BSD-3-Clause"
}
31 changes: 31 additions & 0 deletions style-elements.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env node

var styleElements = require('./');
var pkg = require('./package.json');
var program = require('commander');
var fs = require("fs");
var chalk = require('chalk');

program
.version(pkg.version)
.usage('[options] <stylesheetModule> <stylesheetFunction>')
.option('-o, --output [outputFile]', '(optional) file to write the CSS to', 'out.css')
.parse(process.argv);


if(program.args.length !== 2) {
program.outputHelp();
process.exit(1);
}
else {
var [stylesheetModule, stylesheetFunction] = program.args;
styleElements({stylesheetModule, stylesheetFunction})
.then(result => writeFile(program.output, result))
.then(() => console.warn(chalk.green(`\n----> Success! styles were written to ${program.output}\n`)));
}

function writeFile(...args) {
return new Promise((resolve, reject) => {
return fs.writeFile(...args, err => err ? reject(err) : resolve());
})
}

0 comments on commit eb48218

Please sign in to comment.