Skip to content

Commit 5108c29

Browse files
committed
fix: windows packaging
1 parent ad92fbd commit 5108c29

7 files changed

Lines changed: 36 additions & 30 deletions

File tree

src/bundler/esbuild.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as os from "os";
21
import path from "path";
32
import type { OnLoadArgs, OnResolveArgs, PluginBuild } from "esbuild";
43
import { build } from "esbuild";
@@ -7,21 +6,15 @@ import type { ICompilerResult } from "../definition";
76
import type { IBundledCompilerResult } from "../definition/ICompilerResult";
87
import type { AppsEngineValidator } from "../compiler/AppsEngineValidator";
98

10-
const isWin = os.platform() === "win32";
11-
129
function normalizeAppModulePath(modulePath: string, parentDir: string): string {
1310
const isRelative = /\.\.?\//.test(modulePath);
1411

1512
if (!isRelative) {
1613
return modulePath;
1714
}
1815

19-
const baseDir = path.dirname(parentDir);
20-
const resolvedPath = isWin
21-
? path.join(baseDir, modulePath)
22-
: path.resolve("/", baseDir, modulePath).substring(1);
23-
24-
return `${resolvedPath}.js`;
16+
const baseDir = path.posix.dirname(parentDir);
17+
return `${path.posix.join(baseDir, modulePath)}.js`;
2518
}
2619

2720
export async function bundleCompilation(
@@ -70,13 +63,10 @@ export async function bundleCompilation(
7063

7164
if (isRelative) {
7265
// normalize into the key you used in r.files
73-
let modulePath = normalizeAppModulePath(
66+
const modulePath = normalizeAppModulePath(
7467
args.path,
7568
args.importer,
7669
);
77-
modulePath = modulePath
78-
.replace(/^:\\/, "")
79-
.replace(/\\/g, "/");
8070
if (r.files[modulePath]) {
8171
return {
8272
namespace: "app-source",

src/compiler/AppsEngineValidator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ export class AppsEngineValidator {
135135
return undefined;
136136
}
137137

138-
filepath = path.normalize(
139-
path.join(path.dirname(filename), filepath),
138+
filepath = path.posix.normalize(
139+
path.posix.join(path.posix.dirname(filename), filepath),
140140
);
141141

142142
// Handles import of other files in app's source

src/compiler/TscBasedCompiler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ export class TscBasedCompiler {
211211
withFileTypes: true,
212212
})) {
213213
const full = path.join(dir, entry.name);
214-
const rel = path.join(base, entry.name);
214+
const rel = path.posix.join(base, entry.name);
215215
if (entry.isDirectory()) {
216216
await collect(full, rel);
217217
} else if (entry.isFile() && rel.endsWith(".js")) {

src/compiler/TypescriptCompiler.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ export class TypescriptCompiler {
9191
throw new Error(`Invalid TypeScript file: "${key}".`);
9292
}
9393

94-
result.files[key].name = path.normalize(result.files[key].name);
94+
result.files[key].name = path.posix.normalize(
95+
result.files[key].name,
96+
);
9597
});
9698

9799
let hasExternalDependencies = false;
@@ -121,13 +123,13 @@ export class TypescriptCompiler {
121123
const host = {
122124
getScriptFileNames: () => Object.keys(result.files),
123125
getScriptVersion: (fileName) => {
124-
fileName = path.normalize(fileName);
126+
fileName = path.posix.normalize(fileName.replace(/\\/g, "/"));
125127
const file =
126128
result.files[fileName] || this.getLibraryFile(fileName);
127129
return file?.version?.toString();
128130
},
129131
getScriptSnapshot: (fileName) => {
130-
fileName = path.normalize(fileName);
132+
fileName = path.posix.normalize(fileName.replace(/\\/g, "/"));
131133
const file =
132134
result.files[fileName] || this.getLibraryFile(fileName);
133135

@@ -317,10 +319,20 @@ export class TypescriptCompiler {
317319
}
318320

319321
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);
322+
const posixSourcePath = this.sourcePath
323+
.replace(/\\/g, "/")
324+
.replace(/\/$/, "");
325+
const posixContainingDir = path.posix.dirname(
326+
containingFile.replace(/\\/g, "/"),
327+
);
328+
const currentFolderPath = posixContainingDir.replace(
329+
posixSourcePath,
330+
"",
331+
);
332+
const modulePath = path.posix.join(
333+
currentFolderPath || ".",
334+
moduleName,
335+
);
324336

325337
// Let's ensure we search for the App's modules first
326338
const transformedModule =
@@ -370,7 +382,7 @@ export class TypescriptCompiler {
370382
return undefined;
371383
}
372384

373-
const norm = path.normalize(fileName);
385+
const norm = path.posix.normalize(fileName.replace(/\\/g, "/"));
374386

375387
if (this.libraryFiles[norm]) {
376388
return this.libraryFiles[norm];
@@ -396,7 +408,7 @@ export class TypescriptCompiler {
396408

397409
return (
398410
file.name.trim() !== "" &&
399-
path.normalize(file.name) &&
411+
path.posix.normalize(file.name) &&
400412
file.content.trim() !== ""
401413
);
402414
}

src/compiler/getAppSource.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { promises as fs } from "fs";
2-
import { resolve, relative } from "path";
2+
import path from "path";
33
import type { IAppInfo } from "@rocket.chat/apps-engine/definition/metadata";
44

55
import type {
@@ -13,7 +13,7 @@ async function walkDirectory(directory: string): Promise<ICompilerFile[]> {
1313
const files = await Promise.all(
1414
dirents
1515
.map(async (dirent) => {
16-
const res = resolve(directory, dirent.name);
16+
const res = path.resolve(directory, dirent.name);
1717

1818
const dirsToIgnore = ["node_modules", ".git"];
1919
if (dirsToIgnore.some((dir) => res.includes(dir))) {
@@ -49,7 +49,9 @@ function filterProjectFiles(
4949
// Get the file names like it was inside the project's directory
5050
.map((file: ICompilerFile) => ({
5151
...file,
52-
name: relative(projectDirectory, file.name),
52+
name: path
53+
.relative(projectDirectory, file.name)
54+
.replace(/\\/g, "/"),
5355
}))
5456
);
5557
}

src/misc/Utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class Utilities {
5151
}
5252

5353
public static transformModuleForCustomRequire(moduleName: string): string {
54-
return `${path
54+
return `${path.posix
5555
.normalize(moduleName)
5656
.replace(/\.\.?\//g, "")
5757
.replace(/^\//, "")}.ts`;

src/packager/AppPackager.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ export class AppPackager {
9797

9898
await Promise.all(
9999
matches.map(async (realPath) => {
100-
const zipPath = path.relative(this.fd.folder, realPath);
100+
const zipPath = path
101+
.relative(this.fd.folder, realPath)
102+
.replace(/\\/g, "/");
101103

102104
const fileStat = await fs.stat(realPath);
103105

0 commit comments

Comments
 (0)