From 7f0f4a11f844912f901f0c03d971d3e49ed25bd6 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 14 May 2018 11:15:54 -0400 Subject: [PATCH] main and program are classes --- src/bw.js | 4 +- src/commands/sync.command.ts | 18 +++ src/locales/en/messages.json | 5 + src/main.ts | 153 ++++++++++++---------- src/program.ts | 81 ++++++++++++ src/services/i18n.service.ts | 19 +++ src/services/nodePlatformUtils.service.ts | 4 +- 7 files changed, 210 insertions(+), 74 deletions(-) create mode 100644 src/commands/sync.command.ts create mode 100644 src/locales/en/messages.json create mode 100644 src/program.ts create mode 100644 src/services/i18n.service.ts diff --git a/src/bw.js b/src/bw.js index 9652fe4813..56fc04e9f9 100644 --- a/src/bw.js +++ b/src/bw.js @@ -2,4 +2,6 @@ require('tsconfig-paths').register(); require('ts-node').register(); -require('./main.ts'); +const main = require('./main.ts'); +const m = new main.Main(); +m.run(); diff --git a/src/commands/sync.command.ts b/src/commands/sync.command.ts new file mode 100644 index 0000000000..7883d60af4 --- /dev/null +++ b/src/commands/sync.command.ts @@ -0,0 +1,18 @@ +import * as program from 'commander'; + +import { SyncService } from 'jslib/abstractions/sync.service'; + +export class SyncCommand { + constructor(private syncService: SyncService) { + + } + + async run(cmd: program.Command) { + try { + const result = await this.syncService.fullSync(false); + console.log(result); + } catch (e) { + console.log(e); + } + } +} diff --git a/src/locales/en/messages.json b/src/locales/en/messages.json new file mode 100644 index 0000000000..1557467da5 --- /dev/null +++ b/src/locales/en/messages.json @@ -0,0 +1,5 @@ +{ + "bitwarden": { + "message": "Bitwarden" + } +} diff --git a/src/main.ts b/src/main.ts index 40941cd5d0..5014e99205 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,4 +1,3 @@ -import * as program from 'commander'; import { AuthService } from 'jslib/services/auth.service'; @@ -15,80 +14,90 @@ import { EnvironmentService } from 'jslib/services/environment.service'; import { UserService } from 'jslib/services/user.service'; import { ContainerService } from 'jslib/services/container.service'; import { NodeMessagingService } from './services/nodeMessaging.service'; +import { SyncCommand } from './commands/sync.command'; +import { SyncService } from 'jslib/services/sync.service'; +import { SettingsService } from 'jslib/services/settings.service'; +import { CipherService } from 'jslib/services/cipher.service'; +import { FolderService } from 'jslib/services/folder.service'; +import { CollectionService } from 'jslib/services/collection.service'; +import { LockService } from 'jslib/services/lock.service'; +import { I18nService } from './services/i18n.service'; +import { ConstantsService } from 'jslib/services/constants.service'; +import { PasswordGenerationService } from 'jslib/services/passwordGeneration.service'; +import { TotpService } from 'jslib/services/totp.service'; +import { AuditService } from 'jslib/services/audit.service'; -const platformUtilsService = new NodePlatformUtilsService(); -const cryptoFunctionService = new NodeCryptoFunctionService(); -const storageService = new NodeStorageService('Bitwarden CLI'); -const cryptoService = new CryptoService(storageService, storageService, cryptoFunctionService); -const appIdService = new AppIdService(storageService); -const tokenService = new TokenService(storageService); -const messagingService = new NodeMessagingService(); -const apiService = new ApiService(tokenService, platformUtilsService, (expired: boolean) => { }); -const environmentService = new EnvironmentService(apiService, storageService); -const userService = new UserService(tokenService, storageService); -const containerService = new ContainerService(cryptoService, platformUtilsService); -const authService = new AuthService(cryptoService, apiService, userService, tokenService, appIdService, - null, platformUtilsService, messagingService, true); +import { Program } from './program'; -containerService.attachToWindow(global); -environmentService.setUrlsFromStorage().then(() => { - // Do nothing -}); +export class Main { + messagingService: NodeMessagingService; + storageService: NodeStorageService; + secureStorageService: NodeStorageService; + i18nService: I18nService; + platformUtilsService: NodePlatformUtilsService; + constantsService: ConstantsService; + cryptoService: CryptoService; + tokenService: TokenService; + appIdService: AppIdService; + apiService: ApiService; + environmentService: EnvironmentService; + userService: UserService; + settingsService: SettingsService; + cipherService: CipherService; + folderService: FolderService; + collectionService: CollectionService; + lockService: LockService; + syncService: SyncService; + passwordGenerationService: PasswordGenerationService; + totpService: TotpService; + containerService: ContainerService; + auditService: AuditService; + cryptoFunctionService: NodeCryptoFunctionService; + authService: AuthService; -program - .version('1.0.0', '-v, --version'); + program: Program; -program - .command('login ') - .description('Log into a Bitwarden user account.') - .option('-m, --method ', '2FA method.') - .option('-c, --code ', '2FA code.') - .action(async (email: string, password: string, cmd: program.Command) => { - const command = new LoginCommand(authService); - await command.run(email, password, cmd); - }); + constructor() { + this.i18nService = new I18nService('en', '../locales'); + this.platformUtilsService = new NodePlatformUtilsService(); + this.cryptoFunctionService = new NodeCryptoFunctionService(); + this.storageService = new NodeStorageService('Bitwarden CLI'); + this.cryptoService = new CryptoService(this.storageService, this.storageService, this.cryptoFunctionService); + this.appIdService = new AppIdService(this.storageService); + this.tokenService = new TokenService(this.storageService); + this.messagingService = new NodeMessagingService(); + this.apiService = new ApiService(this.tokenService, this.platformUtilsService, (expired: boolean) => { }); + this.environmentService = new EnvironmentService(this.apiService, this.storageService); + this.userService = new UserService(this.tokenService, this.storageService); + this.containerService = new ContainerService(this.cryptoService, this.platformUtilsService); + this.settingsService = new SettingsService(this.userService, this.storageService); + this.cipherService = new CipherService(this.cryptoService, this.userService, this.settingsService, + this.apiService, this.storageService, this.i18nService, this.platformUtilsService); + this.folderService = new FolderService(this.cryptoService, this.userService, + () => 'No Folder', this.apiService, this.storageService, this.i18nService); + this.collectionService = new CollectionService(this.cryptoService, this.userService, this.storageService, + this.i18nService); + this.lockService = new LockService(this.cipherService, this.folderService, this.collectionService, + this.cryptoService, this.platformUtilsService, this.storageService, this.messagingService, + () => { /* do nothing */ }); + this.syncService = new SyncService(this.userService, this.apiService, this.settingsService, + this.folderService, this.cipherService, this.cryptoService, this.collectionService, + this.storageService, this.messagingService, (expired: boolean) => { }); + this.authService = new AuthService(this.cryptoService, this.apiService, this.userService, this.tokenService, + this.appIdService, this.i18nService, this.platformUtilsService, this.messagingService, true); + this.program = new Program(this); + } -program - .command('logout') - .description('Log out of the current Bitwarden user account.') - .action((cmd) => { - console.log('Logging out...'); - }); + private async init() { + this.containerService.attachToWindow(global); + await this.environmentService.setUrlsFromStorage(); + const locale = await this.storageService.get(ConstantsService.localeKey); + await this.i18nService.init(locale); + await this.authService.init(); + } -program - .command('list ') - .description('List objects.') - .action((object, cmd) => { - console.log('Listing...'); - console.log(object); - }); - -program - .command('get ') - .description('Get an object.') - .action((object, id, cmd) => { - console.log('Getting...'); - console.log(object); - console.log(id); - }); - -program - .command('edit ') - .description('Edit an object.') - .action((object, id, cmd) => { - console.log('Editing...'); - console.log(object); - console.log(id); - }); - -program - .command('delete ') - .description('Delete an object.') - .action((object, id, cmd) => { - console.log('Deleting...'); - console.log(object); - console.log(id); - }); - -program - .parse(process.argv); + async run() { + await this.init(); + this.program.run(); + } +} diff --git a/src/program.ts b/src/program.ts new file mode 100644 index 0000000000..557bb6991e --- /dev/null +++ b/src/program.ts @@ -0,0 +1,81 @@ +import * as program from 'commander'; + +import { LoginCommand } from './commands/login.command'; +import { SyncCommand } from './commands/sync.command'; + +import { Main } from './main'; + +export class Program { + constructor(private main: Main) { } + + run() { + program + .version('1.0.0', '-v, --version'); + + program + .command('login ') + .description('Log into a Bitwarden user account.') + .option('-m, --method ', '2FA method.') + .option('-c, --code ', '2FA code.') + .action(async (email: string, password: string, cmd: program.Command) => { + const command = new LoginCommand(this.main.authService); + await command.run(email, password, cmd); + }); + + program + .command('logout') + .description('Log out of the current Bitwarden user account.') + .action((cmd) => { + console.log('Logging out...'); + }); + + program + .command('sync') + .description('Sync user\'s vault from server.') + .option('-f, --force', 'Force a full sync.') + .action(async (cmd) => { + console.log('Syncing...'); + const command = new SyncCommand(this.main.syncService); + await command.run(cmd); + }); + + program + .command('list ') + .description('List objects.') + .action((object, cmd) => { + console.log('Listing...'); + console.log(object); + }); + + program + .command('get ') + .description('Get an object.') + .action((object, id, cmd) => { + console.log('Getting...'); + console.log(object); + console.log(id); + }); + + program + .command('edit ') + .description('Edit an object.') + .action((object, id, cmd) => { + console.log('Editing...'); + console.log(object); + console.log(id); + }); + + program + .command('delete ') + .description('Delete an object.') + .action((object, id, cmd) => { + console.log('Deleting...'); + console.log(object); + console.log(id); + }); + + + program + .parse(process.argv); + } +} diff --git a/src/services/i18n.service.ts b/src/services/i18n.service.ts new file mode 100644 index 0000000000..8afe8baac4 --- /dev/null +++ b/src/services/i18n.service.ts @@ -0,0 +1,19 @@ +import * as fs from 'fs'; +import * as path from 'path'; + +import { I18nService as BaseI18nService } from 'jslib/services/i18n.service'; + +export class I18nService extends BaseI18nService { + constructor(systemLanguage: string, localesDirectory: string) { + super(systemLanguage, localesDirectory, (formattedLocale: string) => { + const filePath = path.join(__dirname, this.localesDirectory + '/' + formattedLocale + '/messages.json'); + const localesJson = fs.readFileSync(filePath, 'utf8'); + const locales = JSON.parse(localesJson.replace(/^\uFEFF/, '')); // strip the BOM + return Promise.resolve(locales); + }); + + this.supportedTranslationLocales = [ + 'en' + ]; + } +} diff --git a/src/services/nodePlatformUtils.service.ts b/src/services/nodePlatformUtils.service.ts index fcd56ab85c..b18c095ecc 100644 --- a/src/services/nodePlatformUtils.service.ts +++ b/src/services/nodePlatformUtils.service.ts @@ -50,7 +50,9 @@ export class NodePlatformUtilsService implements PlatformUtilsService { return Utils.getHostname(uriString); } - isViewOpen: () => false; + isViewOpen() { + return false; + } launchUri(uri: string, options?: any): void { }