@@ -2,7 +2,6 @@ import { EventEmitter } from "events";
22import fs from "fs" ;
33import path from "path" ;
44
5- import type { IAppInfo } from "@rocket.chat/apps-engine/definition/metadata" ;
65import type {
76 CompilerOptions ,
87 EmitOutput ,
@@ -59,10 +58,24 @@ export class TypescriptCompiler {
5958 appInfo,
6059 sourceFiles : files ,
6160 } : IAppSource ) : ICompilerResult {
61+ // Normalize all file keys, file names, and classFile to POSIX separators
62+ // once up front so getScriptFileNames(), getScriptVersion(), getScriptSnapshot(),
63+ // and all subsequent lookups into result.files operate on consistent keys.
64+ const posixClassFile = appInfo . classFile . replace ( / \\ / g, "/" ) ;
65+ const normalizedFiles : IMapCompilerFile = { } ;
66+ for ( const [ key , file ] of Object . entries ( files ) ) {
67+ const posixKey = key . replace ( / \\ / g, "/" ) ;
68+ normalizedFiles [ posixKey ] = {
69+ ...file ,
70+ name : file . name . replace ( / \\ / g, "/" ) ,
71+ } ;
72+ }
73+ files = normalizedFiles ;
74+
6275 if (
63- ! appInfo . classFile ||
64- ! files [ appInfo . classFile ] ||
65- ! this . isValidFile ( files [ appInfo . classFile ] )
76+ ! posixClassFile ||
77+ ! files [ posixClassFile ] ||
78+ ! this . isValidFile ( files [ posixClassFile ] )
6679 ) {
6780 throw new Error (
6881 `Invalid App package. Could not find the classFile (${ appInfo . classFile } ) file.` ,
@@ -91,7 +104,9 @@ export class TypescriptCompiler {
91104 throw new Error ( `Invalid TypeScript file: "${ key } ".` ) ;
92105 }
93106
94- result . files [ key ] . name = path . normalize ( result . files [ key ] . name ) ;
107+ result . files [ key ] . name = path . posix . normalize (
108+ result . files [ key ] . name ,
109+ ) ;
95110 } ) ;
96111
97112 let hasExternalDependencies = false ;
@@ -121,13 +136,13 @@ export class TypescriptCompiler {
121136 const host = {
122137 getScriptFileNames : ( ) => Object . keys ( result . files ) ,
123138 getScriptVersion : ( fileName ) => {
124- fileName = path . normalize ( fileName ) ;
139+ fileName = path . posix . normalize ( fileName . replace ( / \\ / g , "/" ) ) ;
125140 const file =
126141 result . files [ fileName ] || this . getLibraryFile ( fileName ) ;
127142 return file ?. version ?. toString ( ) ;
128143 } ,
129144 getScriptSnapshot : ( fileName ) => {
130- fileName = path . normalize ( fileName ) ;
145+ fileName = path . posix . normalize ( fileName . replace ( / \\ / g , "/" ) ) ;
131146 const file =
132147 result . files [ fileName ] || this . getLibraryFile ( fileName ) ;
133148
@@ -217,7 +232,7 @@ export class TypescriptCompiler {
217232
218233 result . implemented = this . getImplementedInterfaces (
219234 languageService ,
220- appInfo ,
235+ posixClassFile ,
221236 ) ;
222237
223238 Object . defineProperty ( result , "diagnostics" , {
@@ -240,11 +255,10 @@ export class TypescriptCompiler {
240255 file . compiled = output . outputFiles [ 0 ] . text ;
241256 } ) ;
242257
243- result . mainFile =
244- result . files [ appInfo . classFile . replace ( / \. t s $ / , ".js" ) ] ;
258+ result . mainFile = result . files [ posixClassFile . replace ( / \. t s $ / , ".js" ) ] ;
245259
246260 this . appValidator . checkInheritance (
247- appInfo . classFile . replace ( / \. t s $ / , "" ) ,
261+ posixClassFile . replace ( / \. t s $ / , "" ) ,
248262 result ,
249263 ) ;
250264
@@ -317,10 +331,20 @@ export class TypescriptCompiler {
317331 }
318332
319333 private resolvePath ( containingFile : string , moduleName : string ) : string {
320- const currentFolderPath = path
321- . dirname ( containingFile )
322- . replace ( this . sourcePath . replace ( / \/ $ / , "" ) , "" ) ;
323- const modulePath = path . join ( currentFolderPath , moduleName ) ;
334+ const posixSourcePath = this . sourcePath
335+ . replace ( / \\ / g, "/" )
336+ . replace ( / \/ $ / , "" ) ;
337+ const posixContainingDir = path . posix . dirname (
338+ containingFile . replace ( / \\ / g, "/" ) ,
339+ ) ;
340+ const currentFolderPath = posixContainingDir . replace (
341+ posixSourcePath ,
342+ "" ,
343+ ) ;
344+ const modulePath = path . posix . join (
345+ currentFolderPath || "." ,
346+ moduleName ,
347+ ) ;
324348
325349 // Let's ensure we search for the App's modules first
326350 const transformedModule =
@@ -332,13 +356,11 @@ export class TypescriptCompiler {
332356
333357 private getImplementedInterfaces (
334358 languageService : LanguageService ,
335- appInfo : IAppInfo ,
359+ classFile : string ,
336360 ) : ICompilerResult [ "implemented" ] {
337361 const result : ICompilerResult [ "implemented" ] = [ ] ;
338362
339- const src = languageService
340- . getProgram ( )
341- . getSourceFile ( appInfo . classFile ) ;
363+ const src = languageService . getProgram ( ) . getSourceFile ( classFile ) ;
342364
343365 this . ts . forEachChild ( src , ( n ) => {
344366 if ( ! this . ts . isClassDeclaration ( n ) ) {
@@ -370,7 +392,7 @@ export class TypescriptCompiler {
370392 return undefined ;
371393 }
372394
373- const norm = path . normalize ( fileName ) ;
395+ const norm = path . posix . normalize ( fileName . replace ( / \\ / g , "/" ) ) ;
374396
375397 if ( this . libraryFiles [ norm ] ) {
376398 return this . libraryFiles [ norm ] ;
@@ -394,10 +416,6 @@ export class TypescriptCompiler {
394416 return false ;
395417 }
396418
397- return (
398- file . name . trim ( ) !== "" &&
399- path . normalize ( file . name ) &&
400- file . content . trim ( ) !== ""
401- ) ;
419+ return file . name . trim ( ) !== "" && file . content . trim ( ) !== "" ;
402420 }
403421}
0 commit comments