diff --git a/doc/cli.markdown b/doc/cli.markdown index 3cb391c5..91e95c14 100644 --- a/doc/cli.markdown +++ b/doc/cli.markdown @@ -100,7 +100,6 @@ If you come across any problems or would like to get in touch: - [devices](#devices) - [device <uuid>](#device-uuid) - - [devices supported](#devices-supported) - [device register <application>](#device-register-application) - [device rm <uuid>](#device-rm-uuid) - [device identify <uuid>](#device-identify-uuid) @@ -114,6 +113,7 @@ If you come across any problems or would like to get in touch: - [device move <uuid>](#device-move-uuid) - [device init](#device-init) - [device os-update <uuid>](#device-os-update-uuid) + - [devices supported](#devices-supported) - Environment Variables @@ -374,14 +374,6 @@ Examples: $ balena device 7cf02a6 -## devices supported - -Use this command to get the list of all supported devices - -Examples: - - $ balena devices supported - ## device register <application> Use this command to register a device to an application. @@ -577,6 +569,14 @@ a balenaOS version confirm non interactively +## devices supported + +Use this command to get the list of all supported devices. + +Examples: + + $ balena devices supported + # Environment Variables ## envs diff --git a/lib/actions/device.coffee b/lib/actions/device.coffee index 041c58b8..3cf8a61f 100644 --- a/lib/actions/device.coffee +++ b/lib/actions/device.coffee @@ -115,25 +115,6 @@ exports.info = ] .nodeify(done) -exports.supported = - signature: 'devices supported' - description: 'list all supported devices' - help: ''' - Use this command to get the list of all supported devices - - Examples: - - $ balena devices supported - ''' - action: (params, options, done) -> - balena = require('balena-sdk').fromSharedOptions() - visuals = require('resin-cli-visuals') - - balena.models.config.getDeviceTypes().then (deviceTypes) -> - fields = ['slug', 'name'] - console.log visuals.table.horizontal(_.sortBy(deviceTypes, fields), fields) - .nodeify(done) - exports.register = signature: 'device register ' description: 'register a device' @@ -465,4 +446,6 @@ exports.init = .nodeify(done) -exports.osUpdate = require('./device_ts').osUpdate +tsActions = require('./device_ts') +exports.osUpdate = tsActions.osUpdate +exports.supported = tsActions.supported diff --git a/lib/actions/device_ts.ts b/lib/actions/device_ts.ts index 2aa67a76..a93e3980 100644 --- a/lib/actions/device_ts.ts +++ b/lib/actions/device_ts.ts @@ -19,6 +19,31 @@ import { stripIndent } from 'common-tags'; import { normalizeUuidProp } from '../utils/normalization'; import * as commandOptions from './command-options'; +export const supported: CommandDefinition<{}, {}> = { + signature: 'devices supported', + description: 'list all supported devices', + help: stripIndent` + Use this command to get the list of all supported devices. + + Examples: + + $ balena devices supported + `, + async action(_params, _options) { + const sdk = (await import('balena-sdk')).fromSharedOptions(); + const visuals = await import('resin-cli-visuals'); + const _ = await import('lodash'); + + let deviceTypes = await sdk.models.config.getDeviceTypes(); + const fields = ['slug', 'name']; + deviceTypes = _.sortBy(deviceTypes, fields).filter( + dt => dt.state !== 'DISCONTINUED', + ); + const output = await visuals.table.horizontal(deviceTypes, fields); + console.log(output); + }, +}; + // tslint:disable-next-line:no-namespace namespace OsUpdate { export interface Args { diff --git a/tests/commands/device/supported.spec.ts b/tests/commands/device/supported.spec.ts new file mode 100644 index 00000000..720ce0aa --- /dev/null +++ b/tests/commands/device/supported.spec.ts @@ -0,0 +1,47 @@ +import { expect } from 'chai'; +import * as _ from 'lodash'; +import { runCommand } from '../../helpers'; + +const HELP = ` +Usage: devices supported + +Use this command to get the list of all supported devices. + +Examples: + +\t$ balena devices supported +`; + +const cleanOutput = (output: string[] | string) => { + return _(_.castArray(output)) + .map(log => { + return log.split('\n').map(line => { + return line.trim(); + }); + }) + .flatten() + .compact() + .value(); +}; + +describe('balena devices supported', function() { + it('should list currently supported devices', async () => { + const { out, err } = await runCommand('devices supported'); + + const lines = cleanOutput(out); + + expect(lines[0].replace(/ +/g, ' ')).to.equal('SLUG NAME'); + expect(lines).to.have.lengthOf.at.least(2); + expect(lines.some(l => l.includes('DISCONTINUED'))).to.be.false; + + expect(err).to.have.lengthOf(0); + }); + + it('should print help text with the -h flag', async () => { + const { out, err } = await runCommand('devices supported -h'); + + expect(cleanOutput(out)).to.deep.equal(cleanOutput([HELP])); + + expect(err).to.have.lengthOf(0); + }); +});