@iobroker/js-controller-cli 4.0.0-alpha.8-20210909-001a711c → 4.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2014-2021 bluefox<dogafox@gmail.com>,
3
+ Copyright (c) 2014-2022 bluefox<dogafox@gmail.com>,
4
4
  Copyright (c) 2014 hobbyquaker
5
5
 
6
6
  Permission is hereby granted, free of charge, to any person obtaining a copy
package/README.md CHANGED
@@ -3,4 +3,4 @@ The Library contains the cli classes of ioBroker.
3
3
 
4
4
  ## License
5
5
  MIT
6
- Copyright 2018-2021 bluefox <dogafox@gmail.com>
6
+ Copyright 2018-2022 bluefox <dogafox@gmail.com>
package/index.js CHANGED
@@ -1,18 +1,19 @@
1
1
  module.exports = {
2
- success: require('./lib/cli/messages').success,
3
- warn: require('./lib/cli/messages').warn,
4
- error: require('./lib/cli/messages').error,
5
- tools: require('./lib/cli/cliTools'),
2
+ success: require('./lib/cli/messages').success,
3
+ warn: require('./lib/cli/messages').warn,
4
+ error: require('./lib/cli/messages').error,
5
+ tools: require('./lib/cli/cliTools'),
6
6
  command: {
7
- object: require('./lib/cli/cliObjects.js'),
8
- state: require('./lib/cli/cliStates.js'),
7
+ object: require('./lib/cli/cliObjects.js'),
8
+ state: require('./lib/cli/cliStates.js'),
9
9
  process: require('./lib/cli/cliProcess.js'),
10
10
  message: require('./lib/cli/cliMessage.js'),
11
- logs: require('./lib/cli/cliLogs.js'),
12
- host: require('./lib/cli/cliHost.js'),
13
- cert: require('./lib/cli/cliCert.js'),
11
+ logs: require('./lib/cli/cliLogs.js'),
12
+ host: require('./lib/cli/cliHost.js'),
13
+ cert: require('./lib/cli/cliCert.js'),
14
14
  compact: require('./lib/cli/cliCompact.js'),
15
- debug: require('./lib/cli/cliDebug.js'),
15
+ debug: require('./lib/cli/cliDebug.js'),
16
16
  plugin: require('./lib/cli/cliPlugin.js')
17
- }
17
+ },
18
+ setupList: require('./lib/setup/setupList')
18
19
  };
@@ -6,7 +6,6 @@ const { tools } = require('@iobroker/js-controller-common');
6
6
 
7
7
  /** Command ioBroker cert ... */
8
8
  module.exports = class CLICert extends CLICommand {
9
-
10
9
  /** @param {import('./cliCommand.js').CLICommandOptions} options */
11
10
  constructor(options) {
12
11
  super(options);
@@ -37,18 +36,25 @@ module.exports = class CLICert extends CLICommand {
37
36
  * create new private certificate
38
37
  * @param {any[]} [_args]
39
38
  */
40
- create(_args) {
39
+ async create(_args) {
41
40
  const id = 'system.certificates';
42
41
  const certPropPath = 'native.certificates';
43
42
 
44
43
  const certificates = tools.generateDefaultCertificates();
45
44
  if (certificates) {
46
45
  console.log(JSON.stringify(certificates, null, 2));
47
- // use the command `iobroker object set ...` to update the certificate
48
- const objectsCommandArgs = ['set', id, certPropPath + '=' + JSON.stringify(certificates)];
49
- const objectsCommand = new CLIObjects(this.options);
50
- objectsCommand.execute(objectsCommandArgs);
46
+ for (const cert of Object.keys(certificates)) {
47
+ await new Promise(resolve => {
48
+ // use the command `iobroker object set ...` to update the certificate
49
+ console.log(`Update certificate ${cert}`);
50
+ const objectsCommandArgs = ['set', id, `${certPropPath}.${cert}=${certificates[cert]}`];
51
+ const objectsCommand = new CLIObjects({ ...this.options, callback: resolve });
52
+ objectsCommand.execute(objectsCommandArgs);
53
+ });
54
+ }
51
55
  }
56
+
57
+ this.options.callback();
52
58
  }
53
59
 
54
60
  // view-command
@@ -77,4 +83,4 @@ module.exports = class CLICert extends CLICommand {
77
83
  });
78
84
  });
79
85
  }
80
- };
86
+ };
@@ -6,13 +6,11 @@ const fs = require('fs-extra');
6
6
 
7
7
  /** Command ioBroker compact ... */
8
8
  module.exports = class CLICompact extends CLICommand {
9
-
10
9
  /** @param {import('./cliCommand.js').CLICommandOptions} options */
11
10
  constructor(options) {
12
11
  super(options);
13
12
  this.config = fs.readJSONSync(tools.getConfigFileName());
14
13
  }
15
-
16
14
  /**
17
15
  * Executes a command
18
16
  * @param {any[]} args
@@ -111,13 +109,18 @@ module.exports = class CLICompact extends CLICommand {
111
109
  objects.getObject('system.adapter.' + instance, (err, obj) => {
112
110
  if (!err && obj) {
113
111
  if (!obj.common.compact) {
114
- console.log('This adapter does not support compact mode. The below settings will have no effect!');
112
+ console.log(
113
+ 'This adapter does not support compact mode. The below settings will have no effect!'
114
+ );
115
115
  console.log();
116
116
  } else {
117
117
  console.log('Adapter supports compact mode: ' + !!obj.common.compact);
118
118
  }
119
119
  console.log('Compact mode enabled for instance: ' + !!obj.common.runAsCompactMode);
120
- console.log('Compact group: ' + (obj.common.compactGroup !== undefined ? obj.common.compactGroup : 1));
120
+ console.log(
121
+ 'Compact group: ' +
122
+ (obj.common.compactGroup !== undefined ? obj.common.compactGroup : 1)
123
+ );
121
124
  return void callback();
122
125
  } else {
123
126
  CLI.error.invalidInstance(instance);
@@ -138,7 +141,9 @@ module.exports = class CLICompact extends CLICommand {
138
141
  objects.getObject('system.adapter.' + instance, (err, obj) => {
139
142
  if (!err && obj) {
140
143
  if (!obj.common.compact) {
141
- console.log('This adapter does not support compact mode. The below settings will have no effect!');
144
+ console.log(
145
+ 'This adapter does not support compact mode. The below settings will have no effect!'
146
+ );
142
147
  console.log();
143
148
  } else {
144
149
  console.log('Adapter supports compact mode : ' + !!obj.common.compact);
@@ -158,8 +163,18 @@ module.exports = class CLICompact extends CLICommand {
158
163
  newCompactGroup = groupId;
159
164
  }
160
165
  }
161
- console.log('Compact mode enabled for instance: ' + (newRunAsCompactMode !== undefined ? '--> ' + newRunAsCompactMode : !!obj.common.runAsCompactMode));
162
- console.log('Compact group: ' + (newCompactGroup !== undefined && obj.common.compactGroup !== newCompactGroup ? '--> ' + newCompactGroup : obj.common.compactGroup));
166
+ console.log(
167
+ 'Compact mode enabled for instance: ' +
168
+ (newRunAsCompactMode !== undefined
169
+ ? '--> ' + newRunAsCompactMode
170
+ : !!obj.common.runAsCompactMode)
171
+ );
172
+ console.log(
173
+ 'Compact group: ' +
174
+ (newCompactGroup !== undefined && obj.common.compactGroup !== newCompactGroup
175
+ ? '--> ' + newCompactGroup
176
+ : obj.common.compactGroup)
177
+ );
163
178
  if (newRunAsCompactMode !== undefined || newCompactGroup !== undefined) {
164
179
  if (newCompactGroup !== undefined) {
165
180
  obj.common.compactGroup = newCompactGroup;
@@ -188,5 +203,4 @@ module.exports = class CLICompact extends CLICommand {
188
203
  });
189
204
  });
190
205
  }
191
-
192
206
  };
@@ -2,7 +2,7 @@
2
2
  const CLI = require('./messages.js');
3
3
  const CLICommand = require('./cliCommand.js');
4
4
  const CLITools = require('./cliTools');
5
- const { tools } = require('@iobroker/js-controller-common');
5
+ const { tools, EXIT_CODES } = require('@iobroker/js-controller-common');
6
6
  const child_process = require('child_process');
7
7
 
8
8
  /** Command ioBroker debug ... */
@@ -12,6 +12,32 @@ module.exports = class CLICompact extends CLICommand {
12
12
  super(options);
13
13
  }
14
14
 
15
+ /**
16
+ * Cehcks if the adpter instance is running
17
+ *
18
+ * @param {string} adapter
19
+ * @param {string} instance
20
+ * @return {Promise<boolean>}
21
+ * @private
22
+ */
23
+ _isInstanceRunning(adapter, instance) {
24
+ const { dbConnect } = this.options;
25
+ return new Promise(resolve => {
26
+ dbConnect(async (objects, states) => {
27
+ try {
28
+ const state = await states.getStateAsync(`system.adapter.${adapter}.${instance}.alive`);
29
+ if (state && state.val) {
30
+ resolve(true);
31
+ return;
32
+ }
33
+ } catch {
34
+ // ignore
35
+ }
36
+ resolve(false);
37
+ });
38
+ });
39
+ }
40
+
15
41
  /**
16
42
  * Executes a command
17
43
  * @param {any[]} args
@@ -20,14 +46,16 @@ module.exports = class CLICompact extends CLICommand {
20
46
  const { callback, ...params } = this.options;
21
47
  const adapter = args[0];
22
48
  if (!adapter) {
23
- CLI.error.requiredArgumentMissing(
24
- 'adaptername',
25
- 'debug <adaptername>'
26
- );
49
+ CLI.error.requiredArgumentMissing('adaptername', 'debug <adaptername>');
27
50
  return void callback(34);
28
51
  }
29
52
 
30
- const { instance } = CLITools.splitAdapterOrInstanceIdentifierWithVersion(adapter);
53
+ const { name, instance } = CLITools.splitAdapterOrInstanceIdentifierWithVersion(adapter);
54
+
55
+ if (await this._isInstanceRunning(name, instance || '0')) {
56
+ CLI.error.instanceAlreadyRunning(`${name}.${instance || '0'}`);
57
+ return void callback(EXIT_CODES.ADAPTER_ALREADY_RUNNING);
58
+ }
31
59
 
32
60
  const adapterDir = tools.getAdapterDir(adapter);
33
61
  if (!adapterDir) {
@@ -49,9 +77,7 @@ module.exports = class CLICompact extends CLICommand {
49
77
  const nodeArgs = [
50
78
  ...tools.getDefaultNodeArgs(mainFile),
51
79
  // --inspect[-brk][=[ip]:[port]]
52
- `--inspect${params.wait ? '-brk' : ''}${
53
- !!params.ip || !!params.port ? '=' : ''
54
- }${params.ip || ''}${
80
+ `--inspect${params.wait ? '-brk' : ''}${!!params.ip || !!params.port ? '=' : ''}${params.ip || ''}${
55
81
  !!params.ip && !!params.port ? ':' : ''
56
82
  }${params.port || ''}`
57
83
  ];
@@ -1,14 +1,13 @@
1
1
  'use strict';
2
2
  const CLI = require('./messages.js');
3
3
  const CLICommand = require('./cliCommand.js');
4
- const {enumHosts, enumObjects, getObjectFrom, enumInstances} = require('./cliTools');
4
+ const { enumHosts, enumObjects, getObjectFrom, enumInstances } = require('./cliTools');
5
5
  const { tools } = require('@iobroker/js-controller-common');
6
6
  const os = require('os');
7
7
  const fs = require('fs-extra');
8
8
 
9
9
  /** Command iobroker host ... */
10
10
  module.exports = class CLIHost extends CLICommand {
11
-
12
11
  /** @param {import('./cliCommand').CLICommandOptions} options */
13
12
  constructor(options) {
14
13
  super(options);
@@ -126,7 +125,6 @@ module.exports = class CLIHost extends CLICommand {
126
125
  // Notify the user that we are done
127
126
  CLI.success.hostDeleted(hostname);
128
127
  return void callback();
129
-
130
128
  } catch (err) {
131
129
  CLI.error.unknown(err.message);
132
130
  return void callback(1);
@@ -261,8 +259,8 @@ module.exports = class CLIHost extends CLICommand {
261
259
 
262
260
  // Also rename all instances
263
261
  const instances = await enumInstances(objects);
264
- const instancesToRename = oldHostname === undefined ? instances
265
- : instances.filter(i => i.common.host === oldHostname);
262
+ const instancesToRename =
263
+ oldHostname === undefined ? instances : instances.filter(i => i.common.host === oldHostname);
266
264
  if (instancesToRename.length > 0) {
267
265
  for (const instance of instancesToRename) {
268
266
  // Update each instance object
@@ -272,14 +270,12 @@ module.exports = class CLIHost extends CLICommand {
272
270
  CLI.warn.noInstancesFoundOnHost(oldHostname);
273
271
  }
274
272
  return void callback();
275
-
276
273
  } catch (err) {
277
274
  CLI.error.unknown(err.message);
278
275
  return void callback(1);
279
276
  }
280
277
  });
281
278
  }
282
-
283
279
  };
284
280
 
285
281
  /**
@@ -287,23 +283,19 @@ module.exports = class CLIHost extends CLICommand {
287
283
  * @param {any} objects The objects DB to use
288
284
  * @param {any} instance The instance object
289
285
  * @param {string} newHostname The new hostname the instance should be running on
286
+ * @return Promise<void>
290
287
  */
291
- function changeInstanceHost(objects, instance, newHostname) {
292
- return new Promise(resolve => {
293
- const oldInstanceHost = instance.common.host;
294
- instance.from = getObjectFrom();
295
- instance.ts = Date.now();
296
- instance.common.host = newHostname;
297
- // and save it
298
- objects.setObjectAsync(instance._id, instance)
299
- .then(() => {
300
- CLI.success.instanceHostChanged(instance._id, oldInstanceHost, newHostname);
301
- resolve();
302
- })
303
- .catch(err => {
304
- CLI.error.cannotChangeObject(instance._id, err.message);
305
- // resolve anyways, we don't want to cause errors
306
- resolve();
307
- });
308
- });
288
+ async function changeInstanceHost(objects, instance, newHostname) {
289
+ const oldInstanceHost = instance.common.host;
290
+ instance.from = getObjectFrom();
291
+ instance.ts = Date.now();
292
+ instance.common.host = newHostname;
293
+ // and save it
294
+ try {
295
+ await objects.setObjectAsync(instance._id, instance);
296
+ CLI.success.instanceHostChanged(instance._id, oldInstanceHost, newHostname);
297
+ } catch (e) {
298
+ CLI.error.cannotChangeObject(instance._id, e.message);
299
+ // resolve anyways, we don't want to cause errors
300
+ }
309
301
  }
@@ -10,7 +10,6 @@ const { tools } = require('@iobroker/js-controller-common');
10
10
 
11
11
  /** Command ioBroker state ... */
12
12
  module.exports = class CLILogs extends CLICommand {
13
-
14
13
  /** @param {import('./cliCommand').CLICommandOptions} options */
15
14
  constructor(options) {
16
15
  super(options);
@@ -24,7 +23,6 @@ module.exports = class CLILogs extends CLICommand {
24
23
  * @param {any[]} args
25
24
  */
26
25
  execute(args, params) {
27
-
28
26
  /** @type {string | undefined} */
29
27
  const adapterName = args[0];
30
28
  const watch = params.watch || params.w;
@@ -36,7 +34,7 @@ module.exports = class CLILogs extends CLICommand {
36
34
  };
37
35
 
38
36
  const config = fs.readJSONSync(require.resolve(getConfigFileName()));
39
- const logger = require('../logger')(config.log);
37
+ const logger = require('@iobroker/js-controller-common').logger(config.log);
40
38
  let fileName = logger.getFileName();
41
39
  if (fileName) {
42
40
  let lines = fs.readFileSync(fileName).toString('utf-8').split('\n');
@@ -60,10 +58,10 @@ module.exports = class CLILogs extends CLICommand {
60
58
  fileName = fileName.replace(/\\/g, '/');
61
59
  const parts = fileName.split('/');
62
60
  parts.pop();
63
- chokidar.watch(`${parts.join('/')}/iobroker*`, {awaitWriteFinish: {stabilityThreshold: 500}})
61
+ chokidar
62
+ .watch(`${parts.join('/')}/iobroker*`, { awaitWriteFinish: { stabilityThreshold: 500 } })
64
63
  .on('all', this.watchHandler.bind(this, options))
65
- .on('ready', () => this.isReady = true)
66
- ;
64
+ .on('ready', () => (this.isReady = true));
67
65
  }
68
66
  } else {
69
67
  console.log('No log file found');
@@ -86,12 +84,7 @@ module.exports = class CLILogs extends CLICommand {
86
84
  watchHandler(options, event, path, stats) {
87
85
  if (event === 'add' || !this.fileSizes.has(path)) {
88
86
  this.fileSizes.set(path, stats.size);
89
- if (
90
- stats.size > 0 && (
91
- this.isReady
92
- || (options.complete && this.isTodaysLogfile(path))
93
- )
94
- ) {
87
+ if (stats.size > 0 && (this.isReady || (options.complete && this.isTodaysLogfile(path)))) {
95
88
  this.streamChange(path, 0, options);
96
89
  }
97
90
  } else if (event === 'change') {
@@ -111,7 +104,7 @@ module.exports = class CLILogs extends CLICommand {
111
104
  */
112
105
  isTodaysLogfile(path) {
113
106
  const YYYYMMDDDate = new Date().toJSON().slice(0, 10);
114
- return path.indexOf(YYYYMMDDDate) > -1;
107
+ return path.includes(YYYYMMDDDate);
115
108
  }
116
109
 
117
110
  /**
@@ -123,7 +116,7 @@ module.exports = class CLILogs extends CLICommand {
123
116
  streamChange(path, start, options) {
124
117
  const input = fs.createReadStream(path, {
125
118
  encoding: 'utf8',
126
- start: start,
119
+ start,
127
120
  autoClose: true
128
121
  });
129
122
  if (options.adapterName) {
@@ -131,14 +124,12 @@ module.exports = class CLILogs extends CLICommand {
131
124
  input
132
125
  .pipe(es.split())
133
126
  // @ts-ignore
134
- .pipe(es.filterSync(line => line.indexOf(options.adapterName) > -1))
127
+ .pipe(es.filterSync(line => line.includes(options.adapterName)))
135
128
  .pipe(es.mapSync(line => line + os.EOL))
136
- .pipe(process.stdout)
137
- ;
129
+ .pipe(process.stdout);
138
130
  } else {
139
131
  // just pipe the input through
140
132
  tools.pipeLinewise(input, process.stdout);
141
133
  }
142
134
  }
143
135
  };
144
-
@@ -6,7 +6,6 @@ const { enumInstances } = require('./cliTools');
6
6
 
7
7
  /** Command iobroker object ... */
8
8
  module.exports = class CLIMessage extends CLICommand {
9
-
10
9
  /** @param {import('./cliCommand').CLICommandOptions} options */
11
10
  constructor(options) {
12
11
  super(options);
@@ -19,8 +18,7 @@ module.exports = class CLIMessage extends CLICommand {
19
18
  execute(args) {
20
19
  const { callback, dbConnect, showHelp } = this.options;
21
20
  /** @type {[string, string, any?]} */
22
- // eslint-disable-next-line prefer-const
23
- let [adapter, command, message] = (args);
21
+ let [adapter, command, message] = args;
24
22
  if (adapter === null || adapter === undefined) {
25
23
  CLI.error.requiredArgumentMissing('adapter');
26
24
  showHelp();
@@ -37,11 +35,7 @@ module.exports = class CLIMessage extends CLICommand {
37
35
  }
38
36
  // Try to parse JSON
39
37
  // TODO: can we use the methods from cliObjects?
40
- if (
41
- typeof message === 'string'
42
- && message.startsWith('{')
43
- && message.endsWith('}')
44
- ) {
38
+ if (typeof message === 'string' && message.startsWith('{') && message.endsWith('}')) {
45
39
  message = JSON.parse(message);
46
40
  }
47
41
 
@@ -68,12 +62,9 @@ module.exports = class CLIMessage extends CLICommand {
68
62
  }
69
63
 
70
64
  // Send the message to all targets
71
- const messagePromises = messageTargets.map(
72
- t => sendMessage(states, t, command, message)
73
- );
65
+ const messagePromises = messageTargets.map(t => sendMessage(states, t, command, message));
74
66
  await Promise.all(messagePromises);
75
67
  return void callback();
76
-
77
68
  } catch (err) {
78
69
  CLI.error.unknown(err.message);
79
70
  return void callback(1);