diff --git a/bs.js b/bs.js new file mode 100644 index 0000000..e1da857 --- /dev/null +++ b/bs.js @@ -0,0 +1,18 @@ +import chokidar from 'chokidar'; +import cluster from 'cluster'; +import { QMainWindow } from '@nodegui/nodegui'; +import { watchFile } from 'fs'; +import watch from 'watch'; + +if(cluster.isMaster) { + cluster.fork(); +} else { + // chokidar.watch('test.js', { + // useFsEvents: false, + // usePolling: false, + // "" + // }).on('all', (a, b) => console.log(a, b)); + watch.watchTree('out', console.log); + // watchFile('test.js', console.log) +} + diff --git a/lib/aliases.mjs b/lib/aliases.mjs index bdcac20..4bb3c08 100644 --- a/lib/aliases.mjs +++ b/lib/aliases.mjs @@ -6,7 +6,8 @@ const moduleAliases = { "@tasks": "./out/src/registries/Tasks.js", "@items": "./out/src/registries/Items.js", "@world": "./out/src/World.js", - "@ui": "./out/src/term-ui/UI.js", + // "@ui": "./out/src/term-ui/UI.js", + "@ui": "./out/src/qt/index.js", "@game": "./out/src/Game.js" }; diff --git a/package.json b/package.json index cdd96c4..3505f16 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "@types/fs-extra": "^9.0.11", "@types/mocha": "^8.2.2", "@types/uuid": "^8.3.0", + "@types/watch": "^1.0.2", "@types/ws": "^7.4.5", "@web/dev-server": "^0.1.18", "bonjour": "^3.5.0", @@ -35,6 +36,7 @@ "typescript": "^4.3.2", "uuid": "^8.3.2", "walk-sync": "^3.0.0", + "watch": "^1.0.2", "ws": "^7.4.6", "yarn": "^1.22.10" }, @@ -47,6 +49,7 @@ "lint": "eslint src/**/*.ts", "web": "web-dev-server --open index.html --node-resolve --watch", "profile": "node --inspect-brk --no-warnings --loader ./lib/aliases.mjs --enable-source-maps out/src/index.js", - "gui": "qode qode.js" + "gui": "qode out/src/qt/qode.js", + "qode": "qode" } } diff --git a/qode.js b/qode.js deleted file mode 100644 index ec2bb1c..0000000 --- a/qode.js +++ /dev/null @@ -1,53 +0,0 @@ -import { - QMainWindow, - QWidget, - QLabel, - FlexLayout, - QPushButton, - QIcon -} from '@nodegui/nodegui'; -// import logo from '../assets/logox200.png'; - -const win = new QMainWindow(); -win.setWindowTitle("Hello World"); - -const centralWidget = new QWidget(); -centralWidget.setObjectName("myroot"); -const rootLayout = new FlexLayout(); -centralWidget.setLayout(rootLayout); - -const label = new QLabel(); -label.setObjectName("mylabel"); -label.setText("Hello"); - -const button = new QPushButton(); -// button.setIcon(new QIcon(logo)); - -const label2 = new QLabel(); -label2.setText("World"); -label2.setInlineStyle(` - color: red; -`); - -rootLayout.addWidget(label); -rootLayout.addWidget(button); -rootLayout.addWidget(label2); -win.setCentralWidget(centralWidget); -win.setStyleSheet( - ` - #myroot { - background-color: #009688; - height: '100%'; - align-items: 'center'; - justify-content: 'center'; - } - #mylabel { - font-size: 16px; - font-weight: bold; - padding: 1; - } - ` -); -win.show(); - -global.win = win; \ No newline at end of file diff --git a/src/Clustering.ts b/src/Clustering.ts new file mode 100644 index 0000000..81f1b1c --- /dev/null +++ b/src/Clustering.ts @@ -0,0 +1,86 @@ +import cluster from 'cluster'; +import chokidar from 'chokidar'; +import watch from 'watch'; + +let init: Function = null; +const hotReload = true; +let restartTimer: NodeJS.Timeout = null; + +let worker: cluster.Worker = null; +function createWorker() { + start(); + + function start() { + if (cluster.isMaster) { + worker = cluster.fork(); + // console.log(worker); + // worker.process.stdout.pipe(process.stdout); + // worker.process.stderr.pipe(process.stderr); + worker.on('message', (message) => { + if(message === 'ipc-restart') { + worker.on('exit', () => { + setTimeout(createWorker, 0); + }); + worker.kill(); + } else if (message === 'ipc-quit') { + worker.on('exit', () => { + process.exit(0); + }) + worker.kill(); + } + }); + } + } +} + +// from any cluster context, gracefully exit if needed, and begin anew! +export function restart() { + if (cluster.isWorker) { + process.send('ipc-restart'); + } else if(worker) { + worker.on('exit', () => { + setTimeout(createWorker, 0); + }) + worker.kill(); + } else { + createWorker(); + } +} + +export function quit() { + if (cluster.isWorker) { + process.send('ipc-quit'); + } +} + +export function start() { + if(init) { + if (cluster.isWorker) { + init(); + } else { + // TODO make hotreload actually bring a popup on the client + // so that the user should press enter to enable a reload. + if(hotReload) { + function fileChange(f: watch.FileOrFiles) { + console.log('changes detected'); + // appendFileSync('log.log', evt + ' ' + path + '\n'); + // console.log(cluster.isMaster, evt, path); + if(restartTimer) clearTimeout(restartTimer) + restartTimer = setTimeout(() => { + restart(); + restartTimer = null; + }, 1000); + } + // chokidar.watch('./out').on('all', fileChange); + watch.watchTree('./out', fileChange); + } else { + createWorker(); + } + } + } +} + + +export function setInitialize(fn: Function) { + init = fn; +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index e09b5c9..6a8da03 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,75 +7,12 @@ import { Game } from '@game'; import { isStarted, stop, render } from '@ui'; import { writeFileSync } from 'fs'; import ansi from 'sisteransi'; -import { spawn } from 'child_process'; import cluster from 'cluster'; -import chokidar from 'chokidar'; +if(cluster.isMaster) cluster.setupMaster(); +import { setInitialize, start } from './Clustering.js'; -const hotReload = true; -let restartTimer: NodeJS.Timeout = null; - -let worker: cluster.Worker = null; -function createWorker() { - start(); - - function start() { - if (cluster.isMaster) { - worker = cluster.fork(); - worker.on('message', (message) => { - if(message === 'ipc-restart') { - worker.on('exit', () => { - setTimeout(createWorker, 0); - }) - worker.kill(); - } else if (message === 'ipc-quit') { - worker.on('exit', () => { - process.exit(0); - }) - worker.kill(); - } - }); - } - } -} - -// from any cluster context, gracefully exit if needed, and begin anew! -export function restart() { - if (cluster.isWorker) { - process.send('ipc-restart'); - } else if(worker) { - worker.on('exit', () => { - setTimeout(createWorker, 0); - }) - worker.kill(); - } else { - createWorker(); - } -} - -export function quit() { - if (cluster.isWorker) { - process.send('ipc-quit'); - } -} - -if (cluster.isWorker) { - begin(); -} else { - // TODO make hotreload actually bring a popup on the client - // so that the user should press enter to enable a reload. - if(hotReload) { - chokidar.watch('./out').on('all', (evt, path) => { - appendFileSync('log.log', evt + ' ' + path + '\n'); - if(restartTimer) clearTimeout(restartTimer) - restartTimer = setTimeout(() => { - restart(); - restartTimer = null; - }, 1000); - }) - } else { - createWorker(); - } -} +setInitialize(begin); +start(); async function begin() { console.clear(); diff --git a/src/qt/index.ts b/src/qt/index.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/qt/qode.ts b/src/qt/qode.ts new file mode 100644 index 0000000..bb7e803 --- /dev/null +++ b/src/qt/qode.ts @@ -0,0 +1,54 @@ + +import { + QMainWindow, + QWidget, + QGridLayout, + QPushButton +} from '@nodegui/nodegui'; +import chokidar from 'chokidar'; + +import cluster from 'cluster'; +import { watchFile } from 'fs'; +if (cluster.isMaster) cluster.setupMaster(); +import { setInitialize, start } from '../Clustering.js'; + + +setInitialize(() => { + + const win = new QMainWindow(); + win.setFixedSize(800, 600); + win.setWindowTitle("Hello World"); + const centralWidget = new QWidget(); + centralWidget.setObjectName("myroot"); + win.setCentralWidget(centralWidget); + const rootLayout = new QGridLayout(); + // rootLayout.addWidget + + centralWidget.setLayout(rootLayout); + + + + const button = new QPushButton(); + button.setText('Testing Font Family'); + rootLayout.addWidget(button); + + win.setStyleSheet(` + #myroot { + background-color: black; + height: '100%'; + } + QPushButton { + background: #333333; + } + QPushButton:pressed { + background: cyan; + color: black; + } + * { + font-family: 'MxPlus IBM VGA 8x16'; + font-size: 16px; + } + `); + win.show(); +}); +start(); diff --git a/src/term-ui/EscapeMenu.ts b/src/term-ui/EscapeMenu.ts index 1600cf7..d5fd26d 100644 --- a/src/term-ui/EscapeMenu.ts +++ b/src/term-ui/EscapeMenu.ts @@ -2,7 +2,7 @@ import { Game } from "@game"; import { boxStyle, getTheme } from "@themes"; import { panels } from "./UI.js"; import blessed from 'neo-blessed'; -import { quit, restart } from "../index.js"; +import { quit, restart } from "../Clustering.js"; // TODO convert all these popup-y things to be View based // make them be boxes that have a view diff --git a/test.js b/test.js new file mode 100644 index 0000000..69e3c8a --- /dev/null +++ b/test.js @@ -0,0 +1,15 @@ +// test +//asdf + +//asdasd + +/changes? + +sdfasfd + +fdsd + + + + +hddhfbgdfkljfkfdskljkdlkjdlfkjs \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 45fe9ad..adbe784 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -13,7 +13,7 @@ "@tasks": ["./src/registries/Tasks"], "@items": ["./src/registries/Items"], "@world": ["./src/World"], - "@ui": ["./src/term-ui/UI"], + "@ui": ["./src/qt/index.js"], "@game": ["./src/Game"] }, "noImplicitAny": true diff --git a/yarn.lock b/yarn.lock index 51c1244..b84bb49 100644 --- a/yarn.lock +++ b/yarn.lock @@ -288,6 +288,13 @@ version "8.3.0" resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-8.3.0.tgz#215c231dff736d5ba92410e6d602050cce7e273f" +"@types/watch@^1.0.2": + version "1.0.2" + resolved "https://registry.yarnpkg.com/@types/watch/-/watch-1.0.2.tgz#fdcc629c2a17f9f37dccce8cd1d70a9be89885b3" + integrity sha512-ylzbsiDt6U6yBIP3vhv2zC+mz1AIE1cVCqXW0rfeHWypUsKM8QOGbsNAHCtqANtqHoj7HVXkxDSpKN+LJamscw== + dependencies: + "@types/node" "*" + "@types/ws@^7.4.0": version "7.4.6" resolved "https://registry.yarnpkg.com/@types/ws/-/ws-7.4.6.tgz#c4320845e43d45a7129bb32905e28781c71c1fff" @@ -1165,6 +1172,13 @@ etag@^1.8.1: resolved "https://registry.yarnpkg.com/etag/-/etag-1.8.1.tgz#41ae2eeb65efa62268aebfea83ac7d79299b0887" integrity sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc= +exec-sh@^0.2.0: + version "0.2.2" + resolved "https://registry.yarnpkg.com/exec-sh/-/exec-sh-0.2.2.tgz#2a5e7ffcbd7d0ba2755bdecb16e5a427dfbdec36" + integrity sha512-FIUCJz1RbuS0FKTdaAafAByGS0CPvU3R0MeHxgtl+djzCc//F8HakL8GzmVNZanasTbTAY/3DRFA0KpVqj/eAw== + dependencies: + merge "^1.2.0" + faker@^5.5.3: version "5.5.3" resolved "https://registry.npmjs.org/faker/-/faker-5.5.3.tgz" @@ -1915,6 +1929,11 @@ memory-stream@0: dependencies: readable-stream "~1.0.26-2" +merge@^1.2.0: + version "1.2.1" + resolved "https://registry.yarnpkg.com/merge/-/merge-1.2.1.tgz#38bebf80c3220a8a487b6fcfb3941bb11720c145" + integrity sha512-VjFo4P5Whtj4vsLzsYBu5ayHhoHJ0UqNm7ibvShmbmoz7tGi0vXaoJbGdB+GmDMLUdg8DpQXEIeVDAe8MaABvQ== + mime-db@1.48.0: version "1.48.0" resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.48.0.tgz#e35b31045dd7eada3aaad537ed88a33afbef2d1d" @@ -2905,6 +2924,14 @@ walk-sync@^3.0.0: matcher-collection "^2.0.1" minimatch "^3.0.4" +watch@^1.0.2: + version "1.0.2" + resolved "https://registry.yarnpkg.com/watch/-/watch-1.0.2.tgz#340a717bde765726fa0aa07d721e0147a551df0c" + integrity sha1-NApxe952Vyb6CqB9ch4BR6VR3ww= + dependencies: + exec-sh "^0.2.0" + minimist "^1.2.0" + webidl-conversions@^6.1.0: version "6.1.0" resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-6.1.0.tgz#9111b4d7ea80acd40f5270d666621afa78b69514"