move lowdb to jslib

This commit is contained in:
Kyle Spearrin 2018-05-31 09:08:54 -04:00
parent 661900d038
commit a9e3d31b07
4 changed files with 20 additions and 64 deletions

2
jslib

@ -1 +1 @@
Subproject commit 43ac05d1fc89a5132bd2eb632a85e608e2548252
Subproject commit f618c0b5ee7bcf3cc351721611fabf57dd785211

View File

@ -1,7 +1,8 @@
import * as path from 'path';
import { AuthService } from 'jslib/services/auth.service';
import { I18nService } from './services/i18n.service';
import { LowdbStorageService } from './services/lowdbStorage.service';
import { NodeEnvSecureStorageService } from './services/nodeEnvSecureStorage.service';
import { NodePlatformUtilsService } from './services/nodePlatformUtils.service';
import { NoopMessagingService } from './services/noopMessaging.service';
@ -17,6 +18,7 @@ import { EnvironmentService } from 'jslib/services/environment.service';
import { ExportService } from 'jslib/services/export.service';
import { FolderService } from 'jslib/services/folder.service';
import { LockService } from 'jslib/services/lock.service';
import { LowdbStorageService } from 'jslib/services/lowdbStorage.service';
import { NodeApiService } from 'jslib/services/nodeApi.service';
import { NodeCryptoFunctionService } from 'jslib/services/nodeCryptoFunction.service';
import { PasswordGenerationService } from 'jslib/services/passwordGeneration.service';
@ -57,10 +59,21 @@ export class Main {
program: Program;
constructor() {
let p = null;
if (process.env.BITWARDENCLI_APPDATA_DIR) {
p = path.resolve(process.env.BITWARDENCLI_APPDATA_DIR);
} else if (process.platform === 'darwin') {
p = path.join(process.env.HOME, 'Library/Application Support/Bitwarden CLI');
} else if (process.platform === 'win32') {
p = path.join(process.env.APPDATA, 'Bitwarden CLI');
} else {
p = path.join(process.env.HOME, '.config/Bitwarden CLI');
}
this.i18nService = new I18nService('en', './locales');
this.platformUtilsService = new NodePlatformUtilsService();
this.cryptoFunctionService = new NodeCryptoFunctionService();
this.storageService = new LowdbStorageService('Bitwarden CLI');
this.storageService = new LowdbStorageService(null, p);
this.secureStorageService = new NodeEnvSecureStorageService(this.storageService, () => this.cryptoService);
this.cryptoService = new CryptoService(this.storageService, this.secureStorageService,
this.cryptoFunctionService);
@ -115,6 +128,7 @@ export class Main {
}
private async init() {
this.storageService.init();
this.containerService.attachToWindow(global);
await this.environmentService.setUrlsFromStorage();
const locale = await this.storageService.get<string>(ConstantsService.localeKey);

View File

@ -1,48 +0,0 @@
import * as fs from 'fs';
import * as lowdb from 'lowdb';
import * as FileSync from 'lowdb/adapters/FileSync';
import * as path from 'path';
import { StorageService } from 'jslib/abstractions/storage.service';
import { Utils } from 'jslib/misc/utils';
import { CliUtils } from '../utils';
export class LowdbStorageService implements StorageService {
private db: lowdb.LowdbSync<any>;
constructor(appDirName: string) {
let p = null;
if (process.env.BITWARDENCLI_APPDATA_DIR) {
p = path.resolve(process.env.BITWARDENCLI_APPDATA_DIR);
} else if (process.platform === 'darwin') {
p = path.join(process.env.HOME, 'Library/Application Support', appDirName);
} else if (process.platform === 'win32') {
p = path.join(process.env.APPDATA, appDirName);
} else {
p = path.join(process.env.HOME, '.config', appDirName);
}
if (!fs.existsSync(p)) {
CliUtils.mkdirpSync(p, 755);
}
p = path.join(p, 'data.json');
const adapter = new FileSync(p);
this.db = lowdb(adapter);
}
get<T>(key: string): Promise<T> {
const val = this.db.get(key).value();
return Promise.resolve(val as T);
}
save(key: string, obj: any): Promise<any> {
this.db.set(key, obj).write();
return Promise.resolve();
}
remove(key: string): Promise<any> {
this.db.unset(key).write();
return Promise.resolve();
}
}

View File

@ -6,6 +6,8 @@ import { CipherView } from 'jslib/models/view/cipherView';
import { CollectionView } from 'jslib/models/view/collectionView';
import { FolderView } from 'jslib/models/view/folderView';
import { NodeUtils } from 'jslib/misc/nodeUtils';
export class CliUtils {
static saveFile(data: string | Buffer, output: string, defaultFileName: string) {
let p: string = null;
@ -30,7 +32,7 @@ export class CliUtils {
if (mkdir) {
const dir = p.substring(0, p.lastIndexOf(path.sep));
if (!fs.existsSync(dir)) {
CliUtils.mkdirpSync(dir, 755);
NodeUtils.mkdirpSync(dir, 755);
}
}
@ -44,18 +46,6 @@ export class CliUtils {
});
}
static mkdirpSync(targetDir: string, mode = 755, relative = false, relativeDir: string = null) {
const initialDir = path.isAbsolute(targetDir) ? path.sep : '';
const baseDir = relative ? (relativeDir != null ? relativeDir : __dirname) : '.';
targetDir.split(path.sep).reduce((parentDir, childDir) => {
const dir = path.resolve(baseDir, parentDir, childDir);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, mode);
}
return dir;
}, initialDir);
}
static readStdin(): Promise<string> {
return new Promise((resolve, reject) => {
let input: string = '';