Skip to content

Commit 3e7ebab

Browse files
committed
Refactor
1 parent 4a06a58 commit 3e7ebab

File tree

1 file changed

+72
-63
lines changed

1 file changed

+72
-63
lines changed

src/cmd/esim.js

Lines changed: 72 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@ const execa = require('execa');
99
const SerialCommand = require('./serial');
1010
const FlashCommand = require('./flash');
1111
const path = require('path');
12-
const { verbose } = require('../lib/log');
1312

1413
// TODO: Get these from exports
1514
const PATH_TO_PASS_THROUGH_BINARIES = '/Users/keerthyamisagadda/code/kigen-resources/binaries';
1615

17-
1816
module.exports = class eSimCommands extends CLICommandBase {
1917
constructor() { // TODO: Bring ui class
2018
super();
@@ -60,79 +58,27 @@ module.exports = class eSimCommands extends CLICommandBase {
6058
const platform = platformForId(device.specs.productId).name;
6159
const port = device.port;
6260
console.log(`${os.EOL}Provisioning device ${device.deviceId} with platform ${platform}`);
61+
6362
// Flash firmware and retrieve EID
6463
await this._flashATPassThroughFirmware(device, platform, port);
6564
const eid = await this._getEid(port);
6665
console.log(`${os.EOL}EID: ${eid}`);
6766

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);
8268

69+
// Get profiles for this EID from the input JSON
70+
const profiles = this._getProfiles(eid);
71+
8372
console.log(`${os.EOL}Provisioning the following profiles to EID ${eid}:`);
73+
8474
profiles.forEach((profile, index) => {
8575
const rspUrl = `1\$${profile.smdp}\$${profile.matching_id}`;
8676
console.log(`\t${index + 1}. ${profile.provider} (${rspUrl})`);
8777
});
8878

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+
13682
console.log(`${os.EOL}Provisioning complete`);
13783
}
13884

@@ -262,6 +208,69 @@ module.exports = class eSimCommands extends CLICommandBase {
262208
console.log(`${os.EOL}No existing profiles found`);
263209
}
264210

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+
265274
_addToJson(jsonFile, data) {
266275
try {
267276
// Read and parse existing JSON data

0 commit comments

Comments
 (0)