Skip to content

Commit de5ec24

Browse files
committed
Refactor CommandLineParser and further CLI refactor
1 parent 02699cd commit de5ec24

File tree

10 files changed

+505
-597
lines changed

10 files changed

+505
-597
lines changed

src/CommandLineParser.ts

Lines changed: 139 additions & 294 deletions
Large diffs are not rendered by default.

src/LuaPrinter.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export class LuaPrinter {
117117

118118
let header = "";
119119

120-
if (this.options.noHeader === undefined || this.options.noHeader === false) {
120+
if (!this.options.noHeader) {
121121
header += `--[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]]\n`;
122122
}
123123

@@ -129,8 +129,11 @@ export class LuaPrinter {
129129
header += `require("lualib_bundle");\n`;
130130
}
131131
// Inline lualib features
132-
else if (this.options.luaLibImport === LuaLibImportKind.Inline && luaLibFeatures.size > 0)
133-
{
132+
else if (
133+
(this.options.luaLibImport === undefined ||
134+
this.options.luaLibImport === LuaLibImportKind.Inline) &&
135+
luaLibFeatures.size > 0
136+
) {
134137
header += "-- Lua Library inline imports\n";
135138
header += LuaLib.loadFeatures(luaLibFeatures);
136139
}

src/Transpile.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ export function getTranspileOutput({
9191
preEmitDiagnostics.push(...program.getSemanticDiagnostics());
9292
}
9393

94-
if (options.declaration || options.composite) {
94+
if (preEmitDiagnostics.length === 0 && (options.declaration || options.composite)) {
9595
preEmitDiagnostics.push(...program.getDeclarationDiagnostics());
9696
}
9797

98-
if (preEmitDiagnostics.filter(d => d.category === ts.DiagnosticCategory.Error).length > 0) {
98+
if (preEmitDiagnostics.length > 0) {
9999
return { diagnostics: preEmitDiagnostics, transpiledFiles };
100100
}
101101
}
@@ -180,11 +180,7 @@ export function getTranspileOutput({
180180

181181
programOptions.noEmit = programNoEmit;
182182

183-
if (
184-
noEmit ||
185-
(noEmitOnError &&
186-
diagnostics.filter(d => d.category === ts.DiagnosticCategory.Error).length > 0)
187-
) {
183+
if (noEmit || (noEmitOnError && diagnostics.length > 0)) {
188184
transpiledFiles.clear();
189185
}
190186

src/diagnostics.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import * as ts from "typescript";
2+
3+
export const watchErrorSummary = (errorCount: number): ts.Diagnostic => ({
4+
file: undefined,
5+
start: undefined,
6+
length: undefined,
7+
category: ts.DiagnosticCategory.Message,
8+
code: errorCount === 1 ? 6193 : 6194,
9+
messageText:
10+
errorCount === 1
11+
? "Found 1 error. Watching for file changes."
12+
: `Found ${errorCount} errors. Watching for file changes.`,
13+
});
14+
15+
const createCommandLineError = <Args extends any[]>(
16+
code: number,
17+
getMessage: (...args: Args) => string
18+
) => (...args: Args) => ({
19+
file: undefined,
20+
start: undefined,
21+
length: undefined,
22+
category: ts.DiagnosticCategory.Error,
23+
code,
24+
messageText: getMessage(...args),
25+
});
26+
27+
export const optionProjectCannotBeMixedWithSourceFilesOnACommandLine = createCommandLineError(
28+
5042,
29+
() => "Option 'project' cannot be mixed with source files on a command line."
30+
);
31+
32+
export const cannotFindATsconfigJsonAtTheSpecifiedDirectory = createCommandLineError(
33+
5057,
34+
(dir: string) => `Cannot find a tsconfig.json file at the specified directory: '${dir}'.`
35+
);
36+
37+
export const theSpecifiedPathDoesNotExist = createCommandLineError(
38+
5058,
39+
(dir: string) => `The specified path does not exist: '${dir}'.`
40+
);
41+
42+
export const compilerOptionExpectsAnArgument = createCommandLineError(
43+
6044,
44+
(name: string) => `Compiler option '${name}' expects an argument.`
45+
);
46+
47+
export const argumentForOptionMustBe = createCommandLineError(
48+
6046,
49+
(name: string, values: string) => `Argument for '${name}' option must be: ${values}.`
50+
);
51+
52+
export const optionBuildMustBeFirstCommandLineArgument = createCommandLineError(
53+
6369,
54+
() => "Option '--build' must be the first command line argument."
55+
);

src/index.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import * as fs from "fs";
22
import * as path from "path";
33
import * as ts from "typescript";
4-
import { parseConfigFileContent } from "./CommandLineParser";
4+
import { parseConfigFileWithSystem } from "./CommandLineParser";
55
import { CompilerOptions } from "./CompilerOptions";
66
import { getTranspileOutput, TranspilationResult, TranspiledFile } from "./Transpile";
77

8-
export { parseConfigFileContent } from "./CommandLineParser";
8+
export { parseCommandLine, ParsedCommandLine, updateParsedConfigFile } from "./CommandLineParser";
99
export { CompilerOptions, LuaLibImportKind, LuaTarget } from "./CompilerOptions";
1010
export * from "./Emit";
1111
export * from "./LuaAST";
@@ -30,17 +30,12 @@ export function transpileFiles(
3030
}
3131

3232
export function transpileProject(fileName: string, options?: CompilerOptions): TranspilationResult {
33-
const parseResult = parseConfigFileContent(
34-
fs.readFileSync(fileName, "utf8"),
35-
fileName,
36-
options
37-
);
38-
if (parseResult.isValid === false) {
39-
// TODO: Return diagnostics
40-
throw new Error(parseResult.errorMessage);
33+
const parseResult = parseConfigFileWithSystem(fileName, options);
34+
if (parseResult.errors.length > 0) {
35+
return { diagnostics: parseResult.errors, transpiledFiles: new Map() };
4136
}
4237

43-
return transpileFiles(parseResult.result.fileNames, parseResult.result.options);
38+
return transpileFiles(parseResult.fileNames, parseResult.options);
4439
}
4540

4641
const libCache: { [key: string]: ts.SourceFile } = {};

0 commit comments

Comments
 (0)