1
1
import { getInstance } from "../base" ;
2
2
3
3
import { Octokit } from "octokit" ;
4
- import fetch from 'node-fetch' ;
5
4
6
5
import { Config } from "../config" ;
7
6
import path from "path" ;
8
- import { OutputChannel , commands , window } from "vscode" ;
7
+ import { OutputChannel , extensions , window } from "vscode" ;
9
8
10
- import { writeFile , unlink } from "fs/promises" ;
11
- import os from "os" ;
12
-
13
- const octokit = new Octokit ( ) ;
14
-
15
- // During development, you can set the SERVER_VERSION in .vscode/launch.json
16
- // Otherwise, fall back to the working version
17
- const SERVER_VERSION = process . env [ `SERVER_VERSION` ] || `v1.2.0` ;
9
+ import { stat } from "fs/promises" ;
10
+ import { SERVER_VERSION_FILE } from "./SCVersion" ;
18
11
19
12
const ExecutablePathDir = `$HOME/.vscode/` ;
20
13
@@ -90,79 +83,54 @@ export class ServerComponent {
90
83
*/
91
84
public static async checkForUpdate ( ) : Promise < UpdateStatus > {
92
85
let updateResult : UpdateStatus = UpdateStatus . NONE_AVAILABLE ;
86
+
87
+ const extensionPath = extensions . getExtension ( `halcyontechltd.vscode-db2i` ) . extensionPath ;
93
88
const instance = getInstance ( ) ;
94
89
const connection = instance . getConnection ( ) ;
95
90
96
- const owner = `ThePrez` ;
97
- const repo = `CodeForIBMiServer` ;
98
-
99
91
try {
100
- const result = await octokit . request ( `GET /repos/{owner}/{repo}/releases/tags/${ SERVER_VERSION } ` , {
101
- owner,
102
- repo,
103
- headers : {
104
- 'X-GitHub-Api-Version' : '2022-11-28'
105
- }
106
- } ) ;
92
+ const assetPath = path . join ( extensionPath , `dist` , SERVER_VERSION_FILE ) ;
93
+ const assetExists = await exists ( assetPath ) ;
107
94
108
- ServerComponent . writeOutput ( JSON . stringify ( result ) ) ;
95
+ ServerComponent . writeOutput ( JSON . stringify ( { assetPath , assetExists } ) ) ;
109
96
110
- const newAsset = result . data . assets . find ( asset => asset . name . endsWith ( `.jar` ) ) ;
111
-
112
- if ( newAsset ) {
113
- const url = newAsset . browser_download_url ;
114
- const basename = newAsset . name ;
97
+ if ( assetExists ) {
98
+ const basename = SERVER_VERSION_FILE ;
115
99
const lastInstalledName = Config . getServerComponentName ( ) ;
116
100
117
- if ( lastInstalledName !== basename || this . installed === false ) {
118
- const updateQuestion = await window . showInformationMessage (
119
- `Database Extension update required` ,
120
- {
121
- modal : true ,
122
- detail : `An update to the database server component is required: ${ basename } `
123
- } ,
124
- `Update`
125
- ) ;
126
-
127
- if ( updateQuestion === `Update` ) {
128
- // This means we're currently running a different version,
129
- // or maybe not at all (currentlyInstalled can be undefined)
130
-
131
- // First, we need their home directory
132
- const commandResult = await connection . sendCommand ( {
133
- command : `echo ${ ExecutablePathDir } `
134
- } ) ;
135
-
136
- if ( commandResult . code === 0 && commandResult . stderr === `` ) {
137
- const remotePath = path . posix . join ( commandResult . stdout , basename )
101
+ ServerComponent . writeOutput ( JSON . stringify ( { basename, lastInstalledName} ) ) ;
138
102
139
- const tempFile = path . join ( os . tmpdir ( ) , basename ) ;
103
+ if ( lastInstalledName !== basename || this . installed === false ) {
104
+ // This means we're currently running a different version,
105
+ // or maybe not at all (currentlyInstalled can be undefined)
140
106
141
- await downloadFile ( url , tempFile ) ;
107
+ // First, we need their home directory
108
+ const commandResult = await connection . sendCommand ( {
109
+ command : `echo ${ ExecutablePathDir } `
110
+ } ) ;
142
111
143
- await connection . uploadFiles ( [ { local : tempFile , remote : remotePath } ] ) ;
112
+ if ( commandResult . code === 0 && commandResult . stderr === `` ) {
113
+ const remotePath = path . posix . join ( commandResult . stdout , basename ) ;
144
114
145
- // Clean up the temp file
146
- unlink ( tempFile ) ;
115
+ ServerComponent . writeOutput ( JSON . stringify ( { remotePath, ExecutablePathDir} ) ) ;
147
116
148
- await Config . setServerComponentName ( basename ) ;
117
+ await connection . uploadFiles ( [ { local : assetPath , remote : remotePath } ] ) ;
149
118
150
- window . showInformationMessage ( `Db2 for IBM i extension server component has been updated!` ) ;
151
- this . installed = true ;
152
- updateResult = UpdateStatus . JUST_UPDATED ;
153
-
154
- } else {
155
- updateResult = UpdateStatus . FAILED ;
119
+ await Config . setServerComponentName ( basename ) ;
156
120
157
- this . writeOutput ( JSON . stringify ( commandResult ) ) ;
158
- window . showErrorMessage ( `Something went really wrong when trying to fetch your home directory.` ) . then ( chosen => {
159
- if ( chosen === `Show` ) {
160
- this . outputChannel . show ( ) ;
161
- }
162
- } ) ;
163
- }
121
+ window . showInformationMessage ( `Db2 for IBM i extension server component has been updated!` ) ;
122
+ this . installed = true ;
123
+ updateResult = UpdateStatus . JUST_UPDATED ;
124
+
164
125
} else {
165
- updateResult = UpdateStatus . DECLINED_UPDATE ;
126
+ updateResult = UpdateStatus . FAILED ;
127
+
128
+ this . writeOutput ( JSON . stringify ( commandResult ) ) ;
129
+ window . showErrorMessage ( `Something went really wrong when trying to fetch your home directory.` ) . then ( chosen => {
130
+ if ( chosen === `Show` ) {
131
+ this . outputChannel . show ( ) ;
132
+ }
133
+ } ) ;
166
134
}
167
135
168
136
} else {
@@ -172,9 +140,10 @@ export class ServerComponent {
172
140
173
141
} else {
174
142
// Uh oh. A release was made by there's no jar file??
175
- ServerComponent . writeOutput ( 'Unable to get file name from server component release' ) ;
143
+ ServerComponent . writeOutput ( 'Unable to get file name from server component release. ' ) ;
176
144
updateResult = UpdateStatus . NONE_AVAILABLE ;
177
145
}
146
+
178
147
} catch ( e ) {
179
148
updateResult = UpdateStatus . FAILED ;
180
149
ServerComponent . writeOutput ( JSON . stringify ( e ) ) ;
@@ -191,8 +160,11 @@ export class ServerComponent {
191
160
}
192
161
}
193
162
194
- function downloadFile ( url , outputPath ) {
195
- return fetch ( url )
196
- . then ( x => x . arrayBuffer ( ) )
197
- . then ( x => writeFile ( outputPath , Buffer . from ( x ) ) ) ;
198
- }
163
+ async function exists ( path : string ) {
164
+ try {
165
+ await stat ( path ) ;
166
+ return true ;
167
+ } catch ( e ) {
168
+ return false ;
169
+ }
170
+ }
0 commit comments