Skip to content

Commit

Permalink
Show the spinner after the device selector
Browse files Browse the repository at this point in the history
  • Loading branch information
monkbroc committed Jun 30, 2024
1 parent dd74c86 commit 5924265
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
32 changes: 18 additions & 14 deletions src/cmd/device-protection.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
let addToOutput = [];
let s;
try {
await this.ui.showBusySpinnerUntilResolved('Getting device status', this._withDevice(true, async () => {
await this._withDevice({ spinner: 'Getting device status', putDeviceBackInDfuMode: true }, async () => {
s = await this._getDeviceProtection();
let res;
let helper;
Expand All @@ -61,7 +61,7 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {

const deviceStr = await this._getDeviceString();
addToOutput.push(`${deviceStr}: ${chalk.bold(res)}${os.EOL}${helper}${os.EOL}`);
}));
});
} catch (error) {
// TODO: Log detailed and user-friendly error messages from the device or API instead of displaying the raw error message
throw new Error(`Unable to get device status: ${error.message}${os.EOL}`);
Expand All @@ -88,7 +88,7 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
async disableProtection() {
let addToOutput = [];

await this.ui.showBusySpinnerUntilResolved('Disabling device protection', this._withDevice(true, async () => {
await this._withDevice({ spinner: 'Disabling device protection', putDeviceBackInDfuMode: true }, async () => {
try {
const deviceStr = await this._getDeviceString();
let s = await this._getDeviceProtection();
Expand Down Expand Up @@ -122,7 +122,7 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
} catch (error) {
throw new Error(`Failed to disable device protection: ${error.message}${os.EOL}`);
}
}));
});

addToOutput.forEach((line) => {
this.ui.stdout.write(line);
Expand All @@ -146,7 +146,7 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
async enableProtection({ file } = {}) {
let addToOutput = [];
try {
await this.ui.showBusySpinnerUntilResolved('Enabling device protection', this._withDevice(false, async () => {
await this._withDevice({ spinner: 'Enabling device protection', putDeviceBackInDfuMode: false }, async () => {
const deviceStr = await this._getDeviceString();
const s = await this._getDeviceProtection();

Expand Down Expand Up @@ -188,7 +188,7 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
);
}
}
}));
});
} catch (error) {
throw new Error(`Failed to enable device protection: ${error.message}${os.EOL}`);
}
Expand Down Expand Up @@ -319,19 +319,23 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
* If it is in DFU mode, the device is reset and re-opened expecting it to be in normal mode.
*
* @async
* @param {boolean} putDeviceBackInDfuMode - Checks if device should be put back into dfy mode if the device was in dfu mode at the start of the operation
* @param {Object} options
* @param {boolean} options.putDeviceBackInDfuMode - Checks if device should be put back into dfy mode if the device was in dfu mode at the start of the operation
* @param {Function} options.spinner - The text to display in a spinner until the operation completes
* @param {Function} fn - The function to execute with the device.
* @returns {Promise<*>} The result of the function execution.
*/
async _withDevice(putDeviceBackInDfuMode, fn) {
async _withDevice({ putDeviceBackInDfuMode, spinner }, fn) {
try {
await this._getUsbDevice(this.device);
const deviceWasInDfuMode = this.device.isInDfuMode;
if (deviceWasInDfuMode) {
await this._putDeviceInSafeMode();
}
putDeviceBackInDfuMode = putDeviceBackInDfuMode && deviceWasInDfuMode;
return await fn();
return await this.ui.showBusySpinnerUntilResolved(spinner, (async () => {
const deviceWasInDfuMode = this.device.isInDfuMode;
if (deviceWasInDfuMode) {
await this._putDeviceInSafeMode();
}
putDeviceBackInDfuMode = putDeviceBackInDfuMode && deviceWasInDfuMode;
return await fn();
})());
} finally {
if (putDeviceBackInDfuMode) {
await this._waitForDeviceToReboot();
Expand Down
13 changes: 11 additions & 2 deletions src/cmd/device-protection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ describe('DeviceProtectionCommands', () => {
it('should execute a function with the device in normal (non-dfu) mode', async () => {
const fn = sinon.stub().resolves();

await deviceProtectionCommands._withDevice(true, fn);
await deviceProtectionCommands._withDevice({ putDeviceBackInDfuMode: true }, fn);

expect(deviceProtectionCommands._getUsbDevice).to.have.been.calledOnce;
expect(fn).to.have.been.calledOnce;
Expand All @@ -383,12 +383,21 @@ describe('DeviceProtectionCommands', () => {
sinon.stub(deviceProtectionCommands, '_putDeviceInSafeMode').resolves();
sinon.stub(deviceProtectionCommands, '_waitForDeviceToReboot').resolves();

await deviceProtectionCommands._withDevice(true, fn);
await deviceProtectionCommands._withDevice({ putDeviceBackInDfuMode: true }, fn);

expect(deviceProtectionCommands._putDeviceInSafeMode).to.have.been.calledOnce;
expect(deviceProtectionCommands._waitForDeviceToReboot).to.have.been.calledOnce;
expect(fn).to.have.been.calledOnce;
});

it('shows the spinner', async () => {
const fn = sinon.stub().returns(1234);
sinon.stub(deviceProtectionCommands.ui, 'showBusySpinnerUntilResolved').resolves();

await deviceProtectionCommands._withDevice({ spinner: 'Long operation' }, fn);

expect(deviceProtectionCommands.ui.showBusySpinnerUntilResolved).to.have.been.calledWith('Long operation', 1234);
});
});

describe('_getDeviceString', () => {
Expand Down

0 comments on commit 5924265

Please sign in to comment.