Skip to content

Commit

Permalink
Add typescript transpiling
Browse files Browse the repository at this point in the history
  • Loading branch information
Peterclark1996 committed Jun 11, 2024
1 parent da282dd commit 59349b5
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 11 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Counter Strike Typescript Compiler
This is a tool that allows you to create .vts typescript scripts for your counter strike maps and compile them automatically to .vts_c files. You can also import the type defintions for the `Instance` object in your scripts.

## Features
- Partial type definitions for cspointscript's Instance class
- Auto compile vts typescript files to vts_c, the format needed to run in CS2 maps
- Auto transpiling of typescript to javascript (Needed for vts_c files)

## Installation
<details>
<summary>Automatic (Recommended)</summary>
Expand Down
2 changes: 1 addition & 1 deletion packages/create-s2ts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-s2ts",
"version": "0.1.7",
"version": "0.2.0",
"description": "A tool to scaffold a s2ts project",
"main": "dist/index.js",
"bin": {
Expand Down
4 changes: 2 additions & 2 deletions packages/s2ts/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/s2ts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "s2ts",
"version": "0.1.7",
"version": "0.2.0",
"description": "A tool to automatically compile counter-strike TS files (.vts files)",
"main": "dist/index.js",
"bin": {
Expand All @@ -22,7 +22,7 @@
"compiler"
],
"scripts": {
"build": "tsc",
"build": "tsc --project tsconfig.build.json",
"start": "ts-node --transpile-only --project tsconfig.json src/cli.ts start",
"test": "jest",
"publish-npm": "npm run build && npm publish"
Expand Down
22 changes: 21 additions & 1 deletion packages/s2ts/src/compile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
import { ModuleKind, ScriptTarget, transpileModule } from "typescript"

const version: number = 8

export const compileToVts = (data: string): Buffer => {
export const compileVtsFile = (data: string): Buffer => {
const transpiledData = transpileTypeScript(data)
return compileToVtsc(transpiledData)
}

const transpileTypeScript = (source: string): string => {
const result = transpileModule(source, {
compilerOptions: {
module: ModuleKind.ESNext,
target: ScriptTarget.ES2015,
removeComments: true,
esModuleInterop: false,
allowSyntheticDefaultImports: false
}
})
return result.outputText
}

const compileToVtsc = (data: string): Buffer => {
const dataSize = Buffer.byteLength(data, "utf-8")
const newData: number[] = []
const StatBytes: Uint8Array = serializeCs2kv3(data)
Expand Down
4 changes: 2 additions & 2 deletions packages/s2ts/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import chokidar from "chokidar"
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs"
import path from "path"
import { compileToVts } from "./compile"
import { compileVtsFile } from "./compile"

const watchDir = path.join(process.cwd(), "./scripts").replace(/[\\/]+/g, "/")

Expand All @@ -14,7 +14,7 @@ const processFile = (filePath: string) => {
const standardFilePath = filePath.replace(/[\\/]+/g, "/")

const data = readFileSync(standardFilePath).toString("utf-8")
const compiledBuffer = compileToVts(data)
const compiledBuffer = compileVtsFile(data)

const outputFilePath = standardFilePath.replace(".vts", ".vts_c").replace(sourcePathPart, targetPathPart)

Expand Down
16 changes: 14 additions & 2 deletions packages/s2ts/tests/compile.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
import { readFileSync } from "fs"
import path from "path"
import { compileToVts } from "../src/compile"
import { compileVtsFile } from "../src/compile"

test("I can compile a vts file and get a correctly formed vts_c file", async () => {
const sourcePath = path.join(__dirname, "/resource/test.vts")
const data = readFileSync(sourcePath).toString("utf-8")

const actual = compileToVts(data).toString("utf-8")
const actual = compileVtsFile(data).toString("utf-8")

const expectedPath = path.join(__dirname, "/resource/test.vts_c")
const expected = readFileSync(expectedPath).toString("utf-8")

expect(actual).toBe(expected)
})

test("I can compile a vts typescript file and get a correctly formed vts_c javascript file", async () => {
const sourcePath = path.join(__dirname, "/resource/test_withTypes.vts")
const data = readFileSync(sourcePath).toString("utf-8")

const actual = compileVtsFile(data).toString("utf-8")

const expectedPath = path.join(__dirname, "/resource/test_withTypes.vts_c")
const expected = readFileSync(expectedPath).toString("utf-8")

expect(actual).toBe(expected)
})
Binary file modified packages/s2ts/tests/resource/test.vts_c
Binary file not shown.
14 changes: 14 additions & 0 deletions packages/s2ts/tests/resource/test_withTypes.vts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Instance } from "cspointscript"

type Player = {
name: string
score: number
}

const printPlayer = (player: Player) => {
Instance.Msg(player.name + " has a score of " + player.score)
}

Instance.PublicMethod("PublicFunc", () => {
printPlayer({ name: "Player1", score: 100 })
})
Binary file added packages/s2ts/tests/resource/test_withTypes.vts_c
Binary file not shown.
8 changes: 8 additions & 0 deletions packages/s2ts/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
"noEmit": false
},
"include": ["./src"]
}
2 changes: 1 addition & 1 deletion packages/s2ts/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
"@/*": ["./*"]
},
},
"include": ["./src"],
"include": ["./src", "./tests"],
}

0 comments on commit 59349b5

Please sign in to comment.