Skip to content

Commit

Permalink
WIP logo/terminal stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
TheArcaneBrony committed Oct 27, 2024
1 parent 694c5a0 commit b551c02
Show file tree
Hide file tree
Showing 18 changed files with 1,689 additions and 47 deletions.
Binary file added assets/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 36 additions & 21 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@
flake-utils.url = "github:numtide/flake-utils";
};

outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachSystem flake-utils.lib.allSystems (system:
outputs =
{
self,
nixpkgs,
flake-utils,
}:
flake-utils.lib.eachSystem flake-utils.lib.allSystems (
system:
let
pkgs = import nixpkgs {
inherit system;
};
hashesFile = builtins.fromJSON (builtins.readFile ./hashes.json);
lib = pkgs.lib;
in rec {
in
rec {
packages.default = pkgs.buildNpmPackage {
pname = "spacebar-server-ts";
name = "spacebar-server-ts";
Expand All @@ -41,27 +48,35 @@
npm prune --omit dev --no-save $npmInstallFlags "''${npmInstallFlagsArray[@]}" $npmFlags "''${npmFlagsArray[@]}"
find node_modules -maxdepth 1 -type d -empty -delete
mkdir -p $out/node_modules/
cp -r node_modules/* $out/node_modules/
cp -r dist/ $out/node_modules/@spacebar
#mkdir -p $out/node_modules/
#cp -r node_modules/* $out/node_modules/
#cp -r dist/ $out/node_modules/@spacebar
#for i in dist/**/start.js
#do
# makeWrapper ${pkgs.nodejs-slim}/bin/node $out/bin/start-`dirname ''${i/dist\//}` --prefix NODE_PATH : $out/node_modules --add-flags $out/node_modules/@spacebar`dirname ''${i/dist/}`/start.js
#done
#set +x
#substituteInPlace package.json --replace 'dist/' 'node_modules/@spacebar/'
#find $out/node_modules/@spacebar/ -type f -name "*.js" | while read srcFile; do
# echo Patching imports in ''${srcFile/$out\/node_modules\/@spacebar//}...
# substituteInPlace $srcFile --replace 'require("./' 'require(__dirname + "/'
# substituteInPlace $srcFile --replace 'require("../' 'require(__dirname + "/../'
# substituteInPlace $srcFile --replace ', "assets"' ', "..", "assets"'
# #substituteInPlace $srcFile --replace 'require("@spacebar/' 'require("
#done
#set -x
#cp -r assets/ $out/
#cp package.json $out/
#rm -v $out/assets/openapi.json
##rm -v $out/assets/schemas.json
mkdir -p $out
cp -r assets dist node_modules package.json $out/
for i in dist/**/start.js
do
makeWrapper ${pkgs.nodejs-slim}/bin/node $out/bin/start-`dirname ''${i/dist\//}` --prefix NODE_PATH : $out/node_modules --add-flags $out/node_modules/@spacebar`dirname ''${i/dist/}`/start.js
makeWrapper ${pkgs.nodejs-slim}/bin/node $out/bin/start-`dirname ''${i/dist\//}` --prefix NODE_PATH : $out/node_modules --add-flags $out/$i
done
set +x
substituteInPlace package.json --replace 'dist/' 'node_modules/@spacebar/'
find $out/node_modules/@spacebar/ -type f -name "*.js" | while read srcFile; do
echo Patching imports in ''${srcFile/$out\/node_modules\/@spacebar//}...
substituteInPlace $srcFile --replace 'require("./' 'require(__dirname + "/'
substituteInPlace $srcFile --replace 'require("../' 'require(__dirname + "/../'
substituteInPlace $srcFile --replace ', "assets"' ', "..", "assets"'
#substituteInPlace $srcFile --replace 'require("@spacebar/' 'require("
done
set -x
cp -r assets/ $out/
cp package.json $out/
rm -v $out/assets/openapi.json
#rm -v $out/assets/schemas.json
#debug utils:
#cp $out/node_modules/@spacebar/ $out/build_output -r
Expand Down
96 changes: 96 additions & 0 deletions package-lock.json

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

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,19 @@
"@spacebar/util": "dist/util"
},
"optionalDependencies": {
"@types/ref": "^0.0.32",
"@types/ref-struct": "^0.0.33",
"@yukikaze-bot/erlpack": "^1.0.1",
"erlpack": "^0.1.4",
"ioctl": "^2.0.2",
"jimp": "^0.22.12",
"mysql": "^2.18.1",
"node-ioctl": "*",
"nodemailer-mailgun-transport": "^2.1.5",
"nodemailer-mailjet-transport": "github:n0script22/nodemailer-mailjet-transport",
"nodemailer-sendgrid-transport": "github:Maria-Golomb/nodemailer-sendgrid-transport",
"pg": "^8.11.3",
"ref-struct": "^1.1.0",
"sqlite3": "^5.1.6"
}
}
4 changes: 2 additions & 2 deletions src/api/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ try {
}

if (cluster.isPrimary && process.env.NODE_ENV == "production") {
console.log(`Primary ${process.pid} is running`);
console.log(`Primary PID: ${process.pid}`);

// Fork workers.
for (let i = 0; i < cores; i++) {
cluster.fork();
}

cluster.on("exit", (worker) => {
console.log(`worker ${worker.process.pid} died, restart worker`);
console.log(`Worker ${worker.process.pid} died, restarting worker`);
cluster.fork();
});
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/bundle/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ async function main() {

Sentry.errorHandler(app);

console.log(`[Server] ${green(`listening on port ${bold(port)}`)}`);
console.log(`[Server] ${green(`Listening on port ${bold(port)}`)}`);
}

main().catch(console.error);
35 changes: 17 additions & 18 deletions src/bundle/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

// process.env.MONGOMS_DEBUG = "true";
import moduleAlias from "module-alias";

moduleAlias(__dirname + "../../../package.json");

import "reflect-metadata";
Expand All @@ -26,8 +27,10 @@ import os from "os";
import { red, bold, yellow, cyan } from "picocolors";
import { initStats } from "./stats";
import { config } from "dotenv";

config();
import { execSync } from "child_process";
import { centerString, Logo } from "@spacebar/util";

const cores = process.env.THREADS ? parseInt(process.env.THREADS) : 1;

Expand All @@ -41,23 +44,19 @@ function getCommitOrFail() {

if (cluster.isPrimary) {
const commit = getCommitOrFail();

Logo.printLogo();
console.log(
bold(`
███████╗██████╗ █████╗ ██████╗███████╗██████╗ █████╗ ██████╗
██╔════╝██╔══██╗██╔══██╗██╔════╝██╔════╝██╔══██╗██╔══██╗██╔══██╗
███████╗██████╔╝███████║██║ █████╗ ██████╔╝███████║██████╔╝
╚════██║██╔═══╝ ██╔══██║██║ ██╔══╝ ██╔══██╗██╔══██║██╔══██╗
███████║██║ ██║ ██║╚██████╗███████╗██████╔╝██║ ██║██║ ██║
╚══════╝╚═╝ ╚═╝ ╚═╝ ╚═════╝╚══════╝╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝
spacebar-server | ${yellow(
`Pre-release (${
commit !== null
? commit.slice(0, 7)
: "Unknown (Git cannot be found)"
})`,
)}
${centerString(
`spacebar-server | ${yellow(
`Pre-release (${
commit !== null
? commit.slice(0, 7)
: "Unknown (Git cannot be found)"
})`,
)}`,
64,
)}
Commit Hash: ${
commit !== null
Expand All @@ -74,7 +73,7 @@ Cores: ${cyan(os.cpus().length)} (Using ${cores} thread(s).)

initStats();

console.log(`[Process] starting with ${cores} threads`);
console.log(`[Process] Starting with ${cores} threads`);

if (cores === 1) {
require("./Server");
Expand All @@ -87,7 +86,7 @@ Cores: ${cyan(os.cpus().length)} (Using ${cores} thread(s).)
const delay = process.env.DATABASE?.includes("://") ? 0 : i * 1000;
setTimeout(() => {
cluster.fork();
console.log(`[Process] worker ${cyan(i)} started.`);
console.log(`[Process] Worker ${cyan(i)} started.`);
}, delay);
}

Expand All @@ -102,7 +101,7 @@ Cores: ${cyan(os.cpus().length)} (Using ${cores} thread(s).)
cluster.on("exit", (worker) => {
console.log(
`[Worker] ${red(
`died with PID: ${worker.process.pid} , restarting ...`,
`PID ${worker.process.pid} died, restarting ...`,
)}`,
);
cluster.fork();
Expand Down
36 changes: 31 additions & 5 deletions src/bundle/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,44 @@

import os from "os";
import osu from "node-os-utils";
import { readFileSync } from "node:fs";
import { red } from "picocolors";

export function initStats() {
console.log(`[Path] running in ${__dirname}`);
console.log(`[Path] Running in ${process.cwd()}`);
console.log(`[Path] Running from ${__dirname}`);
try {
console.log(`[CPU] ${osu.cpu.model()} Cores x${osu.cpu.count()}`);
console.log(`[CPU] ${osu.cpu.model()} (x${osu.cpu.count()})`);
} catch {
console.log("[CPU] Failed to get cpu model!");
console.log("[CPU] Failed to get CPU model!");
}

console.log(`[System] ${os.platform()} ${os.arch()}`);
console.log(`[Process] running with PID: ${process.pid}`);
console.log(`[System] ${os.platform()} ${os.release()} ${os.arch()}`);
if (os.platform() == "linux") {
try {
const osReleaseLines = readFileSync(
"/etc/os-release",
"utf8",
).split("\n");
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
const osRelease: any = {};

Check failure on line 42 in src/bundle/stats.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Unexpected any. Specify a different type
for (const line of osReleaseLines) {
if (!line) continue;
const [key, value] = line.match(/(.*?)="?([^"]*)"?/)!.slice(1);
osRelease[key] = value;
}
console.log(
`[System]\x1b[${osRelease.ANSI_COLOR}m ${osRelease.NAME ?? "Unknown"} ${osRelease.VERSION ?? "Unknown"} (${osRelease.BUILD_ID ?? "No build ID"})\x1b[0m`,
);
} catch (e) {
console.log(
"[System] Unknown Linux distribution (missing /etc/os-release)",
);
console.log(e);
}
}
console.log(`[Process] Running with PID: ${process.pid}`);
if (process.getuid && process.getuid() === 0) {
console.warn(
red(
Expand Down
Loading

0 comments on commit b551c02

Please sign in to comment.