Compare commits

..

18 commits

Author SHA1 Message Date
Alt
019c45580f
Merge pull request #32 from davidc789/main
Add exclude feature
2023-06-30 11:23:20 +02:00
david
cc97684069 Run prettier 2023-04-19 07:16:40 +10:00
david
ee7231af9c Added more documentation 2023-04-13 17:10:26 +10:00
david
fbab8a375f Added some documentation 2023-04-13 08:46:42 +10:00
david
544344bbb4 Bug fixes 2023-04-12 17:40:50 +10:00
david
b9467d6a57 Merge remote-tracking branch 'origin/feat/exclude' into feat/exclude
# Conflicts:
#	npx/bitburner-filesync.ts
2023-04-12 17:37:12 +10:00
david
1567a7c650 Merge remote-tracking branch 'origin/feat/exclude' into feat/exclude
# Conflicts:
#	src/fileWatch.ts
2023-04-12 17:36:27 +10:00
david
5cb38deb40 Merge remote-tracking branch 'origin/feat/exclude' into feat/exclude
# Conflicts:
#	src/fileWatch.ts
2023-04-12 17:31:19 +10:00
david
043a2d34b1 Bug fixes 2023-04-12 17:22:14 +10:00
david
2b40438341 Bug fixes 2023-04-11 21:48:05 +10:00
david
fd3392fb23 Added a prepare script to allow installing off GitHub 2023-04-11 15:04:42 +10:00
david
daf7854cd0 Bumped dependency versions to the latest 2023-04-11 14:29:05 +10:00
david
5bebc2832a Added missing type packages to eliminate build errors 2023-04-11 12:20:06 +10:00
david
2f1807812d Added folder exclusion feature and .gitattributes 2023-04-11 12:12:34 +10:00
Alt
7ff71ffbd9
Merge pull request #30 from Pustur/add-eslint
Add eslint
2022-10-12 20:29:03 +02:00
Loris Bettazza
352c083e83 Fix lint errors 2022-10-12 00:17:48 +02:00
Loris Bettazza
3901af1c18 Setup eslint config and script 2022-10-12 00:14:30 +02:00
Loris Bettazza
e5763314ff Add eslint dependencies 2022-10-12 00:14:28 +02:00
18 changed files with 2210 additions and 252 deletions

2
.eslintignore Normal file
View file

@ -0,0 +1,2 @@
node_modules/
dist/

10
.eslintrc Normal file
View file

@ -0,0 +1,10 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
]
}

26
.gitattributes vendored Normal file
View file

@ -0,0 +1,26 @@
# js / ts files
*.js text
*.ts text
# Config text files
*.jsonc text
*.json text
*.yml text
.eslintrc text
.eslintignore text
.gitignore text
.gitattributes text
.prettierignore text
.prettierrc text
# Standard text files
*.txt text
*.md text
*.sh text eol=lf
*.bat text eol=crlf
LICENSE text
# Binary files
*.gif binary
*.jpg binary
*.png binary

View file

@ -1,5 +1,4 @@
{ {
"extends": "@istanbuljs/nyc-config-typescript", "extends": "@istanbuljs/nyc-config-typescript",
"all": true, "all": true
"include": ["src/**"]
} }

View file

@ -1,9 +1,4 @@
#!/usr/bin/env -S node --experimental-specifier-resolution=node #!/usr/bin/env -S node
import { start, push, pull } from "../src/index"; import { start } from "../src/index.js";
import parser from "yargs-parser";
const argv = parser(process.argv.slice(2)); await start();
if (argv.push == true) await push();
else if (argv.pull == true) await pull();
else await start();

2154
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -6,10 +6,12 @@
"bin": "./dist/npx/bitburner-filesync.js", "bin": "./dist/npx/bitburner-filesync.js",
"main": "./dist/npx/bitburner-filesync.js", "main": "./dist/npx/bitburner-filesync.js",
"scripts": { "scripts": {
"prepare": "npm run build",
"build": "tsc --build --verbose --pretty", "build": "tsc --build --verbose --pretty",
"clean": "tsc --build --clean", "clean": "tsc --build --clean",
"format": "prettier -w .", "format": "prettier -w .",
"format:report": "prettier -c .", "format:report": "prettier -c .",
"lint": "eslint . --ext .ts",
"test:all": "tsc && nyc mocha --recursive test", "test:all": "tsc && nyc mocha --recursive test",
"test:single": "mocha", "test:single": "mocha",
"start": "ts-node npx/bitburner-filesync.ts" "start": "ts-node npx/bitburner-filesync.ts"
@ -31,11 +33,10 @@
"homepage": "https://github.com/bitburner-official/bitburner-filesync#readme", "homepage": "https://github.com/bitburner-official/bitburner-filesync#readme",
"dependencies": { "dependencies": {
"cheap-watch": "^1.0.4", "cheap-watch": "^1.0.4",
"convict": "^6.2.3", "convict": "^6.2.4",
"signal-js": "^3.0.1", "signal-js": "^3.0.1",
"typescript": "^4.8.4", "typescript": "^4.8.4",
"ws": "^8.8.1", "ws": "^8.8.1"
"yargs-parser": "^21.1.1"
}, },
"devDependencies": { "devDependencies": {
"@istanbuljs/nyc-config-typescript": "^1.0.2", "@istanbuljs/nyc-config-typescript": "^1.0.2",
@ -47,8 +48,17 @@
"@types/sinon": "^10.0.13", "@types/sinon": "^10.0.13",
"@types/sinon-chai": "^3.2.8", "@types/sinon-chai": "^3.2.8",
"@types/ws": "^8.5.3", "@types/ws": "^8.5.3",
"@typescript-eslint/eslint-plugin": "^5.40.0",
"@typescript-eslint/parser": "^5.40.0",
"@types/parse-json": "^4.0.0",
"@types/react": "^18.0.34",
"@types/react-is": "^17.0.3",
"@types/react-transition-group": "^4.4.5",
"@types/prop-types": "^15.7.5",
"@types/scheduler": "^0.16.3",
"chai": "^4.3.6", "chai": "^4.3.6",
"mocha": "^10.0.0", "eslint": "^8.25.0",
"mocha": "^10.1.0",
"nyc": "^15.1.0", "nyc": "^15.1.0",
"prettier": "^2.7.1", "prettier": "^2.7.1",
"sinon": "^14.0.0", "sinon": "^14.0.0",

View file

@ -2,7 +2,7 @@ import convict from "convict";
import { existsSync } from "fs"; import { existsSync } from "fs";
// Define a schema // Define a schema
export let config = convict({ export const config = convict({
allowedFiletypes: { allowedFiletypes: {
doc: "Filetypes that are synchronized to the game.", doc: "Filetypes that are synchronized to the game.",
format: "Array", format: "Array",
@ -28,6 +28,11 @@ export let config = convict({
env: "BB_SCRIPTFOLDER", env: "BB_SCRIPTFOLDER",
arg: "folder", arg: "folder",
}, },
exclude: {
doc: "A list of folders or files to exclude from the sync.",
format: "Array",
default: [".vscode", ".idea", ".github"],
},
quiet: { quiet: {
doc: "Log less internal events to stdout.", doc: "Log less internal events to stdout.",
format: "Boolean", format: "Boolean",
@ -63,13 +68,6 @@ export let config = convict({
default: false, default: false,
arg: "pushAllOnConnection", arg: "pushAllOnConnection",
}, },
disableWatch: {
doc: "Disable everything besides push/pulling through the external calls.",
format: "Boolean",
env: "BB_MANUAL",
default: false,
arg: "manual",
},
}); });
export function loadConfig() { export function loadConfig() {

View file

@ -1,7 +1,7 @@
export enum EventType { export class EventType {
ConnectionMade, static ConnectionMade = "ConnectionMade";
FileChanged, static FileChanged = "FileChanged";
FileDeleted, static FileDeleted = "FileDeleted";
MessageReceived, static MessageReceived = "MessageReceived";
MessageSend, static MessageSend = "MessageSend";
} }

View file

@ -1,21 +1,45 @@
import CheapWatch from "cheap-watch"; import CheapWatch from "cheap-watch";
import { config } from "./config"; import { config } from "./config.js";
import { EventType } from "./eventTypes"; import { EventType } from "./eventTypes.js";
import { mkdir } from "fs/promises"; import { mkdir } from "fs/promises";
import { resolve } from "path"; import { resolve, relative, isAbsolute } from "path";
import type { Signal } from "signal-js"; import type { Signal } from "signal-js";
import type { File } from "./interfaces"; import type { File } from "./interfaces.js";
/**
* Returns true if a directory is a subdirectory of a parent directory (not necessarily strict).
* @param dir The directory to perform the check on.
* @param parent The parent directory.
*/
function isSubDirOf(dir: string, parent: string) {
const relPath = relative(resolve(parent), resolve(dir));
return !!relPath && !relPath.startsWith("..") && !isAbsolute(relPath);
}
/**
* Returns true if the given file should be watched.
* @param file The provided file.
*/
function fileFilter(file: File) { function fileFilter(file: File) {
// If the file is excluded, skip all other checks and ignore it.
if (config.get("exclude").some((x) => isSubDirOf(file.path, x))) return false;
if (config.get("allowedFiletypes").some((extension) => file.path.endsWith(extension))) return true; if (config.get("allowedFiletypes").some((extension) => file.path.endsWith(extension))) return true;
if (file.stats.isDirectory()) return true; if (file.stats.isDirectory()) return true;
return false; return false;
} }
/**
* Type guard for {@code NodeJS.ErrnoException}.
* @param err
*/
function isError(err: unknown): err is NodeJS.ErrnoException { function isError(err: unknown): err is NodeJS.ErrnoException {
return (err as NodeJS.ErrnoException).code !== undefined; return (err as NodeJS.ErrnoException).code !== undefined;
} }
/**
* Sets up the file watch.
* @param signaller The signal event emitter.
*/
export async function setupWatch(signaller: Signal) { export async function setupWatch(signaller: Signal) {
try { try {
await mkdir(resolve(config.get("scriptsFolder"))); await mkdir(resolve(config.get("scriptsFolder")));

View file

@ -1,7 +1,6 @@
import { setupWatch } from "./fileWatch"; import { setupWatch } from "./fileWatch.js";
import { config, loadConfig } from "./config"; import { config, loadConfig } from "./config.js";
import { setupSocket } from "./networking/webSocket"; import { setupSocket } from "./networking/webSocket.js";
import { WebSocket } from "ws";
import signal from "signal-js"; import signal from "signal-js";
import { RawData } from "ws"; import { RawData } from "ws";
import { import {
@ -9,13 +8,14 @@ import {
fileRemovalEventToMsg, fileRemovalEventToMsg,
requestFilenames, requestFilenames,
requestDefinitionFile, requestDefinitionFile,
} from "./networking/messageGenerators"; } from "./networking/messageGenerators.js";
import { EventType } from "./eventTypes"; import { EventType } from "./eventTypes.js";
import { messageHandler } from "./networking/messageHandler"; import { messageHandler } from "./networking/messageHandler.js";
import { FileEvent } from "./interfaces"; import { FileEvent } from "./interfaces.js";
import { exit } from "process";
import CheapWatch from "cheap-watch";
/**
* Starts the file watcher.
*/
export async function start() { export async function start() {
loadConfig(); loadConfig();
const watch = await setupWatch(signal); const watch = await setupWatch(signal);
@ -24,18 +24,6 @@ export async function start() {
// Add a handler for received messages. // Add a handler for received messages.
signal.on(EventType.MessageReceived, (msg: RawData) => messageHandler(signal, msg, watch.paths)); signal.on(EventType.MessageReceived, (msg: RawData) => messageHandler(signal, msg, watch.paths));
console.log(`Server is ready, running on ${config.get("port")}!`);
process.on("SIGINT", function () {
console.log("Shutting down!");
watch.close();
socket.close();
process.exit();
});
}
async function watchMode(watch: CheapWatch) {
// Add a handler for when a connection to a game is made. // Add a handler for when a connection to a game is made.
signal.on(EventType.ConnectionMade, () => { signal.on(EventType.ConnectionMade, () => {
console.log("Connection made!"); console.log("Connection made!");
@ -67,26 +55,14 @@ async function watchMode(watch: CheapWatch) {
signal.on(EventType.FileDeleted, (fileEvent: FileEvent) => signal.on(EventType.FileDeleted, (fileEvent: FileEvent) =>
signal.emit(EventType.MessageSend, fileRemovalEventToMsg(fileEvent)), signal.emit(EventType.MessageSend, fileRemovalEventToMsg(fileEvent)),
); );
}
console.log(`Server is ready, running on ${config.get("port")}!`);
export async function pull() {
loadConfig(); process.on("SIGINT", function () {
const ws = new WebSocket(`ws://localhost:${config.get("port")}`); console.log("Shutting down!");
ws.onopen = () => { watch.close();
ws.send(JSON.stringify({ jsonrpc: "2.0", method: "manualPullFromGame", id: Number.MAX_SAFE_INTEGER })); socket.close();
ws.close(); process.exit();
exit(0); });
};
}
export async function push() {
loadConfig();
const ws = new WebSocket(`ws://localhost:${config.get("port")}`);
ws.onopen = () => {
ws.send(JSON.stringify({ jsonrpc: "2.0", method: "manualPushToGame", id: Number.MAX_SAFE_INTEGER }));
ws.close();
exit(0);
};
} }

View file

@ -1,7 +1,7 @@
import { readFileSync } from "fs"; import { readFileSync } from "fs";
import { config } from "../config"; import { config } from "../config.js";
import { join } from "path"; import { join } from "path";
import type { FileEvent, Message } from "../interfaces"; import type { FileEvent, Message } from "../interfaces.js";
let messageCounter = 0; let messageCounter = 0;
@ -49,17 +49,6 @@ export function requestFilenames(): Message {
}; };
} }
export function requestAllFiles(): Message {
return {
jsonrpc: "2.0",
method: "getAllFiles",
params: {
server: "home",
},
id: messageCounter++,
};
}
function addLeadingSlash(path: string): string { function addLeadingSlash(path: string): string {
const slashes = path.match("/"); const slashes = path.match("/");
if (slashes) return `/${path}`; if (slashes) return `/${path}`;

View file

@ -1,32 +1,11 @@
import { messageTracker } from "./messageTracker"; import { messageTracker } from "./messageTracker.js";
import { Stats, writeFile } from "fs"; import { Stats, writeFile } from "fs";
import { RawData } from "ws"; import { RawData } from "ws";
import { config } from "../config"; import { config } from "../config.js";
import { EventType } from "../eventTypes"; import { EventType } from "../eventTypes.js";
import { fileChangeEventToMsg, requestAllFiles } from "./messageGenerators"; import { fileChangeEventToMsg } from "./messageGenerators.js";
import type { Signal } from "signal-js"; import type { Signal } from "signal-js";
import { FileContent, Message } from "../interfaces"; import { Message } from "../interfaces.js";
/*function deserialize(data: RawData): Message {
const msg = JSON.parse(data.toString());
if (typeof msg.jsonrpc !== "string" || msg.jsonrpc !== "2.0" || typeof msg.id !== "number") {
throw Error("Malformed data received.");
}
const id: number = msg.id;
let request = messageTracker.get(id);
console.log(msg);
if (msg.error != null) {
throw Error(msg.error);
} else if (msg.result == null) {
throw Error("Malformed JSON received.");
}
return { jsonrpc: "2.0", method: !request? msg.method : request.method, result: msg.result, id };
}*/
function deserialize(data: RawData): Message { function deserialize(data: RawData): Message {
const msg = JSON.parse(data.toString()); const msg = JSON.parse(data.toString());
@ -36,26 +15,23 @@ function deserialize(data: RawData): Message {
} }
const id: number = msg.id; const id: number = msg.id;
const request = id === Number.MAX_SAFE_INTEGER ? { method: msg.method } : messageTracker.get(id); const request = messageTracker.get(id);
console.log("M:", msg); if (typeof request?.method !== "string") {
console.log("R:", request); throw Error("Malformed JSON received.");
} else if (msg.error != null) {
if (msg.error != null) {
throw Error(msg.error); throw Error(msg.error);
} else if (msg.result == null) {
throw Error("Malformed JSON received.");
} }
return { jsonrpc: "2.0", method: request?.method, result: msg.result, id }; return { jsonrpc: "2.0", method: request.method, result: msg.result, id };
} }
export function isStringArray(s: Array<unknown>): s is string[] { export function isStringArray(s: Array<unknown>): s is string[] {
return s.every((s) => typeof s === "string"); return s.every((s) => typeof s === "string");
} }
export function isFileContentArray(a: Array<unknown>): a is FileContent[] {
return a.every((f) => !!f && Object.hasOwn(f, "filename") && Object.hasOwn(f, "content"));
}
export function messageHandler(signaller: Signal, data: RawData, paths: Map<string, Stats>) { export function messageHandler(signaller: Signal, data: RawData, paths: Map<string, Stats>) {
let incoming; let incoming;
@ -73,9 +49,9 @@ export function messageHandler(signaller: Signal, data: RawData, paths: Map<stri
writeFile(config.get("definitionFile").location, incoming.result, (err) => { writeFile(config.get("definitionFile").location, incoming.result, (err) => {
if (err) return console.log(err); if (err) return console.log(err);
}); });
break;
case "getFileNames": break;
case "getFileNames": {
if (!Array.isArray(incoming.result) || !isStringArray(incoming.result)) if (!Array.isArray(incoming.result) || !isStringArray(incoming.result))
return console.log("Malformed data received."); return console.log("Malformed data received.");
@ -85,30 +61,7 @@ export function messageHandler(signaller: Signal, data: RawData, paths: Map<stri
if (!stats.isDirectory() && !gameFiles.includes(fileName)) if (!stats.isDirectory() && !gameFiles.includes(fileName))
signaller.emit(EventType.MessageSend, fileChangeEventToMsg({ path: fileName })); signaller.emit(EventType.MessageSend, fileChangeEventToMsg({ path: fileName }));
}); });
break; }
case "getAllFiles":
if (!Array.isArray(incoming.result) || !isFileContentArray(incoming.result))
return console.log("Malformed data received.");
const gameFiles2 = incoming.result.map((f: FileContent) => {
return { filename: removeLeadingSlash(f.filename), content: f.content };
});
gameFiles2.forEach((f) => console.log(f));
break;
case "manualPullFromGame":
console.log("PULL");
signaller.emit(EventType.MessageSend, requestAllFiles());
break;
case "manualPushToGame":
console.log("PUSH");
paths.forEach((stats, fileName) => {
if (!stats.isDirectory()) signaller.emit(EventType.MessageSend, fileChangeEventToMsg({ path: fileName }));
});
break;
} }
} }

View file

@ -1,4 +1,4 @@
import type { Message } from "../interfaces"; import type { Message } from "../interfaces.js";
class MessageTracker { class MessageTracker {
data = new Map<number, Message>(); data = new Map<number, Message>();

View file

@ -1,9 +1,9 @@
import type { Signal } from "signal-js"; import type { Signal } from "signal-js";
import { WebSocketServer } from "ws"; import { WebSocketServer } from "ws";
import { config } from "../config"; import { config } from "../config.js";
import { EventType } from "../eventTypes"; import { EventType } from "../eventTypes.js";
import { Message } from "../interfaces"; import { Message } from "../interfaces.js";
import { messageTracker } from "./messageTracker"; import { messageTracker } from "./messageTracker.js";
export function setupSocket(signaller: Signal) { export function setupSocket(signaller: Signal) {
const wss = new WebSocketServer({ port: config.get("port") }); const wss = new WebSocketServer({ port: config.get("port") });

6
src/signals.d.ts vendored
View file

@ -1,8 +1,8 @@
declare module "signal-js" { declare module "signal-js" {
export type Signal = typeof signal; export type Signal = typeof signal;
export default class signal { export default class signal {
static on<T>(event: number, callback: (data: T) => void): void; static on<T>(event: string, callback: (data: T) => void): void;
static emit<T>(event: number, data: T): void; static emit<T>(event: string, data: T): void;
static trigger(event: number): void; static trigger(event: string): void;
} }
} }

View file

@ -37,7 +37,7 @@ describe("messageGenerators", () => {
const msg = fileRemovalEventToMsg({ path: "sub/test.js" }); const msg = fileRemovalEventToMsg({ path: "sub/test.js" });
const result = (msg?.params as FileData).filename; const result = (msg?.params as FileData).filename;
if (result && result.hasOwnProperty("filename")) expect(result).to.eq("/sub/test.js"); if (result && Object.prototype.hasOwnProperty.call(result, "filename")) expect(result).to.eq("/sub/test.js");
}); });
}); });
@ -46,7 +46,7 @@ describe("messageGenerators", () => {
const msg = fileRemovalEventToMsg({ path: "test.js" }); const msg = fileRemovalEventToMsg({ path: "test.js" });
const result = (msg?.params as FileData).filename; const result = (msg?.params as FileData).filename;
if (result && result.hasOwnProperty("filename")) expect(result).to.eq("test.js"); if (result && Object.prototype.hasOwnProperty.call(result, "filename")) expect(result).to.eq("test.js");
}); });
}); });
@ -55,7 +55,7 @@ describe("messageGenerators", () => {
const msg = fileRemovalEventToMsg({ path: "/sub/test.js" }); const msg = fileRemovalEventToMsg({ path: "/sub/test.js" });
const result = (msg?.params as FileData).filename; const result = (msg?.params as FileData).filename;
if (result && result.hasOwnProperty("filename")) expect(result).to.eq("/sub/test.js"); if (result && Object.prototype.hasOwnProperty.call(result, "filename")) expect(result).to.eq("/sub/test.js");
}); });
}); });
}); });

View file

@ -1,5 +1,5 @@
import chai from "chai"; import chai from "chai";
import { createSandbox, stub } from "sinon"; import { createSandbox } from "sinon";
import sinonChai from "sinon-chai"; import sinonChai from "sinon-chai";
export async function mochaGlobalSetup() { export async function mochaGlobalSetup() {