Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2446 from micalevisk/fix/issue-2434
fix: exit with an error when some config file was found but lacks on reading permissions
- Loading branch information
Showing
6 changed files
with
92 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,54 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import { Reader } from './reader'; | ||
import { Reader, ReaderFileLackPersmissionsError } from './reader'; | ||
|
||
export class FileSystemReader implements Reader { | ||
constructor(private readonly directory: string) {} | ||
|
||
public list(): Promise<string[]> { | ||
return fs.promises.readdir(this.directory); | ||
public list(): string[] { | ||
return fs.readdirSync(this.directory); | ||
} | ||
|
||
public read(name: string): Promise<string> { | ||
return fs.promises.readFile(path.join(this.directory, name), 'utf8'); | ||
public read(name: string): string { | ||
return fs.readFileSync(path.join(this.directory, name), 'utf8'); | ||
} | ||
|
||
public async readAnyOf(filenames: string[]): Promise<string | undefined> { | ||
try { | ||
for (const file of filenames) { | ||
return await this.read(file); | ||
public readAnyOf( | ||
filenames: string[], | ||
): string | undefined | ReaderFileLackPersmissionsError { | ||
let firstFilePathFoundButWithInsufficientPermissions: string | undefined; | ||
|
||
for (let id = 0; id < filenames.length; id++) { | ||
const file = filenames[id]; | ||
|
||
try { | ||
return this.read(file); | ||
} catch (readErr) { | ||
if ( | ||
!firstFilePathFoundButWithInsufficientPermissions && | ||
typeof readErr?.code === 'string' | ||
) { | ||
const isInsufficientPermissionsError = | ||
readErr.code === 'EACCES' || readErr.code === 'EPERM'; | ||
if (isInsufficientPermissionsError) { | ||
firstFilePathFoundButWithInsufficientPermissions = readErr.path; | ||
} | ||
} | ||
|
||
const isLastFileToLookFor = id === filenames.length - 1; | ||
if (!isLastFileToLookFor) { | ||
continue; | ||
} | ||
|
||
if (firstFilePathFoundButWithInsufficientPermissions) { | ||
return new ReaderFileLackPersmissionsError( | ||
firstFilePathFoundButWithInsufficientPermissions, | ||
readErr.code, | ||
); | ||
} else { | ||
return undefined; | ||
} | ||
} | ||
} catch (err) { | ||
return filenames.length > 0 | ||
? await this.readAnyOf(filenames.slice(1, filenames.length)) | ||
: undefined; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,16 @@ | ||
export class ReaderFileLackPersmissionsError extends Error { | ||
constructor( | ||
public readonly filePath: string, | ||
public readonly fsErrorCode: string, | ||
) { | ||
super(`File ${filePath} lacks read permissions!`); | ||
} | ||
} | ||
|
||
export interface Reader { | ||
list(): string[] | Promise<string[]>; | ||
read(name: string): string | Promise<string>; | ||
readAnyOf(filenames: string[]): string | Promise<string | undefined>; | ||
list(): string[]; | ||
read(name: string): string; | ||
readAnyOf( | ||
filenames: string[], | ||
): string | undefined | ReaderFileLackPersmissionsError; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters