1- import path from 'node:path' ;
2- import os from 'node:os' ;
31import fs from 'node:fs/promises' ;
42import { ConfigKeys } from '../types/config.types' ;
53import { LoginCredentials , WebdavConfig } from '../types/command.types' ;
64import { CryptoService } from './crypto.service' ;
75import { isFileNotFoundError } from '../utils/errors.utils' ;
6+ import {
7+ CREDENTIALS_FILE ,
8+ INTERNXT_CLI_DATA_DIR ,
9+ INTERNXT_CLI_LOGS_DIR ,
10+ WEBDAV_CONFIGS_FILE ,
11+ WEBDAV_DEFAULT_CREATE_FULL_PATH ,
12+ WEBDAV_DEFAULT_HOST ,
13+ WEBDAV_DEFAULT_PORT ,
14+ WEBDAV_DEFAULT_PROTOCOL ,
15+ WEBDAV_DEFAULT_TIMEOUT ,
16+ WEBDAV_SSL_CERTS_DIR ,
17+ } from '../constants/configs' ;
818
919export class ConfigService {
10- static readonly INTERNXT_CLI_DATA_DIR = path . join ( os . homedir ( ) , '.internxt-cli' ) ;
11- static readonly INTERNXT_CLI_LOGS_DIR = path . join ( this . INTERNXT_CLI_DATA_DIR , 'logs' ) ;
12- static readonly INTERNXT_TMP_DIR = os . tmpdir ( ) ;
13- static readonly CREDENTIALS_FILE = path . join ( this . INTERNXT_CLI_DATA_DIR , '.inxtcli' ) ;
14- static readonly DRIVE_SQLITE_FILE = path . join ( this . INTERNXT_CLI_DATA_DIR , 'internxt-cli-drive.sqlite' ) ;
15- static readonly WEBDAV_SSL_CERTS_DIR = path . join ( this . INTERNXT_CLI_DATA_DIR , 'certs' ) ;
16- static readonly WEBDAV_CONFIGS_FILE = path . join ( this . INTERNXT_CLI_DATA_DIR , 'config.webdav.inxt' ) ;
17- static readonly WEBDAV_DEFAULT_HOST = '127.0.0.1' ;
18- static readonly WEBDAV_DEFAULT_PORT = '3005' ;
19- static readonly WEBDAV_DEFAULT_PROTOCOL = 'https' ;
20- static readonly WEBDAV_DEFAULT_TIMEOUT = 0 ;
21- static readonly WEBDAV_DEFAULT_CREATE_FULL_PATH = true ;
2220 public static readonly instance : ConfigService = new ConfigService ( ) ;
2321
2422 /**
@@ -42,7 +40,7 @@ export class ConfigService {
4240 await this . ensureInternxtCliDataDirExists ( ) ;
4341 const credentialsString = JSON . stringify ( loginCredentials ) ;
4442 const encryptedCredentials = CryptoService . instance . encryptText ( credentialsString ) ;
45- await fs . writeFile ( ConfigService . CREDENTIALS_FILE , encryptedCredentials , 'utf8' ) ;
43+ await fs . writeFile ( CREDENTIALS_FILE , encryptedCredentials , 'utf8' ) ;
4644 } ;
4745
4846 /**
@@ -51,9 +49,9 @@ export class ConfigService {
5149 **/
5250 public clearUser = async ( ) : Promise < void > => {
5351 try {
54- const stat = await fs . stat ( ConfigService . CREDENTIALS_FILE ) ;
52+ const stat = await fs . stat ( CREDENTIALS_FILE ) ;
5553 if ( stat . size === 0 ) return ;
56- await fs . writeFile ( ConfigService . CREDENTIALS_FILE , '' , 'utf8' ) ;
54+ await fs . writeFile ( CREDENTIALS_FILE , '' , 'utf8' ) ;
5755 } catch ( error ) {
5856 if ( ! isFileNotFoundError ( error ) ) {
5957 throw error ;
@@ -67,7 +65,7 @@ export class ConfigService {
6765 **/
6866 public readUser = async ( ) : Promise < LoginCredentials | undefined > => {
6967 try {
70- const encryptedCredentials = await fs . readFile ( ConfigService . CREDENTIALS_FILE , 'utf8' ) ;
68+ const encryptedCredentials = await fs . readFile ( CREDENTIALS_FILE , 'utf8' ) ;
7169 const credentialsString = CryptoService . instance . decryptText ( encryptedCredentials ) ;
7270 const loginCredentials = JSON . parse ( credentialsString ) as LoginCredentials ;
7371 return loginCredentials ;
@@ -79,52 +77,52 @@ export class ConfigService {
7977 public saveWebdavConfig = async ( webdavConfig : WebdavConfig ) : Promise < void > => {
8078 await this . ensureInternxtCliDataDirExists ( ) ;
8179 const configs = JSON . stringify ( webdavConfig ) ;
82- await fs . writeFile ( ConfigService . WEBDAV_CONFIGS_FILE , configs , 'utf8' ) ;
80+ await fs . writeFile ( WEBDAV_CONFIGS_FILE , configs , 'utf8' ) ;
8381 } ;
8482
8583 public readWebdavConfig = async ( ) : Promise < WebdavConfig > => {
8684 try {
87- const configsData = await fs . readFile ( ConfigService . WEBDAV_CONFIGS_FILE , 'utf8' ) ;
85+ const configsData = await fs . readFile ( WEBDAV_CONFIGS_FILE , 'utf8' ) ;
8886 const configs = JSON . parse ( configsData ) ;
8987 return {
90- host : configs ?. host ?? ConfigService . WEBDAV_DEFAULT_HOST ,
91- port : configs ?. port ?? ConfigService . WEBDAV_DEFAULT_PORT ,
92- protocol : configs ?. protocol ?? ConfigService . WEBDAV_DEFAULT_PROTOCOL ,
93- timeoutMinutes : configs ?. timeoutMinutes ?? ConfigService . WEBDAV_DEFAULT_TIMEOUT ,
94- createFullPath : configs ?. createFullPath ?? ConfigService . WEBDAV_DEFAULT_CREATE_FULL_PATH ,
88+ host : configs ?. host ?? WEBDAV_DEFAULT_HOST ,
89+ port : configs ?. port ?? WEBDAV_DEFAULT_PORT ,
90+ protocol : configs ?. protocol ?? WEBDAV_DEFAULT_PROTOCOL ,
91+ timeoutMinutes : configs ?. timeoutMinutes ?? WEBDAV_DEFAULT_TIMEOUT ,
92+ createFullPath : configs ?. createFullPath ?? WEBDAV_DEFAULT_CREATE_FULL_PATH ,
9593 } ;
9694 } catch {
9795 return {
98- host : ConfigService . WEBDAV_DEFAULT_HOST ,
99- port : ConfigService . WEBDAV_DEFAULT_PORT ,
100- protocol : ConfigService . WEBDAV_DEFAULT_PROTOCOL ,
101- timeoutMinutes : ConfigService . WEBDAV_DEFAULT_TIMEOUT ,
102- createFullPath : ConfigService . WEBDAV_DEFAULT_CREATE_FULL_PATH ,
96+ host : WEBDAV_DEFAULT_HOST ,
97+ port : WEBDAV_DEFAULT_PORT ,
98+ protocol : WEBDAV_DEFAULT_PROTOCOL ,
99+ timeoutMinutes : WEBDAV_DEFAULT_TIMEOUT ,
100+ createFullPath : WEBDAV_DEFAULT_CREATE_FULL_PATH ,
103101 } ;
104102 }
105103 } ;
106104
107105 ensureInternxtCliDataDirExists = async ( ) => {
108106 try {
109- await fs . access ( ConfigService . INTERNXT_CLI_DATA_DIR ) ;
107+ await fs . access ( INTERNXT_CLI_DATA_DIR ) ;
110108 } catch {
111- await fs . mkdir ( ConfigService . INTERNXT_CLI_DATA_DIR ) ;
109+ await fs . mkdir ( INTERNXT_CLI_DATA_DIR ) ;
112110 }
113111 } ;
114112
115113 ensureWebdavCertsDirExists = async ( ) => {
116114 try {
117- await fs . access ( ConfigService . WEBDAV_SSL_CERTS_DIR ) ;
115+ await fs . access ( WEBDAV_SSL_CERTS_DIR ) ;
118116 } catch {
119- await fs . mkdir ( ConfigService . WEBDAV_SSL_CERTS_DIR ) ;
117+ await fs . mkdir ( WEBDAV_SSL_CERTS_DIR ) ;
120118 }
121119 } ;
122120
123121 ensureInternxtLogsDirExists = async ( ) => {
124122 try {
125- await fs . access ( ConfigService . INTERNXT_CLI_LOGS_DIR ) ;
123+ await fs . access ( INTERNXT_CLI_LOGS_DIR ) ;
126124 } catch {
127- await fs . mkdir ( ConfigService . INTERNXT_CLI_LOGS_DIR ) ;
125+ await fs . mkdir ( INTERNXT_CLI_LOGS_DIR ) ;
128126 }
129127 } ;
130128}
0 commit comments