johnsoncodehk / ts2gamesparks

Convert typescript to GameSparks Cloud Code
8 stars 5 forks source link

Realtime modules/scripts #5

Closed mixecan closed 5 years ago

mixecan commented 5 years ago

Do you have any plan to support realtime modules/scripts?

johnsoncodehk commented 5 years ago

I have not used realtime modules/scripts, I will look at it today. This branch seems to implement it, you can check out: https://github.com/alexhmelnoy/ts2gamesparks

mixecan commented 5 years ago

Thanks! I tried that brach but the build fails...

johnsoncodehk commented 5 years ago

I'm not sure if it works, because I'm not working on the GameSparks project and can't test it.

The changes you need to make are:

  1. Update ts2gamesparks to the latest: https://github.com/johnsoncodehk/ts2gamesparks/commit/8a73fac867b940593f8e61a8c2a0a6eb77f8816a
  2. Add the Realtime API typescript definition: https://github.com/johnsoncodehk/gamesparks-realtime-api-typings
  3. Create a tsconfig file in the rtScripts/ directory (to ignore the modules/ directory), like this: https://github.com/johnsoncodehk/gamesparks-cloud-code-typescript-example/blob/master/src/rtScript/tsconfig.json

Please tell me any problem you found.

mixecan commented 5 years ago

I had the time to have a look into your code but I don't understand the rationale behind you commit (except that it works). Can you explain?

I may have found an alternative solution that makes use of a single tsconfig and builds everything correctly but maybe I'm not completely catching all the steps of your code. I will do a PR so that you can have a look

johnsoncodehk commented 5 years ago

In my guess, the GameSparks module rules are as follows:

  1. rtScript can only introduce modules under rtModules/, and cannot import modules under modules/.
  2. Event can only introduce modules under modules/, but can not introduce modules under rtModules/

(Create two tsconfig.json to avoid referencing the other's module)

  1. modules/ is introduced as a code snippet, so you need to call doRenaming() and doRefactoring() to change the CommonJS module to a code snippet.
  2. rtScript and rtModules are introduced as CommonJS modules, so there is no need to change to code snippets.

Maybe my guess is wrong, I am looking at your submission. (If you are sure that these points are correct or not, please let me know.)

johnsoncodehk commented 5 years ago

I just confirmed the point 2, but I can't confirm the point 1 at the moment because I don't know how to test a rtScript.

mixecan commented 5 years ago
  1. yes
  2. yes, but I would add that anything that is not rtScript or rtModules (request, messages, etc) can introduce modules under modules/
  3. and 4. you're right. i've just received confirmation from devs working in my company. so the if you added is correct (I will push a commit in the PR)

Regarding the two tsconfigs, I tried the following and it seems to work correctly: /tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "lib": ["es5", "es2015"],
    "forceConsistentCasingInFileNames": true,
    "rootDir": "./src",
    "outDir": "./dist/",
    "baseUrl": "./src/",
    "paths": {
      "*": [
        "./modules/*",
      ],
    }
  },
  "include": [
    "src/"
  ]
}

/src/rtScript/tsconfig.json

{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "paths": {
      "*": [
        "./rtModules/*",
      ],
    }
  }
}
johnsoncodehk commented 5 years ago

Thank you for confirming these points! Your idea of using "extends" is fine, I updated it in the example. In addition, I think you can replace "paths" with "baseUrl".

johnsoncodehk commented 5 years ago

I have updated the readme, if there is still a problem, please install the latest version of ts2gamesparks, then follow the readme instructions.

mixecan commented 5 years ago

Thanks for the update! I see that you create the main tsconfig.json under ./src/ but this could be wrong. In fact if I try to build from the root of the project (I usually have a package script that run different commands along with yours) I get an error due to the missing tsconfig.json.

/Users/user/project/ts2gs/node_modules/typescript/lib/typescript.js:1464
            throw e;
            ^

Error: Debug Failure.
    at Object.assertDefined (/Users/user/project/ts2gs/node_modules/typescript/lib/typescript.js:1469:24)
    at /Users/user/project/ts2gs/node_modules/typescript/lib/typescript.js:14663:89
    at String.replace (<anonymous>)
    at formatStringFromArgs (/Users/user/project/ts2gs/node_modules/typescript/lib/typescript.js:14663:21)
    at Object.createCompilerDiagnostic (/Users/user/project/ts2gs/node_modules/typescript/lib/typescript.js:14703:20)
    at tryReadFile (/Users/user/project/ts2gs/node_modules/typescript/lib/typescript.js:24446:40)
    at Object.readJsonConfigFile (/Users/user/project/ts2gs/node_modules/typescript/lib/typescript.js:24434:32)
    at getTsConfig (/Users/user/project/ts2gs/node_modules/ts2gamesparks/lib/ts2gamesparks.js:12:21)
    at build (/Users/user/project/ts2gs/node_modules/ts2gamesparks/lib/ts2gamesparks.js:198:20)
    at Object.<anonymous> (/Users/user/project/ts2gs/node_modules/ts2gamesparks/lib/ts2gamesparks.js:243:5)

IMO the --init flag should create the main tsconfig in the root directory

johnsoncodehk commented 5 years ago

It is recommended to place tsconfig in the src directory, but it is not mandatory. You can try changing the ts2gs of your package script to cd src && ts2gs (Example)

In addition, this issue is a bit like the problem you mentioned. He is using an export error outside of modules and rtModules.

mixecan commented 5 years ago

Sorry for the delay The issue was caused by ts2gs not loading the tsconfig and thus having incorrect baseUrl and paths. A --project <tsconfig.json path> option would be great (I will create an issue) so that cd src is not required.

Currently your setup works fine for us. I will close the issue and reopen it if something occurs!

johnsoncodehk commented 5 years ago

@mixecan I found that the realtime module conversion results of ts2gamesparks are inconsistent with the documentation of GameSparks. Can your realtime script work successfully?

ts2gamesparks conversion results:

function foo() {
    return "foo";
}
exports.foo = foo;

GameSparks documentation:

module.exports.foo = function() {
    return "foo";
}
mixecan commented 5 years ago

We don't have any problem with the realtime scripts. Unless the result is something like

exports = function foo() {
    return "foo";
}

exports should be work exactly as module.exports.

johnsoncodehk commented 5 years ago

@mixecan I see, thank you.