From 0705bdfc58db7ba2faae1d9e77c07c77c3982b4a Mon Sep 17 00:00:00 2001 From: janoshrubos Date: Thu, 21 Jan 2021 15:39:33 +0100 Subject: [PATCH 1/2] fix: handle permissions error on directory creation --- lib/common/file-system.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/common/file-system.ts b/lib/common/file-system.ts index 945f165f97..5d235c85e0 100644 --- a/lib/common/file-system.ts +++ b/lib/common/file-system.ts @@ -372,8 +372,15 @@ export class FileSystem implements IFileSystem { } public ensureDirectoryExists(directoryPath: string): void { - if (!this.exists(directoryPath)) { - this.createDirectory(directoryPath); + try { + if (!this.exists(directoryPath)) { + this.createDirectory(directoryPath); + } + } catch (error) { + const $errors = this.$injector.resolve("errors"); + $errors.fail( + `Unable to generate CLI documentation.\n\nUnable to write to a system directory (${directoryPath}).\n\nYou may need to call the command with 'sudo'.` + ); } } From 3419b4abe32f5486d4bb12f3e55833ce5b6a937f Mon Sep 17 00:00:00 2001 From: janoshrubos Date: Thu, 21 Jan 2021 16:28:33 +0100 Subject: [PATCH 2/2] refactor: common error message --- lib/common/file-system.ts | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/common/file-system.ts b/lib/common/file-system.ts index 5d235c85e0..de27f0cd2e 100644 --- a/lib/common/file-system.ts +++ b/lib/common/file-system.ts @@ -200,7 +200,16 @@ export class FileSystem implements IFileSystem { } public createDirectory(path: string): void { - mkdirp.sync(path); + try { + mkdirp.sync(path); + } catch (error) { + const $errors = this.$injector.resolve("errors"); + let errorMessage = `Unable to create directory ${path}. \nError is: ${error}.`; + if (error.code === "EACCES") { + errorMessage += "\n\nYou may need to call the command with 'sudo'."; + } + $errors.fail(errorMessage); + } } public readDirectory(path: string): string[] { @@ -372,15 +381,8 @@ export class FileSystem implements IFileSystem { } public ensureDirectoryExists(directoryPath: string): void { - try { - if (!this.exists(directoryPath)) { - this.createDirectory(directoryPath); - } - } catch (error) { - const $errors = this.$injector.resolve("errors"); - $errors.fail( - `Unable to generate CLI documentation.\n\nUnable to write to a system directory (${directoryPath}).\n\nYou may need to call the command with 'sudo'.` - ); + if (!this.exists(directoryPath)) { + this.createDirectory(directoryPath); } }