Skip to content

Commit 975a589

Browse files
author
Akos Kitta
committed
#1191: resolve temp path if copying/cloning sketch
Signed-off-by: Akos Kitta <[email protected]>
1 parent 5822879 commit 975a589

File tree

1 file changed

+27
-33
lines changed

1 file changed

+27
-33
lines changed

Diff for: arduino-ide-extension/src/node/sketches-service-impl.ts

+27-33
Original file line numberDiff line numberDiff line change
@@ -341,15 +341,7 @@ export class SketchesServiceImpl
341341

342342
async cloneExample(uri: string): Promise<Sketch> {
343343
const sketch = await this.loadSketch(uri);
344-
const parentPath = await new Promise<string>((resolve, reject) => {
345-
temp.mkdir({ prefix }, (err, dirPath) => {
346-
if (err) {
347-
reject(err);
348-
return;
349-
}
350-
resolve(dirPath);
351-
});
352-
});
344+
const parentPath = await this.createTempFolder();
353345
const destinationUri = FileUri.create(
354346
path.join(parentPath, sketch.name)
355347
).toString();
@@ -373,21 +365,7 @@ export class SketchesServiceImpl
373365
'dec',
374366
];
375367
const today = new Date();
376-
const parentPath = await new Promise<string>((resolve, reject) => {
377-
temp.mkdir({ prefix }, (createError, dirPath) => {
378-
if (createError) {
379-
reject(createError);
380-
return;
381-
}
382-
fs.realpath.native(dirPath, (resolveError, resolvedDirPath) => {
383-
if (resolveError) {
384-
reject(resolveError);
385-
return;
386-
}
387-
resolve(resolvedDirPath);
388-
});
389-
});
390-
});
368+
const parentPath = await this.createTempFolder();
391369
const sketchBaseName = `sketch_${
392370
monthNames[today.getMonth()]
393371
}${today.getDate()}`;
@@ -438,6 +416,30 @@ void loop() {
438416
return this.loadSketch(FileUri.create(sketchDir).toString());
439417
}
440418

419+
/**
420+
* Creates a temp folder and returns with a promise that resolves with the canonicalized absolute pathname of the newly created temp folder.
421+
* This method ensures that the file-system path pointing to the new temp directory is fully resolved.
422+
* 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.
423+
* `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`
424+
*/
425+
private createTempFolder(): Promise<string> {
426+
return new Promise<string>((resolve, reject) => {
427+
temp.mkdir({ prefix }, (createError, dirPath) => {
428+
if (createError) {
429+
reject(createError);
430+
return;
431+
}
432+
fs.realpath.native(dirPath, (resolveError, resolvedDirPath) => {
433+
if (resolveError) {
434+
reject(resolveError);
435+
return;
436+
}
437+
resolve(resolvedDirPath);
438+
});
439+
});
440+
});
441+
}
442+
441443
async getSketchFolder(uri: string): Promise<Sketch | undefined> {
442444
if (!uri) {
443445
return undefined;
@@ -534,15 +536,7 @@ void loop() {
534536
// `ncp` makes a recursion and copies the folders over and over again. In such cases, we copy the source into a temp folder,
535537
// then move it to the desired destination.
536538
const destination = FileUri.fsPath(destinationUri);
537-
let tempDestination = await new Promise<string>((resolve, reject) => {
538-
temp.track().mkdir({ prefix }, async (err, dirPath) => {
539-
if (err) {
540-
reject(err);
541-
return;
542-
}
543-
resolve(dirPath);
544-
});
545-
});
539+
let tempDestination = await this.createTempFolder();
546540
tempDestination = path.join(tempDestination, sketch.name);
547541
await fs.promises.mkdir(tempDestination, { recursive: true });
548542
await copy(source, tempDestination);

0 commit comments

Comments
 (0)