Skip to content

Commit 2bdd195

Browse files
committed
Add option to stringify devices before printing
1 parent 57f5e99 commit 2bdd195

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

cli.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ program
2525
.option('--api-secret [apiSecret]', 'your tuya.com API secret')
2626
.option('--schema <schema>', 'your tuya.com app identifier')
2727
.option('-r, --region [region]', 'the region closest to you from the following list: us=Americas, eu=Europe, cn=Asia', 'us')
28+
.option('-s, --stringify', 'stringify JSON before printing', false)
2829
.action(cloud.list);
2930

3031
// Link a device (old method)
@@ -112,6 +113,7 @@ program
112113
program
113114
.command('wizard')
114115
.description('list devices from an offical app')
116+
.option('-s, --stringify', 'stringify JSON before printing', false)
115117
.action(options => wizard(conf, options));
116118

117119
// Get help

lib/cloud.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ const list = async options => {
77

88
const {devices} = await api.getDevices();
99

10-
console.log(devices);
10+
if (options.stringify) {
11+
console.log(JSON.stringify(devices));
12+
} else {
13+
console.log(devices);
14+
}
1115
};
1216

1317
module.exports = {list};

lib/wizard.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const AggregateError = require('es-aggregate-error/polyfill')();
66

77
const REGIONS = ['eu', 'us', 'cn'];
88

9-
const list = async conf => {
9+
const list = async (conf, options) => {
1010
let questions = [
1111
{
1212
name: 'deviceId',
@@ -99,7 +99,13 @@ const list = async conf => {
9999
const devices = await api.getDevicesByUser(userId);
100100

101101
// Output devices
102-
console.log(devices.map(device => ({name: device.name, id: device.id, key: device.local_key})));
102+
const prettyDevices = devices.map(device => ({name: device.name, id: device.id, key: device.local_key}));
103+
104+
if (options.stringify) {
105+
console.log(JSON.stringify(prettyDevices));
106+
} else {
107+
console.log(prettyDevices);
108+
}
103109

104110
// Save API key and secret
105111
conf.set('apiKey', answers.apiKey);

0 commit comments

Comments
 (0)