This is a starter repo for anyone wanting to use TypeScript to play BitBurner!
The template relies on a number of things that you can probably change to your liking; just be aware you might need to fiddle with settings if you change them.
npm: The Node Package Manager
- In
Git Bashorterminal- clone / fork the repo. You can also click Use as template in GitHub to create a repo from this one
- run
npm install - run
npm run build - run
npm run webserver
- Go to the game
wget http://localhost:9182/bin/utils/initRepo.js /bin/utils/initRepo.jsrun /bin/utils/initRepo.jsrun /bin/sayHelloTs.js
If you see the colored output until the fake error, you should be good to go :)
After the first pull, I usually make an alias
alias pull="run /bin/utils/pullFiles.js" to make things more
convenient.
You can leave the server running and just npm run re to clean
rebuild the whole project, then use the pull alias in-game
whenever you want to update your scripts.
A short description of the most important parts of the template.
{
"scripts": {
"build": "npm-run-all buildTs buildManifest",
"buildTs": "tsc -p tsconfig.json",
"buildManifest": "node bin/generateManifest.js",
"clean": "rm -rf build/*",
"re": "npm-run-all clean buildTs buildManifest",
"webserver": "node bin/webserver.js -p 9182 -d build --enable_cors"
},
"devDependencies": {
"npm-run-all": "^4.1.5",
"rimraf": "^3.0.2",
"typescript": "^4.7.3"
}
}build: Runs everything necessary to buildtsfiles into js and convertjsfiles tonsbefore writing their paths to theresources/manifest.txtfile.buildTs: runstscbuildManifest: runs thebin/generateManifest.jsto generateresources/manifest.txtfile containing paths to all your scripts.clean: cleans build folderre: I like make, sue me :|webserver: runs the tiny webserver that will serve your scripts so that you canwgetthem from the game.- Careful with the server, CORS are activated, so do not serve any sensitive data! You never know~
{
"compilerOptions": {
"target": "ES2021",
"module": "ES2020",
"rootDir": "src/",
"baseUrl": "src/",
"paths": {
"/lib/*": [
"lib/*"
]
},
"outDir": "build/",
"moduleResolution": "node",
"strictNullChecks": true,
"strictPropertyInitialization": true
}
}targetandmodule: Set to generate 'raw' js to avoid any compatibility errors in NS. I did not test every possible value, but these seem to work.commonjsfails, so doeses5, because of the generatedObject.setPorperty(exports ...)and such.rootDirbaseUrlandpaths: Allows replacing import references with an absolute path.- You can change
libto whatever you want, or even add more if you want. outDiris used by the scripts in thebinfolder. Be sure to change them accordingly if you change that.
These are the important bits in the tsc config; the rest is optional.
Refer to the tsc documentation if you want to make modifications.
bin/webserver.js: Just a webserver that serves thebuildfolder to be able towgetyour scripts from the game.bin/generateManifest.js: Simple script thatfinds every.jsin the build folder and writes its path toresources/manifest.txt. This is used to pull all files on your in-game filesystem.
These helper scripts are called by npm run commands listed in
package.json.
The type definition for NetScript (the NS type and other game APIs) is
located in src/lib/Bitburner.t.ts and is taken directly from the
Bitburner repo.
'Executable scripts' are located in the src/bin subfolder and will
generally contain main functions. This is just for tidiness and
completely optional.
'Library' files are stored in src/lib to make it easier to make sure
links are correct when generating .js files.
src/bin/sayHelloTs.ts: A sample script referencing utils and logging dummy string to the terminal to test the setup.src/bin/utils/initRepo.ts: A simple script with no references. Used to init the repo the first time when no files are available on the game home server.src/bin/utils/pullFiles.ts: Utility script thatwgets themanifest.txtfile from the server and downloads all scripts to the same location on the game home.src/lib/Helpers.ts: Just some helper functions to make things neater in executable scripts.
I would recommend not putting any files in the root of the src folder.
For some reason, I had a lot of problems when trying to get them from
the wget cmdlet in-game. It creates a ghost / folder in-game,
sometimes creates ghost files, etc...
Just putting everything in a subfolder (like src/lib or src/bin)
seems to be a workaround.
Putting every file that is referenced by other files in the src/lib
folder helps with the module resolution. Since NS only understands
absolute paths (starting with /, from the root of the game
filesystem), it's easier to make a rule that replaces imports with
their absolute version. If you want to add more than just the
src/lib folder to these roots, you need to change the settings
paths in the tsconfig.json file
Always check that your IDE / tools don't import modules like this:
import { Blep } from "../lib/Blep";BitBurner doesn't understand relative path, so even though this will compile fine, it will not run in-game. Make sure they look like this instead (absolute path, c.f previous paragraph):
import { Blep } from "/lib/Blep";This would result in this javascript:
import {Blep} from "/lib/Blep";which is valid for the game.
Feel free to send PRs!