Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
YoRyan committed May 18, 2022
0 parents commit 3fbb2db
Show file tree
Hide file tree
Showing 15 changed files with 8,138 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "TypeScriptToLua",
"image": "mcr.microsoft.com/vscode/devcontainers/javascript-node:14",
"settings": {
"terminal.integrated.shell.linux": "/bin/bash"
},
"extensions": [
"typescript-to-lua.vscode-typescript-to-lua",
"dbaeumer.vscode-eslint",
"editorconfig.editorconfig",
"esbenp.prettier-vscode"
],
"postCreateCommand": "npm ci"
}
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* text=auto eol=lf
*.{cmd,[cC][mM][dD]} text eol=crlf
*.{bat,[bB][aA][tT]} text eol=crlf
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
node_modules
/dist

yarn.lock
.vscode
.idea
.DS_Store
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package.json
package-lock.json
6 changes: 6 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"printWidth": 120,
"tabWidth": 4,
"arrowParens": "avoid",
"overrides": [{ "files": ["**/*.md", "**/*.yml"], "options": { "tabWidth": 2 } }]
}
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# railworks-tstl-template

Write scripts for Train Simulator in TypeScript with the help of [TypeScriptToLua](https://typescripttolua.github.io/)!
67 changes: 67 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const fsPromises = require("fs/promises"),
gulp = require("gulp"),
filter = require("gulp-filter"),
flatmap = require("gulp-flatmap"),
intermediate = require("gulp-intermediate"),
rename = require("gulp-rename"),
path = require("path"),
ts = require("typescript"),
tstl = require("typescript-to-lua");

exports.default = exports.tstl = function (cb) {
return gulp
.src("src/mod/**/*.ts", { base: "src" })
.pipe(
flatmap(function (stream, file) {
return stream
.pipe(gulp.src(["src/@types/**/*", "src/lib/**/*.ts"], { base: "src" }))
.pipe(
gulp.src(["node_modules/lua-types/**/*", "node_modules/typescript-to-lua/**/*"], { base: "." })
)
.pipe(
intermediate({}, async function (tempDir, cb) {
await compileLua(tempDir, file.relative);
cb();
})
)
.pipe(filter(["mod/**/*.lua"]));
})
)
.pipe(
rename(function (path) {
path.dirname = path.dirname.replace(/^mod\//, "");
})
)
.pipe(gulp.dest("dist"));
};

async function compileLua(tempDir, luaPath) {
// We need the root tsconfig.json node to set the value of "include".
const tsconfig = path.join(tempDir, "tsconfig.json");
await fsPromises.writeFile(
tsconfig,
`{ "include": ["${path.join(tempDir, "@types")}", "${path.join(tempDir, "mod")}"] }`
);

const result = tstl.transpileProject(tsconfig, {
types: ["lua-types/5.0"],
typeRoots: [path.join(tempDir, "@types")],
luaTarget: tstl.LuaTarget.Lua50,
luaLibImport: tstl.LuaLibImportKind.Inline,
sourceMapTraceback: false,
luaBundle: path.join(path.dirname(luaPath), path.basename(luaPath, ".ts") + ".lua"),
// The entry path needs to be absolute so that TSTL sets the correct module name.
luaBundleEntry: path.join(tempDir, luaPath),
});
printDiagnostics(result.diagnostics);
}

function printDiagnostics(diagnostics) {
console.log(
ts.formatDiagnosticsWithColorAndContext(diagnostics, {
getCurrentDirectory: () => ts.sys.getCurrentDirectory(),
getCanonicalFileName: f => f,
getNewLine: () => "\n",
})
);
}
Loading

0 comments on commit 3fbb2db

Please sign in to comment.