Skip to content

Commit eb48218

Browse files
committed
Add cli - generates css and writes to a file
1 parent 5c8196c commit eb48218

4 files changed

Lines changed: 162 additions & 1 deletion

File tree

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ elm-stuff/
44
repl-temp-*
55
.DS_Store
66
*/index.html
7-
index.html
7+
index.html
8+
node_modules
9+

index.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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;

package.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "style-elements",
3+
"version": "3.2.3",
4+
"description": "CSS Preprocoessor for Elm's style-elements",
5+
"main": "index.js",
6+
"bin": {
7+
"style-elements": "./style-elements.js"
8+
},
9+
"directories": {
10+
"example": "examples",
11+
"test": "tests"
12+
},
13+
"dependencies": {
14+
"chalk": "^2.0.1",
15+
"commander": "^2.11.0",
16+
"postcss": "^6.0.6",
17+
"tmp": "0.0.31"
18+
},
19+
"devDependencies": {},
20+
"scripts": {
21+
"test": "echo \"Error: no test specified\" && exit 1"
22+
},
23+
"repository": {
24+
"type": "git",
25+
"url": "http://package.elm-lang.org/packages/mdgriffith/style-elements"
26+
},
27+
"author": "Oliver Searle-Barnes",
28+
"license": "BSD-3-Clause"
29+
}

style-elements.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env node
2+
3+
var styleElements = require('./');
4+
var pkg = require('./package.json');
5+
var program = require('commander');
6+
var fs = require("fs");
7+
var chalk = require('chalk');
8+
9+
program
10+
.version(pkg.version)
11+
.usage('[options] <stylesheetModule> <stylesheetFunction>')
12+
.option('-o, --output [outputFile]', '(optional) file to write the CSS to', 'out.css')
13+
.parse(process.argv);
14+
15+
16+
if(program.args.length !== 2) {
17+
program.outputHelp();
18+
process.exit(1);
19+
}
20+
else {
21+
var [stylesheetModule, stylesheetFunction] = program.args;
22+
styleElements({stylesheetModule, stylesheetFunction})
23+
.then(result => writeFile(program.output, result))
24+
.then(() => console.warn(chalk.green(`\n----> Success! styles were written to ${program.output}\n`)));
25+
}
26+
27+
function writeFile(...args) {
28+
return new Promise((resolve, reject) => {
29+
return fs.writeFile(...args, err => err ? reject(err) : resolve());
30+
})
31+
}

0 commit comments

Comments
 (0)