diff --git a/src/cmd/setup-tachyon.js b/src/cmd/setup-tachyon.js index 20e54004f..e63a8cf3e 100644 --- a/src/cmd/setup-tachyon.js +++ b/src/cmd/setup-tachyon.js @@ -322,11 +322,8 @@ Welcome to the Particle Tachyon setup! This interactive command: async _userConfiguration() { const systemPassword = await this._getSystemPassword(); - const wifi = await this._getWifi(); - const sshPublicKey = await this._getKeys(); - return { systemPassword, wifi, sshPublicKey }; } @@ -353,38 +350,10 @@ Welcome to the Particle Tachyon setup! This interactive command: } async _getSystemPassword() { - const questions = [ - { - type: 'password', - name: 'password', - message: 'Password for the system account:', - validate: (value) => { - if (!value) { - return 'Enter a password for the root account'; - } - return true; - } - }, - { - type: 'password', - name: 'passwordConfirm', - message: 'Re-enter the password for the root account:', - validate: (value) => { - if (!value) { - return 'You need to confirm the password'; - } - return true; - } - } - ]; - const res = await this.ui.prompt(questions); - - //check if the passwords match - if (res.password !== res.passwordConfirm) { - throw new Error('Passwords do not match. Please try again.'); - } - - return res.password; + return this.ui.promptPasswordWithConfirmation({ + customMessage: 'Enter a password for the system account:', + customConfirmationMessage: 'Re-enter the password for the system account:' + }); } async _getWifi() { @@ -410,25 +379,15 @@ Welcome to the Particle Tachyon setup! This interactive command: type: 'input', name: 'ssid', message: 'Enter your WiFi SSID:' - }, - { - type: 'password', - name: 'password', - message: 'Enter your WiFi password:' - }, - { - type: 'password', - name: 'passwordConfirm', - message: 'Re-enter your WiFi password:' - }, + } ]; const res = await this.ui.prompt(questions); + const { password } = await this.ui.promptPasswordWithConfirmation({ + customMessage: 'Enter your WiFi password:', + customConfirmationMessage: 'Re-enter your WiFi password:' + }); - if (res.password !== res.passwordConfirm) { - throw new Error('Passwords do not match. Please try again.'); - } - - return { ssid: res.ssid, password: res.password }; + return { ssid: res.ssid, password }; } async _getKeys() { diff --git a/src/lib/ui/index.js b/src/lib/ui/index.js index a7e01f79d..df16f7fb5 100644 --- a/src/lib/ui/index.js +++ b/src/lib/ui/index.js @@ -40,6 +40,32 @@ module.exports = class UI { return inquirer.prompt(question); } + async promptPasswordWithConfirmation({ customMessage, customConfirmationMessage } = {}) { + let unmatchedPassword = true; + let password; + const questions = [{ + type: 'password', + name: 'requestedPassword', + message: customMessage || 'Enter your password:' + }, + { + type: 'password', + name: 'confirmPassword', + message: customConfirmationMessage || 'Confirm your password:' + }]; + while (unmatchedPassword) { + const { requestedPassword, confirmPassword } = await this.prompt(questions); + // Verify that the passwords match + if (requestedPassword !== confirmPassword) { + this.write('Passwords do not match. Please try again.'); + } else { + password = requestedPassword; + unmatchedPassword = false; + } + } + return password; + } + createProgressBar() { return new cliProgress.SingleBar({ format: '[{bar}] {percentage}% | {description}',