Skip to content

Commit 7f371f4

Browse files
committed
feat:command help and version
1 parent b78d0aa commit 7f371f4

File tree

4 files changed

+70
-86
lines changed

4 files changed

+70
-86
lines changed

README.md

-82
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ js2lua(`let a = 1`, { importStatementHoisting: true });
122122
```
123123
### lua
124124
```lua
125-
126125
(function()
127126
print(1)
128127
end)();
@@ -150,10 +149,6 @@ const e = 1, f = 'a', { o1, o2: o22 } = object, [a1, a2] = array
150149
```
151150
### lua
152151
```lua
153-
154-
155-
156-
157152
local u;
158153
local v;
159154
u, v = unpack(array{
@@ -267,7 +262,6 @@ p1.echoNumbersLength.apply(p2, [1, 2])
267262
```
268263
### lua
269264
```lua
270-
271265
local BasePosition = setmetatable({}, {
272266
__call = function(t)
273267
local self = t:new();
@@ -368,10 +362,7 @@ export function foo() { }
368362
```
369363
### lua
370364
```lua
371-
372-
373365
local _M = {}
374-
375366
local c = 1;
376367
local d = 2;
377368
local e = 3;
@@ -429,10 +420,6 @@ Echo.prototype.echoY = function () {
429420
```
430421
### lua
431422
```lua
432-
433-
434-
435-
436423
local foo = {
437424
bar = function ()
438425
end
@@ -565,10 +552,6 @@ if (a === 1) {
565552
```
566553
### lua
567554
```lua
568-
569-
570-
571-
572555
local a = 1;
573556
if a == 1 then
574557
print(a)
@@ -605,7 +588,6 @@ import d, { e as eAlias, f } from "bar"
605588
```
606589
### lua
607590
```lua
608-
609591
local g = require("bar").default;
610592
local foo = require("bar").foo;
611593
local b = require("bar").a;
@@ -626,10 +608,6 @@ const i = a[0]
626608
```
627609
### lua
628610
```lua
629-
630-
631-
632-
633611
local a = array{};
634612
local i = a[1]
635613
```
@@ -647,10 +625,6 @@ const arr = [true, false]
647625
```
648626
### lua
649627
```lua
650-
651-
652-
653-
654628
local Obj = {
655629
["and"] = 'real'
656630
};
@@ -699,10 +673,6 @@ for(;;) {
699673
```
700674
### lua
701675
```lua
702-
703-
704-
705-
706676
local arr = array{};
707677
do
708678
local i = 0
@@ -754,10 +724,6 @@ let [ok4, ...res4] = xpcall(foo)
754724
```
755725
### lua
756726
```lua
757-
758-
759-
760-
761727
local ok, res = pcall(foo);
762728
local ok2, res2 = xpcall(foo);
763729
local e1, e2 = unpack(bar);
@@ -804,10 +770,6 @@ const d = {
804770
```
805771
### lua
806772
```lua
807-
808-
809-
810-
811773
local foo = 'bar';
812774
local d1 = {
813775
foo = 1,
@@ -885,11 +847,7 @@ a **= 2
885847
```
886848
### lua
887849
```lua
888-
889850
local bit = require("bit")
890-
891-
892-
893851
local obj = {};
894852
local a = 1;
895853
type(a);
@@ -1011,10 +969,6 @@ d.n ??= 100;
1011969
```
1012970
### lua
1013971
```lua
1014-
1015-
1016-
1017-
1018972
local a = 1;
1019973
local b = 'foo';
1020974
local obj = {};
@@ -1094,10 +1048,6 @@ const constraints = {
10941048
```
10951049
### lua
10961050
```lua
1097-
1098-
1099-
1100-
11011051
local foo = {};
11021052
foo.bar = nil;
11031053
print(# foo);
@@ -1127,10 +1077,6 @@ const s6 = a + b + c + d + f
11271077
```
11281078
### lua
11291079
```lua
1130-
1131-
1132-
1133-
11341080
local s1 = b + c;
11351081
local s2 = b .. 'c';
11361082
local s3 = 'c' .. b;
@@ -1149,10 +1095,6 @@ const { x: k1, y: k2, ...k } = { ...d, foo: 'bar' }
11491095
```
11501096
### lua
11511097
```lua
1152-
1153-
1154-
1155-
11561098
local a = array{
11571099
1,
11581100
2,
@@ -1209,10 +1151,6 @@ const s = `1.${2}.3.${'4'}.${foo}`
12091151
```
12101152
### lua
12111153
```lua
1212-
1213-
1214-
1215-
12161154
local foo = 5;
12171155
local s = string.format([=[1.%s.3.%s.%s]=], 2, '4', foo)
12181156
```
@@ -1243,10 +1181,6 @@ switch (c) {
12431181
```
12441182
### lua
12451183
```lua
1246-
1247-
1248-
1249-
12501184
local c = 'v2';
12511185
repeat
12521186
local caseExp = c
@@ -1289,10 +1223,6 @@ if (test) {
12891223
```
12901224
### lua
12911225
```lua
1292-
1293-
1294-
1295-
12961226
local test = '';
12971227
if test then
12981228
error('!')
@@ -1326,12 +1256,8 @@ Array.isArray(1)
13261256
```
13271257
### lua
13281258
```lua
1329-
13301259
local isarray = require("table.isarray");
13311260
local cjson = require("cjson")
1332-
1333-
1334-
13351261
local a = {
13361262
b = ''
13371263
};
@@ -1365,10 +1291,6 @@ try {
13651291
```
13661292
### lua
13671293
```lua
1368-
1369-
1370-
1371-
13721294
local ok , _err = pcall(function()
13731295
local res = math.floor('fooo');
13741296
print(res)
@@ -1402,10 +1324,6 @@ if (--i) {
14021324
```
14031325
### lua
14041326
```lua
1405-
1406-
1407-
1408-
14091327
i = i + 1;
14101328
i = i - 1;
14111329
local a = (function ()

bin/js2lua.js

+68-2
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,82 @@
33
import { js2lua, defaultOptions } from "../src/js2lua.mjs";
44
import fs from "fs";
55
import yargsParser from "yargs-parser";
6+
import { fileURLToPath } from 'url';
7+
import { dirname, join } from 'path';
8+
import { createRequire } from 'module';
9+
10+
const __filename = fileURLToPath(import.meta.url);
11+
const __dirname = dirname(__filename);
12+
const require = createRequire(import.meta.url);
13+
14+
const packageJson = require(join(__dirname, '..', 'package.json'));
15+
const version = packageJson.version;
16+
17+
const helpMessage = `
18+
Usage: js2lua [options] <file...>
19+
20+
Options:
21+
-h, --help Show help information
22+
-v, --version Show version information
23+
24+
Default options:
25+
${Object.entries(defaultOptions).map(([key, value]) => {
26+
const padding = Math.max(0, 35 - key.length);
27+
return ` --${key}${' '.repeat(padding)}${value}`;
28+
}).join('\n')}
29+
30+
To disable an option, use the --no- prefix. For example:
31+
js2lua --no-debug foo.js
32+
33+
Option descriptions:
34+
--debug Enable debug mode, print more information
35+
--tagArrayExpression transform [] to array {} instead of {}
36+
--useColonOnMethod Use colon for method callsf
37+
--importStatementHoisting Hoist import statements
38+
--transform$SymbolToDollar Transform $ symbol to _DOLLAR_
39+
--transformToString Transform toString method
40+
--transformString Transform String constructor
41+
--transformJSONStringify Transform JSON.stringify
42+
--transformJSONParse Transform JSON.parse
43+
--transformParseFloat Transform parseFloat
44+
--transformParseInt Transform parseInt
45+
--transformNumber Transform Number constructor
46+
--transformIsArray Transform Array.isArray
47+
--transformConsoleLog Transform console.log
48+
--moduleExportsToReturn Convert module.exports to return statement
49+
--index0To1 Change index from 0-based to 1-based
50+
--tryTranslateClass Attempt to translate classes
51+
--disableUpdateExpressionCallback Disable update expression callback
52+
--renameCatchErrorIfNeeded Rename catch error if needed
53+
--disableClassCall Disable class calls
54+
`;
655

756
const argv = yargsParser(process.argv.slice(2), {
857
boolean: Object.keys(defaultOptions),
958
alias: {
10-
// o1: "option1",
59+
h: "help",
60+
v: "version"
1161
},
1262
});
1363

64+
if (argv.help) {
65+
console.log(helpMessage);
66+
process.exit(0);
67+
}
68+
69+
if (argv.version) {
70+
console.log(`js2lua version ${version}`);
71+
process.exit(0);
72+
}
73+
1474
const files = argv._;
15-
// console.log(argv);
75+
76+
if (files.length === 0) {
77+
console.error("Error: Please specify at least one input file");
78+
console.log("Use --help to see help information");
79+
process.exit(1);
80+
}
81+
1682
files.forEach((file) => {
1783
const jsCode = fs.readFileSync(file, "utf8");
1884
const luaCode = js2lua(jsCode, argv);

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@xiangnanscu/js2lua",
3-
"version": "0.46.0",
3+
"version": "0.47.0",
44
"type": "module",
55
"description": "Writing LuaJIT with the expressiveness of JavaScript.",
66
"main": "src/js2lua.mjs",

src/js2lua.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1258,6 +1258,6 @@ function js2lua(s, opts) {
12581258
opts.debug && p(luacode);
12591259
// return formatText(luacode);
12601260
// return removeWatermark(Beautify(luacode, {}));
1261-
return luamin.Beautify(luacode, {})
1261+
return luamin.Beautify(luacode, {}).trim()
12621262
}
12631263
export { defaultOptions, js2lua, js2ast };

0 commit comments

Comments
 (0)