mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Add mainModulePath and globalModules CLI flags.
This commit is contained in:
parent
49551eb9eb
commit
ba4f7ff26c
@ -16,20 +16,15 @@ export class 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 stderr - Standard error stream.
|
* @param stderr - Standard error stream.
|
||||||
* @param loaderProperties - Components loader properties.
|
|
||||||
*/
|
*/
|
||||||
public run({
|
public run({
|
||||||
argv = process.argv,
|
argv = process.argv,
|
||||||
stderr = process.stderr,
|
stderr = process.stderr,
|
||||||
loaderProperties = {
|
|
||||||
mainModulePath: path.join(__dirname, '../../'),
|
|
||||||
},
|
|
||||||
}: {
|
}: {
|
||||||
argv?: string[];
|
argv?: string[];
|
||||||
stdin?: ReadStream;
|
stdin?: ReadStream;
|
||||||
stdout?: WriteStream;
|
stdout?: WriteStream;
|
||||||
stderr?: WriteStream;
|
stderr?: WriteStream;
|
||||||
loaderProperties?: LoaderProperties;
|
|
||||||
} = {}): void {
|
} = {}): void {
|
||||||
// Parse the command-line arguments
|
// Parse the command-line arguments
|
||||||
const { argv: params } = yargs(argv.slice(2))
|
const { argv: params } = yargs(argv.slice(2))
|
||||||
@ -37,18 +32,22 @@ export class CliRunner {
|
|||||||
.options({
|
.options({
|
||||||
baseUrl: { type: 'string', alias: 'b' },
|
baseUrl: { type: 'string', alias: 'b' },
|
||||||
config: { type: 'string', alias: 'c' },
|
config: { type: 'string', alias: 'c' },
|
||||||
|
globalModules: { type: 'boolean', alias: 'g' },
|
||||||
loggingLevel: { type: 'string', alias: 'l', default: 'info' },
|
loggingLevel: { type: 'string', alias: 'l', default: 'info' },
|
||||||
|
mainModulePath: { type: 'string', alias: 'm' },
|
||||||
|
podTemplateFolder: { type: 'string', alias: 't' },
|
||||||
port: { type: 'number', alias: 'p', default: 3000 },
|
port: { type: 'number', alias: 'p', default: 3000 },
|
||||||
rootFilePath: { type: 'string', alias: 'f' },
|
rootFilePath: { type: 'string', alias: 'f' },
|
||||||
sparqlEndpoint: { type: 'string', alias: 's' },
|
sparqlEndpoint: { type: 'string', alias: 's' },
|
||||||
podTemplateFolder: { type: 'string', alias: 't' },
|
|
||||||
})
|
})
|
||||||
.help();
|
.help();
|
||||||
|
|
||||||
// Gather settings for instantiating the server
|
// Gather settings for instantiating the server
|
||||||
const configFile = params.config ?
|
const loaderProperties: LoaderProperties = {
|
||||||
path.join(process.cwd(), params.config) :
|
mainModulePath: this.resolvePath(params.mainModulePath),
|
||||||
path.join(__dirname, '/../../config/config-default.json');
|
scanGlobal: params.globalModules,
|
||||||
|
};
|
||||||
|
const configFile = this.resolvePath(params.config, 'config/config-default.json');
|
||||||
const variables = this.createVariables(params);
|
const variables = this.createVariables(params);
|
||||||
|
|
||||||
// Create and execute the server initializer
|
// Create and execute the server initializer
|
||||||
@ -67,6 +66,16 @@ export class CliRunner {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolves a path relative to the current working directory,
|
||||||
|
* falling back to a path relative to this module.
|
||||||
|
*/
|
||||||
|
protected resolvePath(cwdPath?: string | null, modulePath = ''): string {
|
||||||
|
return typeof cwdPath === 'string' ?
|
||||||
|
path.join(process.cwd(), cwdPath) :
|
||||||
|
path.join(__dirname, '../../', modulePath);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Translates command-line parameters into configuration variables
|
* Translates command-line parameters into configuration variables
|
||||||
*/
|
*/
|
||||||
@ -79,7 +88,7 @@ export class CliRunner {
|
|||||||
'urn:solid-server:default:variable:rootFilePath': params.rootFilePath ?? process.cwd(),
|
'urn:solid-server:default:variable:rootFilePath': params.rootFilePath ?? process.cwd(),
|
||||||
'urn:solid-server:default:variable:sparqlEndpoint': params.sparqlEndpoint,
|
'urn:solid-server:default:variable:sparqlEndpoint': params.sparqlEndpoint,
|
||||||
'urn:solid-server:default:variable:podTemplateFolder':
|
'urn:solid-server:default:variable:podTemplateFolder':
|
||||||
params.podTemplateFolder ?? path.join(__dirname, '../../templates'),
|
params.podTemplateFolder ?? this.resolvePath(null, 'templates'),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,8 +3,6 @@ import { Loader } from 'componentsjs';
|
|||||||
import { CliRunner } from '../../../src/init/CliRunner';
|
import { CliRunner } from '../../../src/init/CliRunner';
|
||||||
import type { Initializer } from '../../../src/init/Initializer';
|
import type { Initializer } from '../../../src/init/Initializer';
|
||||||
|
|
||||||
const mainModulePath = path.join(__dirname, '../../../');
|
|
||||||
|
|
||||||
const initializer: jest.Mocked<Initializer> = {
|
const initializer: jest.Mocked<Initializer> = {
|
||||||
handleSafe: jest.fn(),
|
handleSafe: jest.fn(),
|
||||||
} as any;
|
} as any;
|
||||||
@ -19,6 +17,7 @@ jest.mock('componentsjs', (): any => ({
|
|||||||
Loader: jest.fn((): Loader => loader),
|
Loader: jest.fn((): Loader => loader),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
jest.spyOn(process, 'cwd').mockReturnValue('/var/cwd');
|
||||||
const write = jest.spyOn(process.stderr, 'write').mockImplementation(jest.fn());
|
const write = jest.spyOn(process.stderr, 'write').mockImplementation(jest.fn());
|
||||||
const exit = jest.spyOn(process, 'exit').mockImplementation(jest.fn() as any);
|
const exit = jest.spyOn(process, 'exit').mockImplementation(jest.fn() as any);
|
||||||
|
|
||||||
@ -34,7 +33,9 @@ describe('CliRunner', (): void => {
|
|||||||
await initializer.handleSafe();
|
await initializer.handleSafe();
|
||||||
|
|
||||||
expect(Loader).toHaveBeenCalledTimes(1);
|
expect(Loader).toHaveBeenCalledTimes(1);
|
||||||
expect(Loader).toHaveBeenCalledWith({ mainModulePath });
|
expect(Loader).toHaveBeenCalledWith({
|
||||||
|
mainModulePath: path.join(__dirname, '../../../'),
|
||||||
|
});
|
||||||
expect(loader.instantiateFromUrl).toHaveBeenCalledTimes(1);
|
expect(loader.instantiateFromUrl).toHaveBeenCalledTimes(1);
|
||||||
expect(loader.instantiateFromUrl).toHaveBeenCalledWith(
|
expect(loader.instantiateFromUrl).toHaveBeenCalledWith(
|
||||||
'urn:solid-server:default:Initializer',
|
'urn:solid-server:default:Initializer',
|
||||||
@ -61,29 +62,36 @@ describe('CliRunner', (): void => {
|
|||||||
new CliRunner().run({
|
new CliRunner().run({
|
||||||
argv: [
|
argv: [
|
||||||
'node', 'script',
|
'node', 'script',
|
||||||
'-p', '4000',
|
|
||||||
'-b', 'http://pod.example/',
|
'-b', 'http://pod.example/',
|
||||||
'-c', 'myconfig.json',
|
'-c', 'myconfig.json',
|
||||||
'-f', '/root',
|
'-f', '/root',
|
||||||
'-s', 'http://localhost:5000/sparql',
|
'-g',
|
||||||
'-l', 'debug',
|
'-l', 'debug',
|
||||||
|
'-m', 'module/path',
|
||||||
|
'-p', '4000',
|
||||||
|
'-s', 'http://localhost:5000/sparql',
|
||||||
'-t', 'templates',
|
'-t', 'templates',
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
await initializer.handleSafe();
|
await initializer.handleSafe();
|
||||||
|
|
||||||
|
expect(Loader).toHaveBeenCalledTimes(1);
|
||||||
|
expect(Loader).toHaveBeenCalledWith({
|
||||||
|
mainModulePath: '/var/cwd/module/path',
|
||||||
|
scanGlobal: true,
|
||||||
|
});
|
||||||
expect(loader.instantiateFromUrl).toHaveBeenCalledWith(
|
expect(loader.instantiateFromUrl).toHaveBeenCalledWith(
|
||||||
'urn:solid-server:default:Initializer',
|
'urn:solid-server:default:Initializer',
|
||||||
path.join(process.cwd(), 'myconfig.json'),
|
'/var/cwd/myconfig.json',
|
||||||
undefined,
|
undefined,
|
||||||
{
|
{
|
||||||
variables: {
|
variables: {
|
||||||
'urn:solid-server:default:variable:port': 4000,
|
|
||||||
'urn:solid-server:default:variable:baseUrl': 'http://pod.example/',
|
'urn:solid-server:default:variable:baseUrl': 'http://pod.example/',
|
||||||
'urn:solid-server:default:variable:rootFilePath': '/root',
|
|
||||||
'urn:solid-server:default:variable:sparqlEndpoint': 'http://localhost:5000/sparql',
|
|
||||||
'urn:solid-server:default:variable:loggingLevel': 'debug',
|
'urn:solid-server:default:variable:loggingLevel': 'debug',
|
||||||
'urn:solid-server:default:variable:podTemplateFolder': 'templates',
|
'urn:solid-server:default:variable:podTemplateFolder': 'templates',
|
||||||
|
'urn:solid-server:default:variable:port': 4000,
|
||||||
|
'urn:solid-server:default:variable:rootFilePath': '/root',
|
||||||
|
'urn:solid-server:default:variable:sparqlEndpoint': 'http://localhost:5000/sparql',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@ -93,29 +101,36 @@ describe('CliRunner', (): void => {
|
|||||||
new CliRunner().run({
|
new CliRunner().run({
|
||||||
argv: [
|
argv: [
|
||||||
'node', 'script',
|
'node', 'script',
|
||||||
'--port', '4000',
|
|
||||||
'--baseUrl', 'http://pod.example/',
|
'--baseUrl', 'http://pod.example/',
|
||||||
'--config', 'myconfig.json',
|
'--config', 'myconfig.json',
|
||||||
|
'--globalModules',
|
||||||
|
'--loggingLevel', 'debug',
|
||||||
|
'--mainModulePath', 'module/path',
|
||||||
|
'--podTemplateFolder', 'templates',
|
||||||
|
'--port', '4000',
|
||||||
'--rootFilePath', '/root',
|
'--rootFilePath', '/root',
|
||||||
'--sparqlEndpoint', 'http://localhost:5000/sparql',
|
'--sparqlEndpoint', 'http://localhost:5000/sparql',
|
||||||
'--loggingLevel', 'debug',
|
|
||||||
'--podTemplateFolder', 'templates',
|
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
await initializer.handleSafe();
|
await initializer.handleSafe();
|
||||||
|
|
||||||
|
expect(Loader).toHaveBeenCalledTimes(1);
|
||||||
|
expect(Loader).toHaveBeenCalledWith({
|
||||||
|
mainModulePath: '/var/cwd/module/path',
|
||||||
|
scanGlobal: true,
|
||||||
|
});
|
||||||
expect(loader.instantiateFromUrl).toHaveBeenCalledWith(
|
expect(loader.instantiateFromUrl).toHaveBeenCalledWith(
|
||||||
'urn:solid-server:default:Initializer',
|
'urn:solid-server:default:Initializer',
|
||||||
path.join(process.cwd(), 'myconfig.json'),
|
'/var/cwd/myconfig.json',
|
||||||
undefined,
|
undefined,
|
||||||
{
|
{
|
||||||
variables: {
|
variables: {
|
||||||
'urn:solid-server:default:variable:port': 4000,
|
|
||||||
'urn:solid-server:default:variable:baseUrl': 'http://pod.example/',
|
'urn:solid-server:default:variable:baseUrl': 'http://pod.example/',
|
||||||
'urn:solid-server:default:variable:rootFilePath': '/root',
|
|
||||||
'urn:solid-server:default:variable:sparqlEndpoint': 'http://localhost:5000/sparql',
|
|
||||||
'urn:solid-server:default:variable:loggingLevel': 'debug',
|
'urn:solid-server:default:variable:loggingLevel': 'debug',
|
||||||
'urn:solid-server:default:variable:podTemplateFolder': 'templates',
|
'urn:solid-server:default:variable:podTemplateFolder': 'templates',
|
||||||
|
'urn:solid-server:default:variable:port': 4000,
|
||||||
|
'urn:solid-server:default:variable:rootFilePath': '/root',
|
||||||
|
'urn:solid-server:default:variable:sparqlEndpoint': 'http://localhost:5000/sparql',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user