Created
May 2, 2017 18:48
-
-
Save ansongoldade/9275a5dc3a4aa28a43464ff7b3edf56e to your computer and use it in GitHub Desktop.
`lynx-docs init` collection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const rl = require("readline").createInterface({ | |
input: process.stdin, | |
output: process.stdout | |
}); | |
let questions = { | |
dir: { question: "Template directory", default: ".", validate: /^.+$/, response: null }, | |
template: { question: "Create default template file", default: "yes", validate: /^(y(es)?)|(n(o)?)$/i, response: null }, | |
data: { question: "Create default data file", default: "yes", validate: /^(y(es)?)|(n(o)?)$/i, response: null }, | |
port: { question: "Port to listen for requests", default: "3000", validate: /^\d+$/, response: null } | |
}; | |
let position = 0; | |
function askNextQuestion() { | |
let keys = Object.keys(questions); | |
if (position >= keys.length) return handleCompletion(); | |
let key = keys[position]; | |
let q = questions[key]; | |
let prompt = q.question + " (" + q.default+") "; | |
rl.question(prompt, (answer) => { | |
if (answer.length > 0 && !q.validate.exec(answer)) return askNextQuestion(); | |
q.response = answer; | |
if (key === "template" && answer.toLowerCase().startsWith("n")) delete questions.data; | |
position++; | |
askNextQuestion(); | |
}); | |
} | |
function handleCompletion() { | |
console.log("Here is the result\n", questions); | |
rl.close(); | |
process.exit(); | |
} | |
console.log("This utility will walk you through creating a lynx-docs project. It only covers the most common items, and tries to guess sensible defaults.\n"); | |
askNextQuestion(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment