Skip to content

Commit

Permalink
feat: allow other CWD
Browse files Browse the repository at this point in the history
  • Loading branch information
eartharoid committed Jan 11, 2024
1 parent 0e81c48 commit c596e23
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 43 deletions.
5 changes: 1 addition & 4 deletions .gitignore
Expand Up @@ -3,13 +3,10 @@
.vscode/
node_modules/
prisma/
./user/

# files
*.env*
*.db*
*.log
user/config.yml
user/**/*.*
!user/**/.gitkeep
!user/templates/*
summary.md
2 changes: 1 addition & 1 deletion db/sqlite/schema.prisma
Expand Up @@ -4,7 +4,7 @@ generator client {

datasource db {
provider = "sqlite"
url = "file:../user/database.db"
url = env("DB_CONNECTION_URL")
}

model ArchivedChannel {
Expand Down
20 changes: 16 additions & 4 deletions scripts/postinstall.js
Expand Up @@ -4,9 +4,16 @@ const fs = require('fs-extra');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const { short } = require('leeks.js');
const {
resolve, join,
} = require('path');

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

function pathify(path) {
return resolve(__dirname, '../', path);
}

function log(...strings) {
console.log(short('&9[postinstall]&r'), ...strings);
}
Expand All @@ -24,7 +31,7 @@ async function npx(cmd) {
const {
stderr,
stdout,
} = await exec(cmd);
} = await exec(cmd, { cwd: pathify('./') }); // { env } = process.env
if (stdout) console.log(stdout.toString());
if (stderr) console.log(stderr.toString());
}
Expand All @@ -42,15 +49,20 @@ if (!providers.includes(provider)) throw new Error(`DB_PROVIDER must be one of:
log(`provider=${provider}`);
log(`copying ${provider} schema & migrations`);

if (fs.existsSync('./prisma')) {
if (fs.existsSync(pathify('./prisma'))) {
fs.rmSync('./prisma', {
force: true,
recursive: true,
});
} else {
fs.mkdirSync('./prisma');
fs.mkdirSync(pathify('./prisma'));
}
fs.copySync(pathify(`./db/${provider}`), pathify('./prisma')); // copy schema & migrations

if (provider === 'sqlite' && !process.env.DB_CONNECTION_URL) {
process.env.DB_CONNECTION_URL = 'file:' + join(process.cwd(), './user/database.db');
log(`set DB_CONNECTION_URL=${process.env.DB_CONNECTION_URL}`);
}
fs.copySync(`./db/${provider}`, './prisma'); // copy schema & migrations

(async () => {
await npx('prisma generate');
Expand Down
14 changes: 10 additions & 4 deletions scripts/start.sh
@@ -1,10 +1,16 @@
#!/bin/sh
#!/usr/bin/env bash

source="${BASH_SOURCE}"
base_dir=$(dirname $(dirname "$source"))

echo "Checking environment..."
node scripts/preinstall
script=scripts/preinstall
node "$base_dir/$script"

echo "Preparing the database..."
node scripts/postinstall
script=scripts/postinstall
node "$base_dir/$script"

echo "Starting..."
node src/
script=src/
node "$base_dir/$script"
51 changes: 30 additions & 21 deletions src/client.js
Expand Up @@ -15,25 +15,28 @@ const ms = require('ms');

module.exports = class Client extends FrameworkClient {
constructor(config, log) {
super({
intents: [
...[
GatewayIntentBits.DirectMessages,
GatewayIntentBits.DirectMessageReactions,
GatewayIntentBits.DirectMessageTyping,
GatewayIntentBits.MessageContent,
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessages,
super(
{
intents: [
...[
GatewayIntentBits.DirectMessages,
GatewayIntentBits.DirectMessageReactions,
GatewayIntentBits.DirectMessageTyping,
GatewayIntentBits.MessageContent,
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessages,
],
...(process.env.PUBLIC_BOT !== 'true' ? [GatewayIntentBits.GuildPresences] : []),
],
...(process.env.PUBLIC_BOT !== 'true' ? [GatewayIntentBits.GuildPresences] : []),
],
partials: [
Partials.Channel,
Partials.Message,
Partials.Reaction,
],
});
partials: [
Partials.Channel,
Partials.Message,
Partials.Reaction,
],
},
{ baseDir: __dirname },
);

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

/** @type {PrismaClient} */
this.prisma = new PrismaClient({
const prisma_options = {
log: levels.map(level => ({
emit: 'event',
level,
})),
});
};

if (process.env.DB_PROVIDER === 'sqlite' && !process.env.DB_CONNECTION_URL) {
prisma_options.datasources = { db: { url:'file:' + join(process.cwd(), './user/database.db') } };
}

/** @type {PrismaClient} */
this.prisma = new PrismaClient(prisma_options);

this.prisma.$on('error', e => this.log.error.prisma(`${e.target} ${e.message}`));
this.prisma.$on('info', e => this.log.info.prisma(`${e.target} ${e.message}`));
Expand Down
25 changes: 16 additions & 9 deletions src/index.js
Expand Up @@ -29,13 +29,24 @@ console.log(banner(pkg.version)); // print big title

const semver = require('semver');
const { colours } = require('leeks.js');
const path = require('path');

// check node version
if (!semver.satisfies(process.versions.node, pkg.engines.node)) {
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.`));
process.exit(1);
}

const base_dir = path.resolve(path.join(__dirname, '../'));
const cwd = path.resolve(process.cwd());
if (base_dir !== cwd) {
console.log('\x07' + colours.yellowBright('Warning: The current working directory is not the same as the base directory.'));
console.log(colours.yellowBright('This may result in unexpected behaviour, particularly with missing environment variables.'));
console.log(' Base directory: ' + colours.gray(base_dir));
console.log(' CWD: ' + colours.gray(cwd));
console.log(colours.blueBright(' Learn more at https://lnk.earth/dt-cwd.'));
}

// this could be done first, but then there would be no banner :(
process.env.NODE_ENV ??= 'production'; // make sure NODE_ENV is set
require('./env').load(); // load and check environment variables
Expand All @@ -46,16 +57,12 @@ const logger = require('./lib/logger');
const Client = require('./client');
const http = require('./http');

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

const config = YAML.parse(fs.readFileSync('./user/config.yml', 'utf8'));
Expand Down
Empty file added src/user/avatars/.gitkeep
Empty file.
File renamed without changes.
30 changes: 30 additions & 0 deletions src/user/templates/transcript.md.mustache
@@ -0,0 +1,30 @@
#{{ channelName }} ticket transcript

---

* ID: {{ ticket.id }} ({{ guildName }})
* Number: {{ ticket.category.name }} #{{ ticket.number }}
* Topic: {{ #ticket.topic }}{{ . }}{{ /ticket.topic }}{{ ^ticket.topic }}(no topic){{ /ticket.topic }}
* Created on: {{ #ticket }}{{ createdAtFull }}{{ /ticket }}
* Created by: {{ #ticket.createdBy }}"{{ displayName }}" @{{ username }}#{{ discriminator }}{{ /ticket.createdBy }}
* Closed on: {{ #ticket }}{{ closedAtFull }}{{ /ticket }}
* Closed by: {{ #ticket.closedBy }}"{{ displayName }}" @{{ username }}#{{ discriminator }}{{ /ticket.closedBy }}{{ ^ticket.closedBy }}(automated){{ /ticket.closedBy }}
* Closed because: {{ #ticket.closedReason }}{{ ticket.closedReason }}{{ /ticket.closedReason }}{{ ^ticket.closedReason }}(no reason){{ /ticket.closedReason }}
* Claimed by: {{ #ticket.claimedBy }}"{{ displayName }}" @{{ username }}#{{ discriminator }}{{ /ticket.claimedBy }}{{ ^ticket.claimedBy }}(not claimed){{ /ticket.claimedBy }}
{{ #ticket.feedback }}
* Feedback:
* Rating: {{ rating }}/5
* Comment: {{ comment }}{{ ^comment }}(no comment){{ /comment }}
{{ /ticket.feedback }}
* Participants:
{{ #ticket.archivedUsers }}
* "{{ displayName }}" @{{ username }}#{{ discriminator }} ({{ userId }})
{{ /ticket.archivedUsers }}
* Pinned messages: {{ #pinned }}{{ . }}{{ /pinned }}

---

{{ #ticket.archivedMessages }}
<{{ number }}> [{{ createdAtTimestamp }}] {{author.displayName}}: {{ text }}

{{ /ticket.archivedMessages }}
Empty file added src/user/uploads/.gitkeep
Empty file.

0 comments on commit c596e23

Please sign in to comment.