TypeScriptToLua icon indicating copy to clipboard operation
TypeScriptToLua copied to clipboard

Interactive --init idea

Open hazzard993 opened this issue 5 years ago • 0 comments

One thing I struggled with learning tstl is how to use available declarations and get TypeScript to describe only the Lua environment within a tsconfig.json file.

I'm thinking an --init option could help new users with this and provide a more standard way to initialize a project for TypeScriptToLua. Users won't have to copy-paste, consider all available options for a tsconfig.json and tstl or use ones from other projects.

It doesn't go through all that tstl can do and all the TypeScript options available. It creates a tsconfig.json file that works with the available declarations, TypeScriptToLua and a Lua environment.

It can be used in a way that tsc can be. But you don't get a tsconfig.json file with every single possible option. I think that is too overwhelming.

tstl --init --luaTarget JIT --luaLibImport require --noHeader
{
    "compilerOptions": {
        "lib": ["es2017"],
        "strict": true,
        "outDir": "./lua",
        "rootDir": "./src",
        "types": []
    },
    "include": [],
    "tstl": {
        "luaTarget": "JIT",
        "luaLibImport": "require",
        "noHeader": true
    }
}

Note the preferred selection of lib, strict, outDir and rootDir

However if --init is provided by itself. Since users need to consider many more options to get the Lua they want, they are prompted for some answers.

$ tstl --init

Lua Target (JIT, 5.3, 5.2 or 5.1): (JIT)
Location of source files (rootDir): (./src)
Where to place transpiled files (outDir): (./lua)
Enable strict option (strict): (Y/n)

No declarations were found.
To add declarations:
- Clone them and add their paths to "files" ("<folder>/**.d.ts").
- If they are available via npm use "npm install <declaration>" and add their names to "types".

Created tsconfig.json
Use "tstl -p tsconfig.json" to transform .ts files in (rootDir) to (outDir) as .lua files.

This can include linking up declarations within the current folder.

A directory will be considered a declaration source if:

  • The directory is under node_modules/ and has an index.d.ts.
  • The directory is under node_modules/ and has a JIT, 5.1, 5.2 or 5.3 .d.ts file.
  • The directory is named "Dota2Declarations" or "DefoldTypeScript" and is within the current working directory.

A more standard way to identify these could be discussed, but if some directories match any of these criteria the user will encounter this during prompting:

Link "lua-types" to "types"?: (Y/n)
Add "Dota2Declarations" to "include"?: (Y/n)

If JIT/5.1/5.2/5.3 is found. The "types" link has the appropriate target appended to it. ("types": [ "lua-types/jit" ]). This can help when the declarations provided describe varying Lua environments.

So to use it:

$ npm install -g typescript-to-lua
$ npm install lua-types
$ tstl --init
{
    "compilerOptions": {
        "lib": ["es2017"],
        "strict": true,
        "outDir": "./lua",
        "rootDir": "./src",
        "types": [
            "lua-types/JIT"
        ]
    },
    "include": [],
    "tstl": {
        "luaTarget": "JIT"
    }
}

hazzard993 avatar Apr 24 '19 04:04 hazzard993