feat: allow other CWD

This commit is contained in:
Isaac
2024-01-11 00:08:57 +00:00
parent 0e81c4833e
commit c596e237de
10 changed files with 104 additions and 43 deletions

View File

@@ -29,6 +29,7 @@ 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)) {
@@ -36,6 +37,16 @@ if (!semver.satisfies(process.versions.node, pkg.engines.node)) {
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
@@ -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'));