Skip to content

Commit

Permalink
feat: Accept asset paths as config.
Browse files Browse the repository at this point in the history
This allows the server to be run globally with pre-defined configs:
  community-solid-server -c '$PACKAGE_ROOT/config/file.json'
  • Loading branch information
RubenVerborgh committed Aug 3, 2021
1 parent 18a7103 commit f28279e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
22 changes: 5 additions & 17 deletions src/init/AppRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { IComponentsManagerBuilderOptions, LogLevel } from 'componentsjs';
import { ComponentsManager } from 'componentsjs';
import yargs from 'yargs';
import { getLoggerFor } from '../logging/LogUtil';
import { absoluteFilePath, ensureTrailingSlash, joinFilePath } from '../util/PathUtil';
import { ensureTrailingSlash, resolveAssetPath } from '../util/PathUtil';
import type { App } from './App';

export interface CliParams {
Expand Down Expand Up @@ -85,11 +85,11 @@ export class AppRunner {

// Gather settings for instantiating the server
const loaderProperties: IComponentsManagerBuilderOptions<App> = {
mainModulePath: this.resolveFilePath(params.mainModulePath),
mainModulePath: resolveAssetPath(params.mainModulePath),
dumpErrorState: true,
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
this.createApp(loaderProperties, configFile, params)
Expand Down Expand Up @@ -141,22 +141,10 @@ export class AppRunner {
params.baseUrl ? ensureTrailingSlash(params.baseUrl) : `http://localhost:${params.port}/`,
'urn:solid-server:default:variable:loggingLevel': params.loggingLevel,
'urn:solid-server:default:variable:port': params.port,
'urn:solid-server:default:variable:rootFilePath':
this.resolveFilePath(params.rootFilePath),
'urn:solid-server:default:variable:rootFilePath': resolveAssetPath(params.rootFilePath),
'urn:solid-server:default:variable:sparqlEndpoint': params.sparqlEndpoint,
'urn:solid-server:default:variable:showStackTrace': params.showStackTrace,
'urn:solid-server:default:variable:podConfigJson':
this.resolveFilePath(params.podConfigJson),
'urn:solid-server:default:variable:podConfigJson': resolveAssetPath(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);
}
}
5 changes: 3 additions & 2 deletions src/util/PathUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,14 @@ export function getModuleRoot(): string {
return joinFilePath(__dirname, '../../');
}

const modulePath = '$PACKAGE_ROOT/';

/**
* Converts file path inputs into absolute paths.
* Works similar to `absoluteFilePath` but paths that start with '$PACKAGE_ROOT/'
* will be relative to the module directory instead of the cwd.
*/
export function resolveAssetPath(path: string): string {
const modulePath = '$PACKAGE_ROOT/';
export function resolveAssetPath(path: string = modulePath): string {
if (path.startsWith(modulePath)) {
return joinFilePath(getModuleRoot(), path.slice(modulePath.length));
}
Expand Down
15 changes: 15 additions & 0 deletions test/unit/init/AppRunner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> => {
const { argv } = process;
process.argv = [
Expand Down

0 comments on commit f28279e

Please sign in to comment.