@@ -13,7 +13,7 @@ import treeKill from 'tree-kill';
13
13
import describeError from '../src/logging/describeError' ;
14
14
import telemetry from '../src/telemetry/telemetry' ;
15
15
import { isDevelopment } from '../src/utils/environment' ;
16
- import { versionToInstall } from './moduleVersion' ;
16
+ import { coreVersionsToInstall , versionToInstall } from './moduleVersion' ;
17
17
import { getNrfutilLogger } from './nrfutilLogger' ;
18
18
import {
19
19
BackgroundTask ,
@@ -40,18 +40,31 @@ const parseJsonBuffers = <T>(data: Buffer): T[] | undefined => {
40
40
}
41
41
} ;
42
42
43
- const nrfutilSandboxFolder =
44
- process . platform === 'darwin' && process . arch !== 'x64'
45
- ? path . join ( 'nrfutil-sandboxes' , process . arch )
46
- : 'nrfutil-sandboxes' ;
47
-
48
- const prepareEnv = ( baseDir : string , module : string , version : string ) => {
43
+ const nrfutilSandboxPathSegments = (
44
+ baseDir : string ,
45
+ module : string ,
46
+ version : string ,
47
+ coreVersion ?: string
48
+ ) => [
49
+ baseDir ,
50
+ 'nrfutil-sandboxes' ,
51
+ ...( process . platform === 'darwin' && process . arch !== 'x64'
52
+ ? [ process . arch ]
53
+ : [ ] ) ,
54
+ ...( coreVersion != null ? [ coreVersion ] : [ ] ) ,
55
+ module ,
56
+ version ,
57
+ ] ;
58
+
59
+ const prepareEnv = (
60
+ baseDir : string ,
61
+ module : string ,
62
+ version : string ,
63
+ coreVersion ?: string
64
+ ) => {
49
65
const env = { ...process . env } ;
50
66
env . NRFUTIL_HOME = path . join (
51
- baseDir ,
52
- nrfutilSandboxFolder ,
53
- module ,
54
- version
67
+ ...nrfutilSandboxPathSegments ( baseDir , module , version , coreVersion )
55
68
) ;
56
69
fs . mkdirSync ( env . NRFUTIL_HOME , { recursive : true } ) ;
57
70
@@ -124,16 +137,25 @@ export class NrfutilSandbox {
124
137
baseDir : string ;
125
138
module : string ;
126
139
version : string ;
140
+ coreVersion : string | undefined ; // Must only be undefined when the launcher creates a sandbox for a legacy app, which does not specify the required core version
127
141
onLoggingHandlers : ( ( logging : LogMessage , pid ?: number ) => void ) [ ] = [ ] ;
128
142
logLevel : LogLevel = isDevelopment ? 'error' : 'off' ;
129
143
env : ReturnType < typeof prepareEnv > ;
130
144
131
- constructor ( baseDir : string , module : string , version : string ) {
145
+ readonly CORE_VERSION_FOR_LEGACY_APPS = '8.0.0' ;
146
+
147
+ constructor (
148
+ baseDir : string ,
149
+ module : string ,
150
+ version : string ,
151
+ coreVersion ?: string
152
+ ) {
132
153
this . baseDir = baseDir ;
133
154
this . module = module ;
134
155
this . version = version ;
156
+ this . coreVersion = coreVersion ;
135
157
136
- this . env = prepareEnv ( baseDir , module , version ) ;
158
+ this . env = prepareEnv ( baseDir , module , version , coreVersion ) ;
137
159
}
138
160
139
161
private processLoggingData = ( data : NrfutilJson , pid ?: number ) => {
@@ -173,10 +195,12 @@ export class NrfutilSandbox {
173
195
if (
174
196
fs . existsSync (
175
197
path . join (
176
- this . baseDir ,
177
- nrfutilSandboxFolder ,
178
- this . module ,
179
- this . version ,
198
+ ...nrfutilSandboxPathSegments (
199
+ this . baseDir ,
200
+ this . module ,
201
+ this . version ,
202
+ this . coreVersion
203
+ ) ,
180
204
'bin' ,
181
205
`nrfutil-${ this . module } ${
182
206
os . platform ( ) === 'win32' ? '.exe' : ''
@@ -201,15 +225,8 @@ export class NrfutilSandbox {
201
225
force : true ,
202
226
} ) ;
203
227
}
204
- await this . updateNrfUtilCore ( ) ;
205
- await this . spawnNrfutil (
206
- 'install' ,
207
- [ `${ this . module } =${ this . version } ` , '--force' ] ,
208
- onProgress
209
- ) ;
210
- getNrfutilLogger ( ) ?. info (
211
- `Successfully installed nrfutil ${ this . module } version: ${ this . version } `
212
- ) ;
228
+ await this . installNrfUtilCore ( onProgress ) ;
229
+ await this . installNrfUtilCommand ( onProgress ) ;
213
230
} catch ( error ) {
214
231
if ( this . env . NRFUTIL_HOME && fs . existsSync ( this . env . NRFUTIL_HOME ) ) {
215
232
fs . rmSync ( this . env . NRFUTIL_HOME , {
@@ -218,25 +235,61 @@ export class NrfutilSandbox {
218
235
} ) ;
219
236
}
220
237
221
- getNrfutilLogger ( ) ?. error (
222
- `Error while installing nrfutil ${ this . module } version: ${
223
- this . version
224
- } . describeError: ${ describeError ( error ) } `
225
- ) ;
226
238
throw error ;
227
239
}
228
240
} ;
229
241
230
- public updateNrfUtilCore = async (
242
+ private installNrfUtilCore = async (
243
+ onProgress ?: ( progress : Progress , task ?: Task ) => void
244
+ ) => {
245
+ const currentCoreVersion = await this . getCoreVersion ( ) ;
246
+ const requestedCoreVersion =
247
+ this . coreVersion ?? this . CORE_VERSION_FOR_LEGACY_APPS ;
248
+ if ( currentCoreVersion . version === requestedCoreVersion ) {
249
+ getNrfutilLogger ( ) ?. debug (
250
+ `Requested nrfutil core version ${ requestedCoreVersion } is already installed.`
251
+ ) ;
252
+
253
+ return ;
254
+ }
255
+
256
+ await this . install (
257
+ 'core' ,
258
+ requestedCoreVersion ,
259
+ 'self-upgrade' ,
260
+ [ '--to-version' , requestedCoreVersion ] ,
261
+ onProgress
262
+ ) ;
263
+ } ;
264
+
265
+ public installNrfUtilCommand = (
231
266
onProgress ?: ( progress : Progress , task ?: Task ) => void
267
+ ) =>
268
+ this . install (
269
+ this . module ,
270
+ this . version ,
271
+ 'install' ,
272
+ [ `${ this . module } =${ this . version } ` , '--force' ] ,
273
+ onProgress
274
+ ) ;
275
+
276
+ public install = async (
277
+ module : string ,
278
+ version : string ,
279
+ ...args : Parameters < typeof this . spawnNrfutil >
232
280
) => {
233
281
try {
234
- await this . spawnNrfutil ( 'self-upgrade' , [ ] , onProgress ) ;
235
- } catch ( error ) {
236
- // User might not have internet hance fail silently
237
- getNrfutilLogger ( ) ?. error (
238
- `Error while updating the bundled core for nrfutil ${ this . module } .`
282
+ await this . spawnNrfutil ( ...args ) ;
283
+ getNrfutilLogger ( ) ?. info (
284
+ `Successfully installed nrfutil ${ module } version: ${ version } `
239
285
) ;
286
+ } catch ( error ) {
287
+ const errorMessage = `Error while installing nrfutil ${ module } version ${ version } : ${ describeError (
288
+ error
289
+ ) } `;
290
+
291
+ getNrfutilLogger ( ) ?. error ( errorMessage ) ;
292
+ throw new Error ( errorMessage ) ;
240
293
}
241
294
} ;
242
295
@@ -765,22 +818,20 @@ export default async (
765
818
baseDir : string ,
766
819
module : string ,
767
820
version ?: string ,
821
+ coreVersion ?: string ,
768
822
onProgress ?: ( progress : Progress , task ?: Task ) => void
769
823
) => {
770
824
const sandbox = new NrfutilSandbox (
771
825
baseDir ,
772
826
module ,
773
- versionToInstall ( module , version )
827
+ versionToInstall ( module , version ) ,
828
+ coreVersionsToInstall ( coreVersion )
774
829
) ;
775
830
776
831
onProgress ?.( convertNrfutilProgress ( { progressPercentage : 0 } ) ) ;
777
- const result = await sandbox . isSandboxInstalled ( ) ;
778
832
779
- if ( ! result ) {
833
+ if ( ! ( await sandbox . isSandboxInstalled ( ) ) ) {
780
834
await sandbox . prepareSandbox ( onProgress ) ;
781
- } else {
782
- // update nrfutil core
783
- await sandbox . updateNrfUtilCore ( onProgress ) ;
784
835
}
785
836
786
837
onProgress ?.( convertNrfutilProgress ( { progressPercentage : 100 } ) ) ;
0 commit comments