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