Skip to content

Commit

Permalink
#1191: resolve temp path if copying/cloning sketch
Browse files Browse the repository at this point in the history
Signed-off-by: Akos Kitta <a.kitta@arduino.cc>
  • Loading branch information
Akos Kitta authored and kittaakos committed Aug 1, 2022
1 parent 581379f commit 27a2a6c
Showing 1 changed file with 27 additions and 33 deletions.
60 changes: 27 additions & 33 deletions arduino-ide-extension/src/node/sketches-service-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,15 +341,7 @@ export class SketchesServiceImpl

async cloneExample(uri: string): Promise<Sketch> {
const sketch = await this.loadSketch(uri);
const parentPath = await new Promise<string>((resolve, reject) => {
temp.mkdir({ prefix }, (err, dirPath) => {
if (err) {
reject(err);
return;
}
resolve(dirPath);
});
});
const parentPath = await this.createTempFolder();
const destinationUri = FileUri.create(
path.join(parentPath, sketch.name)
).toString();
Expand All @@ -373,21 +365,7 @@ export class SketchesServiceImpl
'dec',
];
const today = new Date();
const parentPath = await new Promise<string>((resolve, reject) => {
temp.mkdir({ prefix }, (createError, dirPath) => {
if (createError) {
reject(createError);
return;
}
fs.realpath.native(dirPath, (resolveError, resolvedDirPath) => {
if (resolveError) {
reject(resolveError);
return;
}
resolve(resolvedDirPath);
});
});
});
const parentPath = await this.createTempFolder();
const sketchBaseName = `sketch_${
monthNames[today.getMonth()]
}${today.getDate()}`;
Expand Down Expand Up @@ -438,6 +416,30 @@ void loop() {
return this.loadSketch(FileUri.create(sketchDir).toString());
}

/**
* Creates a temp folder and returns with a promise that resolves with the canonicalized absolute pathname of the newly created temp folder.
* This method ensures that the file-system path pointing to the new temp directory is fully resolved.
* For example, on Windows, instead of getting an [8.3 filename](https://en.wikipedia.org/wiki/8.3_filename), callers will get a fully resolved path.
* `C:\\Users\\KITTAA~1\\AppData\\Local\\Temp\\.arduinoIDE-unsaved2022615-21100-iahybb.yyvh\\sketch_jul15a` will be `C:\\Users\\kittaakos\\AppData\\Local\\Temp\\.arduinoIDE-unsaved2022615-21100-iahybb.yyvh\\sketch_jul15a`
*/
private createTempFolder(): Promise<string> {
return new Promise<string>((resolve, reject) => {
temp.mkdir({ prefix }, (createError, dirPath) => {
if (createError) {
reject(createError);
return;
}
fs.realpath.native(dirPath, (resolveError, resolvedDirPath) => {
if (resolveError) {
reject(resolveError);
return;
}
resolve(resolvedDirPath);
});
});
});
}

async getSketchFolder(uri: string): Promise<Sketch | undefined> {
if (!uri) {
return undefined;
Expand Down Expand Up @@ -534,15 +536,7 @@ void loop() {
// `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);
let tempDestination = await new Promise<string>((resolve, reject) => {
temp.track().mkdir({ prefix }, async (err, dirPath) => {
if (err) {
reject(err);
return;
}
resolve(dirPath);
});
});
let tempDestination = await this.createTempFolder();
tempDestination = path.join(tempDestination, sketch.name);
await fs.promises.mkdir(tempDestination, { recursive: true });
await copy(source, tempDestination);
Expand Down

0 comments on commit 27a2a6c

Please sign in to comment.