diff --git a/bin/server.ts b/bin/server.ts index 911bb4fb7..9a850be23 100644 --- a/bin/server.ts +++ b/bin/server.ts @@ -1,4 +1,3 @@ #!/usr/bin/env node -import * as Path from 'path'; import { runCli } from '../src/init/CliRunner'; -runCli(Path.join(__dirname, '..'), process.argv); +runCli(); diff --git a/src/init/CliRunner.ts b/src/init/CliRunner.ts index 89c74cc21..be521536a 100644 --- a/src/init/CliRunner.ts +++ b/src/init/CliRunner.ts @@ -11,19 +11,23 @@ const logger = getLoggerFor('CliRunner'); /** * Generic run function for starting the server from a given config * @param args - Command line arguments. - * @param stdin - Standard input stream. - * @param stdout - Standard output stream. * @param stderr - Standard error stream. * @param properties - Components loader properties. */ -export const runCustom = function( - args: string[], - stdin: ReadStream, - stdout: WriteStream, - stderr: WriteStream, - properties: LoaderProperties, -): void { - const { argv } = yargs(args) +export const runCli = function({ + argv = process.argv, + stderr = process.stderr, + properties = { + mainModulePath: path.join(__dirname, '../../'), + }, +}: { + argv?: string[]; + stdin?: ReadStream; + stdout?: WriteStream; + stderr?: WriteStream; + properties?: LoaderProperties; +} = {}): void { + const { argv: params } = yargs(argv.slice(2)) .usage('node ./bin/server.js [args]') .options({ port: { type: 'number', alias: 'p', default: 3000 }, @@ -36,8 +40,8 @@ export const runCustom = function( (async(): Promise => { // Load provided or default config file - const configPath = argv.config ? - path.join(process.cwd(), argv.config) : + const configPath = params.config ? + path.join(process.cwd(), params.config) : path.join(__dirname, '/../../config/config-default.json'); // Setup from config file @@ -46,11 +50,11 @@ export const runCustom = function( const setup: Setup = await loader .instantiateFromUrl('urn:solid-server:default', configPath, undefined, { variables: { - 'urn:solid-server:default:variable:port': argv.port, - 'urn:solid-server:default:variable:base': `http://localhost:${argv.port}/`, - 'urn:solid-server:default:variable:rootFilePath': argv.rootFilePath ?? process.cwd(), - 'urn:solid-server:default:variable:sparqlEndpoint': argv.sparqlEndpoint, - 'urn:solid-server:default:variable:loggingLevel': argv.loggingLevel, + 'urn:solid-server:default:variable:port': params.port, + 'urn:solid-server:default:variable:base': `http://localhost:${params.port}/`, + 'urn:solid-server:default:variable:rootFilePath': params.rootFilePath ?? process.cwd(), + 'urn:solid-server:default:variable:sparqlEndpoint': params.sparqlEndpoint, + 'urn:solid-server:default:variable:loggingLevel': params.loggingLevel, }, }) as Setup; return await setup.setup(); @@ -61,11 +65,3 @@ export const runCustom = function( stderr.write(`${error}\n`); }); }; - -/** - * Run function for starting the server from the command line - * @param moduleRootPath - Path to the module's root. - */ -export const runCli = function(mainModulePath: string, argv: string[]): void { - runCustom(argv, process.stdin, process.stdout, process.stderr, { mainModulePath }); -}; diff --git a/test/unit/init/CliRunner.test.ts b/test/unit/init/CliRunner.test.ts index d9c885264..0b55a3654 100644 --- a/test/unit/init/CliRunner.test.ts +++ b/test/unit/init/CliRunner.test.ts @@ -1,9 +1,9 @@ import * as path from 'path'; -import type { Loader } from 'componentsjs'; +import { Loader } from 'componentsjs'; import { runCli } from '../../../src/init/CliRunner'; import type { Setup } from '../../../src/init/Setup'; -const mainModulePath = path.join(__dirname, '..'); +const mainModulePath = path.join(__dirname, '../../../'); const mockSetup = { setup: jest.fn(async(): Promise => null), @@ -25,9 +25,13 @@ describe('CliRunner', (): void => { }); it('starts the server with default settings.', async(): Promise => { - runCli(mainModulePath, [ 'node', 'script' ]); + runCli({ + argv: [ 'node', 'script' ], + }); await mockSetup.setup(); + expect(Loader).toHaveBeenCalledTimes(1); + expect(Loader).toHaveBeenCalledWith({ mainModulePath }); expect(loader.instantiateFromUrl).toHaveBeenCalledTimes(1); expect(loader.instantiateFromUrl).toHaveBeenCalledWith( 'urn:solid-server:default', @@ -50,13 +54,16 @@ describe('CliRunner', (): void => { }); it('accepts abbreviated flags.', async(): Promise => { - runCli(mainModulePath, [ 'node', 'script', - '-p', '4000', - '-c', 'myconfig.json', - '-f', '/root', - '-s', 'http://localhost:5000/sparql', - '-l', 'debug', - ]); + runCli({ + argv: [ + 'node', 'script', + '-p', '4000', + '-c', 'myconfig.json', + '-f', '/root', + '-s', 'http://localhost:5000/sparql', + '-l', 'debug', + ], + }); await mockSetup.setup(); expect(loader.instantiateFromUrl).toHaveBeenCalledWith( @@ -76,13 +83,16 @@ describe('CliRunner', (): void => { }); it('accepts full flags.', async(): Promise => { - runCli(mainModulePath, [ 'node', 'script', - '--port', '4000', - '--config', 'myconfig.json', - '--rootFilePath', '/root', - '--sparqlEndpoint', 'http://localhost:5000/sparql', - '--loggingLevel', 'debug', - ]); + runCli({ + argv: [ + 'node', 'script', + '--port', '4000', + '--config', 'myconfig.json', + '--rootFilePath', '/root', + '--sparqlEndpoint', 'http://localhost:5000/sparql', + '--loggingLevel', 'debug', + ], + }); await mockSetup.setup(); expect(loader.instantiateFromUrl).toHaveBeenCalledWith( @@ -105,7 +115,7 @@ describe('CliRunner', (): void => { jest.spyOn(process.stderr, 'write'); loader.instantiateFromUrl.mockRejectedValueOnce(new Error('Fatal')); - runCli(mainModulePath, [ 'node', 'script' ]); + runCli(); await new Promise((resolve): any => setImmediate(resolve)); expect(process.stderr.write).toHaveBeenCalledTimes(1);