TypeScript代码批量转换成lua代码

2019-08-14  本文已影响0人  taiyosen

lua是一门很精巧的脚本语言,目前也颇受国内unity游戏开发者的欢迎。假如现在有一个TypeScript项目,想快速改成lua项目,手动修改明显太耗时间了,现在跟大家推荐一个开源node工具:ts2lua。
ts2lua的Github地址
使用ts2lua很简单,下面我们来测试一下。
首先,运行命令行cmd,建立一个目录ts2lua_test用于测试,使用npm安装ts2lua:

G:\>mkdir ts2lua_test

G:\>cd ts2lua_test

G:\ts2lua_test>npm i ts2lua
npm WARN saveError ENOENT: no such file or directory, open 'G:\package.json'
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN enoent ENOENT: no such file or directory, open 'G:\package.json'
npm WARN !invalid#1 No description
npm WARN !invalid#1 No repository field.
npm WARN !invalid#1 No README data
npm WARN !invalid#1 No license field.

+ ts2lua@1.0.13
added 7 packages from 7 contributors and audited 7 packages in 2.669s
found 0 vulnerabilities

接下来我们输入node进入交互模式,使用require命令来使用ts2lua:

G:ts2lua_test\>node
> ts2lua = require('ts2lua')
{ translate: [Function: translate],
  translateFiles: [Function: translateFiles] }
>

可以看到,ts2lua暴露了2个接口,分别是translatetranslateFiles,从方法名字可以看出来,一个用于转换ts语句,一个用于批量转换ts代码文件。先来试试translate方法:

> code = `
... function doStr(a: number, b: number): void {
... return a + b;
... }
... let c = doStr(1, 2);
... console.log('c = ' + c);`
'\nfunction doStr(a: number, b: number): void {\nreturn a + b;\n}\nlet c = doStr(1, 2);\nconsole.log(\'c = \' + c);'
> console.log(ts2lua.translate(code))
function doStr(a, b)
  return a + b
end

local c = doStr(1, 2)
print('c = ' .. c)
undefined
>

可以看到,ts2lua确实将ts代码转换为lua代码了,ts字符串连接被转换成..console.log被转换为print
接下来试试批量转换ts代码文件,先输入.exit退出node,然后在ts2lua_test目录下新建两个目录in和out:

> .exit

G:ts2lua_test\>mkdir in

G:ts2lua_test\>mkdir out

G:ts2lua_test\>

接下来我们在in目录中放入几个ts文件,你可以把自己现有的ts文件放进去,我这里临时写2个简单的ts文件放进去:

G:ts2lua_test\>node
> tscontent = `
... class A {
...   private id = 0;
...   private name = 'classA';
...   constructor(id: number, name: string) {
.....   this.id = id;
.....   this.name = name;
..... }
... }`
'\nclass A {\n  private id = 0;\n  private name = \'classA\';\n  constructor(id: number, name: string) {\n  this.id = id;\n  this.name = name;\n}\n}'
> tscontent2 = `
... class B extends A {
...   private age = 10;
...   constructor(age: number) {
.....   super(2, 'classB');
.....   this.age = age;
..... }
... }`
'\nclass B extends A {\n  private age = 10;\n  constructor(age: number) {\n  super(2, \'classB\');\n  this.age = age;\n}\n}'
> let fs = require('fs')
undefined
> fs.writeFileSync('in/A.ts', tscontent)
undefined
> fs.writeFileSync('in/B.ts', tscontent2)
undefined
> .exit

G:\ts2lua_test>

现在in目录下有了2个ts代码文件,我们使用ts2lua将其转换为lua文件并生成到out目录下:


image.png
G:\ts2lua_test>node
> ts2lua = require('ts2lua')
{ translate: [Function: translate],
  translateFiles: [Function: translateFiles] }
> ts2lua.translateFiles('in', 'out', { ext: '.lua.txt' })
2 .lua files generated.
undefined
> .exit

G:\ts2lua_test>
image.png

可以看到,ts2lua成功生成了后缀名为.lua.txt的lua代码文件并放在out目录下,同时还附带了class.lua.txt和trycatch.lua.txt,这两个是ts2lua使用的简单的面向对象实现方法,下面我们看看生成的lua代码是什么样子的:

A.lua.txt

require("class")

Class:subclass("A")
A.prototype.id = 0;
A.prototype.name = 'classA';
function A.prototype:ctor(id, name)
  self.id = id
  self.name = name
end

B.lua.txt

require("class")

A:subclass("B")
B.prototype.age = 10;
function B.prototype:ctor(age)
  B.super(2, 'classB')
  self.age = age
end

最后,ts2lua提供了TypeScript声明文件,我们来看看这两个接口的定义:

// Type definitions for index.js
// Project: https://github.com/Halliwood/ts2lua 
// Definitions by: Halliwood <https://github.com/Halliwood> 
// Definitions: https://github.com/borisyankov/DefinitelyTyped

export interface TranslateOption {
  ext?: string, 
  style?: 'xlua' | null
}
// ...
/**
 * Translate the input code string.
 * @param tsCode input code string.
 */
export declare function translate(tsCode : string, option ?: TranslateOption): string;
// ...
/**
 * Translate typescript files from the given input path and write lua files into the given output path.
 * @param inputPath input path which contains typescript files to translate.
 * @param outputPath output path where to write lua files into.
 * @param option translate option
 */
export declare function translateFiles(inputPath : string, outputPath : string, option ?: TranslateOption): void;

可见,translateFiles接受一个可选参数option,可以用于指定转换选项,ext字段用于指定生成的lua文件后缀,比如上述例子我们指定的.lua.txt,style字段用于指定生成的lua代码的风格,可以看出目前支持xlua。`
ts2lua还是蛮好用的,大家可以试试~~

上一篇下一篇

猜你喜欢

热点阅读