Skip to content

Commit c596e23

Browse files
committed
feat: allow other CWD
1 parent 0e81c48 commit c596e23

File tree

10 files changed

+104
-43
lines changed

10 files changed

+104
-43
lines changed

.gitignore

+1-4
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@
33
.vscode/
44
node_modules/
55
prisma/
6+
./user/
67

78
# files
89
*.env*
910
*.db*
1011
*.log
11-
user/config.yml
12-
user/**/*.*
13-
!user/**/.gitkeep
14-
!user/templates/*
1512
summary.md

db/sqlite/schema.prisma

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ generator client {
44

55
datasource db {
66
provider = "sqlite"
7-
url = "file:../user/database.db"
7+
url = env("DB_CONNECTION_URL")
88
}
99

1010
model ArchivedChannel {

scripts/postinstall.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@ const fs = require('fs-extra');
44
const util = require('util');
55
const exec = util.promisify(require('child_process').exec);
66
const { short } = require('leeks.js');
7+
const {
8+
resolve, join,
9+
} = require('path');
710

811
const fallback = { prisma: './node_modules/prisma/build/index.js' };
912

13+
function pathify(path) {
14+
return resolve(__dirname, '../', path);
15+
}
16+
1017
function log(...strings) {
1118
console.log(short('&9[postinstall]&r'), ...strings);
1219
}
@@ -24,7 +31,7 @@ async function npx(cmd) {
2431
const {
2532
stderr,
2633
stdout,
27-
} = await exec(cmd);
34+
} = await exec(cmd, { cwd: pathify('./') }); // { env } = process.env
2835
if (stdout) console.log(stdout.toString());
2936
if (stderr) console.log(stderr.toString());
3037
}
@@ -42,15 +49,20 @@ if (!providers.includes(provider)) throw new Error(`DB_PROVIDER must be one of:
4249
log(`provider=${provider}`);
4350
log(`copying ${provider} schema & migrations`);
4451

45-
if (fs.existsSync('./prisma')) {
52+
if (fs.existsSync(pathify('./prisma'))) {
4653
fs.rmSync('./prisma', {
4754
force: true,
4855
recursive: true,
4956
});
5057
} else {
51-
fs.mkdirSync('./prisma');
58+
fs.mkdirSync(pathify('./prisma'));
59+
}
60+
fs.copySync(pathify(`./db/${provider}`), pathify('./prisma')); // copy schema & migrations
61+
62+
if (provider === 'sqlite' && !process.env.DB_CONNECTION_URL) {
63+
process.env.DB_CONNECTION_URL = 'file:' + join(process.cwd(), './user/database.db');
64+
log(`set DB_CONNECTION_URL=${process.env.DB_CONNECTION_URL}`);
5265
}
53-
fs.copySync(`./db/${provider}`, './prisma'); // copy schema & migrations
5466

5567
(async () => {
5668
await npx('prisma generate');

scripts/start.sh

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
#!/bin/sh
1+
#!/usr/bin/env bash
2+
3+
source="${BASH_SOURCE}"
4+
base_dir=$(dirname $(dirname "$source"))
25

36
echo "Checking environment..."
4-
node scripts/preinstall
7+
script=scripts/preinstall
8+
node "$base_dir/$script"
59

610
echo "Preparing the database..."
7-
node scripts/postinstall
11+
script=scripts/postinstall
12+
node "$base_dir/$script"
813

914
echo "Starting..."
10-
node src/
15+
script=src/
16+
node "$base_dir/$script"

src/client.js

+30-21
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,28 @@ const ms = require('ms');
1515

1616
module.exports = class Client extends FrameworkClient {
1717
constructor(config, log) {
18-
super({
19-
intents: [
20-
...[
21-
GatewayIntentBits.DirectMessages,
22-
GatewayIntentBits.DirectMessageReactions,
23-
GatewayIntentBits.DirectMessageTyping,
24-
GatewayIntentBits.MessageContent,
25-
GatewayIntentBits.Guilds,
26-
GatewayIntentBits.GuildMembers,
27-
GatewayIntentBits.GuildMessages,
18+
super(
19+
{
20+
intents: [
21+
...[
22+
GatewayIntentBits.DirectMessages,
23+
GatewayIntentBits.DirectMessageReactions,
24+
GatewayIntentBits.DirectMessageTyping,
25+
GatewayIntentBits.MessageContent,
26+
GatewayIntentBits.Guilds,
27+
GatewayIntentBits.GuildMembers,
28+
GatewayIntentBits.GuildMessages,
29+
],
30+
...(process.env.PUBLIC_BOT !== 'true' ? [GatewayIntentBits.GuildPresences] : []),
2831
],
29-
...(process.env.PUBLIC_BOT !== 'true' ? [GatewayIntentBits.GuildPresences] : []),
30-
],
31-
partials: [
32-
Partials.Channel,
33-
Partials.Message,
34-
Partials.Reaction,
35-
],
36-
});
32+
partials: [
33+
Partials.Channel,
34+
Partials.Message,
35+
Partials.Reaction,
36+
],
37+
},
38+
{ baseDir: __dirname },
39+
);
3740

3841
const locales = {};
3942
fs.readdirSync(join(__dirname, 'i18n'))
@@ -57,13 +60,19 @@ module.exports = class Client extends FrameworkClient {
5760
const levels = ['error', 'info', 'warn'];
5861
if (this.config.logs.level === 'debug') levels.push('query');
5962

60-
/** @type {PrismaClient} */
61-
this.prisma = new PrismaClient({
63+
const prisma_options = {
6264
log: levels.map(level => ({
6365
emit: 'event',
6466
level,
6567
})),
66-
});
68+
};
69+
70+
if (process.env.DB_PROVIDER === 'sqlite' && !process.env.DB_CONNECTION_URL) {
71+
prisma_options.datasources = { db: { url:'file:' + join(process.cwd(), './user/database.db') } };
72+
}
73+
74+
/** @type {PrismaClient} */
75+
this.prisma = new PrismaClient(prisma_options);
6776

6877
this.prisma.$on('error', e => this.log.error.prisma(`${e.target} ${e.message}`));
6978
this.prisma.$on('info', e => this.log.info.prisma(`${e.target} ${e.message}`));

src/index.js

+16-9
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,24 @@ console.log(banner(pkg.version)); // print big title
2929

3030
const semver = require('semver');
3131
const { colours } = require('leeks.js');
32+
const path = require('path');
3233

3334
// check node version
3435
if (!semver.satisfies(process.versions.node, pkg.engines.node)) {
3536
console.log('\x07' + colours.redBright(`Error: Your current Node.js version, ${process.versions.node}, does not meet the requirement "${pkg.engines.node}". Please update to version ${semver.minVersion(pkg.engines.node).version} or higher.`));
3637
process.exit(1);
3738
}
3839

40+
const base_dir = path.resolve(path.join(__dirname, '../'));
41+
const cwd = path.resolve(process.cwd());
42+
if (base_dir !== cwd) {
43+
console.log('\x07' + colours.yellowBright('Warning: The current working directory is not the same as the base directory.'));
44+
console.log(colours.yellowBright('This may result in unexpected behaviour, particularly with missing environment variables.'));
45+
console.log(' Base directory: ' + colours.gray(base_dir));
46+
console.log(' CWD: ' + colours.gray(cwd));
47+
console.log(colours.blueBright(' Learn more at https://lnk.earth/dt-cwd.'));
48+
}
49+
3950
// this could be done first, but then there would be no banner :(
4051
process.env.NODE_ENV ??= 'production'; // make sure NODE_ENV is set
4152
require('./env').load(); // load and check environment variables
@@ -46,16 +57,12 @@ const logger = require('./lib/logger');
4657
const Client = require('./client');
4758
const http = require('./http');
4859

60+
// user directory may or may not exist depending on if sqlite is being used
61+
// so the config file could be missing even though the directory exists
4962
if (!fs.existsSync('./user/config.yml')) {
50-
const examplePath = './user/example.config.yml';
51-
if (!fs.existsSync(examplePath)) {
52-
console.log('\x07' + colours.redBright('The config file does not exist, and the example file is missing so cannot be copied from.'));
53-
process.exit(1);
54-
} else {
55-
console.log('Creating config file...');
56-
fs.copyFileSync(examplePath, './user/config.yml');
57-
console.log(`Copied config to ${'./user/config.yml'}`);
58-
}
63+
console.log('The config file does not exist, copying defaults...');
64+
fs.cpSync(path.join(__dirname, 'user'), './user', { recursive: true });
65+
console.log('Created user directory at', path.join(cwd, 'user'));
5966
}
6067

6168
const config = YAML.parse(fs.readFileSync('./user/config.yml', 'utf8'));

src/user/avatars/.gitkeep

Whitespace-only changes.
File renamed without changes.
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#{{ channelName }} ticket transcript
2+
3+
---
4+
5+
* ID: {{ ticket.id }} ({{ guildName }})
6+
* Number: {{ ticket.category.name }} #{{ ticket.number }}
7+
* Topic: {{ #ticket.topic }}{{ . }}{{ /ticket.topic }}{{ ^ticket.topic }}(no topic){{ /ticket.topic }}
8+
* Created on: {{ #ticket }}{{ createdAtFull }}{{ /ticket }}
9+
* Created by: {{ #ticket.createdBy }}"{{ displayName }}" @{{ username }}#{{ discriminator }}{{ /ticket.createdBy }}
10+
* Closed on: {{ #ticket }}{{ closedAtFull }}{{ /ticket }}
11+
* Closed by: {{ #ticket.closedBy }}"{{ displayName }}" @{{ username }}#{{ discriminator }}{{ /ticket.closedBy }}{{ ^ticket.closedBy }}(automated){{ /ticket.closedBy }}
12+
* Closed because: {{ #ticket.closedReason }}{{ ticket.closedReason }}{{ /ticket.closedReason }}{{ ^ticket.closedReason }}(no reason){{ /ticket.closedReason }}
13+
* Claimed by: {{ #ticket.claimedBy }}"{{ displayName }}" @{{ username }}#{{ discriminator }}{{ /ticket.claimedBy }}{{ ^ticket.claimedBy }}(not claimed){{ /ticket.claimedBy }}
14+
{{ #ticket.feedback }}
15+
* Feedback:
16+
* Rating: {{ rating }}/5
17+
* Comment: {{ comment }}{{ ^comment }}(no comment){{ /comment }}
18+
{{ /ticket.feedback }}
19+
* Participants:
20+
{{ #ticket.archivedUsers }}
21+
* "{{ displayName }}" @{{ username }}#{{ discriminator }} ({{ userId }})
22+
{{ /ticket.archivedUsers }}
23+
* Pinned messages: {{ #pinned }}{{ . }}{{ /pinned }}
24+
25+
---
26+
27+
{{ #ticket.archivedMessages }}
28+
<{{ number }}> [{{ createdAtTimestamp }}] {{author.displayName}}: {{ text }}
29+
30+
{{ /ticket.archivedMessages }}

src/user/uploads/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)