Skip to content

Commit 797a60e

Browse files
refactor: add transform option to upload
1 parent 318666d commit 797a60e

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/Device.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ class Device {
475475
* @param {{
476476
* onScanComplete: (files: string[]) => void,
477477
* onUpload: (file: string) => void,
478+
* transform: (id: string, body: string) => string
478479
* }} options
479480
*/
480481
async upload(source, destination, options) {
@@ -492,7 +493,11 @@ class Device {
492493

493494
const _uploadFile = async (file, destination) => {
494495
this.log.traceShort("uploadFile", file, "to", destination);
495-
const data = Buffer.from(readFileSync(file));
496+
497+
const data = options.transform
498+
? Buffer.from(options.transform(file, readFileSync(file, "utf-8")))
499+
: Buffer.from(readFileSync(file));
500+
496501
return this.adapter.putFile(destination, data, { checkIfSimilarBeforeUpload: true });
497502
};
498503

src/commands/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,8 +622,9 @@ class Commands {
622622
* @param {{fsPath: string}} uri the file/folder to upload
623623
* @param {import('../Device.js').Device} device
624624
* @param {string} destination not including the device.rootPath ( /flash or / )
625+
* @param {(id: string, body: string) => string=} transform transforms content of file during upload
625626
*/
626-
upload: async ({ fsPath }, device, destination) => {
627+
upload: async ({ fsPath }, device, destination, transform) => {
627628
const friendlySource = fsPath.replace(/.*[/\\]/g, "");
628629
if (!device.connected.get()) await device.connect();
629630
try {
@@ -637,6 +638,7 @@ class Commands {
637638
message: `Uploading "${file}" to "${device.displayName}"...`,
638639
increment: 100 / filesAmount,
639640
}),
641+
transform,
640642
});
641643
});
642644
} catch (err) {

0 commit comments

Comments
 (0)