diff --git a/README.md b/README.md index 113e1ba..cce3043 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,30 @@ Powered by Node and a node-canvas a comic can automatically be generated from a json array of users/text and some characters/backgrounds to be chosen at random. -## How to use it +## How to use it from the CLI + +### Installing + +`npm install -g blabber` + +### Using + +``` +$ blabber +$ Who?: Glen +$ Said What?: Test 1 +$ Another? (y/n) y +$ Who?: Sarah +$ Said What?: Test 2 +$ Another? (y/n) y +$ Who?: Glen +$ Said What?: Test 3 +$ Another? (y/n) n +$ Generating Comic... +$ [ Comic Generated @ comics/1.png ] +``` + +## How to use it in other projects ### Generate base64Data diff --git a/blabber.js b/blabber.js new file mode 100755 index 0000000..8b83421 --- /dev/null +++ b/blabber.js @@ -0,0 +1,56 @@ +#!/usr/bin/env node +'use strict'; + +const fs = require('fs'); +const prompt = require('prompt'); +const blabberComic = require('./'); + +prompt.start(); + +const promptMessage = [{ + description: 'Who?', + name: 'user' +}, { + description: 'Said what?', + name: 'text' +}]; +const promptMore = { + description: 'Another? y/n', + pattern: /y|n/i, + message: 'Please type y(es) or n(o) for More', + default: 'y', +}; + +function askForMessage(callback, conversation = []) { + prompt.get(promptMessage, (error, response) => { + conversation.push(response); + callback(conversation); + }); +} + +function askIfMore(conversation) { + prompt.get(promptMore, (error, response) => { + let shouldContinue = response.question.toLowerCase() === 'y'; + + if (shouldContinue) askForMessage(askIfMore, conversation); + else generate(conversation); + }); +} + +function generate(conversation) { + console.log('Generating Comic...'); + + blabberComic(conversation) + .then(rawBase64 => rawBase64.replace(/^data:image\/png;base64,/, '')) + .then(base64 => { + let filename = Math.floor(Date.now() + Math.random()) + '.png'; + + fs.writeFile(filename, base64, 'base64', error => { + if(error) throw error; + else console.log('[ Comic Generated @ ' + filename + ' ]'); + }); + }) + .catch(error => { console.error('Uhoh... something went wrong.', error); }); +} + +askForMessage(askIfMore); diff --git a/index.js b/index.js old mode 100644 new mode 100755 diff --git a/package.json b/package.json index bf9760e..b98a28d 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,9 @@ "scripts": { "test": "node test.js" }, + "bin": { + "blabber": "./blabber.js" + }, "keywords": [ "comic", "generate", @@ -18,6 +21,8 @@ "dependencies": { "canvas": "^1.6.2", "fs": "0.0.1-security", - "lodash": "^4.16.6" + "lodash": "^4.16.6", + "prompt": "^1.0.0", + "yargs": "^6.6.0" } }