mirror of
https://github.com/balena-io/balena-cli.git
synced 2025-02-20 09:26:42 +00:00
Remove dependency on inquirer-dynamic-list
This commit is contained in:
parent
3d2e109e7f
commit
09a59ab03f
@ -22,25 +22,7 @@ import { getChalk, getVisuals } from '../../utils/lazy';
|
||||
async function getDrive(options: {
|
||||
drive?: string;
|
||||
}): Promise<SDK.sourceDestination.BlockDevice> {
|
||||
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<
|
||||
|
@ -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<T> extends DynamicList {
|
||||
constructor(message: string, emptyMessage: string) {
|
||||
super({ message, emptyMessage, choices: [] });
|
||||
}
|
||||
|
||||
protected abstract getThings(): Iterable<T>;
|
||||
|
||||
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<T> {
|
||||
this.refresh();
|
||||
return await super.run();
|
||||
}
|
||||
}
|
@ -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}`;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user