Support of the CLI config.

Signed-off-by: Akos Kitta <kittaakos@typefox.io>
This commit is contained in:
Akos Kitta
2020-01-31 14:50:21 +01:00
parent c7bf98dfa3
commit 807b2ad424
95 changed files with 2944 additions and 22430 deletions

View File

@@ -0,0 +1,55 @@
import * as Ajv from 'ajv';
import * as fs from './fs-extra';
import { injectable } from 'inversify';
import { CLI_CONFIG_SCHEMA_PATH, DefaultCliConfig } from './cli-config';
@injectable()
export class ConfigFileValidator {
protected readonly function = new Ajv().compile(JSON.parse(fs.readFileSync(CLI_CONFIG_SCHEMA_PATH, 'utf8')));
async validate(pathOrObject: string | object): Promise<boolean> {
return this.doValidate(typeof pathOrObject === 'string' ? fs.readFileSync(pathOrObject) : pathOrObject);
}
protected async doValidate(object: object): Promise<boolean> {
const valid = this.function(object);
if (!valid) {
return false;
}
if (!DefaultCliConfig.is(object)) {
return false;
}
const { directories: { data, downloads, user } } = object;
for (const path of [data, downloads, user]) {
const validPath = await this.isValidPath(path);
if (!validPath) {
return false;
}
}
const port = typeof object.daemon.port === 'string' ? Number.parseInt(object.daemon.port, 10) : object.daemon.port;
if (Number.isNaN(port) || port <= 0) {
return false;
}
return true;
}
protected async isValidPath(path: string): Promise<boolean> {
try {
if (!path.trim()) {
return false;
}
const exists = await fs.exists(path);
if (!exists) {
await fs.mkdirp(path);
}
return true;
} catch {
return false;
}
}
}