mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
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:
parent
18a71032c0
commit
f28279e3a5
@ -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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -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));
|
||||||
}
|
}
|
||||||
|
@ -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 = [
|
||||||
|
Loading…
x
Reference in New Issue
Block a user