Merge pull request #9 from Tanimodori/watch

Improve watch service abilities
main
Alt 2 years ago committed by GitHub
commit eda0e45efb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -17,6 +17,12 @@
"plugins": [
"@typescript-eslint"
],
"ignorePatterns": [
"node_modules/**",
"build/**",
"dist/**",
"NetscriptDefinitions.d.ts"
],
"rules": {
}
}

@ -30,8 +30,7 @@ npm i
Write all your typescript source code in the `/src` directory
To autocompile as you save, run `npm run transpile` in a terminal.
To send changed files as you save, run `npm run watch` in a terminal.
To autocompile and send changed files as you save, run `npm run watch` in a terminal.
Have them both running simultaneously so that it all happens automatically.
For Bitburner to receive any files, you need to enter the port `npm run watch` logs to the terminal

@ -0,0 +1,10 @@
const fileSyncJson = require('../filesync.json');
const dist = fileSyncJson['scriptsFolder'];
const src = 'src';
const allowedFiletypes = fileSyncJson['allowedFiletypes'];
module.exports = {
dist,
src,
allowedFiletypes,
};

@ -0,0 +1,7 @@
const fs = require('node:fs');
const { dist } = require('./config');
// ensure dist exist
if (!fs.existsSync(dist)) {
fs.mkdirSync(dist);
}

@ -0,0 +1,90 @@
const fs = require('node:fs');
const path = require('node:path');
const syncDirectory = require('sync-directory');
const fg = require('fast-glob');
const chokidar = require('chokidar');
const { src, dist, allowedFiletypes } = require('./config');
/** Format dist path for printing */
function normalize(p) {
return p.replace(/\\/g, '/');
}
/**
* Sync static files.
* Include init and watch phase.
*/
async function syncStatic() {
return syncDirectory.async(path.resolve(src), path.resolve(dist), {
exclude: (file) => {
const { ext } = path.parse(file);
return ext && !allowedFiletypes.includes(ext);
},
async afterEachSync(event) {
// log file action
let eventType;
if (event.eventType === 'add' || event.eventType === 'init:copy') {
eventType = 'changed';
} else if (event.eventType === 'unlink') {
eventType = 'deleted';
}
if (eventType) {
let relative = event.relativePath;
if (relative[0] === '\\') {
relative = relative.substring(1);
}
console.log(`${normalize(relative)} ${eventType}`);
}
},
watch: true,
deleteOrphaned: true,
});
}
/**
* Sync ts script files.
* Init phase only.
*/
async function initTypeScript() {
const distFiles = await fg(`${dist}/**/*.js`);
for (const distFile of distFiles) {
// search existing *.js file in dist
const relative = path.relative(dist, distFile);
const srcFile = path.resolve(src, relative).replace(/\.js$/, '.ts');
// if srcFile does not exist, delete distFile
if (!fs.existsSync(srcFile)) {
await fs.promises.unlink(distFile);
console.log(`${normalize(relative)} deleted`);
}
}
}
/**
* Sync ts script files.
* Watch phase only.
*/
async function watchTypeScript() {
chokidar.watch(`${src}/**/*.ts`).on('unlink', async (p) => {
// called on *.ts file get deleted
const relative = path.relative(src, p).replace(/\.ts$/, '.js');
const distFile = path.resolve(dist, relative);
// if distFile exists, delete it
if (fs.existsSync(distFile)) {
await fs.promises.unlink(distFile);
console.log(`${normalize(relative)} deleted`);
}
});
}
/**
* Sync ts script files.
* Include init and watch phase.
*/
async function syncTypeScript() {
await initTypeScript();
return watchTypeScript();
}
console.log('Start watching static and ts files...');
syncStatic();
syncTypeScript();

@ -29,30 +29,26 @@ If you need help with your particular system, feel free to ask for help in the O
### 5. Install the needed programs
- type `npm install`
### 6. Starting the Typescript transpiler
- DON'T SKIP EVEN WHEN USING JS ONLY
- In the terminal you opened, run the command `npm run transpile` in your folder.
### 7. Start the Remote File API server
- Open another terminal pointed at your scripts folder, like in step #4.
- Type `npm run watch`
### 6. Starting the watch services
- In the terminal you opened, run the command `npm run watch` in your folder.
- If NPM asks if you want to install something it needs, answer `y` for yes.
- Your firewall may yell at you; allow the connection.
- If the terminal shows `error TS2307: Cannot find module '@ns' or its corresponding type declarations.` (or see the same error at your editor), it is fine. The missing type declaration `NetscriptDefinitions.d.ts` will be downloaded from the game once connected.
### 8. Go back to Bitburner.
### 7. Go back to Bitburner.
- Options -> Remote API -> type in the port: `12525` -> click connect. The icon should turn green and say it's online.
- Your firewall may yell at you again; allow the connection.
### 9. Test that the connection works
### 8. Test that the connection works
- You should see a file `template.js` in the root of your `home` server in Bitburner.
- You should see a NetscriptDefinitions.d.ts automatically appear in the folder on your computer (ex. `C:\Users\yourusername\Workspace\BitburnerScripts\NetscriptDefinitions.d.ts`).
### 10. Try some other files too!
### 9. Try some other files too!
- Copy/create a .js to the `src` folder on your computer and check Bitburner. The file should be transferred!
- Sadly, at the time of writing, Typescript doesn't support 'compiling' text files. So copy/create a .txt in the `dist` folder and check Bitburner. This file should be transferred as well.
- Copy/create a .txt in the `src` folder will be transferred as well now.
### 11. Thats it!
### 10. Thats it!
- You can now make and edit the files in the `src` directory to your liking, and have them be changed in Bitburner automatically.
<br />
<br />

870
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -1,16 +1,24 @@
{
"name": "bitburner-typescript-template",
"version": "1.0.0",
"version": "2.0.0",
"scripts": {
"transpile": "npx tsc -w",
"watch": "npx bitburner-filesync@latest"
"watch:transpile": "tsc -w --preserveWatchOutput",
"watch:local": "node build/watch.js",
"watch:remote": "bitburner-filesync",
"watch:init": "node build/init.js",
"watch:all": "concurrently npm:watch:transpile npm:watch:local npm:watch:remote",
"watch": "npm run watch:init && npm run watch:all"
},
"author": "hydroflame, Hoekstraa, based on work by SlyCedix",
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.35.1",
"@typescript-eslint/parser": "^5.35.1",
"bitburner-filesync": "^1.1.5",
"chokidar": "^3.5.3",
"concurrently": "^7.4.0",
"eslint": "^8.22.0",
"fast-glob": "^3.2.12",
"sync-directory": "^5.1.7",
"typescript": "^4.7.4"
}
}

Loading…
Cancel
Save