mirror of
https://github.com/zoriya/flood.git
synced 2026-05-31 02:15:12 +00:00
config.cli: migrate to TypeScript
This commit is contained in:
@@ -26,7 +26,6 @@ COPY . ./
|
||||
RUN npm ci --no-optional
|
||||
|
||||
# Build assets
|
||||
RUN cp config.cli.js config.js
|
||||
RUN npm run build-assets
|
||||
|
||||
# Now get the clean Node.js image
|
||||
|
||||
Vendored
-7
@@ -1,7 +0,0 @@
|
||||
// @see shared/schema/Config
|
||||
|
||||
import type {Config} from '@shared/schema/Config';
|
||||
|
||||
declare const CONFIG: Config;
|
||||
|
||||
export = CONFIG;
|
||||
+28
-14
@@ -1,9 +1,18 @@
|
||||
const {spawn} = require('child_process');
|
||||
const crypto = require('crypto');
|
||||
const fs = require('fs');
|
||||
const os = require('os');
|
||||
const path = require('path');
|
||||
const {argv} = require('yargs')
|
||||
import {spawn} from 'child_process';
|
||||
import crypto from 'crypto';
|
||||
import fs from 'fs';
|
||||
import os from 'os';
|
||||
import path from 'path';
|
||||
import yargs from 'yargs';
|
||||
|
||||
import {configSchema} from '@shared/schema/Config';
|
||||
|
||||
import type {Config} from '@shared/schema/Config';
|
||||
import type {ClientConnectionSettings} from '@shared/schema/ClientConnectionSettings';
|
||||
|
||||
import {version} from './package.json';
|
||||
|
||||
const {argv} = yargs
|
||||
.env('FLOOD_OPTION_')
|
||||
.option('baseuri', {
|
||||
default: '/',
|
||||
@@ -158,7 +167,7 @@ const {argv} = require('yargs')
|
||||
hidden: true,
|
||||
type: 'boolean',
|
||||
})
|
||||
.version(require('./package.json').version)
|
||||
.version(version)
|
||||
.alias('v', 'version')
|
||||
.help();
|
||||
|
||||
@@ -203,7 +212,7 @@ if (argv.rtorrent) {
|
||||
}
|
||||
|
||||
const DEFAULT_SECRET_PATH = path.join(argv.rundir, 'flood.secret');
|
||||
let secret;
|
||||
let secret: string;
|
||||
|
||||
if (!argv.secret) {
|
||||
try {
|
||||
@@ -223,7 +232,7 @@ if (!argv.secret) {
|
||||
({secret} = argv);
|
||||
}
|
||||
|
||||
let connectionSettings;
|
||||
let connectionSettings: Partial<ClientConnectionSettings> | undefined;
|
||||
if (argv.rtsocket != null || argv.rthost != null) {
|
||||
if (argv.rtsocket != null) {
|
||||
connectionSettings = {
|
||||
@@ -261,19 +270,19 @@ if (argv.rtsocket != null || argv.rthost != null) {
|
||||
};
|
||||
}
|
||||
|
||||
let authMethod = 'default';
|
||||
let authMethod: Config['authMethod'] = 'default';
|
||||
if (argv.noauth || argv.auth === 'none') {
|
||||
authMethod = 'none';
|
||||
}
|
||||
|
||||
let allowedPaths = [];
|
||||
let allowedPaths: string[] = [];
|
||||
if (typeof argv.allowedpath === 'string') {
|
||||
allowedPaths = allowedPaths.concat(argv.allowedpath.split(','));
|
||||
} else if (Array.isArray(argv.allowedpath)) {
|
||||
allowedPaths = allowedPaths.concat(argv.allowedpath);
|
||||
}
|
||||
|
||||
const CONFIG = {
|
||||
const result = configSchema.safeParse({
|
||||
baseURI: argv.baseuri,
|
||||
dbCleanInterval: argv.dbclean,
|
||||
dbPath: path.resolve(path.join(argv.rundir, 'db')),
|
||||
@@ -292,6 +301,11 @@ const CONFIG = {
|
||||
sslCert: argv.sslcert || path.resolve(path.join(argv.rundir, 'fullchain.pem')),
|
||||
allowedPaths: allowedPaths.length > 0 ? allowedPaths : undefined,
|
||||
serveAssets: argv.assets,
|
||||
};
|
||||
});
|
||||
|
||||
module.exports = CONFIG;
|
||||
if (!result.success) {
|
||||
console.error(`Invalid configuration: ${result.error.message}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
export default result.data;
|
||||
+8
-7
@@ -23,10 +23,11 @@ declare global {
|
||||
}
|
||||
}
|
||||
|
||||
const app = express();
|
||||
|
||||
Users.bootstrapServicesForAllUsers();
|
||||
|
||||
const app = express();
|
||||
const servedPath = config.baseURI.endsWith('/') ? config.baseURI : `${config.baseURI}/`;
|
||||
|
||||
// Remove Express header
|
||||
if (process.env.NODE_ENV !== 'development') {
|
||||
app.disable('x-powered-by');
|
||||
@@ -45,22 +46,22 @@ if (config.serveAssets !== false) {
|
||||
app.use(compression());
|
||||
|
||||
// Static assets
|
||||
app.use(paths.servedPath, express.static(paths.appDist));
|
||||
app.use(servedPath, express.static(paths.appDist));
|
||||
|
||||
// Client app routes, serve index.html and client js will figure it out
|
||||
const html = fs.readFileSync(path.join(paths.appDist, 'index.html'), {
|
||||
encoding: 'utf8',
|
||||
});
|
||||
|
||||
app.get(`${paths.servedPath}login`, (_req, res) => {
|
||||
app.get(`${servedPath}login`, (_req, res) => {
|
||||
res.send(html);
|
||||
});
|
||||
|
||||
app.get(`${paths.servedPath}register`, (_req, res) => {
|
||||
app.get(`${servedPath}register`, (_req, res) => {
|
||||
res.send(html);
|
||||
});
|
||||
|
||||
app.get(`${paths.servedPath}overview`, (_req, res) => {
|
||||
app.get(`${servedPath}overview`, (_req, res) => {
|
||||
res.send(html);
|
||||
});
|
||||
} else {
|
||||
@@ -80,6 +81,6 @@ app.use(cookieParser());
|
||||
|
||||
passportConfig(passport);
|
||||
|
||||
app.use(`${paths.servedPath}api`, apiRoutes);
|
||||
app.use(`${servedPath}api`, apiRoutes);
|
||||
|
||||
export default app;
|
||||
|
||||
@@ -3,7 +3,6 @@ import path from 'path';
|
||||
|
||||
import {appDist} from '../../shared/config/paths';
|
||||
import config from '../../config';
|
||||
import {configSchema} from '../../shared/schema/Config';
|
||||
|
||||
const staticAssets = [path.join(appDist, 'index.html')];
|
||||
|
||||
@@ -27,14 +26,6 @@ const enforcePrerequisites = () =>
|
||||
return;
|
||||
}
|
||||
|
||||
// Ensures that there is a proper configuration
|
||||
const result = configSchema.safeParse(config);
|
||||
if (!result.success) {
|
||||
console.error(result.error.message);
|
||||
reject(new Error('Invalid configuration.'));
|
||||
return;
|
||||
}
|
||||
|
||||
// Ensure static assets exist if they need to be served
|
||||
if (!doFilesExist(staticAssets) && config.serveAssets !== false) {
|
||||
reject(new Error(`Static assets are missing.`));
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
import {tempPath} from '../../config';
|
||||
import config from '../../config';
|
||||
|
||||
fs.mkdirSync(tempPath, {recursive: true});
|
||||
fs.mkdirSync(config.tempPath, {recursive: true});
|
||||
|
||||
export const getTempPath = (filename: string): string => {
|
||||
return path.join(tempPath, filename);
|
||||
return path.join(config.tempPath, filename);
|
||||
};
|
||||
|
||||
export const deleteFile = (filename: string): void => {
|
||||
|
||||
Vendored
-1
@@ -9,7 +9,6 @@ declare const PATHS: {
|
||||
clientSrc: string;
|
||||
testsSetup: string;
|
||||
appNodeModules: string;
|
||||
servedPath: string;
|
||||
};
|
||||
|
||||
export = PATHS;
|
||||
|
||||
@@ -1,21 +1,10 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const userConfig = require('../../config');
|
||||
|
||||
// Make sure any symlinks in the project folder are resolved:
|
||||
// https://github.com/facebookincubator/create-react-app/issues/637
|
||||
const appDirectory = path.resolve(path.join(__dirname, '../..'));
|
||||
const resolveApp = (relativePath) => path.resolve(appDirectory, relativePath);
|
||||
const ensureSlash = (questionablePath, needsSlash) => {
|
||||
const hasSlash = questionablePath.endsWith('/');
|
||||
if (hasSlash && !needsSlash) {
|
||||
return questionablePath.substr(questionablePath, questionablePath.length - 1);
|
||||
}
|
||||
if (!hasSlash && needsSlash) {
|
||||
return `${questionablePath}/`;
|
||||
}
|
||||
return questionablePath;
|
||||
};
|
||||
|
||||
const getAppDist = () => {
|
||||
// In production, assets are in assets/.
|
||||
@@ -47,7 +36,6 @@ const PATHS = {
|
||||
clientSrc: resolveApp('client/src'),
|
||||
testsSetup: resolveApp('tests/setupTests.js'),
|
||||
appNodeModules: resolveApp('node_modules'),
|
||||
servedPath: ensureSlash(userConfig.baseURI || '/', true),
|
||||
};
|
||||
|
||||
module.exports = PATHS;
|
||||
|
||||
Reference in New Issue
Block a user