Skip to content

Instantly share code, notes, and snippets.

@machuga
Last active January 9, 2016 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save machuga/47675eb184ae66c7e732 to your computer and use it in GitHub Desktop.
Save machuga/47675eb184ae66c7e732 to your computer and use it in GitHub Desktop.
Express Elm Compilation - basic functionality example. Needs refinement
'use strict';
const spawn = require('child_process').spawn;
const express = require('express');
const app = express();
const server = require('http').createServer(app);
const compile = require("node-elm-compiler").compile;
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
app.get('/', function(req, res) {
// This can go away if extracted into a module
var stderr = '';
var stdout = '';
// List Elm main targets here
const proc = compile(['./frontend/Client.elm'], {
output: './public/client.js',
// Currently need to wrap child_process.spawn with our own modifications
// so that we can tell the stdio to pipe into our app so that we can use it
// otherwise proc.stderr|proc.stdout are null. This needs improved as I
// currently lose terminal escape codes for sending back to our express process stdio.
// PR'ing something to the node-elm-compiler repo to hopefully make this unnecessary
spawn: function(command, args, options) {
return spawn(command, args, Object.assign({}, options, {stdio: 'pipe'}));
}
});
proc.on('close', function(status) {
console.log("Finished compiling Elm modules", status);
if (status === 0) {
// If successful, awesome! Render whatever you'd like
res.sendFile(__dirname + '/public/index.html');
} else {
// If we failed, let's dump stdout and stderr to the page for debugging
res.send(`<html><body><pre>${stdout}\n${stderr}</pre></body></html>`);
// Send the stdout/stderr back to our proc's if we'd like.
console.log(stdout, stderr);
// And now we'll just clean up those vars for reuse
stderr = '';
stdout = '';
}
});
// Log the stdout/stderr data and append to string at top.
proc.stdout.on('data', function(data) {
stdout += data;
});
proc.stderr.on('data', function(data) {
stderr += data;
});
});
app.use(express.static('public'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment