diff --git a/common/web/types/src/kpj/keyman-developer-project.ts b/common/web/types/src/kpj/keyman-developer-project.ts index 9ba1b299c0..ce9f78cf59 100644 --- a/common/web/types/src/kpj/keyman-developer-project.ts +++ b/common/web/types/src/kpj/keyman-developer-project.ts @@ -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) { } diff --git a/common/web/types/src/util/file-types.ts b/common/web/types/src/util/file-types.ts index 230792d9b7..ebad759638 100644 --- a/common/web/types/src/util/file-types.ts +++ b/common/web/types/src/util/file-types.ts @@ -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)) ??