fix(install.js): 修复密码输入处理并添加错误处理

This commit is contained in:
zhang1106
2026-03-17 16:50:16 +08:00
parent 0be142d572
commit 96103039d8
+28 -10
View File
@@ -161,6 +161,15 @@ const rl = readline.createInterface({
output: process.stdout // 标准输出(屏幕) output: process.stdout // 标准输出(屏幕)
}); });
rl.on('close', () => {
process.exit(0);
});
rl.on('error', (err) => {
log.error(`输入错误: ${err.message}`);
process.exit(1);
});
/** /**
* 提问函数 - 获取用户输入 * 提问函数 - 获取用户输入
* *
@@ -196,13 +205,15 @@ function askPassword(question) {
let password = ''; let password = '';
const stdin = process.stdin; const stdin = process.stdin;
const wasRaw = stdin.isRaw;
const cleanup = () => {
if (stdin.isTTY) { if (stdin.isTTY) {
stdin.setRawMode(true); try {
stdin.setRawMode(false);
} catch {}
} }
stdin.resume(); stdin.removeListener('data', onData);
stdin.setEncoding('utf8'); };
const onData = (char) => { const onData = (char) => {
const c = char; const c = char;
@@ -211,28 +222,35 @@ function askPassword(question) {
case '\n': case '\n':
case '\r': case '\r':
case '\u0004': case '\u0004':
if (stdin.isTTY) { cleanup();
stdin.setRawMode(wasRaw || false);
}
stdin.pause();
stdin.removeListener('data', onData);
console.log(); console.log();
resolve(password); resolve(password);
break; break;
case '\u0003': case '\u0003':
process.exit(); cleanup();
console.log('\n已取消');
process.exit(0);
break; break;
case '\u007F': case '\u007F':
case '\b': case '\b':
if (password.length > 0) {
password = password.slice(0, -1); password = password.slice(0, -1);
process.stdout.write('\b \b');
}
break; break;
default: default:
password += c; password += c;
process.stdout.write('*');
break; break;
} }
}; };
if (stdin.isTTY) {
stdin.setRawMode(true);
}
stdin.setEncoding('utf8');
stdin.on('data', onData); stdin.on('data', onData);
stdin.resume();
}); });
} }