Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy all elrs and bin files to the tmp output directory #480

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions src/api/src/library/FirmwareBuilder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,35 @@ export default class FirmwareBuilder {
};
}

getFirmwareBuildPath(target: string, firmwarePath: string): string {
return path.join(firmwarePath, '.pio', 'build', target);
}

getFirmwareBinFiles(target: string, firmwarePath: string): string[] {
const firmwareBuildPath = this.getFirmwareBuildPath(target, firmwarePath);
const binaryExtensions = ['.elrs', '.bin'];

const firmwareBinFiles = fs
.readdirSync(firmwareBuildPath)
.filter((filename) => binaryExtensions.includes(path.extname(filename)));

return firmwareBinFiles.map((filename) =>
path.join(firmwareBuildPath, filename)
);
}

getFirmwareBinPath(target: string, firmwarePath: string): string {
const paths = [
path.join(firmwarePath, '.pio', 'build', target, 'firmware.elrs'),
path.join(firmwarePath, '.pio', 'build', target, 'backpack.bin'),
];
// eslint-disable-next-line no-restricted-syntax
for (const location of paths) {
if (fs.existsSync(location)) {
return location;
}
}
return path.join(firmwarePath, '.pio', 'build', target, 'firmware.bin');
const firmwareBuildPath = this.getFirmwareBuildPath(target, firmwarePath);
const firmwareBinFiles = this.getFirmwareBinFiles(target, firmwarePath);
const searchValues = ['firmware.elrs', 'backpack.bin', 'firmware.bin'];

const matchedBinFile = firmwareBinFiles.find((firmwareBinPath) =>
searchValues.find(
(searchValue) => searchValue === path.basename(firmwareBinPath)
)
);

return matchedBinFile || path.join(firmwareBuildPath, 'firmware.bin');
}

private async storeUserDefines(firmwarePath: string, userDefinesTxt: string) {
Expand Down
31 changes: 17 additions & 14 deletions src/api/src/services/Firmware/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,22 +423,25 @@ export default class FirmwareService {
path.join(os.tmpdir(), `${params.target}_`)
);

// copy with original filename to tmpPath
const tmpFirmwareBinPathOriginalName = path.join(
tmpPath,
path.basename(firmwareBinPath)
// copy all binary files to tmpPath
const binaryFiles = this.builder.getFirmwareBinFiles(
params.target,
firmwarePath
);
try {
await fs.promises.copyFile(
firmwareBinPath,
tmpFirmwareBinPathOriginalName
);
} catch (err) {
this.logger?.error(
`error copying file from ${firmwareBinPath} to ${tmpFirmwareBinPathOriginalName}: ${err}`
);
}

binaryFiles.forEach((file) => {
const newPath = path.join(tmpPath, path.basename(file));

try {
fs.copyFileSync(file, newPath);
} catch (err) {
this.logger?.error(
`error copying file from ${firmwareBinPath} to ${newPath}: ${err}`
);
}
});

// copy named bin file to tmpPath
const tmpFirmwareBinPath = path.join(
tmpPath,
`${newFirmwareBaseName}${firmwareExtension}`
Expand Down