chore(developer): handle unknown file extensions in file types

Projects can contain any type of file, but the fromFilename file type
utility function would only handle known source and binary file types.
This commit is contained in:
Marc Durdin 2023-08-02 12:32:07 +07:00
parent d8e03e0695
commit c20a670f91
2 changed files with 20 additions and 3 deletions

View file

@ -138,7 +138,7 @@ export class KeymanDeveloperProjectFile10 implements KeymanDeveloperProjectFile
return this.callbacks.path.basename(this.filePath);
}
get fileType(): string {
return KeymanFileTypes.sourceTypeFromFilename(this.filename);
return KeymanFileTypes.fromFilename(this.filename);
}
details: KeymanDeveloperProjectFileDetail_Kmn & KeymanDeveloperProjectFileDetail_Kps; // 1.0 only
childFiles: KeymanDeveloperProjectFile[]; // 1.0 only
@ -156,7 +156,7 @@ export class KeymanDeveloperProjectFile20 implements KeymanDeveloperProjectFile
return this.callbacks.path.basename(this.filePath);
}
get fileType() {
return KeymanFileTypes.sourceTypeFromFilename(this.filename);
return KeymanFileTypes.fromFilename(this.filename);
}
constructor(public readonly filePath: string, private readonly callbacks: CompilerCallbacks) {
}

View file

@ -68,6 +68,23 @@ export type All = Source | Binary;
*/
export type Any = string;
/**
* Gets the file type based on extension, dealing with multi-part file
* extensions. Does not sniff contents of file or assume file existence. Does
* transform upper-cased file extensions to lower-case.
* @param filename
* @returns file extension, or `""` if no extension. Note that this return value
* differs from the other, more-specific fromFilename functions below,
* which return `null` if a supported extension is not found.
*/
export function fromFilename(filename: string): Binary | Source | Any {
const result =
sourceOrBinaryTypeFromFilename(filename) ??
filename.match(/\.[^\.]+$/)?.[0] ??
"";
return result;
}
/**
* Gets the file type based on extension, dealing with multi-part file
* extensions. Does not sniff contents of file or assume file existence.
@ -75,7 +92,7 @@ export type Any = string;
* @param filename
* @returns file type, or `null` if not found
*/
export function fromFilename(filename: string): Binary | Source {
export function sourceOrBinaryTypeFromFilename(filename: string): Binary | Source {
filename = filename.toLowerCase();
const result =
ALL_SOURCE.find(type => filename.endsWith(type)) ??