Skip to content

Commit

Permalink
feat:command help and version
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangnanscu committed Oct 11, 2024
1 parent b78d0aa commit 7f371f4
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 86 deletions.
82 changes: 0 additions & 82 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ js2lua(`let a = 1`, { importStatementHoisting: true });
```
### lua
```lua

(function()
print(1)
end)();
Expand Down Expand Up @@ -150,10 +149,6 @@ const e = 1, f = 'a', { o1, o2: o22 } = object, [a1, a2] = array
```
### lua
```lua




local u;
local v;
u, v = unpack(array{
Expand Down Expand Up @@ -267,7 +262,6 @@ p1.echoNumbersLength.apply(p2, [1, 2])
```
### lua
```lua

local BasePosition = setmetatable({}, {
__call = function(t)
local self = t:new();
Expand Down Expand Up @@ -368,10 +362,7 @@ export function foo() { }
```
### lua
```lua


local _M = {}

local c = 1;
local d = 2;
local e = 3;
Expand Down Expand Up @@ -429,10 +420,6 @@ Echo.prototype.echoY = function () {
```
### lua
```lua




local foo = {
bar = function ()
end
Expand Down Expand Up @@ -565,10 +552,6 @@ if (a === 1) {
```
### lua
```lua




local a = 1;
if a == 1 then
print(a)
Expand Down Expand Up @@ -605,7 +588,6 @@ import d, { e as eAlias, f } from "bar"
```
### lua
```lua

local g = require("bar").default;
local foo = require("bar").foo;
local b = require("bar").a;
Expand All @@ -626,10 +608,6 @@ const i = a[0]
```
### lua
```lua




local a = array{};
local i = a[1]
```
Expand All @@ -647,10 +625,6 @@ const arr = [true, false]
```
### lua
```lua




local Obj = {
["and"] = 'real'
};
Expand Down Expand Up @@ -699,10 +673,6 @@ for(;;) {
```
### lua
```lua




local arr = array{};
do
local i = 0
Expand Down Expand Up @@ -754,10 +724,6 @@ let [ok4, ...res4] = xpcall(foo)
```
### lua
```lua




local ok, res = pcall(foo);
local ok2, res2 = xpcall(foo);
local e1, e2 = unpack(bar);
Expand Down Expand Up @@ -804,10 +770,6 @@ const d = {
```
### lua
```lua




local foo = 'bar';
local d1 = {
foo = 1,
Expand Down Expand Up @@ -885,11 +847,7 @@ a **= 2
```
### lua
```lua

local bit = require("bit")



local obj = {};
local a = 1;
type(a);
Expand Down Expand Up @@ -1011,10 +969,6 @@ d.n ??= 100;
```
### lua
```lua




local a = 1;
local b = 'foo';
local obj = {};
Expand Down Expand Up @@ -1094,10 +1048,6 @@ const constraints = {
```
### lua
```lua




local foo = {};
foo.bar = nil;
print(# foo);
Expand Down Expand Up @@ -1127,10 +1077,6 @@ const s6 = a + b + c + d + f
```
### lua
```lua




local s1 = b + c;
local s2 = b .. 'c';
local s3 = 'c' .. b;
Expand All @@ -1149,10 +1095,6 @@ const { x: k1, y: k2, ...k } = { ...d, foo: 'bar' }
```
### lua
```lua




local a = array{
1,
2,
Expand Down Expand Up @@ -1209,10 +1151,6 @@ const s = `1.${2}.3.${'4'}.${foo}`
```
### lua
```lua




local foo = 5;
local s = string.format([=[1.%s.3.%s.%s]=], 2, '4', foo)
```
Expand Down Expand Up @@ -1243,10 +1181,6 @@ switch (c) {
```
### lua
```lua




local c = 'v2';
repeat
local caseExp = c
Expand Down Expand Up @@ -1289,10 +1223,6 @@ if (test) {
```
### lua
```lua




local test = '';
if test then
error('!')
Expand Down Expand Up @@ -1326,12 +1256,8 @@ Array.isArray(1)
```
### lua
```lua

local isarray = require("table.isarray");
local cjson = require("cjson")



local a = {
b = ''
};
Expand Down Expand Up @@ -1365,10 +1291,6 @@ try {
```
### lua
```lua




local ok , _err = pcall(function()
local res = math.floor('fooo');
print(res)
Expand Down Expand Up @@ -1402,10 +1324,6 @@ if (--i) {
```
### lua
```lua




i = i + 1;
i = i - 1;
local a = (function ()
Expand Down
70 changes: 68 additions & 2 deletions bin/js2lua.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,82 @@
import { js2lua, defaultOptions } from "../src/js2lua.mjs";
import fs from "fs";
import yargsParser from "yargs-parser";
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import { createRequire } from 'module';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const require = createRequire(import.meta.url);

const packageJson = require(join(__dirname, '..', 'package.json'));
const version = packageJson.version;

const helpMessage = `
Usage: js2lua [options] <file...>
Options:
-h, --help Show help information
-v, --version Show version information
Default options:
${Object.entries(defaultOptions).map(([key, value]) => {
const padding = Math.max(0, 35 - key.length);
return ` --${key}${' '.repeat(padding)}${value}`;
}).join('\n')}
To disable an option, use the --no- prefix. For example:
js2lua --no-debug foo.js
Option descriptions:
--debug Enable debug mode, print more information
--tagArrayExpression transform [] to array {} instead of {}
--useColonOnMethod Use colon for method callsf
--importStatementHoisting Hoist import statements
--transform$SymbolToDollar Transform $ symbol to _DOLLAR_
--transformToString Transform toString method
--transformString Transform String constructor
--transformJSONStringify Transform JSON.stringify
--transformJSONParse Transform JSON.parse
--transformParseFloat Transform parseFloat
--transformParseInt Transform parseInt
--transformNumber Transform Number constructor
--transformIsArray Transform Array.isArray
--transformConsoleLog Transform console.log
--moduleExportsToReturn Convert module.exports to return statement
--index0To1 Change index from 0-based to 1-based
--tryTranslateClass Attempt to translate classes
--disableUpdateExpressionCallback Disable update expression callback
--renameCatchErrorIfNeeded Rename catch error if needed
--disableClassCall Disable class calls
`;

const argv = yargsParser(process.argv.slice(2), {
boolean: Object.keys(defaultOptions),
alias: {
// o1: "option1",
h: "help",
v: "version"
},
});

if (argv.help) {
console.log(helpMessage);
process.exit(0);
}

if (argv.version) {
console.log(`js2lua version ${version}`);
process.exit(0);
}

const files = argv._;
// console.log(argv);

if (files.length === 0) {
console.error("Error: Please specify at least one input file");
console.log("Use --help to see help information");
process.exit(1);
}

files.forEach((file) => {
const jsCode = fs.readFileSync(file, "utf8");
const luaCode = js2lua(jsCode, argv);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@xiangnanscu/js2lua",
"version": "0.46.0",
"version": "0.47.0",
"type": "module",
"description": "Writing LuaJIT with the expressiveness of JavaScript.",
"main": "src/js2lua.mjs",
Expand Down
2 changes: 1 addition & 1 deletion src/js2lua.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1258,6 +1258,6 @@ function js2lua(s, opts) {
opts.debug && p(luacode);
// return formatText(luacode);
// return removeWatermark(Beautify(luacode, {}));
return luamin.Beautify(luacode, {})
return luamin.Beautify(luacode, {}).trim()
}
export { defaultOptions, js2lua, js2ast };

0 comments on commit 7f371f4

Please sign in to comment.