feat: Accept asset paths as config.

This allows the server to be run globally with pre-defined configs:
  community-solid-server -c '$PACKAGE_ROOT/config/file.json'
This commit is contained in:
Ruben Verborgh 2021-08-03 12:08:41 +01:00
parent 18a71032c0
commit f28279e3a5
3 changed files with 23 additions and 19 deletions

View File

@ -5,7 +5,7 @@ import type { IComponentsManagerBuilderOptions, LogLevel } from 'componentsjs';
import { ComponentsManager } from 'componentsjs'; import { ComponentsManager } from 'componentsjs';
import yargs from 'yargs'; import yargs from 'yargs';
import { getLoggerFor } from '../logging/LogUtil'; import { getLoggerFor } from '../logging/LogUtil';
import { absoluteFilePath, ensureTrailingSlash, joinFilePath } from '../util/PathUtil'; import { ensureTrailingSlash, resolveAssetPath } from '../util/PathUtil';
import type { App } from './App'; import type { App } from './App';
export interface CliParams { export interface CliParams {
@ -85,11 +85,11 @@ export class AppRunner {
// Gather settings for instantiating the server // Gather settings for instantiating the server
const loaderProperties: IComponentsManagerBuilderOptions<App> = { const loaderProperties: IComponentsManagerBuilderOptions<App> = {
mainModulePath: this.resolveFilePath(params.mainModulePath), mainModulePath: resolveAssetPath(params.mainModulePath),
dumpErrorState: true, dumpErrorState: true,
logLevel: params.loggingLevel as LogLevel, logLevel: params.loggingLevel as LogLevel,
}; };
const configFile = this.resolveFilePath(params.config, 'config/default.json'); const configFile = resolveAssetPath(params.config ?? '$PACKAGE_ROOT/config/default.json');
// Create and execute the app // Create and execute the app
this.createApp(loaderProperties, configFile, params) this.createApp(loaderProperties, configFile, params)
@ -141,22 +141,10 @@ export class AppRunner {
params.baseUrl ? ensureTrailingSlash(params.baseUrl) : `http://localhost:${params.port}/`, params.baseUrl ? ensureTrailingSlash(params.baseUrl) : `http://localhost:${params.port}/`,
'urn:solid-server:default:variable:loggingLevel': params.loggingLevel, 'urn:solid-server:default:variable:loggingLevel': params.loggingLevel,
'urn:solid-server:default:variable:port': params.port, 'urn:solid-server:default:variable:port': params.port,
'urn:solid-server:default:variable:rootFilePath': 'urn:solid-server:default:variable:rootFilePath': resolveAssetPath(params.rootFilePath),
this.resolveFilePath(params.rootFilePath),
'urn:solid-server:default:variable:sparqlEndpoint': params.sparqlEndpoint, 'urn:solid-server:default:variable:sparqlEndpoint': params.sparqlEndpoint,
'urn:solid-server:default:variable:showStackTrace': params.showStackTrace, 'urn:solid-server:default:variable:showStackTrace': params.showStackTrace,
'urn:solid-server:default:variable:podConfigJson': 'urn:solid-server:default:variable:podConfigJson': resolveAssetPath(params.podConfigJson),
this.resolveFilePath(params.podConfigJson),
}; };
} }
/**
* Resolves a path relative to the current working directory,
* falling back to a path relative to this module.
*/
protected resolveFilePath(cwdPath?: string | null, modulePath = ''): string {
return typeof cwdPath === 'string' ?
absoluteFilePath(cwdPath) :
joinFilePath(__dirname, '../../', modulePath);
}
} }

View File

@ -173,13 +173,14 @@ export function getModuleRoot(): string {
return joinFilePath(__dirname, '../../'); return joinFilePath(__dirname, '../../');
} }
const modulePath = '$PACKAGE_ROOT/';
/** /**
* Converts file path inputs into absolute paths. * Converts file path inputs into absolute paths.
* Works similar to `absoluteFilePath` but paths that start with '$PACKAGE_ROOT/' * Works similar to `absoluteFilePath` but paths that start with '$PACKAGE_ROOT/'
* will be relative to the module directory instead of the cwd. * will be relative to the module directory instead of the cwd.
*/ */
export function resolveAssetPath(path: string): string { export function resolveAssetPath(path: string = modulePath): string {
const modulePath = '$PACKAGE_ROOT/';
if (path.startsWith(modulePath)) { if (path.startsWith(modulePath)) {
return joinFilePath(getModuleRoot(), path.slice(modulePath.length)); return joinFilePath(getModuleRoot(), path.slice(modulePath.length));
} }

View File

@ -207,6 +207,21 @@ describe('AppRunner', (): void => {
); );
}); });
it('accepts asset paths for the config flag.', async(): Promise<void> => {
new AppRunner().runCli({
argv: [
'node', 'script',
'--config', '$PACKAGE_ROOT/config/file.json',
],
});
await new Promise(setImmediate);
expect(manager.configRegistry.register).toHaveBeenCalledTimes(1);
expect(manager.configRegistry.register).toHaveBeenCalledWith(
joinFilePath(__dirname, '../../../config/file.json'),
);
});
it('uses the default process.argv in case none are provided.', async(): Promise<void> => { it('uses the default process.argv in case none are provided.', async(): Promise<void> => {
const { argv } = process; const { argv } = process;
process.argv = [ process.argv = [