Skip to content

Commit

Permalink
ATL-941: Fixed recursive folder issue on Save as
Browse files Browse the repository at this point in the history
Signed-off-by: Akos Kitta <kittaakos@typefox.io>
  • Loading branch information
Akos Kitta authored and kittaakos committed Mar 15, 2021
1 parent 5a262d4 commit 9cd9146
Showing 1 changed file with 36 additions and 16 deletions.
52 changes: 36 additions & 16 deletions arduino-ide-extension/src/node/sketches-service-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,27 +332,47 @@ void loop() {
await this.loadSketch(sketch.uri); // Sanity check.
return sketch.uri;
}

const copy = async (sourcePath: string, destinationPath: string) => {
return new Promise<void>((resolve, reject) => {
ncp.ncp(sourcePath, destinationPath, async error => {
if (error) {
reject(error);
return;
}
const newName = path.basename(destinationPath);
try {
const oldPath = path.join(destinationPath, new URI(sketch.mainFileUri).path.base);
const newPath = path.join(destinationPath, `${newName}.ino`);
if (oldPath !== newPath) {
await promisify(fs.rename)(oldPath, newPath);
}
await this.loadSketch(FileUri.create(destinationPath).toString()); // Sanity check.
resolve();
} catch (e) {
reject(e);
}
});
});
}
// https://github.com/arduino/arduino-ide/issues/65
// When copying `/path/to/sketchbook/sketch_A` to `/path/to/sketchbook/sketch_A/anything` on a non-POSIX filesystem,
// `ncp` makes a recursion and copies the folders over and over again. In such cases, we copy the source into a temp folder,
// then move it to the desired destination.
const destination = FileUri.fsPath(destinationUri);
await new Promise<void>((resolve, reject) => {
ncp.ncp(source, destination, async error => {
if (error) {
reject(error);
let tempDestination = await new Promise<string>((resolve, reject) => {
temp.track().mkdir({ prefix }, async (err, dirPath) => {
if (err) {
reject(err);
return;
}
const newName = path.basename(destination);
try {
const oldPath = path.join(destination, new URI(sketch.mainFileUri).path.base);
const newPath = path.join(destination, `${newName}.ino`);
if (oldPath !== newPath) {
await promisify(fs.rename)(oldPath, newPath);
}
await this.loadSketch(destinationUri); // Sanity check.
resolve();
} catch (e) {
reject(e);
}
resolve(dirPath);
});
});
tempDestination = path.join(tempDestination, sketch.name);
await fs.promises.mkdir(tempDestination, { recursive: true });
await copy(source, tempDestination);
await copy(tempDestination, destination);
return FileUri.create(destination).toString();
}

Expand Down

0 comments on commit 9cd9146

Please sign in to comment.