bdy 1.7.51-dev → 1.7.53-dev

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.
Files changed (38) hide show
  1. package/package.json +1 -1
  2. package/src/cfg.js +15 -3
  3. package/src/command/agent/install.js +2 -3
  4. package/src/command/agent/restart.js +2 -3
  5. package/src/command/agent/run.js +2 -2
  6. package/src/command/agent/start.js +2 -3
  7. package/src/command/agent/status.js +2 -3
  8. package/src/command/agent/stop.js +2 -3
  9. package/src/command/agent/tunnel/list.js +2 -3
  10. package/src/command/agent/tunnel/start.js +2 -3
  11. package/src/command/agent/tunnel/status.js +2 -3
  12. package/src/command/agent/tunnel/stop.js +2 -3
  13. package/src/command/agent/tunnel.js +2 -3
  14. package/src/command/agent/uninstall.js +2 -3
  15. package/src/command/agent/update.js +2 -3
  16. package/src/command/agent/version.js +2 -3
  17. package/src/command/agent.js +2 -3
  18. package/src/command/config/add.js +2 -3
  19. package/src/command/config/get/region.js +2 -3
  20. package/src/command/config/get/timeout.js +2 -3
  21. package/src/command/config/get/token.js +2 -3
  22. package/src/command/config/get/tunnel.js +2 -3
  23. package/src/command/config/get/tunnels.js +2 -3
  24. package/src/command/config/get/whitelist.js +2 -3
  25. package/src/command/config/get.js +2 -3
  26. package/src/command/config/remove/tunnel.js +2 -3
  27. package/src/command/config/remove.js +2 -3
  28. package/src/command/config/set/region.js +2 -3
  29. package/src/command/config/set/timeout.js +2 -3
  30. package/src/command/config/set/token.js +2 -3
  31. package/src/command/config/set/whitelist.js +2 -3
  32. package/src/command/config/set.js +2 -3
  33. package/src/command/config.js +2 -3
  34. package/src/command/start.js +2 -3
  35. package/src/command/version.js +2 -2
  36. package/src/index.js +8 -4
  37. package/src/logger.js +5 -1
  38. package/src/utils.js +81 -5
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bdy",
3
3
  "preferGlobal": false,
4
- "version": "1.7.51-dev",
4
+ "version": "1.7.53-dev",
5
5
  "type": "commonjs",
6
6
  "license": "MIT",
7
7
  "dependencies": {
package/src/cfg.js CHANGED
@@ -34,19 +34,27 @@ class Cfg {
34
34
  mkdirSync(this.dir, {
35
35
  recursive: true,
36
36
  });
37
- chmodSync(this.dir, 0o777);
38
37
  } catch (err) {
39
38
  throw new Error(ERR_CANT_CREATE_DIR_IN_HOME('.bdy'));
40
39
  }
40
+ try {
41
+ chmodSync(this.dir, 0o777);
42
+ } catch {
43
+ // do nothing
44
+ }
41
45
  }
42
46
 
43
47
  createEmptyFile() {
44
48
  try {
45
49
  writeFileSync(this.file, '{}', 'utf-8');
46
- chmodSync(this.file, 0o666);
47
50
  } catch (err) {
48
51
  throw new Error(ERR_CANT_CREATE_DIR_IN_HOME('.bdy/cfg.json'));
49
52
  }
53
+ try {
54
+ chmodSync(this.file, 0o666);
55
+ } catch {
56
+ // do nothing
57
+ }
50
58
  }
51
59
 
52
60
  ensureDir() {
@@ -217,10 +225,14 @@ class Cfg {
217
225
  this.ensureDir();
218
226
  try {
219
227
  writeFileSync(this.file, JSON.stringify(this.json, null, 4), 'utf-8');
220
- chmodSync(this.file, 0o666);
221
228
  } catch (err) {
222
229
  throw new Error(ERR_CANT_CREATE_DIR_IN_HOME('.bdy/cfg.json'));
223
230
  }
231
+ try {
232
+ chmodSync(this.file, 0o666);
233
+ } catch {
234
+ // do nothing
235
+ }
224
236
  }
225
237
  }
226
238
 
@@ -1,4 +1,3 @@
1
- const { Command } = require('commander');
2
1
  const Output = require('../../output.js');
3
2
  const Input = require('../../input.js');
4
3
  const {
@@ -25,6 +24,7 @@ const {
25
24
  TXT_AGENT_ENABLED,
26
25
  OPTION_AGENT_DEBUG
27
26
  } = require('../../texts');
27
+ const { newCommand } = require('../../utils');
28
28
 
29
29
  const removeAllAndExit = async (txt, id, host, token) => {
30
30
  try {
@@ -45,8 +45,7 @@ const removeAllAndExit = async (txt, id, host, token) => {
45
45
  Output.exitError(txt);
46
46
  };
47
47
 
48
- const commandAgentInstall = new Command('install');
49
- commandAgentInstall.description(DESC_COMMAND_AGENT_INSTALL);
48
+ const commandAgentInstall = newCommand('install', DESC_COMMAND_AGENT_INSTALL);
50
49
  commandAgentInstall.option('-s, --start', OPTION_AGENT_START);
51
50
  commandAgentInstall.option('-i, --id <id>', OPTION_AGENT_ID);
52
51
  commandAgentInstall.option('-t, --token <token>', OPTION_AGENT_TOKEN);
@@ -1,4 +1,3 @@
1
- const { Command } = require('commander');
2
1
  const Output = require('../../output.js');
3
2
  const {
4
3
  DESC_COMMAND_AGENT_RESTART,
@@ -7,9 +6,9 @@ const {
7
6
  const ApiAgent = require('../../api/agent.js');
8
7
  const AgentManager = require('../../agent/manager');
9
8
  const { ERR_AGENT_NOT_ENABLED } = require('../../texts');
9
+ const { newCommand } = require('../../utils');
10
10
 
11
- const commandAgentRestart = new Command('restart');
12
- commandAgentRestart.description(DESC_COMMAND_AGENT_RESTART);
11
+ const commandAgentRestart = newCommand('restart', DESC_COMMAND_AGENT_RESTART);
13
12
  commandAgentRestart.action(async () => {
14
13
  const isEnabled = await AgentManager.system.isEnabled();
15
14
  if (!isEnabled) {
@@ -1,8 +1,8 @@
1
- const { Command } = require('commander');
2
1
  const AgentManager = require('../../agent/manager');
3
2
  const logger = require('../../logger');
3
+ const { newCommand } = require('../../utils');
4
4
 
5
- const commandAgentRun = new Command('run');
5
+ const commandAgentRun = newCommand('run');
6
6
  commandAgentRun.option('--id <id>');
7
7
  commandAgentRun.option('--host <host>');
8
8
  commandAgentRun.option('--token <token>');
@@ -1,4 +1,3 @@
1
- const { Command } = require('commander');
2
1
  const Output = require('../../output.js');
3
2
  const {
4
3
  DESC_COMMAND_AGENT_START,
@@ -7,9 +6,9 @@ const {
7
6
  const ApiAgent = require('../../api/agent.js');
8
7
  const AgentManager = require('../../agent/manager');
9
8
  const { ERR_AGENT_NOT_ENABLED } = require('../../texts');
9
+ const { newCommand } = require('../../utils');
10
10
 
11
- const commandAgentStart = new Command('start');
12
- commandAgentStart.description(DESC_COMMAND_AGENT_START);
11
+ const commandAgentStart = newCommand('start', DESC_COMMAND_AGENT_START);
13
12
  commandAgentStart.action(async () => {
14
13
  const isEnabled = await AgentManager.system.isEnabled();
15
14
  if (!isEnabled) {
@@ -1,4 +1,3 @@
1
- const { Command } = require('commander');
2
1
  const Output = require('../../output.js');
3
2
  const {
4
3
  DESC_COMMAND_AGENT_STATUS,
@@ -16,9 +15,9 @@ const {
16
15
  } = require('../../utils.js');
17
16
  const AgentManager = require('../../agent/manager');
18
17
  const { ERR_AGENT_NOT_RUNNING } = require('../../texts');
18
+ const { newCommand } = require('../../utils');
19
19
 
20
- const commandAgentStatus = new Command('status');
21
- commandAgentStatus.description(DESC_COMMAND_AGENT_STATUS);
20
+ const commandAgentStatus = newCommand('status', DESC_COMMAND_AGENT_STATUS);
22
21
  commandAgentStatus.action(async () => {
23
22
  const isEnabled = await AgentManager.system.isEnabled();
24
23
  if (!isEnabled) {
@@ -1,4 +1,3 @@
1
- const { Command } = require('commander');
2
1
  const Output = require('../../output.js');
3
2
  const {
4
3
  DESC_COMMAND_AGENT_STOP,
@@ -7,9 +6,9 @@ const {
7
6
  const ApiAgent = require('../../api/agent.js');
8
7
  const AgentManager = require('../../agent/manager');
9
8
  const { ERR_AGENT_NOT_ENABLED } = require('../../texts');
9
+ const { newCommand } = require('../../utils');
10
10
 
11
- const commandAgentStop = new Command('stop');
12
- commandAgentStop.description(DESC_COMMAND_AGENT_STOP);
11
+ const commandAgentStop = newCommand('stop', DESC_COMMAND_AGENT_STOP);
13
12
  commandAgentStop.action(async () => {
14
13
  const isEnabled = await AgentManager.system.isEnabled();
15
14
  if (!isEnabled) {
@@ -1,4 +1,3 @@
1
- const { Command } = require('commander');
2
1
  const Output = require('../../../output.js');
3
2
  const {
4
3
  DESC_COMMAND_AGENT_TUNNEL_LIST,
@@ -6,9 +5,9 @@ const {
6
5
  } = require('../../../texts.js');
7
6
  const ApiAgent = require('../../../api/agent');
8
7
  const AgentManager = require('../../../agent/manager');
8
+ const { newCommand } = require('../../../utils');
9
9
 
10
- const commandAgentTunnelList = new Command('list');
11
- commandAgentTunnelList.description(DESC_COMMAND_AGENT_TUNNEL_LIST);
10
+ const commandAgentTunnelList = newCommand('list', DESC_COMMAND_AGENT_TUNNEL_LIST);
12
11
  commandAgentTunnelList.action(async () => {
13
12
  const isEnabled = await AgentManager.system.isEnabled();
14
13
  if (!isEnabled) {
@@ -1,4 +1,3 @@
1
- const { Command } = require('commander');
2
1
  const Cfg = require('../../../cfg.js');
3
2
  const Output = require('../../../output.js');
4
3
  const {
@@ -11,9 +10,9 @@ const {
11
10
  } = require('../../../texts.js');
12
11
  const ApiAgent = require('../../../api/agent.js');
13
12
  const AgentManager = require('../../../agent/manager');
13
+ const { newCommand } = require('../../../utils');
14
14
 
15
- const commandAgentTunnelStart = new Command('start');
16
- commandAgentTunnelStart.description(DESC_COMMAND_AGENT_TUNNEL_START);
15
+ const commandAgentTunnelStart = newCommand('start', DESC_COMMAND_AGENT_TUNNEL_START);
17
16
  commandAgentTunnelStart.argument('<name>', OPTION_NAME);
18
17
  commandAgentTunnelStart.action(async (name) => {
19
18
  if (!Cfg.hasTunnel(name)) {
@@ -1,4 +1,3 @@
1
- const { Command } = require('commander');
2
1
  const {
3
2
  DESC_COMMAND_AGENT_TUNNEL_STATUS,
4
3
  ERR_AGENT_NOT_ENABLED,
@@ -8,9 +7,9 @@ const Output = require('../../../output.js');
8
7
  const ApiAgent = require('../../../api/agent.js');
9
8
  const AgentSocketTunnel = require('../../../agent/socket/tunnel.js');
10
9
  const AgentManager = require('../../../agent/manager');
10
+ const { newCommand } = require('../../../utils');
11
11
 
12
- const commandAgentTunnelStatus = new Command('status');
13
- commandAgentTunnelStatus.description(DESC_COMMAND_AGENT_TUNNEL_STATUS);
12
+ const commandAgentTunnelStatus = newCommand('status', DESC_COMMAND_AGENT_TUNNEL_STATUS);
14
13
  commandAgentTunnelStatus.argument('<id>', OPTION_ID);
15
14
  commandAgentTunnelStatus.action(async (id) => {
16
15
  const isEnabled = await AgentManager.system.isEnabled();
@@ -1,4 +1,3 @@
1
- const { Command } = require('commander');
2
1
  const Output = require('../../../output.js');
3
2
  const {
4
3
  DESC_COMMAND_AGENT_TUNNEL_STOP,
@@ -8,9 +7,9 @@ const {
8
7
  } = require('../../../texts.js');
9
8
  const ApiAgent = require('../../../api/agent.js');
10
9
  const AgentManager = require('../../../agent/manager');
10
+ const { newCommand } = require('../../../utils');
11
11
 
12
- const commandAgentTunnelStop = new Command('stop');
13
- commandAgentTunnelStop.description(DESC_COMMAND_AGENT_TUNNEL_STOP);
12
+ const commandAgentTunnelStop = newCommand('stop', DESC_COMMAND_AGENT_TUNNEL_STOP);
14
13
  commandAgentTunnelStop.argument('<id>', OPTION_ID);
15
14
  commandAgentTunnelStop.action(async (id) => {
16
15
  const isEnabled = await AgentManager.system.isEnabled();
@@ -1,4 +1,3 @@
1
- const { Command } = require('commander');
2
1
  const commandAgentTunnelTcp = require('./tunnel/tcp.js');
3
2
  const commandAgentTunnelList = require('./tunnel/list.js');
4
3
  const commandAgentTunnelStop = require('./tunnel/stop.js');
@@ -7,9 +6,9 @@ const commandAgentTunnelHttp = require('./tunnel/http.js');
7
6
  const commandAgentTunnelStart = require('./tunnel/start.js');
8
7
  const { DESC_COMMAND_AGENT_TUNNEL } = require('../../texts.js');
9
8
  const commandAgentTunnelStatus = require('./tunnel/status.js');
9
+ const { newCommand } = require('../../utils');
10
10
 
11
- const commandAgentTunnel = new Command('tunnel');
12
- commandAgentTunnel.description(DESC_COMMAND_AGENT_TUNNEL);
11
+ const commandAgentTunnel = newCommand('tunnel', DESC_COMMAND_AGENT_TUNNEL);
13
12
  commandAgentTunnel.addCommand(commandAgentTunnelTcp);
14
13
  commandAgentTunnel.addCommand(commandAgentTunnelTls);
15
14
  commandAgentTunnel.addCommand(commandAgentTunnelHttp);
@@ -1,4 +1,3 @@
1
- const { Command } = require('commander');
2
1
  const Output = require('../../output.js');
3
2
  const {
4
3
  DESC_COMMAND_AGENT_UNINSTALL,
@@ -7,9 +6,9 @@ const {
7
6
  const AgentManager = require('../../agent/manager.js');
8
7
  const ApiBuddy = require('../../api/buddy.js');
9
8
  const { ERR_AGENT_ADMIN_RIGHTS } = require('../../texts');
9
+ const { newCommand } = require('../../utils');
10
10
 
11
- const commandAgentUninstall = new Command('disable');
12
- commandAgentUninstall.description(DESC_COMMAND_AGENT_UNINSTALL);
11
+ const commandAgentUninstall = newCommand('disable', DESC_COMMAND_AGENT_UNINSTALL);
13
12
  commandAgentUninstall.action(async () => {
14
13
  const hasAdminRights = await AgentManager.system.hasAdminRights();
15
14
  if (!hasAdminRights) {
@@ -1,4 +1,3 @@
1
- const { Command } = require('commander');
2
1
  const {
3
2
  DESC_COMMAND_AGENT_UPDATE,
4
3
  ERR_AGENT_NOT_ENABLED,
@@ -12,9 +11,9 @@ const { ERR_AGENT_ADMIN_RIGHTS,
12
11
  TXT_UPDATING_AGENT
13
12
  } = require('../../texts');
14
13
  const ApiAgent = require('../../api/agent');
14
+ const { newCommand } = require('../../utils');
15
15
 
16
- const commandAgentUpdate = new Command('update');
17
- commandAgentUpdate.description(DESC_COMMAND_AGENT_UPDATE);
16
+ const commandAgentUpdate = newCommand('update', DESC_COMMAND_AGENT_UPDATE);
18
17
  commandAgentUpdate.hideVersionUpdate = true;
19
18
  commandAgentUpdate.action(async () => {
20
19
  const hasAdminRights = await AgentManager.system.hasAdminRights();
@@ -1,4 +1,3 @@
1
- const { Command } = require('commander');
2
1
  const Output = require('../../output.js');
3
2
  const {
4
3
  DESC_COMMAND_AGENT_VERSION,
@@ -6,9 +5,9 @@ const {
6
5
  ERR_SWW
7
6
  } = require('../../texts.js');
8
7
  const AgentManager = require('../../agent/manager');
8
+ const { newCommand } = require('../../utils');
9
9
 
10
- const commandAgentVersion = new Command('version');
11
- commandAgentVersion.description(DESC_COMMAND_AGENT_VERSION);
10
+ const commandAgentVersion = newCommand('version', DESC_COMMAND_AGENT_VERSION);
12
11
  commandAgentVersion.action(async () => {
13
12
  const isEnabled = await AgentManager.system.isEnabled();
14
13
  if (!isEnabled) {
@@ -1,4 +1,3 @@
1
- const { Command } = require('commander');
2
1
  const commandAgentStart = require('./agent/start.js');
3
2
  const commandAgentStatus = require('./agent/status.js');
4
3
  const commandAgentRestart = require('./agent/restart.js');
@@ -10,9 +9,9 @@ const commandAgentUninstall = require('./agent/uninstall.js');
10
9
  const commandAgentVersion = require('./agent/version.js');
11
10
  const { DESC_COMMAND_AGENT } = require('../texts.js');
12
11
  const commandAgentRun = require('./agent/run');
12
+ const { newCommand } = require('../utils');
13
13
 
14
- const commandAgent = new Command('agent');
15
- commandAgent.description(DESC_COMMAND_AGENT);
14
+ const commandAgent = newCommand('agent', DESC_COMMAND_AGENT);
16
15
  commandAgent.addCommand(commandAgentInstall);
17
16
  commandAgent.addCommand(commandAgentStart);
18
17
  commandAgent.addCommand(commandAgentStop);
@@ -1,11 +1,10 @@
1
- const { Command } = require('commander');
2
1
  const commandConfigAddTcp = require('./add/tcp.js');
3
2
  const commandConfigAddHttp = require('./add/http.js');
4
3
  const commandConfigAddTls = require('./add/tls.js');
5
4
  const { DESC_COMMAND_CONFIG_ADD } = require('../../texts.js');
5
+ const { newCommand } = require('../../utils');
6
6
 
7
- const commandConfigAdd = new Command('add');
8
- commandConfigAdd.description(DESC_COMMAND_CONFIG_ADD);
7
+ const commandConfigAdd = newCommand('add', DESC_COMMAND_CONFIG_ADD);
9
8
  commandConfigAdd.addCommand(commandConfigAddTcp);
10
9
  commandConfigAdd.addCommand(commandConfigAddTls);
11
10
  commandConfigAdd.addCommand(commandConfigAddHttp);
@@ -1,11 +1,10 @@
1
- const { Command } = require('commander');
2
1
  const Cfg = require('../../../cfg.js');
3
2
  const Output = require('../../../output.js');
4
3
  const { DESC_COMMAND_CONFIG_GET_REGION } = require('../../../texts.js');
5
4
  const Format = require('../../../format');
5
+ const { newCommand } = require('../../../utils');
6
6
 
7
- const commandConfigGetRegion = new Command('region');
8
- commandConfigGetRegion.description(DESC_COMMAND_CONFIG_GET_REGION);
7
+ const commandConfigGetRegion = newCommand('region', DESC_COMMAND_CONFIG_GET_REGION);
9
8
  commandConfigGetRegion.action(() => {
10
9
  Output.exitNormal(Format.region(Cfg.getRegion()));
11
10
  });
@@ -1,11 +1,10 @@
1
- const { Command } = require('commander');
2
1
  const Output = require('../../../output.js');
3
2
  const Cfg = require('../../../cfg.js');
4
3
  const { DESC_COMMAND_CONFIG_GET_TIMEOUT } = require('../../../texts.js');
5
4
  const Format = require('../../../format');
5
+ const { newCommand } = require('../../../utils');
6
6
 
7
- const commandConfigGetTimeout = new Command('timeout');
8
- commandConfigGetTimeout.description(DESC_COMMAND_CONFIG_GET_TIMEOUT);
7
+ const commandConfigGetTimeout = newCommand('timeout', DESC_COMMAND_CONFIG_GET_TIMEOUT);
9
8
  commandConfigGetTimeout.action(() => {
10
9
  Output.exitNormal(Format.timeout(Cfg.getTimeout()));
11
10
  });
@@ -1,11 +1,10 @@
1
- const { Command } = require('commander');
2
1
  const Cfg = require('../../../cfg.js');
3
2
  const Output = require('../../../output.js');
4
3
  const { DESC_COMMAND_CONFIG_GET_TOKEN } = require('../../../texts.js');
5
4
  const Format = require('../../../format');
5
+ const { newCommand } = require('../../../utils');
6
6
 
7
- const commandConfigGetToken = new Command('token');
8
- commandConfigGetToken.description(DESC_COMMAND_CONFIG_GET_TOKEN);
7
+ const commandConfigGetToken = newCommand('token', DESC_COMMAND_CONFIG_GET_TOKEN);
9
8
  commandConfigGetToken.action(() => {
10
9
  Output.exitNormal(Format.token(Cfg.getToken()));
11
10
  });
@@ -1,4 +1,3 @@
1
- const { Command } = require('commander');
2
1
  const Cfg = require('../../../cfg.js');
3
2
  const Output = require('../../../output.js');
4
3
  const {
@@ -6,9 +5,9 @@ const {
6
5
  ERR_TUNNEL_NOT_FOUND,
7
6
  OPTION_NAME
8
7
  } = require('../../../texts.js');
8
+ const { newCommand } = require('../../../utils');
9
9
 
10
- const commandConfigGetTunnel = new Command('tunnel');
11
- commandConfigGetTunnel.description(DESC_COMMAND_CONFIG_GET_TUNNEL);
10
+ const commandConfigGetTunnel = newCommand('tunnel', DESC_COMMAND_CONFIG_GET_TUNNEL);
12
11
  commandConfigGetTunnel.argument('<name>', OPTION_NAME);
13
12
  commandConfigGetTunnel.action((name) => {
14
13
  if (!Cfg.hasTunnel(name)) {
@@ -1,10 +1,9 @@
1
- const { Command } = require('commander');
2
1
  const Cfg = require('../../../cfg.js');
3
2
  const Output = require('../../../output.js');
4
3
  const { DESC_COMMAND_CONFIG_GET_TUNNELS } = require('../../../texts.js');
4
+ const { newCommand } = require('../../../utils');
5
5
 
6
- const commandConfigGetTunnels = new Command('tunnels');
7
- commandConfigGetTunnels.description(DESC_COMMAND_CONFIG_GET_TUNNELS);
6
+ const commandConfigGetTunnels = newCommand('tunnels', DESC_COMMAND_CONFIG_GET_TUNNELS);
8
7
  commandConfigGetTunnels.action(() => {
9
8
  Output.configTunnels(Cfg.getTunnels());
10
9
  Output.exitNormal();
@@ -1,11 +1,10 @@
1
- const { Command } = require('commander');
2
1
  const Output = require('../../../output.js');
3
2
  const Cfg = require('../../../cfg.js');
4
3
  const { DESC_COMMAND_CONFIG_GET_WHITELIST } = require('../../../texts.js');
5
4
  const Format = require('../../../format');
5
+ const { newCommand } = require('../../../utils');
6
6
 
7
- const commandConfigGetWhitelist = new Command('whitelist');
8
- commandConfigGetWhitelist.description(DESC_COMMAND_CONFIG_GET_WHITELIST);
7
+ const commandConfigGetWhitelist = newCommand('whitelist', DESC_COMMAND_CONFIG_GET_WHITELIST);
9
8
  commandConfigGetWhitelist.action(() => {
10
9
  Output.exitNormal(Format.whitelist(Cfg.getWhitelist()));
11
10
  });
@@ -1,4 +1,3 @@
1
- const { Command } = require('commander');
2
1
  const commandConfigGetRegion = require('./get/region.js');
3
2
  const commandConfigGetToken = require('./get/token.js');
4
3
  const commandConfigGetTunnel = require('./get/tunnel.js');
@@ -6,9 +5,9 @@ const commandConfigGetWhitelist = require('./get/whitelist.js');
6
5
  const commandConfigGetTimeout = require('./get/timeout.js');
7
6
  const commandConfigGetTunnels = require('./get/tunnels.js');
8
7
  const { DESC_COMMAND_CONFIG_GET } = require('../../texts.js');
8
+ const { newCommand } = require('../../utils');
9
9
 
10
- const commandConfigGet = new Command('get');
11
- commandConfigGet.description(DESC_COMMAND_CONFIG_GET);
10
+ const commandConfigGet = newCommand('get', DESC_COMMAND_CONFIG_GET);
12
11
  commandConfigGet.addCommand(commandConfigGetRegion);
13
12
  commandConfigGet.addCommand(commandConfigGetToken);
14
13
  commandConfigGet.addCommand(commandConfigGetWhitelist);
@@ -1,4 +1,3 @@
1
- const { Command } = require('commander');
2
1
  const Cfg = require('../../../cfg.js');
3
2
  const Output = require('../../../output.js');
4
3
  const {
@@ -7,9 +6,9 @@ const {
7
6
  OPTION_NAME,
8
7
  TXT_TUNNEL_REMOVED
9
8
  } = require('../../../texts.js');
9
+ const { newCommand } = require('../../../utils');
10
10
 
11
- const commandConfigRemoveTunnel = new Command('tunnel');
12
- commandConfigRemoveTunnel.description(DESC_COMMAND_CONFIG_REMOVE_TUNNEL);
11
+ const commandConfigRemoveTunnel = newCommand('tunnel', DESC_COMMAND_CONFIG_REMOVE_TUNNEL);
13
12
  commandConfigRemoveTunnel.argument('<name>', OPTION_NAME);
14
13
  commandConfigRemoveTunnel.action((name) => {
15
14
  if (!Cfg.hasTunnel(name)) {
@@ -1,9 +1,8 @@
1
- const { Command } = require('commander');
2
1
  const commandConfigRemoveTunnel = require('./remove/tunnel.js');
3
2
  const { DESC_COMMAND_CONFIG_REMOVE } = require('../../texts.js');
3
+ const { newCommand } = require('../../utils');
4
4
 
5
- const commandConfigRemove = new Command('rm');
6
- commandConfigRemove.description(DESC_COMMAND_CONFIG_REMOVE);
5
+ const commandConfigRemove = newCommand('rm', DESC_COMMAND_CONFIG_REMOVE);
7
6
  commandConfigRemove.addCommand(commandConfigRemoveTunnel);
8
7
 
9
8
  module.exports = commandConfigRemove;
@@ -1,4 +1,3 @@
1
- const { Command } = require('commander');
2
1
  const Cfg = require('../../../cfg.js');
3
2
  const Output = require('../../../output.js');
4
3
  const Input = require('../../../input.js');
@@ -7,9 +6,9 @@ const {
7
6
  OPTION_DEFAULT_REGION,
8
7
  TXT_REGION_SAVED
9
8
  } = require('../../../texts.js');
9
+ const { newCommand } = require('../../../utils');
10
10
 
11
- const commandConfigSetRegion = new Command('region');
12
- commandConfigSetRegion.description(DESC_COMMAND_CONFIG_SET_REGION);
11
+ const commandConfigSetRegion = newCommand('region', DESC_COMMAND_CONFIG_SET_REGION);
13
12
  commandConfigSetRegion.argument('<region>', OPTION_DEFAULT_REGION);
14
13
  commandConfigSetRegion.action((region) => {
15
14
  Cfg.setRegion(Input.region(region));
@@ -1,4 +1,3 @@
1
- const { Command } = require('commander');
2
1
  const Output = require('../../../output.js');
3
2
  const Cfg = require('../../../cfg.js');
4
3
  const Input = require('../../../input.js');
@@ -10,9 +9,9 @@ const {
10
9
  OPTION_TIMEOUT,
11
10
  TXT_TIMEOUT_SAVED
12
11
  } = require('../../../texts.js');
12
+ const { newCommand } = require('../../../utils');
13
13
 
14
- const commandConfigSetTimeout = new Command('timeout');
15
- commandConfigSetTimeout.description(DESC_COMMAND_CONFIG_SET_TIMEOUT);
14
+ const commandConfigSetTimeout = newCommand('timeout', DESC_COMMAND_CONFIG_SET_TIMEOUT);
16
15
  commandConfigSetTimeout.argument('[timeout]', OPTION_TIMEOUT, DEFAULT_TIMEOUT);
17
16
  commandConfigSetTimeout.action((timeout) => {
18
17
  Cfg.setTimeout(Input.timeout(timeout));
@@ -1,4 +1,3 @@
1
- const { Command } = require('commander');
2
1
  const Cfg = require('../../../cfg.js');
3
2
  const Output = require('../../../output.js');
4
3
  const {
@@ -6,9 +5,9 @@ const {
6
5
  OPTION_TOKEN,
7
6
  TXT_TOKEN_SAVED
8
7
  } = require('../../../texts.js');
8
+ const { newCommand } = require('../../../utils');
9
9
 
10
- const commandConfigSetToken = new Command('token');
11
- commandConfigSetToken.description(DESC_COMMAND_CONFIG_SET_TOKEN);
10
+ const commandConfigSetToken = newCommand('token', DESC_COMMAND_CONFIG_SET_TOKEN);
12
11
  commandConfigSetToken.argument('[token]', OPTION_TOKEN, '');
13
12
  commandConfigSetToken.action((token) => {
14
13
  Cfg.setToken(token);
@@ -1,4 +1,3 @@
1
- const { Command } = require('commander');
2
1
  const Cfg = require('../../../cfg.js');
3
2
  const Output = require('../../../output.js');
4
3
  const Input = require('../../../input.js');
@@ -7,9 +6,9 @@ const {
7
6
  OPTION_WHITELIST,
8
7
  TXT_WHITELIST_SAVED
9
8
  } = require('../../../texts.js');
9
+ const { newCommand } = require('../../../utils');
10
10
 
11
- const commandConfigSetWhitelist = new Command('whitelist');
12
- commandConfigSetWhitelist.description(DESC_COMMAND_CONFIG_SET_WHITELIST);
11
+ const commandConfigSetWhitelist = newCommand('whitelist', DESC_COMMAND_CONFIG_SET_WHITELIST);
13
12
  commandConfigSetWhitelist.argument('[cidrs...]', OPTION_WHITELIST, []);
14
13
  commandConfigSetWhitelist.action((whitelist) => {
15
14
  Cfg.setWhitelist(Input.whitelist(whitelist));
@@ -1,12 +1,11 @@
1
- const { Command } = require('commander');
2
1
  const commandConfigSetToken = require('./set/token.js');
3
2
  const commandConfigSetRegion = require('./set/region.js');
4
3
  const commandConfigSetWhitelist = require('./set/whitelist.js');
5
4
  const commandConfigSetTimeout = require('./set/timeout.js');
6
5
  const { DESC_COMMAND_CONFIG_SET } = require('../../texts.js');
6
+ const { newCommand } = require('../../utils');
7
7
 
8
- const commandConfigSet = new Command('set');
9
- commandConfigSet.description(DESC_COMMAND_CONFIG_SET);
8
+ const commandConfigSet = newCommand('set', DESC_COMMAND_CONFIG_SET);
10
9
  commandConfigSet.addCommand(commandConfigSetToken);
11
10
  commandConfigSet.addCommand(commandConfigSetRegion);
12
11
  commandConfigSet.addCommand(commandConfigSetWhitelist);
@@ -1,12 +1,11 @@
1
- const { Command } = require('commander');
2
1
  const commandConfigSet = require('./config/set.js');
3
2
  const commandConfigGet = require('./config/get.js');
4
3
  const commandConfigAdd = require('./config/add.js');
5
4
  const commandConfigRemove = require('./config/remove.js');
6
5
  const { DESC_COMMAND_CONFIG } = require('../texts.js');
6
+ const { newCommand } = require('../utils');
7
7
 
8
- const commandConfig = new Command('config');
9
- commandConfig.description(DESC_COMMAND_CONFIG);
8
+ const commandConfig = newCommand('config', DESC_COMMAND_CONFIG);
10
9
  commandConfig.addCommand(commandConfigSet);
11
10
  commandConfig.addCommand(commandConfigAdd);
12
11
  commandConfig.addCommand(commandConfigGet);
@@ -1,4 +1,3 @@
1
- const { Command } = require('commander');
2
1
  const Cfg = require('../cfg.js');
3
2
  const Output = require('../output.js');
4
3
  const {
@@ -8,9 +7,9 @@ const {
8
7
  TXT_OPENING_TUNNEL
9
8
  } = require('../texts.js');
10
9
  const ApiBuddy = require('../api/buddy.js');
10
+ const { newCommand } = require('../utils');
11
11
 
12
- const commandStart = new Command('start');
13
- commandStart.description(DESC_COMMAND_START);
12
+ const commandStart = newCommand('start', DESC_COMMAND_START);
14
13
  commandStart.argument('<name>', OPTION_NAME);
15
14
  commandStart.action(async (name) => {
16
15
  if (!Cfg.hasTunnel(name)) {
@@ -1,8 +1,8 @@
1
- const { Command } = require('commander');
2
1
  const { getVersion } = require('../utils.js');
3
2
  const Output = require('../output.js');
3
+ const { newCommand } = require('../utils');
4
4
 
5
- const commandVersion = new Command('version');
5
+ const commandVersion = newCommand('version');
6
6
  commandVersion.action(() => {
7
7
  Output.exitNormal(getVersion());
8
8
  });
package/src/index.js CHANGED
@@ -1,4 +1,3 @@
1
- const { Command } = require('commander');
2
1
  const commandConfig = require('./command/config.js');
3
2
  const Output = require('./output.js');
4
3
  const commandTcp = require('./command/tcp.js');
@@ -10,7 +9,10 @@ const logger = require('./logger.js');
10
9
  const commandVersion = require('./command/version.js');
11
10
  const commandPre = require('./command/pre.js');
12
11
  const stream = require('stream');
13
- const { isDocker } = require('./utils');
12
+ const { isDocker,
13
+ formatHelp,
14
+ newCommand
15
+ } = require('./utils');
14
16
  const { DESC_PROGRAM } = require('./texts');
15
17
 
16
18
  stream.setDefaultHighWaterMark(false, 67108864);
@@ -20,9 +22,11 @@ process.on('uncaughtException', (err) => {
20
22
  Output.exitError(err);
21
23
  });
22
24
 
23
- const program = new Command();
24
- program.description(DESC_PROGRAM);
25
+ const program = newCommand(null, DESC_PROGRAM);
25
26
  program.hook('preAction', commandPre);
27
+ program.configureHelp({
28
+ formatHelp: formatHelp
29
+ });
26
30
  program.addCommand(commandConfig);
27
31
  program.addCommand(commandTcp);
28
32
  program.addCommand(commandTls);
package/src/logger.js CHANGED
@@ -31,7 +31,11 @@ class Logger {
31
31
  this.logPath = resolve(this.rootPath, 'cli.log');
32
32
  this.log1Path = resolve(this.rootPath, 'cli.1.log');
33
33
  this.logStream = fs.openSync(this.logPath, 'w');
34
- fs.chmodSync(this.logPath, 0o666);
34
+ try {
35
+ fs.chmodSync(this.logPath, 0o666);
36
+ } catch {
37
+ // do nothing
38
+ }
35
39
  this.p = pino({
36
40
  level: process.env.DEBUG === '1' ? 'debug' : 'info',
37
41
  timestamp: () => `, "time": "${new Date().toISOString()}"`,
package/src/utils.js CHANGED
@@ -41,7 +41,7 @@ const {
41
41
  ERR_SUBDOMAIN_TAKEN,
42
42
  ERR_TUNNELS_DISABLED,
43
43
  ERR_AGENT_LIMIT_REACHED,
44
- ERR_TUNNEL_LIMIT_REACHED
44
+ ERR_TUNNEL_LIMIT_REACHED,
45
45
  } = require('./texts');
46
46
 
47
47
  const REGION_US = 'US';
@@ -243,13 +243,87 @@ const getPlatform = () => {
243
243
  return os.platform();
244
244
  };
245
245
 
246
+ const formatHelp = (cmd, helper) => {
247
+ const termWidth = helper.padWidth(cmd, helper);
248
+ const helpWidth = helper.helpWidth || 80;
249
+ const itemIndentWidth = 2;
250
+ const itemSeparatorWidth = 2; // between term and description
251
+ function formatItem(term, description) {
252
+ if (description) {
253
+ const fullText = `${term.padEnd(termWidth + itemSeparatorWidth)}${description}`;
254
+ return helper.wrap(fullText, helpWidth - itemIndentWidth, termWidth + itemSeparatorWidth);
255
+ }
256
+ return term;
257
+ }
258
+ function formatList(textArray) {
259
+ return textArray.join('\n').replace(/^/gm, ' '.repeat(itemIndentWidth));
260
+ }
261
+
262
+ // Usage
263
+ let output = ['USAGE:', ` ${helper.commandUsage(cmd)}`, ''];
264
+
265
+ // Description
266
+ const commandDescription = helper.commandDescription(cmd);
267
+ if (commandDescription.length > 0) {
268
+ output = output.concat(['DESCRIPTION:', ` ${helper.wrap(commandDescription, helpWidth, itemIndentWidth)}`, '']);
269
+ }
270
+
271
+ // Arguments
272
+ const argumentList = helper.visibleArguments(cmd).map((argument) => {
273
+ return formatItem(helper.argumentTerm(argument), helper.argumentDescription(argument));
274
+ });
275
+ if (argumentList.length > 0) {
276
+ output = output.concat(['ARGUMENTS:', formatList(argumentList), '']);
277
+ }
278
+
279
+ // Options
280
+ const optionList = helper.visibleOptions(cmd).map((option) => {
281
+ return formatItem(helper.optionTerm(option), helper.optionDescription(option));
282
+ });
283
+ if (optionList.length > 0) {
284
+ output = output.concat(['OPTIONS:', formatList(optionList), '']);
285
+ }
286
+
287
+ if (this.showGlobalOptions) {
288
+ const globalOptionList = helper.visibleGlobalOptions(cmd).map((option) => {
289
+ return formatItem(helper.optionTerm(option), helper.optionDescription(option));
290
+ });
291
+ if (globalOptionList.length > 0) {
292
+ output = output.concat(['GLOBAL OPTIONS:', formatList(globalOptionList), '']);
293
+ }
294
+ }
295
+
296
+ // Commands
297
+ const commandList = helper.visibleCommands(cmd).map((cmd) => {
298
+ return formatItem(helper.subcommandTerm(cmd), helper.subcommandDescription(cmd));
299
+ });
300
+ if (commandList.length > 0) {
301
+ output = output.concat(['COMMANDS:', formatList(commandList), '']);
302
+ }
303
+
304
+ return output.join('\n');
305
+ };
306
+
307
+ const newCommand = (name, desc) => {
308
+ const cmd = new Command(name);
309
+ if (desc) cmd.description(desc);
310
+ cmd.configureHelp({
311
+ formatHelp: formatHelp
312
+ });
313
+ return cmd;
314
+ };
315
+
246
316
  const getHomeDirectory = () => {
247
317
  if (isDocker()) return '/buddy';
248
318
  const r = homedir();
249
319
  const p = path.join(r, '.bdy');
250
320
  if (!existsSync(p)) {
251
321
  mkdirSync(p, { recursive: true });
252
- chmodSync(p, 0o777);
322
+ try {
323
+ chmodSync(p, 0o777);
324
+ } catch {
325
+ // do nothing
326
+ }
253
327
  }
254
328
  return p;
255
329
  };
@@ -340,7 +414,7 @@ const createSshHostKey = () => {
340
414
  };
341
415
 
342
416
  const getBasicCommandTcp = () => {
343
- const commandTcp = new Command('tcp');
417
+ const commandTcp = newCommand('tcp');
344
418
  commandTcp.alias('TCP');
345
419
  commandTcp.option('-r, --region <region>', OPTION_REGION);
346
420
  commandTcp.option('-w, --whitelist <cidrs...>', OPTION_WHITELIST);
@@ -351,7 +425,7 @@ const getBasicCommandTcp = () => {
351
425
  };
352
426
 
353
427
  const getBasicCommandHttp = () => {
354
- const commandHttp = new Command('http');
428
+ const commandHttp = newCommand('http');
355
429
  commandHttp.alias('HTTP');
356
430
  commandHttp.option('-r, --region <region>', OPTION_REGION);
357
431
  commandHttp.option('-w, --whitelist <cidrs...>', OPTION_WHITELIST);
@@ -373,7 +447,7 @@ const getBasicCommandHttp = () => {
373
447
  };
374
448
 
375
449
  const getBasicCommandTls = () => {
376
- const commandTls = new Command('tls');
450
+ const commandTls = newCommand('tls');
377
451
  commandTls.alias('TLS');
378
452
  commandTls.option('-r, --region <region>', OPTION_REGION);
379
453
  commandTls.option('-w, --whitelist <cidrs...>', OPTION_WHITELIST);
@@ -448,6 +522,8 @@ module.exports = {
448
522
  isLinux,
449
523
  isWindows,
450
524
  createSshHostKey,
525
+ formatHelp,
526
+ newCommand,
451
527
  AGENT_STATUS_INITIALIZING,
452
528
  AGENT_STATUS_FETCH_FAILED,
453
529
  AGENT_STATUS_DISABLED,