@internxt/cli 1.3.2 → 1.4.1

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 (112) hide show
  1. package/.env +13 -0
  2. package/README.md +590 -176
  3. package/dist/commands/add-cert.d.ts +10 -3
  4. package/dist/commands/add-cert.js +24 -38
  5. package/dist/commands/config.d.ts +15 -12
  6. package/dist/commands/config.js +24 -28
  7. package/dist/commands/create-folder.d.ts +16 -8
  8. package/dist/commands/create-folder.js +67 -24
  9. package/dist/commands/delete-permanently-file.d.ts +18 -0
  10. package/dist/commands/delete-permanently-file.js +63 -0
  11. package/dist/commands/delete-permanently-folder.d.ts +18 -0
  12. package/dist/commands/delete-permanently-folder.js +63 -0
  13. package/dist/commands/download-file.d.ts +26 -0
  14. package/dist/commands/{download.js → download-file.js} +98 -56
  15. package/dist/commands/list.d.ts +12 -12
  16. package/dist/commands/list.js +45 -73
  17. package/dist/commands/login.d.ts +11 -9
  18. package/dist/commands/login.js +46 -49
  19. package/dist/commands/logout.d.ts +7 -2
  20. package/dist/commands/logout.js +14 -8
  21. package/dist/commands/logs.d.ts +11 -2
  22. package/dist/commands/logs.js +16 -3
  23. package/dist/commands/move-file.d.ts +9 -7
  24. package/dist/commands/move-file.js +38 -37
  25. package/dist/commands/move-folder.d.ts +9 -7
  26. package/dist/commands/move-folder.js +38 -37
  27. package/dist/commands/rename-file.d.ts +24 -0
  28. package/dist/commands/rename-file.js +80 -0
  29. package/dist/commands/rename-folder.d.ts +24 -0
  30. package/dist/commands/rename-folder.js +80 -0
  31. package/dist/commands/trash-clear.d.ts +9 -5
  32. package/dist/commands/trash-clear.js +21 -19
  33. package/dist/commands/trash-file.d.ts +21 -0
  34. package/dist/commands/trash-file.js +58 -0
  35. package/dist/commands/trash-folder.d.ts +21 -0
  36. package/dist/commands/trash-folder.js +58 -0
  37. package/dist/commands/trash-list.d.ts +10 -11
  38. package/dist/commands/trash-list.js +33 -62
  39. package/dist/commands/trash-restore-file.d.ts +10 -8
  40. package/dist/commands/trash-restore-file.js +33 -35
  41. package/dist/commands/trash-restore-folder.d.ts +10 -8
  42. package/dist/commands/trash-restore-folder.js +33 -35
  43. package/dist/commands/upload-file.d.ts +21 -0
  44. package/dist/commands/upload-file.js +140 -0
  45. package/dist/commands/webdav-config.d.ts +11 -9
  46. package/dist/commands/webdav-config.js +40 -66
  47. package/dist/commands/webdav.d.ts +16 -9
  48. package/dist/commands/webdav.js +77 -61
  49. package/dist/commands/whoami.d.ts +14 -2
  50. package/dist/commands/whoami.js +40 -11
  51. package/dist/hooks/prerun/auth_check.js +6 -4
  52. package/dist/services/auth.service.d.ts +2 -8
  53. package/dist/services/auth.service.js +33 -24
  54. package/dist/services/config.service.d.ts +3 -3
  55. package/dist/services/config.service.js +8 -8
  56. package/dist/services/crypto.service.d.ts +1 -3
  57. package/dist/services/crypto.service.js +11 -36
  58. package/dist/services/database/drive-file/drive-file.repository.d.ts +3 -3
  59. package/dist/services/database/drive-folder/drive-folder.repository.d.ts +3 -3
  60. package/dist/services/drive/drive-file.service.d.ts +1 -8
  61. package/dist/services/drive/drive-file.service.js +9 -23
  62. package/dist/services/drive/drive-folder.service.d.ts +1 -4
  63. package/dist/services/drive/drive-folder.service.js +2 -5
  64. package/dist/services/drive/trash.service.d.ts +2 -0
  65. package/dist/services/drive/trash.service.js +8 -0
  66. package/dist/services/keys.service.js +17 -7
  67. package/dist/services/network/network-facade.service.d.ts +1 -3
  68. package/dist/services/network/network-facade.service.js +20 -13
  69. package/dist/services/network/upload.service.d.ts +0 -1
  70. package/dist/services/sdk-manager.service.d.ts +2 -3
  71. package/dist/services/sdk-manager.service.js +14 -12
  72. package/dist/services/validation.service.d.ts +10 -0
  73. package/dist/services/validation.service.js +40 -1
  74. package/dist/types/command.types.d.ts +26 -17
  75. package/dist/types/command.types.js +38 -24
  76. package/dist/types/network.types.d.ts +0 -1
  77. package/dist/types/webdav.types.d.ts +1 -2
  78. package/dist/utils/cli.utils.d.ts +28 -19
  79. package/dist/utils/cli.utils.js +64 -44
  80. package/dist/utils/errors.utils.d.ts +1 -1
  81. package/dist/utils/errors.utils.js +3 -3
  82. package/dist/utils/hash.utils.d.ts +4 -7
  83. package/dist/utils/hash.utils.js +6 -6
  84. package/dist/utils/inquirer.utils.d.ts +4 -0
  85. package/dist/utils/inquirer.utils.js +24 -0
  86. package/dist/utils/network.utils.d.ts +2 -2
  87. package/dist/utils/network.utils.js +10 -8
  88. package/dist/utils/pm2.utils.d.ts +5 -1
  89. package/dist/utils/pm2.utils.js +35 -3
  90. package/dist/utils/stream.utils.d.ts +1 -4
  91. package/dist/utils/webdav.utils.js +4 -4
  92. package/dist/utils/xml.utils.js +4 -1
  93. package/dist/webdav/handlers/GET.handler.js +8 -3
  94. package/dist/webdav/handlers/MKCOL.handler.js +2 -6
  95. package/dist/webdav/handlers/MOVE.handler.js +0 -3
  96. package/dist/webdav/handlers/PROPFIND.handler.js +2 -2
  97. package/dist/webdav/handlers/PUT.handler.js +14 -9
  98. package/dist/webdav/middewares/errors.middleware.js +1 -1
  99. package/dist/webdav/middewares/request-logger.middleware.d.ts +1 -2
  100. package/dist/webdav/middewares/request-logger.middleware.js +6 -5
  101. package/dist/webdav/webdav-server.js +2 -3
  102. package/oclif.manifest.json +517 -350
  103. package/package.json +42 -47
  104. package/dist/commands/download.d.ts +0 -19
  105. package/dist/commands/rename.d.ts +0 -18
  106. package/dist/commands/rename.js +0 -109
  107. package/dist/commands/trash.d.ts +0 -15
  108. package/dist/commands/trash.js +0 -85
  109. package/dist/commands/upload.d.ts +0 -15
  110. package/dist/commands/upload.js +0 -93
  111. package/dist/services/analytics.service.d.ts +0 -22
  112. package/dist/services/analytics.service.js +0 -20
@@ -0,0 +1,140 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const core_1 = require("@oclif/core");
7
+ const promises_1 = require("node:fs/promises");
8
+ const node_fs_1 = require("node:fs");
9
+ const network_facade_service_1 = require("../services/network/network-facade.service");
10
+ const sdk_manager_service_1 = require("../services/sdk-manager.service");
11
+ const auth_service_1 = require("../services/auth.service");
12
+ const cli_utils_1 = require("../utils/cli.utils");
13
+ const config_service_1 = require("../services/config.service");
14
+ const node_path_1 = __importDefault(require("node:path"));
15
+ const drive_file_service_1 = require("../services/drive/drive-file.service");
16
+ const upload_service_1 = require("../services/network/upload.service");
17
+ const crypto_service_1 = require("../services/crypto.service");
18
+ const download_service_1 = require("../services/network/download.service");
19
+ const errors_utils_1 = require("../utils/errors.utils");
20
+ const command_types_1 = require("../types/command.types");
21
+ const validation_service_1 = require("../services/validation.service");
22
+ const types_1 = require("@internxt/sdk/dist/drive/storage/types");
23
+ class UploadFile extends core_1.Command {
24
+ static args = {};
25
+ static description = 'Upload a file to Internxt Drive';
26
+ static aliases = ['upload:file'];
27
+ static examples = ['<%= config.bin %> <%= command.id %>'];
28
+ static flags = {
29
+ ...cli_utils_1.CLIUtils.CommonFlags,
30
+ file: core_1.Flags.string({
31
+ char: 'f',
32
+ description: 'The path to the file on your system.',
33
+ required: false,
34
+ }),
35
+ destination: core_1.Flags.string({
36
+ char: 'i',
37
+ description: 'The folder id where the file is going to be uploaded to. Leave empty for the root folder.',
38
+ required: false,
39
+ parse: cli_utils_1.CLIUtils.parseEmpty,
40
+ }),
41
+ };
42
+ static enableJsonFlag = true;
43
+ run = async () => {
44
+ const { flags } = await this.parse(UploadFile);
45
+ const nonInteractive = flags['non-interactive'];
46
+ const userCredentials = await config_service_1.ConfigService.instance.readUser();
47
+ if (!userCredentials)
48
+ throw new command_types_1.MissingCredentialsError();
49
+ const filePath = await this.getFilePath(flags['file'], nonInteractive);
50
+ const stats = await (0, promises_1.stat)(filePath);
51
+ if (!stats.size) {
52
+ throw new Error('The file is empty. Uploading empty files is not allowed.');
53
+ }
54
+ let destinationFolderUuid = await this.getDestinationFolderUuid(flags['destination'], nonInteractive);
55
+ if (destinationFolderUuid.trim().length === 0) {
56
+ destinationFolderUuid = userCredentials.user.rootFolderId;
57
+ }
58
+ cli_utils_1.CLIUtils.doing('Preparing Network');
59
+ const { user } = await auth_service_1.AuthService.instance.getAuthDetails();
60
+ const networkModule = sdk_manager_service_1.SdkManager.instance.getNetwork({
61
+ user: user.bridgeUser,
62
+ pass: user.userId,
63
+ });
64
+ const networkFacade = new network_facade_service_1.NetworkFacade(networkModule, upload_service_1.UploadService.instance, download_service_1.DownloadService.instance, crypto_service_1.CryptoService.instance);
65
+ cli_utils_1.CLIUtils.done();
66
+ const timer = cli_utils_1.CLIUtils.timer();
67
+ const fileStream = (0, node_fs_1.createReadStream)(filePath);
68
+ const progressBar = cli_utils_1.CLIUtils.progress({
69
+ format: 'Uploading file [{bar}] {percentage}%',
70
+ linewrap: true,
71
+ });
72
+ progressBar.start(1, 0);
73
+ const [uploadPromise, abortable] = await networkFacade.uploadFromStream(user.bucket, user.mnemonic, stats.size, fileStream, {
74
+ progressCallback: (progress) => {
75
+ progressBar.update(progress);
76
+ },
77
+ });
78
+ process.on('SIGINT', () => {
79
+ abortable.abort('SIGINT received');
80
+ process.exit(1);
81
+ });
82
+ const uploadResult = await uploadPromise;
83
+ progressBar.stop();
84
+ const fileInfo = node_path_1.default.parse(filePath);
85
+ const createdDriveFile = await drive_file_service_1.DriveFileService.instance.createFile({
86
+ plain_name: fileInfo.name,
87
+ type: fileInfo.ext.replaceAll('.', ''),
88
+ size: stats.size,
89
+ folder_id: destinationFolderUuid,
90
+ id: uploadResult.fileId,
91
+ bucket: user.bucket,
92
+ encrypt_version: types_1.EncryptionVersion.Aes03,
93
+ name: '',
94
+ });
95
+ const uploadTime = timer.stop();
96
+ this.log('\n');
97
+ const message = `File uploaded in ${uploadTime}ms, view it at ${config_service_1.ConfigService.instance.get('DRIVE_URL')}/file/${createdDriveFile.uuid}`;
98
+ cli_utils_1.CLIUtils.success(this.log.bind(this), message);
99
+ return { success: true, message, file: createdDriveFile };
100
+ };
101
+ catch = async (error) => {
102
+ errors_utils_1.ErrorUtils.report(this.error.bind(this), error, { command: this.id });
103
+ cli_utils_1.CLIUtils.error(this.log.bind(this), error.message);
104
+ this.exit(1);
105
+ };
106
+ getDestinationFolderUuid = async (destinationFolderUuidFlag, nonInteractive) => {
107
+ const destinationFolderUuid = await cli_utils_1.CLIUtils.getValueFromFlag({
108
+ value: destinationFolderUuidFlag,
109
+ name: UploadFile.flags['destination'].name,
110
+ }, {
111
+ nonInteractive,
112
+ prompt: {
113
+ message: 'What is the destination folder id? (leave empty for the root folder)',
114
+ options: { type: 'input' },
115
+ },
116
+ }, {
117
+ validate: validation_service_1.ValidationService.instance.validateUUIDv4,
118
+ error: new command_types_1.NotValidFolderUuidError(),
119
+ canBeEmpty: true,
120
+ }, this.log.bind(this));
121
+ return destinationFolderUuid;
122
+ };
123
+ getFilePath = async (fileFlag, nonInteractive) => {
124
+ const filePath = await cli_utils_1.CLIUtils.getValueFromFlag({
125
+ value: fileFlag,
126
+ name: UploadFile.flags['file'].name,
127
+ }, {
128
+ nonInteractive,
129
+ prompt: {
130
+ message: 'What is the path to the file on your computer?',
131
+ options: { type: 'input' },
132
+ },
133
+ }, {
134
+ validate: validation_service_1.ValidationService.instance.validateFileExists,
135
+ error: new command_types_1.NotValidDirectoryError(),
136
+ }, this.log.bind(this));
137
+ return filePath;
138
+ };
139
+ }
140
+ exports.default = UploadFile;
@@ -1,17 +1,19 @@
1
1
  import { Command } from '@oclif/core';
2
2
  export default class WebDAVConfig extends Command {
3
+ static readonly args: {};
3
4
  static readonly description = "Edit the configuration of the Internxt CLI WebDav server as the port or the protocol.";
4
- static readonly args: {
5
- action: import("@oclif/core/lib/interfaces").Arg<string, Record<string, unknown>>;
6
- };
5
+ static readonly aliases: never[];
7
6
  static readonly examples: string[];
8
7
  static readonly flags: {
9
8
  port: import("@oclif/core/lib/interfaces").OptionFlag<string | undefined, import("@oclif/core/lib/interfaces").CustomOptions>;
10
- 'non-interactive': import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
9
+ https: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
10
+ http: import("@oclif/core/lib/interfaces").BooleanFlag<boolean>;
11
11
  };
12
- run(): Promise<void>;
13
- catch(error: Error): Promise<void>;
14
- private static readonly MAX_ATTEMPTS;
15
- getWebDAVPort: (webdavPortFlag: string | undefined, nonInteractive: boolean) => Promise<string>;
16
- getNewWebDAVPortInteractively: () => Promise<string>;
12
+ static readonly enableJsonFlag = true;
13
+ run: () => Promise<{
14
+ success: boolean;
15
+ message: string;
16
+ config: import("../types/command.types").WebdavConfig;
17
+ }>;
18
+ catch: (error: Error) => Promise<never>;
17
19
  }
@@ -7,85 +7,59 @@ const errors_utils_1 = require("../utils/errors.utils");
7
7
  const command_types_1 = require("../types/command.types");
8
8
  const validation_service_1 = require("../services/validation.service");
9
9
  class WebDAVConfig extends core_1.Command {
10
+ static args = {};
10
11
  static description = 'Edit the configuration of the Internxt CLI WebDav server as the port or the protocol.';
11
- static args = {
12
- action: core_1.Args.string({
13
- required: true,
14
- options: ['set-http', 'set-https', 'change-port'],
15
- }),
16
- };
17
- static examples = [
18
- '<%= config.bin %> <%= command.id %> set-http',
19
- '<%= config.bin %> <%= command.id %> set-https',
20
- '<%= config.bin %> <%= command.id %> change-port',
21
- ];
12
+ static aliases = [];
13
+ static examples = ['<%= config.bin %> <%= command.id %>'];
22
14
  static flags = {
23
- ...cli_utils_1.CLIUtils.CommonFlags,
24
15
  port: core_1.Flags.string({
25
16
  char: 'p',
26
- description: 'The new port that the WebDAV server is going to be have.',
17
+ description: 'The new port for the WebDAV server.',
18
+ required: false,
19
+ }),
20
+ https: core_1.Flags.boolean({
21
+ char: 's',
22
+ description: 'Configures the WebDAV server to use HTTPS with self-signed certificates.',
23
+ required: false,
24
+ exclusive: ['http'],
25
+ }),
26
+ http: core_1.Flags.boolean({
27
+ char: 'h',
28
+ description: 'Configures the WebDAV server to use insecure plain HTTP.',
27
29
  required: false,
30
+ exclusive: ['https'],
28
31
  }),
29
32
  };
30
- async run() {
31
- const { args, flags } = await this.parse(WebDAVConfig);
32
- const nonInteractive = flags['non-interactive'];
33
+ static enableJsonFlag = true;
34
+ run = async () => {
35
+ const { flags } = await this.parse(WebDAVConfig);
33
36
  const webdavConfig = await config_service_1.ConfigService.instance.readWebdavConfig();
34
- if (args.action !== 'change-port' && flags['port']) {
35
- cli_utils_1.CLIUtils.warning('The port flag will be ignored; it can only be used with the "change-port" action.');
36
- }
37
- switch (args.action) {
38
- case 'set-http': {
39
- await config_service_1.ConfigService.instance.saveWebdavConfig({
40
- ...webdavConfig,
41
- protocol: 'http',
42
- });
43
- cli_utils_1.CLIUtils.success('On the next start, the WebDAV server will use HTTP.');
44
- break;
45
- }
46
- case 'set-https': {
47
- await config_service_1.ConfigService.instance.saveWebdavConfig({
48
- ...webdavConfig,
49
- protocol: 'https',
50
- });
51
- cli_utils_1.CLIUtils.success('On the next start, the WebDAV server will use HTTPS.');
52
- break;
37
+ const port = flags['port'];
38
+ if (port) {
39
+ if (validation_service_1.ValidationService.instance.validateTCPIntegerPort(port)) {
40
+ webdavConfig['port'] = port;
53
41
  }
54
- case 'change-port': {
55
- const newPort = await this.getWebDAVPort(flags['port'], nonInteractive);
56
- await config_service_1.ConfigService.instance.saveWebdavConfig({
57
- ...webdavConfig,
58
- port: newPort,
59
- });
60
- cli_utils_1.CLIUtils.success('For the next start, the Webdav server will be served at the new port: ' + newPort);
61
- break;
42
+ else {
43
+ throw new command_types_1.NotValidPortError();
62
44
  }
63
45
  }
64
- }
65
- async catch(error) {
66
- errors_utils_1.ErrorUtils.report(error, { command: this.id });
67
- cli_utils_1.CLIUtils.error(error.message);
68
- this.exit(1);
69
- }
70
- static MAX_ATTEMPTS = 3;
71
- getWebDAVPort = async (webdavPortFlag, nonInteractive) => {
72
- let port = cli_utils_1.CLIUtils.getValueFromFlag({
73
- value: webdavPortFlag,
74
- name: WebDAVConfig.flags['port'].name,
75
- error: new command_types_1.NotValidPortError(),
76
- canBeEmpty: true,
77
- }, nonInteractive, (port) => validation_service_1.ValidationService.instance.validateTCPIntegerPort(port));
78
- if (!port) {
79
- port = (await this.getNewWebDAVPortInteractively()).trim();
46
+ const http = flags['http'];
47
+ if (http) {
48
+ webdavConfig['protocol'] = 'http';
80
49
  }
81
- return port;
50
+ const https = flags['https'];
51
+ if (https) {
52
+ webdavConfig['protocol'] = 'https';
53
+ }
54
+ await config_service_1.ConfigService.instance.saveWebdavConfig(webdavConfig);
55
+ const message = `On the next start, the WebDAV server will use the next config: ${JSON.stringify(webdavConfig)}`;
56
+ cli_utils_1.CLIUtils.success(this.log.bind(this), message);
57
+ return { success: true, message, config: webdavConfig };
82
58
  };
83
- getNewWebDAVPortInteractively = () => {
84
- return cli_utils_1.CLIUtils.promptWithAttempts({
85
- message: 'What is the new WebDAV server port?',
86
- options: { required: false },
87
- error: new command_types_1.NotValidPortError(),
88
- }, WebDAVConfig.MAX_ATTEMPTS, (port) => validation_service_1.ValidationService.instance.validateTCPIntegerPort(port));
59
+ catch = async (error) => {
60
+ errors_utils_1.ErrorUtils.report(this.error.bind(this), error, { command: this.id });
61
+ cli_utils_1.CLIUtils.error(this.log.bind(this), error.message);
62
+ this.exit(1);
89
63
  };
90
64
  }
91
65
  exports.default = WebDAVConfig;
@@ -1,14 +1,21 @@
1
1
  import { Command } from '@oclif/core';
2
2
  export default class Webdav extends Command {
3
- static readonly description = "Enable, disable, restart or get the status of the Internxt CLI WebDav server";
4
- static examples: string[];
5
- static flags: {};
6
- static args: {
3
+ static readonly args: {
7
4
  action: import("@oclif/core/lib/interfaces").Arg<string, Record<string, unknown>>;
8
5
  };
9
- enableWebDav(): Promise<void>;
10
- disableWebDav(): Promise<void>;
11
- restartWebDav(): Promise<void>;
12
- webDAVStatus(): Promise<void>;
13
- run(): Promise<void>;
6
+ static readonly description = "Enable, disable, restart or get the status of the Internxt CLI WebDav server";
7
+ static readonly aliases: never[];
8
+ static readonly examples: string[];
9
+ static readonly flags: {};
10
+ static readonly enableJsonFlag = true;
11
+ run: () => Promise<{
12
+ success: boolean;
13
+ message: string;
14
+ action: string;
15
+ }>;
16
+ catch: (error: Error) => Promise<never>;
17
+ private enableWebDav;
18
+ private disableWebDav;
19
+ private restartWebDav;
20
+ private webDAVStatus;
14
21
  }
@@ -4,11 +4,17 @@ const core_1 = require("@oclif/core");
4
4
  const pm2_utils_1 = require("../utils/pm2.utils");
5
5
  const cli_utils_1 = require("../utils/cli.utils");
6
6
  const config_service_1 = require("../services/config.service");
7
- const analytics_service_1 = require("../services/analytics.service");
8
- const auth_service_1 = require("../services/auth.service");
9
7
  const drive_database_manager_service_1 = require("../services/database/drive-database-manager.service");
8
+ const errors_utils_1 = require("../utils/errors.utils");
10
9
  class Webdav extends core_1.Command {
10
+ static args = {
11
+ action: core_1.Args.string({
12
+ required: true,
13
+ options: ['enable', 'disable', 'restart', 'status'],
14
+ }),
15
+ };
11
16
  static description = 'Enable, disable, restart or get the status of the Internxt CLI WebDav server';
17
+ static aliases = [];
12
18
  static examples = [
13
19
  '<%= config.bin %> <%= command.id %> enable',
14
20
  '<%= config.bin %> <%= command.id %> disable',
@@ -16,91 +22,101 @@ class Webdav extends core_1.Command {
16
22
  '<%= config.bin %> <%= command.id %> status',
17
23
  ];
18
24
  static flags = {};
19
- static args = {
20
- action: core_1.Args.string({
21
- required: true,
22
- options: ['enable', 'disable', 'restart', 'status'],
23
- }),
25
+ static enableJsonFlag = true;
26
+ run = async () => {
27
+ const { args } = await this.parse(Webdav);
28
+ let message = '';
29
+ let success = true;
30
+ await pm2_utils_1.PM2Utils.connect();
31
+ switch (args.action) {
32
+ case 'enable': {
33
+ message = await this.enableWebDav();
34
+ break;
35
+ }
36
+ case 'disable': {
37
+ message = await this.disableWebDav();
38
+ break;
39
+ }
40
+ case 'restart': {
41
+ message = await this.restartWebDav();
42
+ break;
43
+ }
44
+ case 'status': {
45
+ message = await this.webDAVStatus();
46
+ break;
47
+ }
48
+ default: {
49
+ success = false;
50
+ message = `Expected one of this command actions: ${Webdav.args.action.options}`;
51
+ break;
52
+ }
53
+ }
54
+ pm2_utils_1.PM2Utils.disconnect();
55
+ return { success, message, action: args.action };
24
56
  };
25
- async enableWebDav() {
57
+ catch = async (error) => {
58
+ errors_utils_1.ErrorUtils.report(this.error.bind(this), error, { command: this.id });
59
+ if (error instanceof Error) {
60
+ cli_utils_1.CLIUtils.error(this.log.bind(this), error.message);
61
+ }
62
+ else {
63
+ cli_utils_1.CLIUtils.error(this.log.bind(this), JSON.stringify(error));
64
+ }
65
+ this.exit(1);
66
+ };
67
+ enableWebDav = async () => {
26
68
  cli_utils_1.CLIUtils.doing('Starting Internxt WebDav server...');
27
69
  await drive_database_manager_service_1.DriveDatabaseManager.clean();
28
- await pm2_utils_1.PM2Utils.connect();
29
70
  await pm2_utils_1.PM2Utils.killWebDavServer();
30
71
  await pm2_utils_1.PM2Utils.startWebDavServer();
31
72
  cli_utils_1.CLIUtils.done();
32
73
  const { status } = await pm2_utils_1.PM2Utils.webdavServerStatus();
33
74
  const webdavConfigs = await config_service_1.ConfigService.instance.readWebdavConfig();
34
75
  if (status === 'online') {
35
- core_1.ux.log(`\nWebDav server status: ${core_1.ux.colorize('green', 'online')}\n`);
36
- cli_utils_1.CLIUtils.success(`Internxt WebDav server started successfully at ${webdavConfigs.protocol}://${config_service_1.ConfigService.WEBDAV_LOCAL_URL}:${webdavConfigs.port}`);
37
- core_1.ux.log(`\n[If the above URL is not working, the WebDAV server can be accessed directly via your localhost IP at: ${webdavConfigs.protocol}://127.0.0.1:${webdavConfigs.port} ]\n`);
38
- const authDetails = await auth_service_1.AuthService.instance.getAuthDetails();
39
- await analytics_service_1.AnalyticsService.instance.track('WebDAVEnabled', { app: 'internxt-cli', userId: authDetails.user.uuid });
76
+ const message = `Internxt WebDav server started successfully at ${webdavConfigs.protocol}://${config_service_1.ConfigService.WEBDAV_LOCAL_URL}:${webdavConfigs.port}`;
77
+ cli_utils_1.CLIUtils.log(this.log.bind(this), `\nWebDav server status: ${core_1.ux.colorize('green', 'online')}\n`);
78
+ cli_utils_1.CLIUtils.success(this.log.bind(this), message);
79
+ cli_utils_1.CLIUtils.log(this.log.bind(this), `\n[If the above URL is not working, the WebDAV server can be accessed directly via your localhost IP at: ${webdavConfigs.protocol}://127.0.0.1:${webdavConfigs.port} ]\n`);
80
+ return message;
40
81
  }
41
82
  else {
42
- core_1.ux.log(`WebDav server status: ${core_1.ux.colorize('red', status)}`);
83
+ const message = `WebDav server status: ${core_1.ux.colorize('red', status)}`;
84
+ cli_utils_1.CLIUtils.log(this.log.bind(this), message);
85
+ return message;
43
86
  }
44
- }
45
- async disableWebDav() {
87
+ };
88
+ disableWebDav = async () => {
46
89
  cli_utils_1.CLIUtils.doing('Stopping Internxt WebDav server...');
47
- await pm2_utils_1.PM2Utils.connect();
48
90
  await pm2_utils_1.PM2Utils.killWebDavServer();
49
91
  cli_utils_1.CLIUtils.done();
50
- cli_utils_1.CLIUtils.success('Internxt WebDav server stopped successfully');
51
- }
52
- async restartWebDav() {
92
+ const message = 'Internxt WebDav server stopped successfully';
93
+ cli_utils_1.CLIUtils.success(this.log.bind(this), message);
94
+ return message;
95
+ };
96
+ restartWebDav = async () => {
53
97
  cli_utils_1.CLIUtils.doing('Restarting Internxt WebDav server...');
54
98
  await drive_database_manager_service_1.DriveDatabaseManager.clean();
55
- await pm2_utils_1.PM2Utils.connect();
56
99
  const { status } = await pm2_utils_1.PM2Utils.webdavServerStatus();
57
100
  if (status === 'online') {
58
101
  await pm2_utils_1.PM2Utils.killWebDavServer();
59
102
  await pm2_utils_1.PM2Utils.startWebDavServer();
60
103
  cli_utils_1.CLIUtils.done();
61
- cli_utils_1.CLIUtils.success('Internxt WebDav server restarted successfully');
104
+ const message = 'Internxt WebDav server restarted successfully';
105
+ cli_utils_1.CLIUtils.success(this.log.bind(this), message);
106
+ return message;
62
107
  }
63
108
  else {
64
109
  cli_utils_1.CLIUtils.done();
65
- cli_utils_1.CLIUtils.error('Internxt WebDav server is not running, cannot restart');
110
+ const message = 'Internxt WebDav server is not running, it wont be restarted';
111
+ cli_utils_1.CLIUtils.warning(this.log.bind(this), message);
112
+ return message;
66
113
  }
67
- }
68
- async webDAVStatus() {
69
- await pm2_utils_1.PM2Utils.connect();
114
+ };
115
+ webDAVStatus = async () => {
70
116
  const { status } = await pm2_utils_1.PM2Utils.webdavServerStatus();
71
- this.log(`Internxt WebDAV server status: ${status}`);
72
- }
73
- async run() {
74
- const { args } = await this.parse(Webdav);
75
- try {
76
- switch (args.action) {
77
- case 'enable': {
78
- await this.enableWebDav();
79
- break;
80
- }
81
- case 'disable': {
82
- await this.disableWebDav();
83
- break;
84
- }
85
- case 'restart': {
86
- await this.restartWebDav();
87
- break;
88
- }
89
- case 'status': {
90
- await this.webDAVStatus();
91
- break;
92
- }
93
- }
94
- }
95
- catch (error) {
96
- if (error instanceof Error) {
97
- cli_utils_1.CLIUtils.error(error.message);
98
- }
99
- else {
100
- cli_utils_1.CLIUtils.error(JSON.stringify(error));
101
- }
102
- }
103
- this.exit(0);
104
- }
117
+ const message = `Internxt WebDAV server status: ${status}`;
118
+ cli_utils_1.CLIUtils.log(this.log.bind(this), message);
119
+ return message;
120
+ };
105
121
  }
106
122
  exports.default = Webdav;
@@ -1,9 +1,21 @@
1
1
  import { Command } from '@oclif/core';
2
+ import { LoginCredentials } from '../types/command.types';
2
3
  export default class Whoami extends Command {
3
4
  static readonly args: {};
4
5
  static readonly description = "Display the current user logged into the Internxt CLI.";
6
+ static readonly aliases: never[];
5
7
  static readonly examples: string[];
6
8
  static readonly flags: {};
7
- catch(error: Error): Promise<void>;
8
- run(): Promise<void>;
9
+ static readonly enableJsonFlag = true;
10
+ run: () => Promise<{
11
+ success: boolean;
12
+ message: string;
13
+ login?: undefined;
14
+ } | {
15
+ success: boolean;
16
+ message: string;
17
+ login: LoginCredentials;
18
+ }>;
19
+ catch: (error: Error) => Promise<never>;
20
+ private checkUserAndTokens;
9
21
  }
@@ -1,27 +1,56 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const core_1 = require("@oclif/core");
4
- const config_service_1 = require("../services/config.service");
5
4
  const cli_utils_1 = require("../utils/cli.utils");
6
5
  const errors_utils_1 = require("../utils/errors.utils");
6
+ const config_service_1 = require("../services/config.service");
7
+ const validation_service_1 = require("../services/validation.service");
8
+ const drive_database_manager_service_1 = require("../services/database/drive-database-manager.service");
7
9
  class Whoami extends core_1.Command {
8
10
  static args = {};
9
11
  static description = 'Display the current user logged into the Internxt CLI.';
12
+ static aliases = [];
10
13
  static examples = ['<%= config.bin %> <%= command.id %>'];
11
14
  static flags = {};
12
- async catch(error) {
13
- errors_utils_1.ErrorUtils.report(error, { command: this.id });
14
- cli_utils_1.CLIUtils.error(error.message);
15
- this.exit(1);
16
- }
17
- async run() {
15
+ static enableJsonFlag = true;
16
+ run = async () => {
18
17
  const userCredentials = await config_service_1.ConfigService.instance.readUser();
19
- if (userCredentials?.user?.email) {
20
- cli_utils_1.CLIUtils.success(`You are logged in with: ${userCredentials.user.email}`);
18
+ if (!userCredentials) {
19
+ const message = 'You are not logged in.';
20
+ cli_utils_1.CLIUtils.error(this.log.bind(this), message);
21
+ return { success: false, message };
21
22
  }
22
23
  else {
23
- cli_utils_1.CLIUtils.error('You are not logged in');
24
+ const validCreds = this.checkUserAndTokens(userCredentials);
25
+ if (!validCreds) {
26
+ const message = 'Your session has expired. You have been logged out. Please log in again.';
27
+ await config_service_1.ConfigService.instance.clearUser();
28
+ await drive_database_manager_service_1.DriveDatabaseManager.clean();
29
+ cli_utils_1.CLIUtils.error(this.log.bind(this), message);
30
+ return { success: false, message };
31
+ }
32
+ else {
33
+ const message = `You are logged in as: ${userCredentials.user.email}.`;
34
+ cli_utils_1.CLIUtils.success(this.log.bind(this), message);
35
+ return { success: true, message, login: userCredentials };
36
+ }
37
+ }
38
+ };
39
+ catch = async (error) => {
40
+ errors_utils_1.ErrorUtils.report(this.error.bind(this), error, { command: this.id });
41
+ cli_utils_1.CLIUtils.error(this.log.bind(this), error.message);
42
+ this.exit(1);
43
+ };
44
+ checkUserAndTokens = (loginCreds) => {
45
+ if (!(loginCreds?.newToken && loginCreds?.token && loginCreds?.user?.mnemonic)) {
46
+ return false;
24
47
  }
25
- }
48
+ const oldTokenDetails = validation_service_1.ValidationService.instance.validateTokenAndCheckExpiration(loginCreds.token);
49
+ const newTokenDetails = validation_service_1.ValidationService.instance.validateTokenAndCheckExpiration(loginCreds.newToken);
50
+ const goodMnemonic = validation_service_1.ValidationService.instance.validateMnemonic(loginCreds.user.mnemonic);
51
+ const goodToken = oldTokenDetails.isValid && !oldTokenDetails.expiration.expired;
52
+ const goodNewToken = newTokenDetails.isValid && !newTokenDetails.expiration.expired;
53
+ return goodToken && goodNewToken && goodMnemonic;
54
+ };
26
55
  }
27
56
  exports.default = Whoami;
@@ -10,7 +10,6 @@ const logs_1 = __importDefault(require("../../commands/logs"));
10
10
  const cli_utils_1 = require("../../utils/cli.utils");
11
11
  const sdk_manager_service_1 = require("../../services/sdk-manager.service");
12
12
  const auth_service_1 = require("../../services/auth.service");
13
- const command_types_1 = require("../../types/command.types");
14
13
  const webdav_1 = __importDefault(require("../../commands/webdav"));
15
14
  const webdav_config_1 = __importDefault(require("../../commands/webdav-config"));
16
15
  const CommandsToSkip = [whoami_1.default, login_1.default, logout_1.default, logs_1.default, webdav_1.default, webdav_config_1.default];
@@ -23,13 +22,16 @@ const hook = async function (opts) {
23
22
  token,
24
23
  newToken,
25
24
  });
25
+ cli_utils_1.CLIUtils.done();
26
+ cli_utils_1.CLIUtils.clearPreviousLine();
26
27
  }
27
28
  catch (error) {
28
- cli_utils_1.CLIUtils.error(new command_types_1.MissingCredentialsError().message);
29
+ const err = error;
30
+ cli_utils_1.CLIUtils.done();
31
+ cli_utils_1.CLIUtils.clearPreviousLine();
32
+ cli_utils_1.CLIUtils.error(this.log.bind(this), err.message);
29
33
  opts.context.exit(1);
30
34
  }
31
- cli_utils_1.CLIUtils.done();
32
- cli_utils_1.CLIUtils.clearPreviousLine();
33
35
  }
34
36
  };
35
37
  exports.default = hook;
@@ -1,14 +1,8 @@
1
1
  import { LoginCredentials } from '../types/command.types';
2
- import { UserSettings } from '@internxt/sdk/dist/shared/types/userSettings';
3
2
  export declare class AuthService {
4
3
  static readonly instance: AuthService;
5
4
  doLogin: (email: string, password: string, twoFactorCode?: string) => Promise<LoginCredentials>;
6
5
  is2FANeeded: (email: string) => Promise<boolean>;
7
- getUser: () => Promise<UserSettings>;
8
- getAuthDetails: () => Promise<{
9
- token: string;
10
- newToken: string;
11
- mnemonic: string;
12
- user: UserSettings;
13
- }>;
6
+ getAuthDetails: () => Promise<LoginCredentials>;
7
+ refreshUserTokens: (oldCreds: LoginCredentials) => Promise<LoginCredentials>;
14
8
  }