mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
refactor: Refactor runCli to take optional arguments.
This commit is contained in:
parent
c3b5387efb
commit
528688bc4c
@ -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();
|
||||
|
@ -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<string> => {
|
||||
// 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 });
|
||||
};
|
||||
|
@ -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<any> => null),
|
||||
@ -25,9 +25,13 @@ describe('CliRunner', (): void => {
|
||||
});
|
||||
|
||||
it('starts the server with default settings.', async(): Promise<void> => {
|
||||
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<void> => {
|
||||
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<void> => {
|
||||
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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user