diff --git a/src/init/AppRunner.ts b/src/init/AppRunner.ts index 390acfefb..086c796dd 100644 --- a/src/init/AppRunner.ts +++ b/src/init/AppRunner.ts @@ -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 { @@ -85,11 +85,11 @@ export class AppRunner { // Gather settings for instantiating the server const loaderProperties: IComponentsManagerBuilderOptions = { - 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) @@ -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); - } } diff --git a/src/util/PathUtil.ts b/src/util/PathUtil.ts index ae8857cbc..c118769d5 100644 --- a/src/util/PathUtil.ts +++ b/src/util/PathUtil.ts @@ -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)); } diff --git a/test/unit/init/AppRunner.test.ts b/test/unit/init/AppRunner.test.ts index 9d7f4b32c..4530914a7 100644 --- a/test/unit/init/AppRunner.test.ts +++ b/test/unit/init/AppRunner.test.ts @@ -207,6 +207,21 @@ describe('AppRunner', (): void => { ); }); + it('accepts asset paths for the config flag.', async(): Promise => { + 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 => { const { argv } = process; process.argv = [