-
Notifications
You must be signed in to change notification settings - Fork 121
/
CliRunner.ts
106 lines (97 loc) · 4.02 KB
/
CliRunner.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* eslint-disable unicorn/no-process-exit */
import * as path from 'path';
import type { ReadStream, WriteStream } from 'tty';
import type { LoaderProperties } from 'componentsjs';
import { Loader } from 'componentsjs';
import yargs from 'yargs';
import { getLoggerFor } from '../logging/LogUtil';
import { ensureTrailingSlash } from '../util/PathUtil';
import type { Initializer } from './Initializer';
export class CliRunner {
private readonly logger = getLoggerFor(this);
/**
* Generic run function for starting the server from a given config
* @param args - Command line arguments.
* @param stderr - Standard error stream.
*/
public run({
argv = process.argv,
stderr = process.stderr,
}: {
argv?: string[];
stdin?: ReadStream;
stdout?: WriteStream;
stderr?: WriteStream;
} = {}): void {
// Parse the command-line arguments
const { argv: params } = yargs(argv.slice(2))
.usage('node ./bin/server.js [args]')
.options({
baseUrl: { type: 'string', alias: 'b' },
config: { type: 'string', alias: 'c' },
globalModules: { type: 'boolean', alias: 'g' },
loggingLevel: { type: 'string', alias: 'l', default: 'info' },
mainModulePath: { type: 'string', alias: 'm' },
podTemplateFolder: { type: 'string', alias: 't' },
port: { type: 'number', alias: 'p', default: 3000 },
rootFilePath: { type: 'string', alias: 'f' },
sparqlEndpoint: { type: 'string', alias: 's' },
})
.help();
// Gather settings for instantiating the server
const loaderProperties: LoaderProperties = {
mainModulePath: this.resolvePath(params.mainModulePath),
scanGlobal: params.globalModules,
};
const configFile = this.resolvePath(params.config, 'config/config-default.json');
const variables = this.createVariables(params);
// Create and execute the server initializer
this.createInitializer(loaderProperties, configFile, variables)
.then(
async(initializer): Promise<void> => initializer.handleSafe(),
(error: Error): void => {
// Instantiation of components has failed, so there is no logger to use
stderr.write(`Error: could not instantiate server from ${configFile}\n`);
stderr.write(`${error.stack}\n`);
process.exit(1);
},
).catch((error): void => {
this.logger.error(`Could not initialize server: ${error}`, { error });
process.exit(1);
});
}
/**
* 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
*/
protected createVariables(params: Record<string, any>): Record<string, any> {
return {
'urn:solid-server:default:variable:baseUrl':
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': params.rootFilePath ?? process.cwd(),
'urn:solid-server:default:variable:sparqlEndpoint': params.sparqlEndpoint,
'urn:solid-server:default:variable:podTemplateFolder':
params.podTemplateFolder ?? this.resolvePath(null, 'templates'),
};
}
/**
* Creates the server initializer
*/
protected async createInitializer(loaderProperties: LoaderProperties, configFile: string,
variables: Record<string, any>): Promise<Initializer> {
const loader = new Loader(loaderProperties);
await loader.registerAvailableModuleResources();
const initializer = 'urn:solid-server:default:Initializer';
return await loader.instantiateFromUrl(initializer, configFile, undefined, { variables }) as Initializer;
}
}