Skip to content

Commit 76a5889

Browse files
author
Akos Kitta
committed
feat: spare detecting invalid sketch name error
The invalid sketch name detection requires at least one extra FS access. Do not try to detect invalid sketch name error, but use the original `NotFound` from the CLI. Signed-off-by: Akos Kitta <[email protected]>
1 parent 74d93f4 commit 76a5889

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

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

+31-19
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,13 @@ export class SketchesServiceImpl
188188
}
189189

190190
async loadSketch(uri: string): Promise<SketchWithDetails> {
191+
return this.doLoadSketch(uri);
192+
}
193+
194+
private async doLoadSketch(
195+
uri: string,
196+
detectInvalidSketchNameError = true
197+
): Promise<SketchWithDetails> {
191198
const { client, instance } = await this.coreClient;
192199
const req = new LoadSketchRequest();
193200
const requestSketchPath = FileUri.fsPath(uri);
@@ -202,17 +209,19 @@ export class SketchesServiceImpl
202209
if (err) {
203210
let rejectWith: unknown = err;
204211
if (isNotFoundError(err)) {
205-
const invalidMainSketchFilePath = await isInvalidSketchNameError(
206-
err,
207-
requestSketchPath
208-
);
209-
if (invalidMainSketchFilePath) {
210-
rejectWith = SketchesError.InvalidName(
211-
err.details,
212-
FileUri.create(invalidMainSketchFilePath).toString()
212+
rejectWith = SketchesError.NotFound(err.details, uri);
213+
// detecting the invalid sketch name error is not for free as it requires multiple filesystem access.
214+
if (detectInvalidSketchNameError) {
215+
const invalidMainSketchFilePath = await isInvalidSketchNameError(
216+
err,
217+
requestSketchPath
213218
);
214-
} else {
215-
rejectWith = SketchesError.NotFound(err.details, uri);
219+
if (invalidMainSketchFilePath) {
220+
rejectWith = SketchesError.InvalidName(
221+
err.details,
222+
FileUri.create(invalidMainSketchFilePath).toString()
223+
);
224+
}
216225
}
217226
}
218227
reject(rejectWith);
@@ -318,7 +327,7 @@ export class SketchesServiceImpl
318327

319328
let sketch: Sketch | undefined = undefined;
320329
try {
321-
sketch = await this.loadSketch(uri);
330+
sketch = await this.doLoadSketch(uri, false);
322331
this.logger.debug(
323332
`Loaded sketch ${JSON.stringify(
324333
sketch
@@ -391,7 +400,7 @@ export class SketchesServiceImpl
391400
)) {
392401
let sketch: SketchWithDetails | undefined = undefined;
393402
try {
394-
sketch = await this.loadSketch(uri);
403+
sketch = await this.doLoadSketch(uri, false);
395404
} catch {}
396405
if (!sketch) {
397406
needsUpdate = true;
@@ -414,14 +423,14 @@ export class SketchesServiceImpl
414423

415424
async cloneExample(uri: string): Promise<Sketch> {
416425
const [sketch, parentPath] = await Promise.all([
417-
this.loadSketch(uri),
426+
this.doLoadSketch(uri, false),
418427
this.createTempFolder(),
419428
]);
420429
const destinationUri = FileUri.create(
421430
path.join(parentPath, sketch.name)
422431
).toString();
423432
const copiedSketchUri = await this.copy(sketch, { destinationUri });
424-
return this.loadSketch(copiedSketchUri);
433+
return this.doLoadSketch(copiedSketchUri, false);
425434
}
426435

427436
async createNewSketch(): Promise<Sketch> {
@@ -478,7 +487,7 @@ export class SketchesServiceImpl
478487
fs.mkdir(sketchDir, { recursive: true }),
479488
]);
480489
await fs.writeFile(sketchFile, inoContent, { encoding: 'utf8' });
481-
return this.loadSketch(FileUri.create(sketchDir).toString());
490+
return this.doLoadSketch(FileUri.create(sketchDir).toString(), false);
482491
}
483492

484493
/**
@@ -529,7 +538,7 @@ export class SketchesServiceImpl
529538
uri: string
530539
): Promise<SketchWithDetails | undefined> {
531540
try {
532-
const sketch = await this.loadSketch(uri);
541+
const sketch = await this.doLoadSketch(uri, false);
533542
return sketch;
534543
} catch (err) {
535544
if (SketchesError.NotFound.is(err) || SketchesError.InvalidName.is(err)) {
@@ -554,7 +563,7 @@ export class SketchesServiceImpl
554563
}
555564
// Nothing to do when source and destination are the same.
556565
if (sketch.uri === destinationUri) {
557-
await this.loadSketch(sketch.uri); // Sanity check.
566+
await this.doLoadSketch(sketch.uri, false); // Sanity check.
558567
return sketch.uri;
559568
}
560569

@@ -575,7 +584,10 @@ export class SketchesServiceImpl
575584
if (oldPath !== newPath) {
576585
await fs.rename(oldPath, newPath);
577586
}
578-
await this.loadSketch(FileUri.create(destinationPath).toString()); // Sanity check.
587+
await this.doLoadSketch(
588+
FileUri.create(destinationPath).toString(),
589+
false
590+
); // Sanity check.
579591
resolve();
580592
} catch (e) {
581593
reject(e);
@@ -597,7 +609,7 @@ export class SketchesServiceImpl
597609
}
598610

599611
async archive(sketch: Sketch, destinationUri: string): Promise<string> {
600-
await this.loadSketch(sketch.uri); // sanity check
612+
await this.doLoadSketch(sketch.uri, false); // sanity check
601613
const { client } = await this.coreClient;
602614
const archivePath = FileUri.fsPath(destinationUri);
603615
// The CLI cannot override existing archives, so we have to wipe it manually: https://github.com/arduino/arduino-cli/issues/1160

0 commit comments

Comments
 (0)