Skip to content

Commit 424cce0

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

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
@@ -328,15 +328,7 @@ export class SketchesServiceImpl
328328

329329
async cloneExample(uri: string): Promise<Sketch> {
330330
const sketch = await this.loadSketch(uri);
331-
const parentPath = await new Promise<string>((resolve, reject) => {
332-
temp.mkdir({ prefix }, (err, dirPath) => {
333-
if (err) {
334-
reject(err);
335-
return;
336-
}
337-
resolve(dirPath);
338-
});
339-
});
331+
const parentPath = await this.createTempFolder();
340332
const destinationUri = FileUri.create(
341333
path.join(parentPath, sketch.name)
342334
).toString();
@@ -360,21 +352,7 @@ export class SketchesServiceImpl
360352
'dec',
361353
];
362354
const today = new Date();
363-
const parentPath = await new Promise<string>((resolve, reject) => {
364-
temp.mkdir({ prefix }, (createError, dirPath) => {
365-
if (createError) {
366-
reject(createError);
367-
return;
368-
}
369-
fs.realpath.native(dirPath, (resolveError, resolvedDirPath) => {
370-
if (resolveError) {
371-
reject(resolveError);
372-
return;
373-
}
374-
resolve(resolvedDirPath);
375-
});
376-
});
377-
});
355+
const parentPath = await this.createTempFolder();
378356
const sketchBaseName = `sketch_${
379357
monthNames[today.getMonth()]
380358
}${today.getDate()}`;
@@ -425,6 +403,30 @@ void loop() {
425403
return this.loadSketch(FileUri.create(sketchDir).toString());
426404
}
427405

406+
/**
407+
* Creates a temp folder and returns with a promise that resolves with the canonicalized absolute pathname of the newly created temp folder.
408+
* This method ensures that the file-system path pointing to the new temp directory is fully resolved.
409+
* 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.
410+
* `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`
411+
*/
412+
private createTempFolder(): Promise<string> {
413+
return new Promise<string>((resolve, reject) => {
414+
temp.mkdir({ prefix }, (createError, dirPath) => {
415+
if (createError) {
416+
reject(createError);
417+
return;
418+
}
419+
fs.realpath.native(dirPath, (resolveError, resolvedDirPath) => {
420+
if (resolveError) {
421+
reject(resolveError);
422+
return;
423+
}
424+
resolve(resolvedDirPath);
425+
});
426+
});
427+
});
428+
}
429+
428430
async getSketchFolder(uri: string): Promise<Sketch | undefined> {
429431
if (!uri) {
430432
return undefined;
@@ -532,15 +534,7 @@ void loop() {
532534
// `ncp` makes a recursion and copies the folders over and over again. In such cases, we copy the source into a temp folder,
533535
// then move it to the desired destination.
534536
const destination = FileUri.fsPath(destinationUri);
535-
let tempDestination = await new Promise<string>((resolve, reject) => {
536-
temp.track().mkdir({ prefix }, async (err, dirPath) => {
537-
if (err) {
538-
reject(err);
539-
return;
540-
}
541-
resolve(dirPath);
542-
});
543-
});
537+
let tempDestination = await this.createTempFolder();
544538
tempDestination = path.join(tempDestination, sketch.name);
545539
await fs.promises.mkdir(tempDestination, { recursive: true });
546540
await copy(source, tempDestination);

0 commit comments

Comments
 (0)