Skip to content

Instantly share code, notes, and snippets.

@guychouk
Created September 26, 2022 00:22
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 guychouk/cca710ea0ae4607b0b23e007147768da to your computer and use it in GitHub Desktop.
Save guychouk/cca710ea0ae4607b0b23e007147768da to your computer and use it in GitHub Desktop.
Console app TypeScript skeleton πŸ’€
import fs from 'fs';
import glob from 'glob';
import chalk from 'chalk';
import EventEmitter from 'events';
type Configuration = {
first: string;
last: string;
};
const log = console.log;
const prompt = new EventEmitter();
const resultStyle = chalk.blue.bold;
const questionStyle = chalk.greenBright.bold;
const results: Configuration = { first: '', last: '' };
let currentQuestion = '';
process.stdin.resume();
process.stdin.on('data', function (data) {
prompt.emit(currentQuestion, data.toString().trim());
});
prompt.on(':new', function (name, question) {
currentQuestion = name;
log(question);
process.stdout.write('> ');
});
prompt.emit(':new', 'first', questionStyle('First name?'));
prompt.on('first', function (data) {
results.first = data;
prompt.emit(':new', 'last', questionStyle('Last name?'));
});
prompt.on('last', function (data) {
results.last = data;
prompt.emit(':end');
});
prompt.on(':end', function () {
log(resultStyle(`Your name is ${results.first} ${results.last}`));
process.stdin.pause();
});
{
"dependencies": {
"glob": "8.0.3",
"chalk": "5.0.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment