From 09a59ab03f7e770ee451b2c2b09d13089599b27c Mon Sep 17 00:00:00 2001 From: Pagan Gazzard Date: Mon, 9 Mar 2020 16:05:53 +0000 Subject: [PATCH] Remove dependency on inquirer-dynamic-list --- lib/actions/local/flash.ts | 20 +----------- lib/utils/visuals/custom-dynamic-list.ts | 41 ------------------------ lib/utils/visuals/drive-list.ts | 35 -------------------- 3 files changed, 1 insertion(+), 95 deletions(-) delete mode 100644 lib/utils/visuals/custom-dynamic-list.ts delete mode 100644 lib/utils/visuals/drive-list.ts diff --git a/lib/actions/local/flash.ts b/lib/actions/local/flash.ts index 7a18608c..74f62aa2 100644 --- a/lib/actions/local/flash.ts +++ b/lib/actions/local/flash.ts @@ -22,25 +22,7 @@ import { getChalk, getVisuals } from '../../utils/lazy'; async function getDrive(options: { drive?: string; }): Promise { - const sdk = await import('etcher-sdk'); - - const adapter = new sdk.scanner.adapters.BlockDeviceAdapter(() => false); - const scanner = new sdk.scanner.Scanner([adapter]); - await scanner.start(); - let drive: SDK.sourceDestination.BlockDevice; - if (options.drive !== undefined) { - const d = scanner.getBy('device', options.drive); - if (d === undefined || !(d instanceof sdk.sourceDestination.BlockDevice)) { - throw new Error(`Drive not found: ${options.drive}`); - } - drive = d; - } else { - const { DriveList } = await import('../../utils/visuals/drive-list'); - const driveList = new DriveList(scanner); - drive = await driveList.run(); - } - scanner.stop(); - return drive; + return options.drive || getVisuals().drive('Select a drive'); } export const flash: CommandDefinition< diff --git a/lib/utils/visuals/custom-dynamic-list.ts b/lib/utils/visuals/custom-dynamic-list.ts deleted file mode 100644 index 01ae6ce0..00000000 --- a/lib/utils/visuals/custom-dynamic-list.ts +++ /dev/null @@ -1,41 +0,0 @@ -/** - * @license - * Copyright 2019 Balena Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -import DynamicList = require('inquirer-dynamic-list'); - -export abstract class CustomDynamicList extends DynamicList { - constructor(message: string, emptyMessage: string) { - super({ message, emptyMessage, choices: [] }); - } - - protected abstract getThings(): Iterable; - - protected abstract format(thing: T): string; - - public refresh(): void { - this.opt.choices.choices = []; - this.opt.choices.realChoices = []; - for (const thing of this.getThings()) { - this.addChoice({ name: this.format(thing), value: thing }); - } - this.render(); - } - - public async run(): Promise { - this.refresh(); - return await super.run(); - } -} diff --git a/lib/utils/visuals/drive-list.ts b/lib/utils/visuals/drive-list.ts deleted file mode 100644 index a5850224..00000000 --- a/lib/utils/visuals/drive-list.ts +++ /dev/null @@ -1,35 +0,0 @@ -import * as _sdk from 'etcher-sdk'; - -import { getChalk } from '../lazy'; -import { CustomDynamicList } from './custom-dynamic-list'; - -export class DriveList extends CustomDynamicList< - _sdk.sourceDestination.BlockDevice -> { - constructor(private scanner: _sdk.scanner.Scanner) { - super( - 'Select a drive', - `${getChalk().red('x')} No available drives were detected, plug one in!`, - ); - const refresh = this.refresh.bind(this); - scanner.on('attach', refresh); - scanner.on('detach', refresh); - } - - protected *getThings() { - const sdk: typeof _sdk = require('etcher-sdk'); - for (const drive of this.scanner.drives) { - if (drive instanceof sdk.sourceDestination.BlockDevice) { - yield drive; - } - } - } - - protected format(drive: _sdk.sourceDestination.BlockDevice) { - const size = - drive.size != null - ? `${(drive.size / 1e9).toFixed(1).toString()} GB` - : 'Unknown size'; - return `${drive.device} (${size}) - ${drive.description}`; - } -}