@vexify-org/yaggs 6.2.0 → 6.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +9 -0
- package/lib/parser.js +71 -0
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -113,6 +113,15 @@ class Yaggs {
|
|
|
113
113
|
return this.parser.checkVersion(packageName, currentVersion);
|
|
114
114
|
}
|
|
115
115
|
|
|
116
|
+
async prompt(questions) {
|
|
117
|
+
return this.parser.prompt(questions);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
progress(percentage, message = '') {
|
|
121
|
+
this.parser.progress(percentage, message);
|
|
122
|
+
return this;
|
|
123
|
+
}
|
|
124
|
+
|
|
116
125
|
check(fn) {
|
|
117
126
|
this.middleware(fn);
|
|
118
127
|
return this;
|
package/lib/parser.js
CHANGED
|
@@ -715,6 +715,77 @@ complete -c ${this.scriptName} `;
|
|
|
715
715
|
}
|
|
716
716
|
}
|
|
717
717
|
|
|
718
|
+
async prompt(questions) {
|
|
719
|
+
const readline = require('readline');
|
|
720
|
+
const rl = readline.createInterface({
|
|
721
|
+
input: process.stdin,
|
|
722
|
+
output: process.stdout
|
|
723
|
+
});
|
|
724
|
+
|
|
725
|
+
const answers = {};
|
|
726
|
+
|
|
727
|
+
for (const question of questions) {
|
|
728
|
+
const answer = await new Promise((resolve) => {
|
|
729
|
+
const defaultVal = question.default !== undefined ? ` (${question.default})` : '';
|
|
730
|
+
const choices = question.choices ? ` [${question.choices.join('/')}]` : '';
|
|
731
|
+
const required = question.required ? '*' : '';
|
|
732
|
+
|
|
733
|
+
rl.question(`\u001b[34m${question.name}${required}${choices}${defaultVal}:\u001b[0m `, (input) => {
|
|
734
|
+
const value = input.trim() || question.default;
|
|
735
|
+
|
|
736
|
+
if (question.required && !value) {
|
|
737
|
+
console.log('\u001b[31mThis field is required!\u001b[0m');
|
|
738
|
+
rl.emit('close');
|
|
739
|
+
resolve(null);
|
|
740
|
+
return;
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
if (question.choices && value && !question.choices.includes(value)) {
|
|
744
|
+
console.log(`\u001b[31mInvalid choice. Must be one of: ${question.choices.join(', ')}\u001b[0m`);
|
|
745
|
+
rl.emit('close');
|
|
746
|
+
resolve(null);
|
|
747
|
+
return;
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
if (question.type === 'number') {
|
|
751
|
+
resolve(parseFloat(value));
|
|
752
|
+
} else if (question.type === 'integer') {
|
|
753
|
+
resolve(parseInt(value, 10));
|
|
754
|
+
} else if (question.type === 'boolean') {
|
|
755
|
+
resolve(['true', '1', 'yes', 'y'].includes(value.toLowerCase()));
|
|
756
|
+
} else {
|
|
757
|
+
resolve(value);
|
|
758
|
+
}
|
|
759
|
+
});
|
|
760
|
+
});
|
|
761
|
+
|
|
762
|
+
if (answer === null) {
|
|
763
|
+
rl.close();
|
|
764
|
+
return null;
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
answers[question.name] = answer;
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
rl.close();
|
|
771
|
+
return answers;
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
progress(percentage, message = '') {
|
|
775
|
+
const barLength = 40;
|
|
776
|
+
const filled = Math.round(barLength * percentage / 100);
|
|
777
|
+
const empty = barLength - filled;
|
|
778
|
+
|
|
779
|
+
const bar = '\u001b[32m' + '█'.repeat(filled) + '\u001b[0m' + '░'.repeat(empty);
|
|
780
|
+
const percent = percentage.toFixed(1);
|
|
781
|
+
|
|
782
|
+
process.stdout.write(`\r${bar} ${percent}% ${message}`);
|
|
783
|
+
|
|
784
|
+
if (percentage >= 100) {
|
|
785
|
+
process.stdout.write('\n');
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
|
|
718
789
|
getHelp() {
|
|
719
790
|
let help = `\u001b[36m${this.scriptName}\u001b[0m`;
|
|
720
791
|
|