From 77b763a88fb20ca23b4a7c95622034507762f111 Mon Sep 17 00:00:00 2001 From: Alexis Svinartchouk Date: Thu, 20 Dec 2018 14:33:06 +0100 Subject: [PATCH] Update util available-drives action * switch from coffeescript to typescript * use etcher-sdk instead of drivelist Change-Type: patch --- lib/actions/util.coffee | 56 ----------------------------------- lib/actions/util.ts | 65 +++++++++++++++++++++++++++++++++++++++++ package.json | 1 - 3 files changed, 65 insertions(+), 57 deletions(-) delete mode 100644 lib/actions/util.coffee create mode 100644 lib/actions/util.ts diff --git a/lib/actions/util.coffee b/lib/actions/util.coffee deleted file mode 100644 index 2b2bdbe7..00000000 --- a/lib/actions/util.coffee +++ /dev/null @@ -1,56 +0,0 @@ -### -Copyright 2016-2017 Balena - -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. -### - -_ = require('lodash') - -exports.availableDrives = - # TODO: dedupe with https://github.com/balena-io-modules/resin-cli-visuals/blob/master/lib/widgets/drive/index.coffee - signature: 'util available-drives' - description: 'list available drives' - help: """ - Use this command to list your machine's drives usable for writing the OS image to. - Skips the system drives. - """ - action: -> - Promise = require('bluebird') - drivelist = require('drivelist') - driveListAsync = Promise.promisify(drivelist.list) - chalk = require('chalk') - visuals = require('resin-cli-visuals') - - formatDrive = (drive) -> - size = drive.size / 1000000000 - return { - device: drive.device - size: "#{size.toFixed(1)} GB" - description: drive.description - } - - getDrives = -> - driveListAsync().then (drives) -> - return _.reject(drives, system: true) - - getDrives() - .then (drives) -> - if not drives.length - console.error("#{chalk.red('x')} No available drives were detected, plug one in!") - return - - console.log visuals.table.horizontal drives.map(formatDrive), [ - 'device' - 'size' - 'description' - ] diff --git a/lib/actions/util.ts b/lib/actions/util.ts new file mode 100644 index 00000000..093448a8 --- /dev/null +++ b/lib/actions/util.ts @@ -0,0 +1,65 @@ +/* + * decaffeinate suggestions: + * DS102: Remove unnecessary code created because of implicit returns + * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md + */ +/* +Copyright 2016-2017 Balena + +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 { CommandDefinition } from 'capitano'; +import chalk from 'chalk'; +import { stripIndent } from 'common-tags'; + +export const availableDrives: CommandDefinition<{}, {}> = { + signature: 'util available-drives', + description: 'list available drives', + help: stripIndent` + Use this command to list your machine's drives usable for writing the OS image to. + Skips the system drives. + `, + async action() { + const sdk = await import('etcher-sdk'); + const visuals = await import('resin-cli-visuals'); + + const adapter = new sdk.scanner.adapters.BlockDeviceAdapter(() => false); + const scanner = new sdk.scanner.Scanner([adapter]); + await scanner.start(); + + function formatDrive(drive: any) { + const size = drive.size / 1000000000; + return { + device: drive.device, + size: `${size.toFixed(1)} GB`, + description: drive.description, + }; + } + + if (scanner.drives.size === 0) { + console.error( + `${chalk.red('x')} No available drives were detected, plug one in!`, + ); + } else { + console.log( + visuals.table.horizontal(Array.from(scanner.drives).map(formatDrive), [ + 'device', + 'size', + 'description', + ]), + ); + } + scanner.stop(); + }, +}; diff --git a/package.json b/package.json index 5be18604..9b107cd2 100644 --- a/package.json +++ b/package.json @@ -126,7 +126,6 @@ "docker-toolbelt": "^3.3.5", "dockerode": "^2.5.5", "dockerode-options": "^0.2.1", - "drivelist": "^5.0.22", "ejs": "^2.5.7", "etcher-sdk": "^0.2.0", "event-stream": "3.3.4",