ATL-941: Fixed recursive folder issue on Save as

Signed-off-by: Akos Kitta <kittaakos@typefox.io>
This commit is contained in:
Akos Kitta 2021-03-12 14:20:21 +01:00 committed by Akos Kitta
parent 5a262d42c1
commit 9cd91464e3

View File

@ -332,27 +332,47 @@ void loop() {
await this.loadSketch(sketch.uri); // Sanity check. await this.loadSketch(sketch.uri); // Sanity check.
return sketch.uri; 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); const destination = FileUri.fsPath(destinationUri);
await new Promise<void>((resolve, reject) => { let tempDestination = await new Promise<string>((resolve, reject) => {
ncp.ncp(source, destination, async error => { temp.track().mkdir({ prefix }, async (err, dirPath) => {
if (error) { if (err) {
reject(error); reject(err);
return; return;
} }
const newName = path.basename(destination); resolve(dirPath);
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);
}
}); });
}); });
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(); return FileUri.create(destination).toString();
} }