@@ -9,12 +9,10 @@ const execa = require('execa');
9
9
const SerialCommand = require ( './serial' ) ;
10
10
const FlashCommand = require ( './flash' ) ;
11
11
const path = require ( 'path' ) ;
12
- const { verbose } = require ( '../lib/log' ) ;
13
12
14
13
// TODO: Get these from exports
15
14
const PATH_TO_PASS_THROUGH_BINARIES = '/Users/keerthyamisagadda/code/kigen-resources/binaries' ;
16
15
17
-
18
16
module . exports = class eSimCommands extends CLICommandBase {
19
17
constructor ( ) { // TODO: Bring ui class
20
18
super ( ) ;
@@ -60,79 +58,27 @@ module.exports = class eSimCommands extends CLICommandBase {
60
58
const platform = platformForId ( device . specs . productId ) . name ;
61
59
const port = device . port ;
62
60
console . log ( `${ os . EOL } Provisioning device ${ device . deviceId } with platform ${ platform } ` ) ;
61
+
63
62
// Flash firmware and retrieve EID
64
63
await this . _flashATPassThroughFirmware ( device , platform , port ) ;
65
64
const eid = await this . _getEid ( port ) ;
66
65
console . log ( `${ os . EOL } EID: ${ eid } ` ) ;
67
66
68
- // await this._checkForExistingProfiles(port);
69
-
70
- // Parse the JSON to get EID and profiles
71
- const input = fs . readFileSync ( this . inputJson ) ;
72
- const inputJsonData = JSON . parse ( input ) ;
73
-
74
- // Get the profile list that matches the EID that is given by the field eid
75
- const eidBlock = inputJsonData . EIDs . find ( ( block ) => block . esim_id === eid ) ;
76
-
77
- if ( ! eidBlock || ! eidBlock . profiles || eidBlock . profiles . length === 0 ) {
78
- throw new Error ( 'No profiles to provision in the input JSON' ) ;
79
- }
80
-
81
- const profiles = eidBlock ?. profiles ;
67
+ await this . _checkForExistingProfiles ( port ) ;
82
68
69
+ // Get profiles for this EID from the input JSON
70
+ const profiles = this . _getProfiles ( eid ) ;
71
+
83
72
console . log ( `${ os . EOL } Provisioning the following profiles to EID ${ eid } :` ) ;
73
+
84
74
profiles . forEach ( ( profile , index ) => {
85
75
const rspUrl = `1\$${ profile . smdp } \$${ profile . matching_id } ` ;
86
76
console . log ( `\t${ index + 1 } . ${ profile . provider } (${ rspUrl } )` ) ;
87
77
} ) ;
88
78
89
- // Download each profile
90
- for ( const [ index , profile ] of profiles . entries ( ) ) {
91
- const rspUrl = `1\$${ profile . smdp } \$${ profile . matching_id } ` ;
92
- console . log ( `${ os . EOL } ${ index + 1 } . Downloading ${ profile . provider } profile from ${ rspUrl } ` ) ;
93
-
94
- const start = Date . now ( ) ;
95
- let timeTaken = 0 ;
96
- let iccid = null ;
97
-
98
- try {
99
- const res = await execa ( this . lpa , [ 'download' , rspUrl , `--serial=${ port } ` ] ) ;
100
- timeTaken = ( ( Date . now ( ) - start ) / 1000 ) . toFixed ( 2 ) ;
101
-
102
- const output = res . stdout ;
103
- if ( output . includes ( 'Profile successfully downloaded' ) ) {
104
- console . log ( `${ os . EOL } \tProfile successfully downloaded in ${ timeTaken } sec` ) ;
105
- const iccidLine = output . split ( '\n' ) . find ( ( line ) => line . includes ( 'Profile with ICCID' ) ) ;
106
- if ( iccidLine ) {
107
- iccid = iccidLine . split ( ' ' ) [ 4 ] ; // Extract ICCID
108
- }
109
- } else {
110
- console . log ( `${ os . EOL } \tProfile download failed` ) ;
111
- }
112
-
113
- const outputData = {
114
- EID : eid ,
115
- provider : profile . provider ,
116
- iccid,
117
- time : timeTaken ,
118
- success : true ,
119
- output,
120
- } ;
121
-
122
- this . _addToJson ( this . outputJson , outputData ) ;
123
- } catch ( err ) {
124
- const outputData = {
125
- EID : eid ,
126
- provider : profile . provider ,
127
- iccid,
128
- success : false ,
129
- time : timeTaken ,
130
- output : err . message ,
131
- } ;
132
- this . _addToJson ( this . outputJson , outputData ) ;
133
- throw new Error ( 'Failed to download profile' ) ;
134
- }
135
- }
79
+ // Download each profile and update the JSON output
80
+ await this . _doDownload ( profiles , port , eid ) ;
81
+
136
82
console . log ( `${ os . EOL } Provisioning complete` ) ;
137
83
}
138
84
@@ -262,6 +208,69 @@ module.exports = class eSimCommands extends CLICommandBase {
262
208
console . log ( `${ os . EOL } No existing profiles found` ) ;
263
209
}
264
210
211
+ _getProfiles ( eid ) {
212
+ const input = fs . readFileSync ( this . inputJson ) ;
213
+ const inputJsonData = JSON . parse ( input ) ;
214
+
215
+ // Get the profile list that matches the EID that is given by the field eid
216
+ const eidBlock = inputJsonData . EIDs . find ( ( block ) => block . esim_id === eid ) ;
217
+
218
+ if ( ! eidBlock || ! eidBlock . profiles || eidBlock . profiles . length === 0 ) {
219
+ throw new Error ( 'No profiles to provision in the input JSON' ) ;
220
+ }
221
+
222
+ const profiles = eidBlock ?. profiles ;
223
+ }
224
+
225
+ async _doDownload ( profiles , port , eid ) {
226
+ for ( const [ index , profile ] of profiles . entries ( ) ) {
227
+ const rspUrl = `1\$${ profile . smdp } \$${ profile . matching_id } ` ;
228
+ console . log ( `${ os . EOL } ${ index + 1 } . Downloading ${ profile . provider } profile from ${ rspUrl } ` ) ;
229
+
230
+ const start = Date . now ( ) ;
231
+ let timeTaken = 0 ;
232
+ let iccid = null ;
233
+
234
+ try {
235
+ const res = await execa ( this . lpa , [ 'download' , rspUrl , `--serial=${ port } ` ] ) ;
236
+ timeTaken = ( ( Date . now ( ) - start ) / 1000 ) . toFixed ( 2 ) ;
237
+
238
+ const output = res . stdout ;
239
+ if ( output . includes ( 'Profile successfully downloaded' ) ) {
240
+ console . log ( `${ os . EOL } \tProfile successfully downloaded in ${ timeTaken } sec` ) ;
241
+ const iccidLine = output . split ( '\n' ) . find ( ( line ) => line . includes ( 'Profile with ICCID' ) ) ;
242
+ if ( iccidLine ) {
243
+ iccid = iccidLine . split ( ' ' ) [ 4 ] ; // Extract ICCID
244
+ }
245
+ } else {
246
+ console . log ( `${ os . EOL } \tProfile download failed` ) ;
247
+ }
248
+
249
+ const outputData = {
250
+ EID : eid ,
251
+ provider : profile . provider ,
252
+ iccid,
253
+ time : timeTaken ,
254
+ success : true ,
255
+ output,
256
+ } ;
257
+
258
+ this . _addToJson ( this . outputJson , outputData ) ;
259
+ } catch ( err ) {
260
+ const outputData = {
261
+ EID : eid ,
262
+ provider : profile . provider ,
263
+ iccid,
264
+ success : false ,
265
+ time : timeTaken ,
266
+ output : err . message ,
267
+ } ;
268
+ this . _addToJson ( this . outputJson , outputData ) ;
269
+ throw new Error ( 'Failed to download profile' ) ;
270
+ }
271
+ }
272
+ }
273
+
265
274
_addToJson ( jsonFile , data ) {
266
275
try {
267
276
// Read and parse existing JSON data
0 commit comments