move shared CLI items to jslib

This commit is contained in:
Kyle Spearrin 2019-03-15 22:34:59 -04:00
parent b7a07bb483
commit a3ba833f0d
32 changed files with 58 additions and 352 deletions

2
jslib

@ -1 +1 @@
Subproject commit 49e06e77c4913867fc468f7d9e0b2b1529c1d181
Subproject commit 13a160fb795a69bad1edbb3fc5fd5c7c15396e03

View File

@ -5,8 +5,8 @@ import { AuthService } from 'jslib/services/auth.service';
import { I18nService } from './services/i18n.service';
import { NodeEnvSecureStorageService } from './services/nodeEnvSecureStorage.service';
import { NodePlatformUtilsService } from './services/nodePlatformUtils.service';
import { NoopMessagingService } from './services/noopMessaging.service';
import { CliPlatformUtilsService } from 'jslib/cli/services/cliPlatformUtils.service';
import { AppIdService } from 'jslib/services/appId.service';
import { AuditService } from 'jslib/services/audit.service';
@ -23,6 +23,7 @@ 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 { NoopMessagingService } from 'jslib/services/noopMessaging.service';
import { PasswordGenerationService } from 'jslib/services/passwordGeneration.service';
import { SearchService } from 'jslib/services/search.service';
import { SettingsService } from 'jslib/services/settings.service';
@ -36,12 +37,15 @@ import { Program } from './program';
// Polyfills
(global as any).DOMParser = new jsdom.JSDOM().window.DOMParser;
// tslint:disable-next-line
const packageJson = require('../package.json');
export class Main {
messagingService: NoopMessagingService;
storageService: LowdbStorageService;
secureStorageService: NodeEnvSecureStorageService;
i18nService: I18nService;
platformUtilsService: NodePlatformUtilsService;
platformUtilsService: CliPlatformUtilsService;
constantsService: ConstantsService;
cryptoService: CryptoService;
tokenService: TokenService;
@ -81,7 +85,7 @@ export class Main {
}
this.i18nService = new I18nService('en', './locales');
this.platformUtilsService = new NodePlatformUtilsService();
this.platformUtilsService = new CliPlatformUtilsService('cli', packageJson);
this.cryptoFunctionService = new NodeCryptoFunctionService();
this.storageService = new LowdbStorageService(null, p, true);
this.secureStorageService = new NodeEnvSecureStorageService(this.storageService, () => this.cryptoService);

View File

@ -2,8 +2,8 @@ import * as program from 'commander';
import { EnvironmentService } from 'jslib/abstractions/environment.service';
import { Response } from '../models/response';
import { MessageResponse } from '../models/response/messageResponse';
import { Response } from 'jslib/cli/models/response';
import { MessageResponse } from 'jslib/cli/models/response/messageResponse';
export class ConfigCommand {
constructor(private environmentService: EnvironmentService) { }

View File

@ -10,7 +10,8 @@ import { UserService } from 'jslib/abstractions/user.service';
import { Cipher } from 'jslib/models/export/cipher';
import { Folder } from 'jslib/models/export/folder';
import { Response } from '../models/response';
import { Response } from 'jslib/cli/models/response';
import { CipherResponse } from '../models/response/cipherResponse';
import { FolderResponse } from '../models/response/folderResponse';

View File

@ -4,7 +4,7 @@ import { CipherService } from 'jslib/abstractions/cipher.service';
import { FolderService } from 'jslib/abstractions/folder.service';
import { UserService } from 'jslib/abstractions/user.service';
import { Response } from '../models/response';
import { Response } from 'jslib/cli/models/response';
export class DeleteCommand {
constructor(private cipherService: CipherService, private folderService: FolderService,

View File

@ -6,7 +6,8 @@ import { FolderService } from 'jslib/services/folder.service';
import { Cipher } from 'jslib/models/export/cipher';
import { Folder } from 'jslib/models/export/folder';
import { Response } from '../models/response';
import { Response } from 'jslib/cli/models/response';
import { CipherResponse } from '../models/response/cipherResponse';
import { FolderResponse } from '../models/response/folderResponse';

View File

@ -1,7 +1,7 @@
import * as program from 'commander';
import { Response } from '../models/response';
import { StringResponse } from '../models/response/stringResponse';
import { Response } from 'jslib/cli/models/response';
import { StringResponse } from 'jslib/cli/models/response/stringResponse';
import { CliUtils } from '../utils';

View File

@ -4,8 +4,8 @@ import * as inquirer from 'inquirer';
import { CryptoService } from 'jslib/abstractions/crypto.service';
import { ExportService } from 'jslib/abstractions/export.service';
import { Response } from '../models/response';
import { MessageResponse } from '../models/response/messageResponse';
import { Response } from 'jslib/cli/models/response';
import { MessageResponse } from 'jslib/cli/models/response/messageResponse';
import { CliUtils } from '../utils';

View File

@ -2,8 +2,8 @@ import * as program from 'commander';
import { PasswordGenerationService } from 'jslib/abstractions/passwordGeneration.service';
import { Response } from '../models/response';
import { StringResponse } from '../models/response/stringResponse';
import { Response } from 'jslib/cli/models/response';
import { StringResponse } from 'jslib/cli/models/response/stringResponse';
export class GenerateCommand {
constructor(private passwordGenerationService: PasswordGenerationService) { }

View File

@ -29,13 +29,14 @@ import { CipherView } from 'jslib/models/view/cipherView';
import { CollectionView } from 'jslib/models/view/collectionView';
import { FolderView } from 'jslib/models/view/folderView';
import { Response } from '../models/response';
import { Response } from 'jslib/cli/models/response';
import { MessageResponse } from 'jslib/cli/models/response/messageResponse';
import { StringResponse } from 'jslib/cli/models/response/stringResponse';
import { CipherResponse } from '../models/response/cipherResponse';
import { CollectionResponse } from '../models/response/collectionResponse';
import { FolderResponse } from '../models/response/folderResponse';
import { MessageResponse } from '../models/response/messageResponse';
import { OrganizationResponse } from '../models/response/organizationResponse';
import { StringResponse } from '../models/response/stringResponse';
import { TemplateResponse } from '../models/response/templateResponse';
import { CliUtils } from '../utils';

View File

@ -1,8 +1,8 @@
import * as program from 'commander';
import { ImportService } from 'jslib/abstractions/import.service';
import { Response } from '../models/response';
import { MessageResponse } from '../models/response/messageResponse';
import { Response } from 'jslib/cli/models/response';
import { MessageResponse } from 'jslib/cli/models/response/messageResponse';
import { CliUtils } from '../utils';

View File

@ -8,11 +8,12 @@ import { FolderService } from 'jslib/abstractions/folder.service';
import { SearchService } from 'jslib/abstractions/search.service';
import { UserService } from 'jslib/abstractions/user.service';
import { Response } from '../models/response';
import { Response } from 'jslib/cli/models/response';
import { ListResponse } from 'jslib/cli/models/response/listResponse';
import { CipherResponse } from '../models/response/cipherResponse';
import { CollectionResponse } from '../models/response/collectionResponse';
import { FolderResponse } from '../models/response/folderResponse';
import { ListResponse } from '../models/response/listResponse';
import { OrganizationResponse } from '../models/response/organizationResponse';
import { CliUtils } from '../utils';

View File

@ -2,8 +2,8 @@ import * as program from 'commander';
import { LockService } from 'jslib/abstractions/lock.service';
import { Response } from '../models/response';
import { MessageResponse } from '../models/response/messageResponse';
import { Response } from 'jslib/cli/models/response';
import { MessageResponse } from 'jslib/cli/models/response/messageResponse';
export class LockCommand {
constructor(private lockService: LockService) { }

View File

@ -11,8 +11,8 @@ import { AuthService } from 'jslib/abstractions/auth.service';
import { CryptoFunctionService } from 'jslib/abstractions/cryptoFunction.service';
import { SyncService } from 'jslib/abstractions/sync.service';
import { Response } from '../models/response';
import { MessageResponse } from '../models/response/messageResponse';
import { Response } from 'jslib/cli/models/response';
import { MessageResponse } from 'jslib/cli/models/response/messageResponse';
import { Utils } from 'jslib/misc/utils';

View File

@ -2,8 +2,8 @@ import * as program from 'commander';
import { AuthService } from 'jslib/abstractions/auth.service';
import { Response } from '../models/response';
import { MessageResponse } from '../models/response/messageResponse';
import { Response } from 'jslib/cli/models/response';
import { MessageResponse } from 'jslib/cli/models/response/messageResponse';
export class LogoutCommand {
constructor(private authService: AuthService, private logoutCallback: () => Promise<void>) { }

View File

@ -2,7 +2,8 @@ import * as program from 'commander';
import { CipherService } from 'jslib/abstractions/cipher.service';
import { Response } from '../models/response';
import { Response } from 'jslib/cli/models/response';
import { CipherResponse } from '../models/response/cipherResponse';
import { CliUtils } from '../utils';

View File

@ -2,9 +2,9 @@ import * as program from 'commander';
import { SyncService } from 'jslib/abstractions/sync.service';
import { Response } from '../models/response';
import { MessageResponse } from '../models/response/messageResponse';
import { StringResponse } from '../models/response/stringResponse';
import { Response } from 'jslib/cli/models/response';
import { MessageResponse } from 'jslib/cli/models/response/messageResponse';
import { StringResponse } from 'jslib/cli/models/response/stringResponse';
export class SyncCommand {
constructor(private syncService: SyncService) { }

View File

@ -5,8 +5,8 @@ import { CryptoService } from 'jslib/abstractions/crypto.service';
import { CryptoFunctionService } from 'jslib/abstractions/cryptoFunction.service';
import { UserService } from 'jslib/abstractions/user.service';
import { Response } from '../models/response';
import { MessageResponse } from '../models/response/messageResponse';
import { Response } from 'jslib/cli/models/response';
import { MessageResponse } from 'jslib/cli/models/response/messageResponse';
import { Utils } from 'jslib/misc/utils';

View File

@ -1,81 +0,0 @@
import * as program from 'commander';
import * as fetch from 'node-fetch';
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
import { Response } from '../models/response';
import { MessageResponse } from '../models/response/messageResponse';
export class UpdateCommand {
inPkg: boolean = false;
constructor(private platformUtilsService: PlatformUtilsService) {
this.inPkg = !!(process as any).pkg;
}
async run(cmd: program.Command): Promise<Response> {
const currentVersion = this.platformUtilsService.getApplicationVersion();
const response = await fetch.default('https://api.github.com/repos/bitwarden/cli/releases/latest');
if (response.status === 200) {
const responseJson = await response.json();
const res = new MessageResponse(null, null);
const tagName: string = responseJson.tag_name;
if (tagName === ('v' + currentVersion)) {
res.title = 'No update available.';
res.noColor = true;
return Response.success(res);
}
let downloadUrl: string = null;
if (responseJson.assets != null) {
for (const a of responseJson.assets) {
const download: string = a.browser_download_url;
if (download == null) {
continue;
}
if (download.indexOf('.zip') === -1) {
continue;
}
if (process.platform === 'win32' && download.indexOf('bw-windows') > -1) {
downloadUrl = download;
break;
} else if (process.platform === 'darwin' && download.indexOf('bw-macos') > -1) {
downloadUrl = download;
break;
} else if (process.platform === 'linux' && download.indexOf('bw-linux') > -1) {
downloadUrl = download;
break;
}
}
}
res.title = 'A new version is available: ' + tagName;
if (downloadUrl == null) {
downloadUrl = 'https://github.com/bitwarden/cli/releases';
} else {
res.raw = downloadUrl;
}
res.message = '';
if (responseJson.body != null && responseJson.body !== '') {
res.message = responseJson.body + '\n\n';
}
res.message += 'You can download this update at ' + downloadUrl;
if (this.inPkg) {
res.message += '\n\nIf you installed this CLI through a package manager ' +
'you should probably update using its update command instead.';
} else {
res.message += '\n\nIf you installed this CLI through NPM ' +
'you should update using `npm install -g @bitwarden/cli`';
}
return Response.success(res);
} else {
return Response.error('Error contacting update API: ' + response.status);
}
}
}

View File

@ -1,43 +0,0 @@
import { BaseResponse } from './response/baseResponse';
export class Response {
static error(error: any): Response {
const res = new Response();
res.success = false;
if (typeof (error) === 'string') {
res.message = error;
} else {
res.message = error.message != null ? error.message : error.toString();
}
return res;
}
static notFound(): Response {
return Response.error('Not found.');
}
static badRequest(message: string): Response {
return Response.error(message);
}
static multipleResults(ids: string[]): Response {
let msg = 'More than one result was found. Try getting a specific object by `id` instead. ' +
'The following objects were found:';
ids.forEach((id) => {
msg += '\n' + id;
});
return Response.error(msg);
}
static success(data?: BaseResponse): Response {
const res = new Response();
res.success = true;
res.data = data;
return res;
}
success: boolean;
message: string;
errorCode: number;
data: BaseResponse;
}

View File

@ -1,3 +0,0 @@
export interface BaseResponse {
object: string;
}

View File

@ -1,8 +1,9 @@
import { CipherWithIds } from 'jslib/models/export/cipherWithIds';
import { CipherView } from 'jslib/models/view/cipherView';
import { BaseResponse } from 'jslib/cli/models/response/baseResponse';
import { AttachmentResponse } from './attachmentResponse';
import { BaseResponse } from './baseResponse';
import { LoginResponse } from './loginResponse';
import { PasswordHistoryResponse } from './passwordHistoryResponse';

View File

@ -1,7 +1,7 @@
import { CollectionWithId } from 'jslib/models/export/collectionWithId';
import { CollectionView } from 'jslib/models/view/collectionView';
import { BaseResponse } from './baseResponse';
import { BaseResponse } from 'jslib/cli/models/response/baseResponse';
export class CollectionResponse extends CollectionWithId implements BaseResponse {
object: string;

View File

@ -1,7 +1,7 @@
import { FolderWithId } from 'jslib/models/export/folderWithId';
import { FolderView } from 'jslib/models/view/folderView';
import { BaseResponse } from './baseResponse';
import { BaseResponse } from 'jslib/cli/models/response/baseResponse';
export class FolderResponse extends FolderWithId implements BaseResponse {
object: string;

View File

@ -1,11 +0,0 @@
import { BaseResponse } from './baseResponse';
export class ListResponse implements BaseResponse {
object: string;
data: BaseResponse[];
constructor(data: BaseResponse[]) {
this.object = 'list';
this.data = data;
}
}

View File

@ -1,15 +0,0 @@
import { BaseResponse } from './baseResponse';
export class MessageResponse implements BaseResponse {
object: string;
title: string;
message: string;
raw: string;
noColor = false;
constructor(title: string, message: string) {
this.object = 'message';
this.title = title;
this.message = message;
}
}

View File

@ -1,4 +1,4 @@
import { BaseResponse } from './baseResponse';
import { BaseResponse } from 'jslib/cli/models/response/baseResponse';
import { Organization } from 'jslib/models/domain/organization';

View File

@ -1,11 +0,0 @@
import { BaseResponse } from './baseResponse';
export class StringResponse implements BaseResponse {
object: string;
data: string;
constructor(data: string) {
this.object = 'string';
this.data = data;
}
}

View File

@ -1,4 +1,4 @@
import { BaseResponse } from './baseResponse';
import { BaseResponse } from 'jslib/cli/models/response/baseResponse';
export class TemplateResponse implements BaseResponse {
object: string;

View File

@ -19,12 +19,14 @@ import { LogoutCommand } from './commands/logout.command';
import { ShareCommand } from './commands/share.command';
import { SyncCommand } from './commands/sync.command';
import { UnlockCommand } from './commands/unlock.command';
import { UpdateCommand } from './commands/update.command';
import { Response } from './models/response';
import { ListResponse } from './models/response/listResponse';
import { MessageResponse } from './models/response/messageResponse';
import { StringResponse } from './models/response/stringResponse';
import { UpdateCommand } from 'jslib/cli/commands/update.command';
import { Response } from 'jslib/cli/models/response';
import { ListResponse } from 'jslib/cli/models/response/listResponse';
import { MessageResponse } from 'jslib/cli/models/response/messageResponse';
import { StringResponse } from 'jslib/cli/models/response/stringResponse';
import { TemplateResponse } from './models/response/templateResponse';
import { CliUtils } from './utils';
@ -553,7 +555,7 @@ export class Program {
writeLn('', true);
})
.action(async (cmd) => {
const command = new UpdateCommand(this.main.platformUtilsService);
const command = new UpdateCommand(this.main.platformUtilsService, 'cli', 'bw');
const response = await command.run(cmd);
this.processResponse(response);
});

View File

@ -1,135 +0,0 @@
import { DeviceType } from 'jslib/enums/deviceType';
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
// tslint:disable-next-line
const pjson = require('../../package.json');
export class NodePlatformUtilsService implements PlatformUtilsService {
identityClientId: string;
private deviceCache: DeviceType = null;
constructor() {
this.identityClientId = 'cli';
}
getDevice(): DeviceType {
if (!this.deviceCache) {
switch (process.platform) {
case 'win32':
this.deviceCache = DeviceType.WindowsDesktop;
break;
case 'darwin':
this.deviceCache = DeviceType.MacOsDesktop;
break;
case 'linux':
default:
this.deviceCache = DeviceType.LinuxDesktop;
break;
}
}
return this.deviceCache;
}
getDeviceString(): string {
const device = DeviceType[this.getDevice()].toLowerCase();
return device.replace('desktop', '');
}
isFirefox() {
return false;
}
isChrome() {
return false;
}
isEdge() {
return false;
}
isOpera() {
return false;
}
isVivaldi() {
return false;
}
isSafari() {
return false;
}
isIE() {
return false;
}
isMacAppStore() {
return false;
}
analyticsId() {
return null as string;
}
isViewOpen() {
return false;
}
lockTimeout(): number {
return null;
}
launchUri(uri: string, options?: any): void {
throw new Error('Not implemented.');
}
saveFile(win: Window, blobData: any, blobOptions: any, fileName: string): void {
throw new Error('Not implemented.');
}
getApplicationVersion(): string {
return pjson.version;
}
supportsU2f(win: Window) {
return false;
}
supportsDuo(): boolean {
return false;
}
showToast(type: 'error' | 'success' | 'warning' | 'info', title: string, text: string | string[],
options?: any): void {
throw new Error('Not implemented.');
}
showDialog(text: string, title?: string, confirmText?: string, cancelText?: string, type?: string):
Promise<boolean> {
throw new Error('Not implemented.');
}
eventTrack(action: string, label?: string, options?: any) {
throw new Error('Not implemented.');
}
isDev(): boolean {
return process.env.BWCLI_ENV === 'development';
}
isSelfHost(): boolean {
return false;
}
copyToClipboard(text: string, options?: any): void {
throw new Error('Not implemented.');
}
readFromClipboard(options?: any): Promise<string> {
throw new Error('Not implemented.');
}
}

View File

@ -1,7 +0,0 @@
import { MessagingService } from 'jslib/abstractions/messaging.service';
export class NoopMessagingService implements MessagingService {
send(subscriber: string, arg: any = {}) {
// Do nothing...
}
}