Skip to content

Commit 26b05e1

Browse files
committed
Added dontRequireLuaLib
More cleanup Removed usage from readme
1 parent 6f8d154 commit 26b05e1

File tree

2 files changed

+52
-75
lines changed

2 files changed

+52
-75
lines changed

README.md

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,6 @@ More detailed documentation and info on writing declarations can be found [on th
2020

2121
`tstl -p path/to/tsconfig.json`
2222

23-
**Usage**
24-
```
25-
tstl [options] [files...]
26-
27-
In addition to the options listed below you can also pass options for the
28-
typescript compiler (For a list of options use tsc -h).
29-
30-
NOTES:
31-
- The tsc options might have no effect.
32-
- Options in tsconfig.json are prioritized.
33-
34-
Options:
35-
--lt, --luaTarget Specify Lua target version.
36-
[string] [choices: "JIT", "5.3"] [default: "JIT"]
37-
--ah, --addHeader Specify if a header will be added to compiled files.
38-
[boolean] [default: false]
39-
-h, --help Show help [boolean]
40-
-v, --version Show version number [boolean]
41-
42-
Examples:
43-
tstl path/to/file.ts [...] Compile files
44-
tstl -p path/to/tsconfig.json Compile project
45-
```
46-
4723
**Example tsconfig.json**
4824
```
4925
{

src/CommandLineParser.ts

Lines changed: 52 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -16,43 +16,43 @@ export interface ParsedCommandLine extends ts.ParsedCommandLine {
1616
}
1717

1818
const optionDeclarations: { [key: string]: yargs.Options } = {
19-
'lt': {
20-
alias: 'luaTarget',
19+
'luaTarget': {
20+
alias: 'lt',
2121
choices: ['JIT', '5.3'],
2222
default: 'JIT',
2323
describe: 'Specify Lua target version.',
2424
type: 'string'
2525
},
26-
'ah': {
27-
alias: 'addHeader',
26+
'addHeader': {
27+
alias: 'ah',
2828
describe: 'Specify if a header will be added to compiled files.',
2929
default: false,
3030
type: 'boolean'
3131
},
32-
'h': {
33-
alias: 'help'
32+
'dontRequireLuaLib': {
33+
describe: 'Dont require lua library that enables advanced Typescipt/JS functionality.',
34+
default: false,
35+
type: 'boolean'
3436
},
35-
'v': {
36-
alias: 'version'
37-
}
3837
};
3938

4039

4140
/**
4241
* Pares the supplied arguments.
4342
* The result will include arguments supplied via CLI and arguments from tsconfig.
4443
*/
45-
export function parseCommandLine(args: ReadonlyArray<string>) : ParsedCommandLine {
46-
const argv = yargs
47-
.usage(dedent(`tstl [options] [files...]
44+
export function parseCommandLine(args: ReadonlyArray<string>): ParsedCommandLine {
45+
const parsedArgs = yargs
46+
.usage(dedent(`Syntax: tstl [options] [files...]
4847
4948
In addition to the options listed below you can also pass options for the typescript compiler (For a list of options use tsc -h).
5049
51-
NOTES:
52-
- The tsc options might have no effect.
53-
- Options in tsconfig.json are prioritized.`))
50+
Notes:
51+
- The tsc options might have no effect.
52+
- Options in tsconfig.json are prioritized.`))
5453
.example('tstl path/to/file.ts [...]', 'Compile files')
5554
.example('tstl -p path/to/tsconfig.json', 'Compile project')
55+
.wrap(yargs.terminalWidth())
5656
.options(optionDeclarations)
5757
.argv;
5858

@@ -61,6 +61,10 @@ export function parseCommandLine(args: ReadonlyArray<string>) : ParsedCommandLin
6161
// Run diagnostics to check for invalid tsc/tstl options
6262
runDiagnostics(commandLine);
6363

64+
// Add TSTL options from CLI
65+
addTSTLOptions(commandLine, parsedArgs);
66+
67+
// Load config
6468
if (commandLine.options.project) {
6569
findConfigFile(commandLine);
6670
let configPath = commandLine.options.project;
@@ -69,13 +73,11 @@ export function parseCommandLine(args: ReadonlyArray<string>) : ParsedCommandLin
6973
commandLine = ts.parseJsonConfigFileContent(configJson.config, ts.sys, path.dirname(configPath), commandLine.options);
7074
}
7175

72-
// Add compiler options that are ignored by TS parsers
73-
for (const arg in commandLine.raw) {
74-
// we dont have to check if the options is vlaid becuase we already validated at this point.
75-
if (getOptionNameMap()[arg]) {
76-
commandLine.options[arg] = commandLine.raw[arg];
77-
}
78-
}
76+
// Add TSTL options from tsconfig
77+
addTSTLOptions(commandLine);
78+
79+
// Run diagnostics again to check for errors in tsconfig
80+
runDiagnostics(commandLine);
7981

8082
if (commandLine.options.project && !commandLine.options.rootDir) {
8183
commandLine.options.rootDir = path.dirname(commandLine.options.project);
@@ -89,9 +91,6 @@ export function parseCommandLine(args: ReadonlyArray<string>) : ParsedCommandLin
8991
commandLine.options.outDir = commandLine.options.rootDir;
9092
}
9193

92-
// Run diagnostics again to check for errors in tsconfig
93-
runDiagnostics(commandLine);
94-
9594
return commandLine;
9695
}
9796

@@ -101,53 +100,55 @@ interface OptionNameMap {
101100

102101
let optionNameMapCache: OptionNameMap;
103102

104-
/**
105-
* Gets a list of valid tstl options including aliases
106-
* That can be used for arguemtn checking.
107-
*/
108-
function getOptionNameMap(): OptionNameMap{
109-
if (optionNameMapCache) {
110-
return optionNameMapCache;
111-
}
112-
let optNameMap: OptionNameMap = {};
113-
for (let key in optionDeclarations) {
114-
optNameMap[key] = true
115-
let alias = optionDeclarations[key].alias;
116-
if (typeof alias === "string") {
117-
optNameMap[alias] = true;
118-
} else {
119-
alias.forEach((val) => optNameMap[val] = true);
103+
function addTSTLOptions(commandLine: ts.ParsedCommandLine, additionalArgs?: yargs.Arguments) {
104+
additionalArgs = additionalArgs ? additionalArgs : commandLine.raw
105+
// Add compiler options that are ignored by TS parsers
106+
for (const arg in additionalArgs) {
107+
// we dont have to check if the options is vlaid becuase we already validated at this point.
108+
if (optionDeclarations[arg]) {
109+
commandLine.options[arg] = additionalArgs[arg];
120110
}
121111
}
122-
optionNameMapCache = optNameMap;
123-
return optNameMap;
124112
}
125113

126114
/** Check the current state of the ParsedCommandLine for errors */
127115
function runDiagnostics(commandLine: ts.ParsedCommandLine) {
128116
const tsInvalidCompilerOptionErrorCode = 5023;
129117

130118
if (commandLine.errors.length !== 0) {
119+
// Generate a list of valid option names and aliases
120+
let optionNames = [];
121+
for (let key in optionDeclarations) {
122+
optionNames.push(key);
123+
let alias = optionDeclarations[key].alias;
124+
if (alias) {
125+
if (typeof alias === "string") {
126+
optionNames.push(alias);
127+
} else {
128+
optionNames.push(...alias);
129+
}
130+
}
131+
}
132+
131133
commandLine.errors.forEach((err) => {
132134
let ignore = false;
133135
// Ignore errors caused by tstl specific compiler options
134136
if (err.code == tsInvalidCompilerOptionErrorCode) {
135-
for (const key in getOptionNameMap()) {
136-
if (err.messageText.toString().indexOf(key) !== -1) {
137+
for (const optionName of optionNames) {
138+
if (err.messageText.toString().indexOf(optionName) !== -1) {
137139
ignore = true;
138140
}
139141
}
140-
}
141-
142-
if (!ignore) {
143-
console.log(`error TS${err.code}: ${err.messageText}`);
144-
process.exit(1);
142+
if (!ignore) {
143+
console.log(`error TS${err.code}: ${err.messageText}`);
144+
process.exit(1);
145+
}
145146
}
146147
});
147148
}
148149
}
149150

150-
/** Find configFile, function form ts api seems to be broken? */
151+
/** Find configFile, function from ts api seems to be broken? */
151152
function findConfigFile(commandLine: ts.ParsedCommandLine) {
152153
let configPath = path.isAbsolute(commandLine.options.project) ? commandLine.options.project : path.join(process.cwd(), commandLine.options.project);
153154
if (fs.statSync(configPath).isDirectory()) {

0 commit comments

Comments
 (0)