Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
keeramis committed Jan 16, 2025
1 parent 1771f6b commit 76cf00b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 76 deletions.
8 changes: 3 additions & 5 deletions src/cmd/flash.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ module.exports = class FlashCommand extends CLICommandBase {
}

async flashTachyon({ verbose, files }) {
this.ui.write(`${os.EOL}Ensure only one device is connected to the computer${os.EOL}`);
this.ui.write(`${os.EOL}Ensure that only one device is connected to the computer before proceeding.${os.EOL}`);

let zipFile;
let includeDir = '';
Expand Down Expand Up @@ -101,11 +101,11 @@ module.exports = class FlashCommand extends CLICommandBase {
filesToProgram = files;
}

this.ui.write(`Starting download. The download may take several minutes...${os.EOL}`);
this.ui.write(`Starting download. This may take several minutes...${os.EOL}`);
const outputLog = path.join(process.cwd(), `qdl-output-${Date.now()}.log`);
try {
// put the output in a log file if not verbose
this.ui.write(`Output log file: ${outputLog}${os.EOL}`);
this.ui.write(`Logs are being written to: ${outputLog}${os.EOL}`);
await qdl.run({
files: filesToProgram,
includeDir,
Expand All @@ -115,13 +115,11 @@ module.exports = class FlashCommand extends CLICommandBase {
ui: this.ui,
outputLogFile: outputLog
});
this.ui.write('Download complete.');
fs.appendFileSync(outputLog, 'Download complete.');
} catch (error) {
this.ui.write('Download failed');
fs.appendFileSync(outputLog, 'Download failed.');
}
// TODO: Handle errors
}

async _extractFlashFilesFromDir(dirPath) {
Expand Down
139 changes: 68 additions & 71 deletions src/lib/qdl.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,95 +25,92 @@ async function getExecutable() {
}

async function run({ files, includeDir, updateFolder, zip, ui, outputLogFile }) {
const qdl = '/Users/keerthyamisagadda/code/tachyon-qdl/qdl';
await fs.chmod(qdl, 0o755);
const qdlPath = '/Users/keerthyamisagadda/code/tachyon-qdl/qdl';
await fs.chmod(qdlPath, 0o755);

const qdlArgs = [
'--storage',
TACHYON_STORAGE_TYPE,
const qdlArguments = [
'--storage', TACHYON_STORAGE_TYPE,
...(zip ? ['--zip', zip] : []),
...(includeDir ? ['--include', includeDir] : []),
...files
];

const progressBar = ui.createProgressBar();
let currentModuleName = '', currentModuleSectors = 0;
let totalSectorsInAllFiles = 0, totalSectorsFlashed = 0, progressBarInitialized = false;

try {
const qdlProcess = execa(qdl, qdlArgs, {
cwd: updateFolder || process.cwd(),
stdio: 'pipe'
});

let currentModule = '';
let currentSectorsTotal = 0;

const processOutput = (chunk, type) => {
const lines = chunk
.toString()
.split('\n')
.map(line => line.trim())
.filter(Boolean);

for (const line of lines) {
// write each line to the log file
fs.appendFileSync(outputLogFile, line + '\n');

if (line.includes('Waiting for EDL device')) {
handleError(qdlProcess, `${os.EOL}Device is not in EDL mode${os.EOL}`);
return;
const handleError = (process, message) => {
progressBar.stop();
ui.stdout.write(message);
process.kill();
};

const processLogLine = (line, process) => {
fs.appendFileSync(outputLogFile, `${line}\n`);

if (line.includes('Waiting for EDL device')) {
handleError(process, `Device is not in EDL mode${os.EOL}`);
} else if (line.includes('[ERROR]')) {
handleError(process, `${os.EOL}Error detected: ${line}${os.EOL}`);
} else if (line.includes('status=getProgramInfo')) {
const match = line.match(/sectors_total=(\d+)/);
if (match) {
totalSectorsInAllFiles += parseInt(match[1], 10);
}
} else if (line.includes('status=Start flashing module')) {
const moduleNameMatch = line.match(/module=(.*?),/);
const sectorsTotalMatch = line.match(/sectors_total=(\d+)/);
if (moduleNameMatch && sectorsTotalMatch) {
currentModuleName = moduleNameMatch[1];
currentModuleSectors = parseInt(sectorsTotalMatch[1], 10);

if (!progressBarInitialized) {
progressBarInitialized = true;
progressBar.start(totalSectorsInAllFiles, totalSectorsFlashed, { description: `Flashing ${currentModuleName}` });
} else {
progressBar.update(totalSectorsFlashed, { description: `Flashing ${currentModuleName}` });
}

if (line.includes('[ERROR]')) {
handleError(qdlProcess, `${os.EOL}Error detected in ${type} stream: ${line}${os.EOL}`);
return;
}
} else if (line.includes('status=Flashing module')) {
const sectorsFlashedMatch = line.match(/sectors_done=(\d+)/);
if (sectorsFlashedMatch) {
const sectorsFlashed = parseInt(sectorsFlashedMatch[1], 10);
progressBar.update(totalSectorsFlashed + sectorsFlashed, { description: `Flashing module: ${currentModuleName} (${sectorsFlashed}/${currentModuleSectors} sectors)` });

if (sectorsFlashed === currentModuleSectors) {
totalSectorsFlashed += currentModuleSectors;
progressBar.update({ description: `Flashed ${currentModuleName}` });
}

if (line.includes('status=Start flashing module')) {
const moduleMatch = line.match(/module=(.*?),/);
const sectorsTotalMatch = line.match(/sectors_total=(\d+),/);

if (moduleMatch && sectorsTotalMatch) {
currentModule = moduleMatch[1];
currentSectorsTotal = parseInt(sectorsTotalMatch[1], 10);

progressBar.start(currentSectorsTotal, 0, {
description: `Flashing ${currentModule}`
});
}
} else if (line.includes('status=Flashing module')) {
const sectorsDoneMatch = line.match(/sectors_done=(\d+)(,|$)/);

if (sectorsDoneMatch) {
const sectorsDone = parseInt(sectorsDoneMatch[1], 10);
progressBar.update(sectorsDone);

if (sectorsDone === currentSectorsTotal) {
progressBar.update({
description: `Flashed ${currentModule} (${currentSectorsTotal} sectors)`
});
progressBar.stop();
}
}
if (totalSectorsFlashed === totalSectorsInAllFiles) {
progressBar.update({ description: 'Flashing complete' });
progressBar.stop();
}
}
};
}
};

const handleError = (process, message) => {
progressBar.stop();
ui.stdout.write(message);
process.kill();
try {
const qdlProcess = execa(qdlPath, qdlArguments, { cwd: updateFolder || process.cwd(), stdio: 'pipe' });

const handleStream = (stream) => {
stream.on('data', chunk => {
chunk.toString().split('\n').map(line => line.trim()).filter(Boolean).forEach(line => {
processLogLine(line, qdlProcess);
});
});
};

qdlProcess.stdout?.on('data', chunk => processOutput(chunk, 'stdout'));
qdlProcess.stderr?.on('data', chunk => processOutput(chunk, 'stderr'));
handleStream(qdlProcess.stdout, 'stdout');
handleStream(qdlProcess.stderr, 'stderr');

await qdlProcess;
progressBar.stop();
return { success: true };
} catch (err) {
progressBar.stop();
throw err;
}
return;
} catch (error) {
throw error;
} finally {
progressBar.stop();
}
}


Expand Down

0 comments on commit 76cf00b

Please sign in to comment.