Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

typescript-to-lua

Package Overview
Dependencies
Maintainers
2
Versions
157
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typescript-to-lua - npm Package Compare versions

Comparing version 0.0.5 to 0.0.6

131

dist/Compiler.js

@@ -5,10 +5,10 @@ #!/usr/bin/env node

var ts = require("typescript");
var fs = require("fs");
var path = require("path");
// ES6 syntax broken
var minimist = require("minimist");
var dedent = require("dedent");
var Transpiler_1 = require("./Transpiler");
var TSHelper_1 = require("./TSHelper");
function compile(fileNames, options, projectRoot) {
// Verify target
if (options.target != "lua") {
console.error("Wrong compilation target! Add \"target\": \"lua\" to your tsconfig.json!");
process.exit(1);
}
function compile(fileNames, options) {
var program = ts.createProgram(fileNames, options);

@@ -35,16 +35,16 @@ var checker = program.getTypeChecker();

if (!sourceFile.isDeclarationFile) {
// Print AST for debugging
//printAST(sourceFile, 0);
try {
var rootDir = options.rootDir;
if (!rootDir) {
rootDir = process.cwd();
}
// Transpile AST
var addHeader = options.noHeader === true ? false : true;
var lua = Transpiler_1.LuaTranspiler.transpileSourceFile(sourceFile, checker, addHeader);
var outPath = sourceFile.fileName.substring(0, sourceFile.fileName.lastIndexOf(".")) + ".lua";
var lua = Transpiler_1.LuaTranspiler.transpileSourceFile(sourceFile, checker, options);
var outPath = sourceFile.fileName;
if (options.outDir) {
var extension = options.outDir;
if (extension[extension.length - 1] != "/")
extension = extension + "/";
outPath = outPath.replace(projectRoot + "/", projectRoot + "/" + extension);
console.log(outPath);
outPath = path.join(options.outDir, sourceFile.fileName.replace(rootDir, ""));
}
// change extension
var fileNameLua = path.basename(outPath, path.extname(outPath)) + '.lua';
outPath = path.join(path.dirname(outPath), fileNameLua);
// Write output

@@ -77,24 +77,87 @@ ts.sys.writeFile(outPath, lua);

}
// Try to find tsconfig.json
var filename = process.argv[2].split("\\").join("/");
var filepath = filename.substring(0, filename.lastIndexOf("/"));
var configPath = ts.findConfigFile(filepath, ts.sys.fileExists);
if (configPath) {
configPath = configPath.split("\\").join("/");
var projectRoot = configPath.substring(0, configPath.lastIndexOf("/"));
// Find all files
var files = ts.sys.readDirectory(projectRoot, [".ts"]);
// Read config
var configFile = ts.readConfigFile(configPath, ts.sys.readFile);
if (configFile.error) {
console.error("Error occured:");
console.error(configFile.error);
// Removes the option and value form argv
function removeInvalidOptionsFromArgv(commandLine, invalidOptions) {
var result = commandLine.slice();
for (var _i = 0, invalidOptions_1 = invalidOptions; _i < invalidOptions_1.length; _i++) {
var opt = invalidOptions_1[_i];
var index = result.indexOf('--' + opt);
if (index === -1) {
index = result.indexOf('-' + opt);
}
if (index !== -1) {
result.splice(index, 2);
}
}
else {
compile(files, configFile.config.compilerOptions, projectRoot);
return result;
}
// Polyfill for report diagnostics
function logError(commandLine) {
if (commandLine.errors.length !== 0) {
commandLine.errors.forEach(function (err) {
console.log(err.messageText);
});
process.exit(1);
}
}
else {
console.error("Could not find tsconfig.json, place one in your project root!");
function executeCommandLine(args) {
// Right now luaTarget, version and help are the only cli options, if more are added we should
// add a more advanced custom parser
var argv = minimist(args, {
string: ['l'],
alias: { h: 'help', v: 'version', l: 'luaTarget' },
"default": { luaTarget: 'JIT' },
'--': true,
stopEarly: true
});
var tstlVersion;
try {
tstlVersion = require('../package').version;
}
catch (e) {
tstlVersion = "0.0.0";
}
var helpString = dedent("Version: " + tstlVersion + "\n Syntax: tstl [options] [files...]\n\n In addtion to the options listed below you can also pass options for the\n typescript compiler (For a list of options use tsc -h). NOTE: The tsc options\n might have no effect.\n\n Options:\n --version Show version number\n --luaTarget, -l Specify Lua target version: 'JIT' (Default), '5.1', '5.2',\n '5.3'\n --project, -p Compile the project given the path to its configuration file,\n or to a folder with a 'tsconfig.json'\n --help Show this message\n ");
if (argv.help) {
console.log(helpString);
process.exit(0);
}
if (argv.version) {
console.log("Version: " + require('../package').version);
}
var validLuaTargets = ['JIT', '5.3', '5.2', '5.1'];
if (!validLuaTargets.some(function (val) { return val === argv.luaTarget; })) {
console.error("Invalid lua target valid targets are: " + validLuaTargets.toString());
}
// Remove tstl options otherwise ts will emit an error
var sanitizedArgs = removeInvalidOptionsFromArgv(args, ['l', 'luaTarget']);
var commandLine = ts.parseCommandLine(sanitizedArgs);
logError(commandLine);
commandLine.options.luaTarget = argv.luaTarget;
var configPath;
if (commandLine.options.project) {
configPath = path.isAbsolute(commandLine.options.project) ? commandLine.options.project : path.join(process.cwd(), commandLine.options.project);
if (fs.statSync(configPath).isDirectory()) {
configPath = path.join(configPath, 'tsconfig.json');
}
else if (fs.statSync(configPath).isFile() && path.extname(configPath) === ".ts") {
// if supplied project points to a .ts fiel we try to find the config
configPath = ts.findConfigFile(configPath, ts.sys.fileExists);
}
commandLine.options.project = configPath;
var configContents = fs.readFileSync(configPath).toString();
var configJson = ts.parseConfigFileTextToJson(configPath, configContents);
commandLine = ts.parseJsonConfigFileContent(configJson.config, ts.sys, path.dirname(configPath), commandLine.options);
// Append lua target to options array since its ignored by TS parsers
// option supplied on CLI is prioritized
if (argv.luaTarget === "JIT" && commandLine.raw.luaTarget !== argv.luaTarget) {
commandLine.options.luaTarget = commandLine.raw.luaTarget;
}
}
if (configPath && !commandLine.options.rootDir) {
commandLine.options.rootDir = path.dirname(configPath);
}
logError(commandLine);
compile(commandLine.fileNames, commandLine.options);
}
executeCommandLine(process.argv.slice(2));
//# sourceMappingURL=Compiler.js.map

@@ -28,5 +28,6 @@ "use strict";

var LuaTranspiler = /** @class */ (function () {
function LuaTranspiler(checker) {
function LuaTranspiler(checker, options) {
this.indent = "";
this.checker = checker;
this.options = options;
this.genVarCounter = 0;

@@ -39,5 +40,5 @@ this.transpilingSwitch = false;

// Transpile a source file
LuaTranspiler.transpileSourceFile = function (node, checker, addHeader) {
var transpiler = new LuaTranspiler(checker);
var header = addHeader ? "--=======================================================================================\n"
LuaTranspiler.transpileSourceFile = function (node, checker, options) {
var transpiler = new LuaTranspiler(checker, options);
var header = options.addHeader ? "--=======================================================================================\n"
+ "-- Generated by TypescriptToLua transpiler https://github.com/Perryvw/TypescriptToLua \n"

@@ -646,2 +647,5 @@ + "-- Date: " + new Date().toDateString() + "\n"

// should throw an exception if codepoint is used sub 5.3
if (identifier.escapedText === "fromCodePoint" && this.options.luaTarget !== '5.3') {
throw new TranspileError("Unsupported string property " + identifier.escapedText + " is only supported for lua 5.3.", identifier);
}
if (translation[identifier.escapedText]) {

@@ -648,0 +652,0 @@ return "" + translation[identifier.escapedText];

{
"name": "typescript-to-lua",
"license": "MIT",
"version": "0.0.5",
"version": "0.0.6",
"repository": "https://github.com/Perryvw/TypescriptToLua",

@@ -9,3 +9,3 @@ "scripts": {

"test": "tsc -p ./test && node ./test/runner.js",
"coverage": "nyc npm test && nyc report --reporter=lcov",
"coverage": "nyc npm test && nyc report --reporter=text-lcov > coverage.lcov && codecov",
"coverage-html": "nyc npm test && nyc report --reporter=html",

@@ -22,2 +22,3 @@ "release-patch": "npm version patch",

"dependencies": {
"minimist": "^1.2.0",
"typescript": "^2.7.2"

@@ -27,2 +28,3 @@ },

"alsatian": "^2.2.1",
"codecov": "^3.0.0",
"dedent": "^0.7.0",

@@ -29,0 +31,0 @@ "deep-equal": "^1.0.1",

# TypescriptToLua
[![Build Status](https://travis-ci.org/Perryvw/TypescriptToLua.svg?branch=master)](https://travis-ci.org/Perryvw/TypescriptToLua) [![Coverage](https://codecov.io/gh/perryvw/typescripttolua/branch/master/graph/badge.svg)](https://codecov.io/gh/perryvw/typescripttolua)
Typescript to lua transpiler.
[![Build Status](https://travis-ci.org/Perryvw/TypescriptToLua.svg?branch=master)](https://travis-ci.org/Perryvw/TypescriptToLua)
## Usage Guide
**Install**
`npm install -g typescript-to-lua`
`tstl path/to-my-file.ts`
**Compile Files**
`tstl path/to-my-file.ts path/to-my-other-file.ts`
**Compile Projects**
`tstl -p path/to-my/tsconfig.json`
**Options**
```
Syntax: tstl [options] [files...]
In addtion to the options listed below you can also pass options for the
typescript compiler (For a list of options use tsc -h). NOTE: The tsc options
might have no effect.
Options:
--version Show version number
--luaTarget, -l Specify Lua target version: 'JIT' (Default), '5.1', '5.2',
'5.3'
--project, -p Compile the project given the path to its configuration file,
or to a folder with a 'tsconfig.json'
--help Show this message
```
**Optionally:**

@@ -19,7 +44,2 @@ Add the lualib files from dist/ to your project. This helper library unlocks additional typescript functions:

### Transpiling a TypeScript project to Lua
The compiler will automatically try to find a typescript configuration file `tsconfig.json` in the files. If found it will transpile all TypeScript files in subdirectories of the project.
**To prevent accidental compilation to Lua, you are required to add a `"target": "lua"` entry in your tsconfig compilerOptions.**
## Sublime Text integration

@@ -35,5 +55,5 @@ This compiler works great in combination with the [Sublime Text Typescript plugin](https://github.com/Microsoft/TypeScript-Sublime-Plugin) (available through the package manager as `TypeScript`).

{
"cmd": ["tstl", "$file"]
"cmd": ["tstl", "-p", "$file"]
}
```
Save this in your Sublime settings as a `TypeScriptToLua.sublime-build`. You can now select the TypeScriptToLua build system in `Tools > Build System` to build using the normal hotkey (`ctrl+B`), or if you have multiple TypeScript projects open, you can choose your compiler before building by pressing `ctrl+shift+B`.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc