Skip to content

Commit

Permalink
fix: added missing progress bar for uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobrosenberg committed May 24, 2022
1 parent e3d6c76 commit aacee2d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
16 changes: 14 additions & 2 deletions src/Device.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,12 @@ class Device {
* Uploads file or folder to device
* @param {string} source
* @param {string} destination
* @param {{
* onScanComplete: (files: string[]) => void,
* onUpload: (file: string) => void,
* }} options
*/
async upload(source, destination) {
async upload(source, destination, options) {
destination = posix.join(this.rootPath, `/${destination}`.replace(/\/+/g, "/"));
const root = source;
const ignores = [...this.pymakr.config.get().get("ignore")];
Expand All @@ -399,6 +403,9 @@ class Device {

const isIgnore = picomatch(ignores);

/** @type {{source: string, destination: string}[]} */
const queue = [];

const _uploadFile = async (file, destination) => {
this.log.traceShort("uploadFile", file, "to", destination);
const data = Buffer.from(readFileSync(file));
Expand All @@ -421,11 +428,16 @@ class Device {
// 'The "from" argument must be of type string. Received null'
const relativePath = relative(root, source);
if (!isIgnore(relativePath))
return statSync(source).isDirectory() ? _uploadDir(source, destination) : _uploadFile(source, destination);
return statSync(source).isDirectory() ? _uploadDir(source, destination) : queue.push({ source, destination });
};

this.log.info("upload", source, "to", destination);
await _upload(source, destination);
options.onScanComplete(queue.map((entry) => entry.source));
for (const file of queue) {
options.onUpload(relative(root, file.source));
await _uploadFile(file.source, file.destination);
}
this.log.info("upload completed");
}
}
Expand Down
16 changes: 12 additions & 4 deletions src/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class Commands {
try {
const templatePath = `${__dirname}/../../templates/${templateId}`;
await device.adapter.remove(device.rootPath, true);
await device.upload(templatePath, "/");
await this.commands.upload({ fsPath: templatePath }, device, "/");
resolve();
} catch (err) {
this.log.error(err);
Expand Down Expand Up @@ -492,7 +492,7 @@ class Commands {
*/
uploadProject: async ({ device, project }) => {
await device.adapter.remove(device.rootPath, true);
device.upload(project.folder, "/");
this.commands.upload({ fsPath: project.folder }, device, "/");
},

/**
Expand All @@ -518,7 +518,7 @@ class Commands {

/**
* Uploads a file/folder to a connected device
* @param {vscode.Uri} uri the file/folder to upload
* @param {{fsPath: string}} uri the file/folder to upload
* @param {import('../Device.js').Device} device
* @param {string} destination not including the device.rootPath ( /flash or / )
*/
Expand All @@ -528,7 +528,15 @@ class Commands {
try {
await vscode.window.withProgress({ location: vscode.ProgressLocation.Notification }, async (progress) => {
progress.report({ message: `Uploading "${friendlySource}" to "${device.displayName}"...` });
await device.upload(fsPath, destination);
let filesAmount = 0;
await device.upload(fsPath, destination, {
onScanComplete: (files) => (filesAmount = files.length),
onUpload: (file) =>
progress.report({
message: `Uploading "${file}" to "${device.displayName}"...`,
increment: 100 / filesAmount,
}),
});
});
} catch (err) {
const errors = ["failed to upload", fsPath, "to", destination, "\r\nReason:", err];
Expand Down

0 comments on commit aacee2d

Please sign in to comment.