config.cli: migrate to TypeScript

This commit is contained in:
Jesse Chan
2021-01-30 19:11:22 +08:00
parent d54878d99a
commit 5afa79b274
9 changed files with 39 additions and 55 deletions
-1
View File
@@ -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
View File
@@ -1,7 +0,0 @@
// @see shared/schema/Config
import type {Config} from '@shared/schema/Config';
declare const CONFIG: Config;
export = CONFIG;
-1
View File
@@ -1 +0,0 @@
config.cli.js
+28 -14
View File
@@ -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
View File
@@ -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;
-9
View File
@@ -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.`));
+3 -3
View File
@@ -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 => {
-1
View File
@@ -9,7 +9,6 @@ declare const PATHS: {
clientSrc: string;
testsSetup: string;
appNodeModules: string;
servedPath: string;
};
export = PATHS;
-12
View File
@@ -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;