JSFiddle - React, Tailwind, and code Playground
by sfoster
JavaScript
/**
* Module dependencies.
*/
var fs = require('fs');
var stdin = null;
var topics = {},
outDir = process.cwd();
function listen(){
if(!stdin || !stdin.readable){
stdin = null;
stdin = process.openStdin();
stdin.setEncoding('utf8');
stdin.on('data', function (chunk) {
var subscribers = topics["/stdin/data"] || [];
subscribers.forEach(function(fn){
fn(chunk);
});
});
stdin.on('end', function () {
// no need to manually destroy the stream?
var subscribers = topics["/stdin/end"] || [];
subscribers.forEach(function(fn){
fn();
})
});
}
}
function subscribe(topic, fn) {
var subscribers = topics[topic] || (topics[topic] = []);
subscribers.push(fn);
return {
destroy: function(){
var idx = subscribers.indexOf(fn);
if(idx >= 0){
subscribers.splice(idx, 1);
}
}
};
}
var plan = {
title: {
source: 'stdinSingle',
preFn: function(data){
console.log("What's the title?");
},
postFn: function(data){
var filename = data.toLowerCase().replace(/[^\w]+/g, '_');
filename = filename.replace(/_{2,}/g, '_');
filename = filename.replace(/^_|_$/, '');
filename += ".js";
},
next: 'content'
},
content: {
source: 'stdinMulti',
preFn: function(data){
console.log("What's the content? Use ^D to end");
},
postFn: function(data){
console.log("got content input: ", data.join(''));
},
next: 'more'
},
more: {
source: 'stdinSingle',
preFn: function(){
console.log("Go again?");
},
postFn: function(data){
if(data.match(/y/i)){
plan.more.next =...