mirror of
https://github.com/citra-emu/discord-bot.git
synced 2024-12-22 19:36:07 +00:00
package: use parcel bundler ...
* Significantly reduced deployment size * Bot starts up much more faster
This commit is contained in:
parent
a382896362
commit
3c1019e817
2
.github/workflows/nodejs.yml
vendored
2
.github/workflows/nodejs.yml
vendored
|
@ -2,7 +2,7 @@ name: Node.js CI
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches: [ master ]
|
branches: [ "*" ]
|
||||||
pull_request:
|
pull_request:
|
||||||
branches: [ master ]
|
branches: [ master ]
|
||||||
|
|
||||||
|
|
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -51,3 +51,5 @@ config/development.json
|
||||||
CMakeLists.txt.user*
|
CMakeLists.txt.user*
|
||||||
|
|
||||||
/dist
|
/dist
|
||||||
|
# Parcel's cache
|
||||||
|
/.parcel-cache
|
||||||
|
|
4
.parcelrc
Normal file
4
.parcelrc
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
{
|
||||||
|
"extends": "@parcel/config-default",
|
||||||
|
"resolvers": ["@parcel/resolver-glob", "..."]
|
||||||
|
}
|
10
Dockerfile
10
Dockerfile
|
@ -4,9 +4,10 @@ FROM node:16-alpine AS build
|
||||||
WORKDIR /usr/src/app
|
WORKDIR /usr/src/app
|
||||||
|
|
||||||
# Install app dependencies and add source files
|
# Install app dependencies and add source files
|
||||||
COPY package.json yarn.lock tsconfig.json ./
|
COPY package.json yarn.lock env.json bundle.sh .parcelrc ./
|
||||||
COPY src/ ./src
|
COPY src/ ./src
|
||||||
RUN yarn install && yarn build && rm -f dist/*.map
|
COPY patches/ ./patches
|
||||||
|
RUN apk add python3 build-base && yarn install && sh bundle.sh
|
||||||
|
|
||||||
# Second stage
|
# Second stage
|
||||||
FROM node:16-alpine
|
FROM node:16-alpine
|
||||||
|
@ -15,9 +16,6 @@ WORKDIR /usr/src/app
|
||||||
|
|
||||||
# Copy artifacts
|
# Copy artifacts
|
||||||
COPY --from=build /usr/src/app/dist/ ./
|
COPY --from=build /usr/src/app/dist/ ./
|
||||||
COPY --from=build /usr/src/app/node_modules ./node_modules
|
|
||||||
COPY env.json src/responses.json ./
|
|
||||||
COPY src/responses ./responses/
|
|
||||||
|
|
||||||
RUN addgroup -S app -g 50000 && \
|
RUN addgroup -S app -g 50000 && \
|
||||||
adduser -S -g app -u 50000 app && \
|
adduser -S -g app -u 50000 app && \
|
||||||
|
|
27
bundle.sh
Executable file
27
bundle.sh
Executable file
|
@ -0,0 +1,27 @@
|
||||||
|
#!/bin/bash -e
|
||||||
|
yarn
|
||||||
|
|
||||||
|
[ -d "dist" ] && rm -rf dist
|
||||||
|
|
||||||
|
echo "[+] Applying patches to accommodate bundler ..."
|
||||||
|
for i in patches/*.patch; do
|
||||||
|
echo "Applying $i ..."
|
||||||
|
patch -Np1 --no-backup-if-mismatch -i "$i"
|
||||||
|
done
|
||||||
|
|
||||||
|
yarn run parcel build
|
||||||
|
|
||||||
|
echo "[+] Installing non-bundle-able packages ..."
|
||||||
|
cd "dist"
|
||||||
|
echo '{"name": "citra-discord-bot","license": "GPL-2.0+"}' > package.json
|
||||||
|
yarn add discord.js@^13
|
||||||
|
cd ..
|
||||||
|
|
||||||
|
echo "[+] Reversing patches ..."
|
||||||
|
for i in patches/*.patch; do
|
||||||
|
echo "Reversing $i ..."
|
||||||
|
patch -Np1 -R -i "$i"
|
||||||
|
done
|
||||||
|
|
||||||
|
echo "[+] Removing patch backup files ..."
|
||||||
|
find . -name "*.orig" -print -delete
|
26
package.json
26
package.json
|
@ -8,22 +8,36 @@
|
||||||
"subdomain": "citra-emu",
|
"subdomain": "citra-emu",
|
||||||
"analyze": true,
|
"analyze": true,
|
||||||
"license": "GPL-2.0+",
|
"license": "GPL-2.0+",
|
||||||
|
"source": "src/server.ts",
|
||||||
|
"main": "dist/server.js",
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 16"
|
||||||
|
},
|
||||||
|
"targets": {
|
||||||
|
"main": {
|
||||||
|
"includeNodeModules": {
|
||||||
|
"discord.js": false
|
||||||
|
},
|
||||||
|
"context": "node",
|
||||||
|
"optimize": true
|
||||||
|
}
|
||||||
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"checkenv": "^1.2.2",
|
"checkenv": "^1.2.2",
|
||||||
"discord.js": "^13.2.0",
|
"discord.js": "^13.2.0",
|
||||||
"ip": "^1.1.5",
|
"ip": "^1.1.5",
|
||||||
"logdna": "^3.5.2",
|
"logdna": "^3.5.2",
|
||||||
"logdna-winston": "^4.0.1",
|
"logdna-winston": "^4.0.1",
|
||||||
"node-fetch": "^2",
|
"node-fetch": "^3",
|
||||||
"string-similarity": "^4.0.4",
|
"string-similarity": "^4.0.4",
|
||||||
"typescript": "^4.4.4",
|
|
||||||
"winston": "^3.3.3"
|
"winston": "^3.3.3"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@parcel/resolver-glob": "^2.0.0",
|
||||||
"@tsconfig/node14": "^1.0.1",
|
"@tsconfig/node14": "^1.0.1",
|
||||||
"@types/ip": "^1.1.0",
|
"@types/ip": "^1.1.0",
|
||||||
"@types/node": "^16.11.2",
|
"@types/node": "^16.11.2",
|
||||||
"@types/node-fetch": "^2",
|
"@types/node-fetch": "^3",
|
||||||
"@types/string-similarity": "^4.0.0",
|
"@types/string-similarity": "^4.0.0",
|
||||||
"@types/ws": "^8.2.0",
|
"@types/ws": "^8.2.0",
|
||||||
"eslint": "^8.0.1",
|
"eslint": "^8.0.1",
|
||||||
|
@ -32,10 +46,14 @@
|
||||||
"eslint-plugin-node": "^11.1.0",
|
"eslint-plugin-node": "^11.1.0",
|
||||||
"eslint-plugin-promise": "^5.1.1",
|
"eslint-plugin-promise": "^5.1.1",
|
||||||
"eslint-plugin-standard": "^4.1.0",
|
"eslint-plugin-standard": "^4.1.0",
|
||||||
"ts-node": "^10.3.1"
|
"parcel": "^2.0.0",
|
||||||
|
"ts-node": "^10.3.1",
|
||||||
|
"typescript": "^4.4.4"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
"check": "yarn run tsc --noEmit",
|
||||||
"build": "yarn run tsc",
|
"build": "yarn run tsc",
|
||||||
|
"dist": "./bundle.sh",
|
||||||
"serve": "yarn run ts-node ./src/server.ts"
|
"serve": "yarn run ts-node ./src/server.ts"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
95
patches/0001-server-make-it-bundler-friendly.patch
Normal file
95
patches/0001-server-make-it-bundler-friendly.patch
Normal file
|
@ -0,0 +1,95 @@
|
||||||
|
From 2ae094e7ad43d9137fafe84a36eac3e9bf0a8245 Mon Sep 17 00:00:00 2001
|
||||||
|
From: liushuyu <liushuyu011@gmail.com>
|
||||||
|
Date: Wed, 15 Sep 2021 22:49:40 -0600
|
||||||
|
Subject: [PATCH] server: make it bundler friendly
|
||||||
|
|
||||||
|
---
|
||||||
|
src/server.ts | 49 ++++++++++++++++++++-----------------------------
|
||||||
|
1 file changed, 20 insertions(+), 29 deletions(-)
|
||||||
|
|
||||||
|
diff --git a/src/server.ts b/src/server.ts
|
||||||
|
index bc63ae7..7cf8825 100644
|
||||||
|
--- a/src/server.ts
|
||||||
|
+++ b/src/server.ts
|
||||||
|
@@ -1,5 +1,7 @@
|
||||||
|
// Check for environmental variables.
|
||||||
|
-require('checkenv').check();
|
||||||
|
+const checkenv = require('checkenv');
|
||||||
|
+checkenv.setConfig(require('../env.json'));
|
||||||
|
+checkenv.check();
|
||||||
|
|
||||||
|
import discord = require('discord.js');
|
||||||
|
import path = require('path');
|
||||||
|
@@ -9,6 +11,10 @@ import logger from './logging';
|
||||||
|
import state from './state';
|
||||||
|
import * as data from './data';
|
||||||
|
import { IModule, ITrigger } from './models/interfaces';
|
||||||
|
+// Parcel glob imports
|
||||||
|
+import commands from './commands/*.ts';
|
||||||
|
+import triggers from './triggers/*.ts';
|
||||||
|
+import responses from './responses/*.json';
|
||||||
|
|
||||||
|
interface IModuleMap {
|
||||||
|
[name: string]: IModule;
|
||||||
|
@@ -233,37 +239,16 @@ client.on('message', message => {
|
||||||
|
});
|
||||||
|
|
||||||
|
// Cache all command modules.
|
||||||
|
-cachedModules = {};
|
||||||
|
-fs.readdirSync('./commands/').forEach(function (file) {
|
||||||
|
- // Load the module if it's a script.
|
||||||
|
- if (path.extname(file) === '.js') {
|
||||||
|
- if (file.includes('.disabled')) {
|
||||||
|
- logger.info(`Did not load disabled module: ${file}`);
|
||||||
|
- } else {
|
||||||
|
- const moduleName = path.basename(file, '.js').toLowerCase();
|
||||||
|
- logger.info(`Loaded module: ${moduleName} from ${file}`);
|
||||||
|
- cachedModules[moduleName] = require(`./commands/${file}`);
|
||||||
|
- }
|
||||||
|
- }
|
||||||
|
+cachedModules = commands;
|
||||||
|
+Object.entries(commands).forEach(function (command) {
|
||||||
|
+ logger.info(`Loaded command: ${command[0]}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Cache all triggers.
|
||||||
|
cachedTriggers = [];
|
||||||
|
-fs.readdirSync('./triggers/').forEach(function (file) {
|
||||||
|
- // Load the module if it's a script.
|
||||||
|
- if (path.extname(file) === '.js') {
|
||||||
|
- if (file.includes('.disabled')) {
|
||||||
|
- logger.info(`Did not load disabled trigger: ${file}`);
|
||||||
|
- } else {
|
||||||
|
- const moduleName = path.basename(file, '.js').toLowerCase();
|
||||||
|
- logger.info(`Loaded trigger: ${moduleName} from ${file}`);
|
||||||
|
- try {
|
||||||
|
- cachedTriggers.push(require(`./triggers/${file}`));
|
||||||
|
- } catch (e) {
|
||||||
|
- logger.error(`Could not load trigger ${moduleName}: ${e}`);
|
||||||
|
- }
|
||||||
|
- }
|
||||||
|
- }
|
||||||
|
+Object.entries(triggers).forEach((trigger: [string, ITrigger]) => {
|
||||||
|
+ cachedTriggers.push(trigger[1]);
|
||||||
|
+ logger.info(`Loaded trigger: ${trigger[0]}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
data.readWarnings();
|
||||||
|
@@ -271,7 +256,13 @@ data.readBans();
|
||||||
|
|
||||||
|
// Load custom responses
|
||||||
|
if (process.env.DATA_CUSTOM_RESPONSES) {
|
||||||
|
- data.readCustomResponses();
|
||||||
|
+ // Load the responses file into the responses variable.
|
||||||
|
+ state.responses = responses[process.env.TENANT];
|
||||||
|
+ if (!state.responses) {
|
||||||
|
+ logger.error(`Failed to load ${process.env.TENANT} from cache! Custom responses are disabled.`);
|
||||||
|
+ } else {
|
||||||
|
+ logger.debug(`Loaded ${process.env.TENANT} responses from cache.`);
|
||||||
|
+ }
|
||||||
|
}
|
||||||
|
|
||||||
|
client.login(process.env.DISCORD_LOGIN_TOKEN);
|
||||||
|
--
|
||||||
|
2.33.0
|
||||||
|
|
11
patches/0002-winston-use-bundled-logform.patch
Normal file
11
patches/0002-winston-use-bundled-logform.patch
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
--- a/node_modules/winston/dist/winston.js 2021-08-26 22:26:24.296000000 -0600
|
||||||
|
+++ b/node_modules/winston/dist/winston.js 2021-09-15 23:35:13.136034453 -0600
|
||||||
|
@@ -6,7 +6,7 @@
|
||||||
|
*/
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
-var logform = require('logform');
|
||||||
|
+import * as logform from 'logform/dist/browser.js';
|
||||||
|
|
||||||
|
var _require = require('./winston/common'),
|
||||||
|
warn = _require.warn;
|
Loading…
Reference in a new issue