Merge pull request #283 from MicBlaze/patch-1

Made it possible for exe to be compiled with `pkg`
This commit is contained in:
Cohee
2023-05-12 23:43:27 +03:00
committed by GitHub
3 changed files with 35 additions and 27 deletions

View File

@ -51,11 +51,14 @@
"no-path-concat": "off", "no-path-concat": "off",
"no-var": "off" "no-var": "off"
}, },
"main": "server.js",
"pkg": { "pkg": {
"assets": [ "assets": [
"node_modules/open/xdg-open/", "node_modules/**/*",
"public", "poe_graphql/**/*"
"uploads" ],
] "scripts": [
} "server.js"
]
}
} }

View File

@ -20,7 +20,10 @@ const cliArguments = yargs(hideBin(process.argv))
}).argv; }).argv;
// change all relative paths // change all relative paths
process.chdir(__dirname) // process.chdir(__dirname)
//console.log('cwd is: ',process.cwd()) //correct external project path
const path = require('path');
process.chdir(path.dirname(process.execPath));
const express = require('express'); const express = require('express');
const compression = require('compression'); const compression = require('compression');
@ -42,7 +45,7 @@ const encode = require('png-chunks-encode');
const PNGtext = require('png-chunk-text'); const PNGtext = require('png-chunk-text');
const jimp = require('jimp'); const jimp = require('jimp');
const path = require('path'); //const path = require('path');
const sanitize = require('sanitize-filename'); const sanitize = require('sanitize-filename');
const mime = require('mime-types'); const mime = require('mime-types');
@ -60,10 +63,10 @@ const utf8Encode = new TextEncoder();
const utf8Decode = new TextDecoder('utf-8', { ignoreBOM: true }); const utf8Decode = new TextDecoder('utf-8', { ignoreBOM: true });
const commandExistsSync = require('command-exists').sync; const commandExistsSync = require('command-exists').sync;
const config = require(path.join(__dirname, './config.conf')); const config = require(path.join(process.cwd(), './config.conf'));
const server_port = process.env.SILLY_TAVERN_PORT || config.port; const server_port = process.env.SILLY_TAVERN_PORT || config.port;
const whitelistPath = path.join(__dirname, "./whitelist.txt"); const whitelistPath = path.join(process.cwd(), "./whitelist.txt");
let whitelist = config.whitelist; let whitelist = config.whitelist;
if (fs.existsSync(whitelistPath)) { if (fs.existsSync(whitelistPath)) {
@ -254,7 +257,7 @@ app.use((req, res, next) => {
console.log(filePath); console.log(filePath);
fs.access(filePath, fs.constants.R_OK, (err) => { fs.access(filePath, fs.constants.R_OK, (err) => {
if (!err) { if (!err) {
res.sendFile(filePath, { root: __dirname }); res.sendFile(filePath, { root: process.cwd() });
} else { } else {
res.send('Character not found: ' + filePath); res.send('Character not found: ' + filePath);
//next(); //next();
@ -265,10 +268,10 @@ app.use((req, res, next) => {
} }
}); });
app.use(express.static(__dirname + "/public", { refresh: true })); app.use(express.static(process.cwd() + "/public", { refresh: true }));
app.use('/backgrounds', (req, res) => { app.use('/backgrounds', (req, res) => {
const filePath = decodeURIComponent(path.join(__dirname, 'public/backgrounds', req.url.replace(/%20/g, ' '))); const filePath = decodeURIComponent(path.join(process.cwd(), 'public/backgrounds', req.url.replace(/%20/g, ' ')));
fs.readFile(filePath, (err, data) => { fs.readFile(filePath, (err, data) => {
if (err) { if (err) {
res.status(404).send('File not found'); res.status(404).send('File not found');
@ -280,7 +283,7 @@ app.use('/backgrounds', (req, res) => {
}); });
app.use('/characters', (req, res) => { app.use('/characters', (req, res) => {
const filePath = decodeURIComponent(path.join(__dirname, charactersPath, req.url.replace(/%20/g, ' '))); const filePath = decodeURIComponent(path.join(process.cwd(), charactersPath, req.url.replace(/%20/g, ' ')));
fs.readFile(filePath, (err, data) => { fs.readFile(filePath, (err, data) => {
if (err) { if (err) {
res.status(404).send('File not found'); res.status(404).send('File not found');
@ -291,16 +294,16 @@ app.use('/characters', (req, res) => {
}); });
app.use(multer({ dest: "uploads" }).single("avatar")); app.use(multer({ dest: "uploads" }).single("avatar"));
app.get("/", function (request, response) { app.get("/", function (request, response) {
response.sendFile(__dirname + "/public/index.html"); response.sendFile(process.cwd() + "/public/index.html");
}); });
app.get("/notes/*", function (request, response) { app.get("/notes/*", function (request, response) {
response.sendFile(__dirname + "/public" + request.url + ".html"); response.sendFile(process.cwd() + "/public" + request.url + ".html");
}); });
app.get('/get_faq', function (_, response) { app.get('/get_faq', function (_, response) {
response.sendFile(__dirname + "/faq.md"); response.sendFile(process.cwd() + "/faq.md");
}); });
app.get('/get_readme', function (_, response) { app.get('/get_readme', function (_, response) {
response.sendFile(__dirname + "/readme.md"); response.sendFile(process.cwd() + "/readme.md");
}); });
app.get('/deviceinfo', function (request, response) { app.get('/deviceinfo', function (request, response) {
const userAgent = request.header('user-agent'); const userAgent = request.header('user-agent');
@ -315,7 +318,7 @@ app.get('/version', function (_, response) {
pkgVersion = pkgJson.version; pkgVersion = pkgJson.version;
if (commandExistsSync('git')) { if (commandExistsSync('git')) {
gitRevision = require('child_process') gitRevision = require('child_process')
.execSync('git rev-parse --short HEAD', { cwd: __dirname }) .execSync('git rev-parse --short HEAD', { cwd: process.cwd() })
.toString().trim(); .toString().trim();
gitBranch = require('child_process') gitBranch = require('child_process')
@ -1685,7 +1688,7 @@ app.post("/exportcharacter", jsonParser, async function (request, response) {
switch (request.body.format) { switch (request.body.format) {
case 'png': case 'png':
return response.sendFile(filename, { root: __dirname }); return response.sendFile(filename, { root: process.cwd() });
case 'json': { case 'json': {
try { try {
let json = await charaRead(filename); let json = await charaRead(filename);
@ -1715,7 +1718,7 @@ app.post("/exportcharacter", jsonParser, async function (request, response) {
await webp.cwebp(filename, inputWebpPath, '-q 95'); await webp.cwebp(filename, inputWebpPath, '-q 95');
await webp.webpmux_add(inputWebpPath, outputWebpPath, metadataPath, 'exif'); await webp.webpmux_add(inputWebpPath, outputWebpPath, metadataPath, 'exif');
response.sendFile(outputWebpPath, { root: __dirname }, () => { response.sendFile(outputWebpPath, { root: process.cwd() }, () => {
fs.rmSync(inputWebpPath); fs.rmSync(inputWebpPath);
fs.rmSync(metadataPath); fs.rmSync(metadataPath);
fs.rmSync(outputWebpPath); fs.rmSync(outputWebpPath);
@ -2360,7 +2363,7 @@ app.get('/thumbnail', jsonParser, async function (request, response) {
if (config.disableThumbnails == true) { if (config.disableThumbnails == true) {
const pathToOriginalFile = path.join(getOriginalFolder(type), file); const pathToOriginalFile = path.join(getOriginalFolder(type), file);
return response.sendFile(pathToOriginalFile, { root: __dirname }); return response.sendFile(pathToOriginalFile, { root: process.cwd() });
} }
const pathToCachedFile = await generateThumbnail(type, file); const pathToCachedFile = await generateThumbnail(type, file);
@ -2369,7 +2372,7 @@ app.get('/thumbnail', jsonParser, async function (request, response) {
return response.sendStatus(404); return response.sendStatus(404);
} }
return response.sendFile(pathToCachedFile, { root: __dirname }); return response.sendFile(pathToCachedFile, { root: process.cwd() });
}); });
/* OpenAI */ /* OpenAI */

View File

@ -3,9 +3,11 @@
* allow access to the endpoint after successful authentication. * allow access to the endpoint after successful authentication.
*/ */
const {dirname} = require('path'); //const {dirname} = require('path');
const appDir = dirname(require.main.filename); //const appDir = dirname(require.main.filename);
const config = require(appDir + '/config.conf'); //const config = require(appDir + '/config.conf');
const path = require('path');
const config = require(path.join(process.cwd(), './config.conf'));
const unauthorizedResponse = (res) => { const unauthorizedResponse = (res) => {
res.set('WWW-Authenticate', 'Basic realm="SillyTavern", charset="UTF-8"'); res.set('WWW-Authenticate', 'Basic realm="SillyTavern", charset="UTF-8"');