From 276affd8ebcfea71d510541afd9545f287fdebd3 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Tue, 22 May 2018 17:36:54 -0400 Subject: [PATCH] update self --- package-lock.json | 14 ++++++++++++++ package.json | 2 ++ src/commands/update.command.ts | 27 ++++++++++++++++++++++++++- src/program.ts | 4 +++- 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 82c77b1de8d..65d8e04023f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4,6 +4,15 @@ "lockfileVersion": 1, "requires": true, "dependencies": { + "@types/adm-zip": { + "version": "0.4.31", + "resolved": "https://registry.npmjs.org/@types/adm-zip/-/adm-zip-0.4.31.tgz", + "integrity": "sha1-ozdrn6j0xunAeMF20t8srreTneM=", + "dev": true, + "requires": { + "@types/node": "10.0.8" + } + }, "@types/commander": { "version": "2.12.2", "resolved": "https://registry.npmjs.org/@types/commander/-/commander-2.12.2.tgz", @@ -108,6 +117,11 @@ "acorn": "5.5.3" } }, + "adm-zip": { + "version": "0.4.11", + "resolved": "https://registry.npmjs.org/adm-zip/-/adm-zip-0.4.11.tgz", + "integrity": "sha512-L8vcjDTCOIJk7wFvmlEUN7AsSb8T+2JrdP7KINBjzr24TJ5Mwj590sLu3BC7zNZowvJWa/JtPmD8eJCzdtDWjA==" + }, "ajv": { "version": "6.5.0", "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.5.0.tgz", diff --git a/package.json b/package.json index 6765f16767e..5ab87333ce6 100644 --- a/package.json +++ b/package.json @@ -44,6 +44,7 @@ "assets": "./build/**/*" }, "devDependencies": { + "@types/adm-zip": "^0.4.31", "@types/commander": "^2.12.2", "@types/form-data": "^2.2.1", "@types/lowdb": "^1.0.1", @@ -64,6 +65,7 @@ "webpack": "^3.10.0" }, "dependencies": { + "adm-zip": "0.4.11", "chalk": "2.4.1", "commander": "2.15.1", "form-data": "2.3.2", diff --git a/src/commands/update.command.ts b/src/commands/update.command.ts index ce6f34b3f8a..e4182b6c356 100644 --- a/src/commands/update.command.ts +++ b/src/commands/update.command.ts @@ -1,3 +1,4 @@ +import * as AdmZip from 'adm-zip'; import * as program from 'commander'; import * as fetch from 'node-fetch'; @@ -49,13 +50,37 @@ export class UpdateCommand { } } + if (cmd.self || false) { + const zipResponse = await fetch.default(downloadUrl); + if (zipResponse.status === 200) { + try { + const zipBuffer = await zipResponse.buffer(); + const zip = new AdmZip(zipBuffer); + zip.extractAllTo(__dirname, true); + res.title = 'Updated self to ' + tagName + '.'; + if (responseJson.body != null && responseJson.body !== '') { + res.message = responseJson.body; + } + return Response.success(res); + } catch { + return Response.error('Error extracting update to ' + __dirname); + } + } else { + return Response.error('Error downloading update: ' + zipResponse.status); + } + } + res.title = 'A new version is available: ' + tagName; if (downloadUrl == null) { downloadUrl = 'https://github.com/bitwarden/cli/releases'; } else { res.raw = downloadUrl; } - res.message = 'You can download this update at: ' + downloadUrl + '\n' + + res.message = ''; + if (responseJson.body != null && responseJson.body !== '') { + res.message = responseJson.body + '\n\n'; + } + res.message += 'You can download this update at: ' + downloadUrl + '\n' + 'If you installed this CLI through a package manager ' + 'you should probably update using its update command instead.'; return Response.success(res); diff --git a/src/program.ts b/src/program.ts index 13ea37d49c0..f767adc4d59 100644 --- a/src/program.ts +++ b/src/program.ts @@ -466,6 +466,7 @@ export class Program { program .command('update') .description('Check for updates.') + .option('--self', 'Attempt to automatically update self.') .on('--help', () => { writeLn('\n Notes:'); writeLn(''); @@ -477,9 +478,10 @@ export class Program { writeLn(''); writeLn(' bw update'); writeLn(' bw update --raw'); + writeLn(' bw update --self'); writeLn('', true); }) - .action(async (object, id, cmd) => { + .action(async (cmd) => { const command = new UpdateCommand(this.main.platformUtilsService); const response = await command.run(cmd); this.processResponse(response);