Current Dev State

This commit is contained in:
Tim Lorsbach
2025-06-23 20:13:54 +02:00
parent b4f9bb277d
commit ded50edaa2
22617 changed files with 4345095 additions and 174 deletions

View File

@ -0,0 +1,56 @@
'use strict';
var _ = require('lodash');
var readlineFacade = require('readline2');
/**
* Base interface class other can inherits from
*/
var UI = module.exports = function (opt) {
// Instantiate the Readline interface
// @Note: Don't reassign if already present (allow test to override the Stream)
if (!this.rl) {
this.rl = readlineFacade.createInterface(_.extend({
terminal: true
}, opt));
}
this.rl.resume();
this.onForceClose = this.onForceClose.bind(this);
// Make sure new prompt start on a newline when closing
this.rl.on('SIGINT', this.onForceClose);
process.on('exit', this.onForceClose);
};
/**
* Handle the ^C exit
* @return {null}
*/
UI.prototype.onForceClose = function () {
this.close();
console.log('\n'); // Line return
};
/**
* Close the interface and cleanup listeners
*/
UI.prototype.close = function () {
// Remove events listeners
this.rl.removeListener('SIGINT', this.onForceClose);
process.removeListener('exit', this.onForceClose);
// Restore prompt functionnalities
this.rl.output.unmute();
// Close the readline
this.rl.output.end();
this.rl.pause();
this.rl.close();
this.rl = null;
};

View File

@ -0,0 +1,98 @@
/**
* Sticky bottom bar user interface
*/
var util = require("util");
var through = require("through");
var Base = require("./baseUI");
var rlUtils = require("../utils/readline");
var _ = require("lodash");
/**
* Module exports
*/
module.exports = Prompt;
/**
* Constructor
*/
function Prompt( opt ) {
opt || (opt = {});
Base.apply( this, arguments );
this.log = through( this.writeLog.bind(this) );
this.bottomBar = opt.bottomBar || "";
this.render();
}
util.inherits( Prompt, Base );
/**
* Render the prompt to screen
* @return {Prompt} self
*/
Prompt.prototype.render = function() {
this.write(this.bottomBar);
return this;
};
/**
* Update the bottom bar content and rerender
* @param {String} bottomBar Bottom bar content
* @return {Prompt} self
*/
Prompt.prototype.updateBottomBar = function( bottomBar ) {
this.bottomBar = bottomBar;
rlUtils.clearLine(this.rl, 1);
return this.render();
};
/**
* Rerender the prompt
* @return {Prompt} self
*/
Prompt.prototype.writeLog = function( data ) {
rlUtils.clearLine(this.rl, 1);
this.rl.output.write(this.enforceLF(data.toString()));
return this.render();
};
/**
* Make sure line end on a line feed
* @param {String} str Input string
* @return {String} The input string with a final line feed
*/
Prompt.prototype.enforceLF = function( str ) {
return str.match(/[\r\n]$/) ? str : str + "\n";
};
/**
* Helper for writing message in Prompt
* @param {Prompt} prompt - The Prompt object that extends tty
* @param {String} message - The message to be output
*/
Prompt.prototype.write = function (message) {
var msgLines = message.split(/\n/);
this.height = msgLines.length;
// Write message to screen and setPrompt to control backspace
this.rl.setPrompt( _.last(msgLines) );
if ( this.rl.output.rows === 0 && this.rl.output.columns === 0 ) {
/* When it's a tty through serial port there's no terminal info and the render will malfunction,
so we need enforce the cursor to locate to the leftmost position for rendering. */
rlUtils.left( this.rl, message.length + this.rl.line.length );
}
this.rl.output.write( message );
};

View File

@ -0,0 +1,126 @@
'use strict';
var _ = require('lodash');
var rx = require('rx-lite');
var util = require('util');
var runAsync = require('run-async');
var utils = require('../utils/utils');
var Base = require('./baseUI');
/**
* Base interface class other can inherits from
*/
var PromptUI = module.exports = function (prompts, opt) {
Base.call(this, opt);
this.prompts = prompts;
};
util.inherits(PromptUI, Base);
PromptUI.prototype.run = function (questions, allDone) {
// Keep global reference to the answers
this.answers = {};
this.completed = allDone;
// Make sure questions is an array.
if (_.isPlainObject(questions)) {
questions = [questions];
}
// Create an observable, unless we received one as parameter.
// Note: As this is a public interface, we cannot do an instanceof check as we won't
// be using the exact same object in memory.
var obs = _.isArray(questions) ? rx.Observable.from(questions) : questions;
this.process = obs
.concatMap(this.processQuestion.bind(this))
.publish(); // `publish` creates a hot Observable. It prevents duplicating prompts.
this.process.subscribe(
_.noop,
function (err) { throw err; },
this.onCompletion.bind(this)
);
return this.process.connect();
};
/**
* Once all prompt are over
*/
PromptUI.prototype.onCompletion = function () {
this.close();
if (_.isFunction(this.completed)) {
this.completed(this.answers);
}
};
PromptUI.prototype.processQuestion = function (question) {
return rx.Observable.defer(function () {
var obs = rx.Observable.create(function (obs) {
obs.onNext(question);
obs.onCompleted();
});
return obs
.concatMap(this.setDefaultType.bind(this))
.concatMap(this.filterIfRunnable.bind(this))
.concatMap(utils.fetchAsyncQuestionProperty.bind(null, question, 'message', this.answers))
.concatMap(utils.fetchAsyncQuestionProperty.bind(null, question, 'default', this.answers))
.concatMap(utils.fetchAsyncQuestionProperty.bind(null, question, 'choices', this.answers))
.concatMap(this.fetchAnswer.bind(this));
}.bind(this));
};
PromptUI.prototype.fetchAnswer = function (question) {
var Prompt = this.prompts[question.type];
var prompt = new Prompt(question, this.rl, this.answers);
var answers = this.answers;
return utils.createObservableFromAsync(function () {
var done = this.async();
prompt.run(function (answer) {
answers[question.name] = answer;
done({ name: question.name, answer: answer });
});
});
};
PromptUI.prototype.setDefaultType = function (question) {
// Default type to input
if (!this.prompts[question.type]) {
question.type = 'input';
}
return rx.Observable.defer(function () {
return rx.Observable.return(question);
});
};
PromptUI.prototype.filterIfRunnable = function (question) {
if (question.when == null) {
return rx.Observable.return(question);
}
var handleResult = function (obs, shouldRun) {
if (shouldRun) {
obs.onNext(question);
}
obs.onCompleted();
};
var answers = this.answers;
return rx.Observable.defer(function () {
return rx.Observable.create(function (obs) {
if (_.isBoolean(question.when)) {
handleResult(obs, question.when);
return;
}
runAsync(question.when, function (shouldRun) {
handleResult(obs, shouldRun);
}, answers);
});
});
};