Skip to content

Commit 28b5b52

Browse files
committed
Fix invalid behavior with relative outFile and outDir
1 parent 52b5c50 commit 28b5b52

2 files changed

Lines changed: 21 additions & 19 deletions

File tree

src/Emit.ts

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,23 @@ export function emitTranspiledFiles(
1818
): OutputFile[] {
1919
let { rootDir, outDir, outFile, luaLibImport } = options;
2020

21-
if (rootDir === undefined) {
22-
const configFileName = options.configFilePath as string | undefined;
23-
// TODO: Use getCommonSourceDirectory
24-
rootDir = configFileName ? path.dirname(configFileName) : process.cwd();
25-
}
21+
const configFileName = options.configFilePath as string | undefined;
22+
// TODO: Use getCommonSourceDirectory
23+
const baseDir = configFileName ? path.dirname(configFileName) : process.cwd();
2624

27-
if (outDir === undefined) {
28-
outDir = rootDir;
29-
}
25+
rootDir = rootDir || baseDir;
26+
outDir = outDir ? path.resolve(baseDir, outDir) : rootDir;
3027

3128
const files: OutputFile[] = [];
3229
for (const [fileName, { lua, sourceMap, declaration, declarationMap }] of transpiledFiles) {
3330
let outPath = fileName;
3431
if (outDir !== rootDir) {
35-
const relativeSourcePath = path.resolve(fileName).replace(path.resolve(rootDir), "");
36-
outPath = path.join(outDir, relativeSourcePath);
32+
outPath = path.resolve(outDir, path.relative(rootDir, fileName));
3733
}
3834

3935
// change extension or rename to outFile
4036
if (outFile) {
41-
if (path.isAbsolute(outFile)) {
42-
outPath = outFile;
43-
} else {
44-
// append to workingDir or outDir
45-
outPath = path.resolve(outDir, outFile);
46-
}
37+
outPath = path.isAbsolute(outFile) ? outFile : path.resolve(baseDir, outFile);
4738
} else {
4839
outPath = trimExt(outPath) + ".lua";
4940
}
@@ -75,10 +66,9 @@ export function emitTranspiledFiles(
7566
);
7667
}
7768

78-
let outPath = path.resolve(path.join(rootDir, "lualib_bundle.lua"));
69+
let outPath = path.resolve(rootDir, "lualib_bundle.lua");
7970
if (outDir !== rootDir) {
80-
const relativeSourcePath = path.resolve(outPath).replace(path.resolve(rootDir), "");
81-
outPath = path.join(outDir, relativeSourcePath);
71+
outPath = path.join(outDir, path.relative(rootDir, outPath));
8272
}
8373

8474
files.push({ name: normalizeSlashes(outPath), text: lualibContent });

test/transpile/outFile.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,15 @@ test("should support outFile with declaration", () => {
3030
expect(diagnostics).not.toHaveDiagnostics();
3131
expect(emittedFiles).toEqual(["output.d.ts", "output.script"]);
3232
});
33+
34+
test("should resolve outFile relative to base directory", () => {
35+
jest.spyOn(process, "cwd").mockReturnValue(__dirname);
36+
const { diagnostics, emittedFiles, emitResult } = buildVirtualProject([inputFilePath], {
37+
outFile: "output.script",
38+
outDir: "out",
39+
declaration: true,
40+
});
41+
42+
expect(diagnostics).not.toHaveDiagnostics();
43+
expect(emittedFiles).toEqual(["output.d.ts", "output.script"]);
44+
});

0 commit comments

Comments
 (0)