1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-21 02:33:46 +00:00
Files
browser/apps/cli/src/commands/update.command.ts
Matt Gibson 78248db590 Platform/pm 19/platform team file moves (#5460)
* Rename service-factory folder

* Move cryptographic service factories

* Move crypto models

* Move crypto services

* Move domain base class

* Platform code owners

* Move desktop log services

* Move log files

* Establish component library ownership

* Move background listeners

* Move background background

* Move localization to Platform

* Move browser alarms to Platform

* Move browser state to Platform

* Move CLI state to Platform

* Move Desktop native concerns to Platform

* Move flag and misc to Platform

* Lint fixes

* Move electron state to platform

* Move web state to Platform

* Move lib state to Platform

* Fix broken tests

* Rename interface to idiomatic TS

* `npm run prettier` 🤖

* Resolve review feedback

* Set platform as owners of web core and shared

* Expand moved services

* Fix test types

---------

Co-authored-by: Hinton <hinton@users.noreply.github.com>
2023-06-06 15:34:53 -05:00

110 lines
3.3 KiB
TypeScript

import * as fetch from "node-fetch";
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
import { Response } from "../models/response";
import { MessageResponse } from "../models/response/message.response";
const CLIENTS_RELEASE_LIST_ENDPOINT = "https://api.github.com/repos/bitwarden/clients/releases";
const DEFAULT_DOWNLOAD_URL = "https://github.com/bitwarden/clients/releases";
const UPDATE_COMMAND = "npm install -g @bitwarden/cli";
export class UpdateCommand {
inPkg = false;
constructor(private platformUtilsService: PlatformUtilsService) {
this.inPkg = !!(process as any).pkg;
}
async run(): Promise<Response> {
const response = await fetch.default(CLIENTS_RELEASE_LIST_ENDPOINT);
if (response.status !== 200) {
return Response.error("Error contacting update API: " + response.status);
}
const responseJson = await response.json();
const cliRelease = responseJson.find((r: any) => r.tag_name.includes("cli"));
if (cliRelease === undefined || cliRelease === null) {
return Response.error("Could not find latest CLI version.");
}
const currentVersion = await this.platformUtilsService.getApplicationVersion();
if (cliRelease.tag_name === "cli-v" + currentVersion) {
const response = new MessageResponse(null, null);
response.title = "No update available.";
response.noColor = true;
return Response.success(response);
}
const res = this.getFoundUpdateResponse(cliRelease);
return Response.success(res);
}
private getFoundUpdateResponse(release: any) {
const downloadUrl = this.getDownloadUrl(release.assets);
const response = new MessageResponse(null, null);
response.title = "A new version is available: " + release.tag_name;
response.raw = downloadUrl;
response.message = this.getMessage(release, downloadUrl);
return response;
}
private getMessage(release: any, downloadUrl: string) {
let message = "";
if (release.body != null && release.body !== "") {
message = release.body + "\n\n";
}
message += "You can download this update at " + downloadUrl;
if (this.inPkg) {
message +=
"\n\nIf you installed this CLI through a package manager " +
"you should probably update using its update command instead.";
} else {
message +=
"\n\nIf you installed this CLI through NPM " +
"you should update using `" +
UPDATE_COMMAND +
"`";
}
return message;
}
private getDownloadUrl(assets: any) {
if (assets == null) {
return DEFAULT_DOWNLOAD_URL;
}
let downloadUrl: string = DEFAULT_DOWNLOAD_URL;
for (const a of 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;
}
}
return downloadUrl;
}
}